Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_xml.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 5 | * @brief XML data parser for libyang |
| 6 | * |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 7 | * Copyright (c) 2019 - 2022 CESNET, z.s.p.o. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 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 | |
Michal Vasko | 742a5b1 | 2022-02-24 16:07:27 +0100 | [diff] [blame] | 16 | #define _GNU_SOURCE |
| 17 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
Michal Vasko | 742a5b1 | 2022-02-24 16:07:27 +0100 | [diff] [blame] | 23 | #include "compat.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 24 | #include "context.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 25 | #include "dict.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 26 | #include "in_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | #include "log.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 28 | #include "ly_common.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 29 | #include "parser_data.h" |
| 30 | #include "parser_internal.h" |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 31 | #include "plugins_exts.h" |
Michal Vasko | 51de7b7 | 2022-04-29 09:50:22 +0200 | [diff] [blame] | 32 | #include "plugins_internal.h" |
| 33 | #include "schema_compile_node.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "set.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 35 | #include "tree.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 36 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | #include "tree_data_internal.h" |
| 38 | #include "tree_schema.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 39 | #include "tree_schema_internal.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 40 | #include "validation.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 41 | #include "xml.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 42 | |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 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 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 46 | void |
| 47 | lyd_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 Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 56 | /** |
aPiecek | 77a4f50 | 2024-07-03 08:34:48 +0200 | [diff] [blame] | 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 | { |
| 69 | if (prefix_len && attr_len) { |
| 70 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\" at attribute \"%.*s\".", |
| 71 | (int)prefix_len, prefix, (int)attr_len, attr_name); |
| 72 | } else if (prefix_len) { |
| 73 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", |
| 74 | (int)prefix_len, prefix); |
| 75 | } else { |
| 76 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing XML namespace."); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 81 | * @brief Parse and create XML metadata. |
| 82 | * |
| 83 | * @param[in] lydctx XML data parser context. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 84 | * @param[in] sparent Schema node of the parent. |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 85 | * @param[out] meta List of created metadata instances. |
| 86 | * @return LY_ERR value. |
| 87 | */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 88 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 89 | lydxml_metadata(struct lyd_xml_ctx *lydctx, const struct lysc_node *sparent, struct lyd_meta **meta) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 90 | { |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 91 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 92 | const struct lyxml_ns *ns; |
| 93 | struct lys_module *mod; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 94 | const char *name; |
| 95 | size_t name_len; |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 96 | LY_ARRAY_COUNT_TYPE u; |
| 97 | ly_bool filter_attrs = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 98 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 99 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 100 | *meta = NULL; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 101 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 102 | LOG_LOCSET(sparent, NULL); |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 103 | |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 104 | /* check for NETCONF filter unqualified attributes */ |
Michal Vasko | 1b2a3f4 | 2022-12-20 09:38:28 +0100 | [diff] [blame] | 105 | if (!strcmp(sparent->module->name, "notifications")) { |
| 106 | /* ancient module that does not even use the extension */ |
| 107 | filter_attrs = 1; |
| 108 | } else { |
| 109 | LY_ARRAY_FOR(sparent->exts, u) { |
| 110 | if (!strcmp(sparent->exts[u].def->name, "get-filter-element-attributes") && |
| 111 | !strcmp(sparent->exts[u].def->module->name, "ietf-netconf")) { |
| 112 | filter_attrs = 1; |
| 113 | break; |
| 114 | } |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 118 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
| 119 | if (!xmlctx->prefix_len) { |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 120 | /* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */ |
| 121 | if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) || |
| 122 | !ly_strncmp("select", xmlctx->name, xmlctx->name_len))) { |
| 123 | mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf"); |
| 124 | if (!mod) { |
| 125 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, |
| 126 | "Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes."); |
| 127 | ret = LY_ENOTFOUND; |
| 128 | goto cleanup; |
| 129 | } |
| 130 | goto create_meta; |
| 131 | } |
| 132 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 133 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 134 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 135 | (int)xmlctx->name_len, xmlctx->name); |
aPiecek | f410c2e | 2024-06-20 13:09:07 +0200 | [diff] [blame] | 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 | LY_DPARSER_ERR_GOTO(LY_EVALID, ret = LY_EVALID, lydctx, cleanup); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 140 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 141 | |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 142 | /* skip attr */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 143 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 144 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 145 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 146 | continue; |
| 147 | } |
| 148 | |
| 149 | /* get namespace of the attribute to find its annotation definition */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 150 | ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 151 | if (!ns) { |
aPiecek | 77a4f50 | 2024-07-03 08:34:48 +0200 | [diff] [blame] | 152 | lydxml_log_namespace_err(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len); |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 153 | ret = LY_ENOTFOUND; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 154 | goto cleanup; |
| 155 | } |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 156 | |
| 157 | /* get the module with metadata definition */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 158 | mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 159 | if (!mod) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 160 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 161 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 162 | "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 163 | ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "", |
| 164 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 165 | ret = LY_ENOTFOUND; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 166 | goto cleanup; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 167 | } |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 168 | |
| 169 | /* skip attr */ |
| 170 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 171 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 172 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 173 | continue; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 176 | create_meta: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 177 | /* remember meta name and get its content */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 178 | name = xmlctx->name; |
| 179 | name_len = xmlctx->name_len; |
| 180 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 181 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 182 | |
| 183 | /* create metadata */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 184 | ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 185 | xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, sparent); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 186 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 187 | |
| 188 | /* next attribute */ |
| 189 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 190 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 191 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 192 | cleanup: |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 193 | LOG_LOCBACK(1, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 194 | if (ret) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 195 | lyd_free_meta_siblings(*meta); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 196 | *meta = NULL; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 197 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 198 | return ret; |
| 199 | } |
| 200 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 201 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 202 | lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 203 | { |
| 204 | LY_ERR ret = LY_SUCCESS; |
| 205 | const struct lyxml_ns *ns; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 206 | void *val_prefix_data; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 207 | LY_VALUE_FORMAT format; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 208 | struct lyd_attr *attr2; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 209 | const char *name, *prefix; |
| 210 | size_t name_len, prefix_len; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 211 | |
| 212 | assert(attr); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 213 | *attr = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 214 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 215 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 216 | if (*attr) { |
| 217 | attr2 = *attr; |
| 218 | } else { |
| 219 | attr2 = NULL; |
| 220 | } |
| 221 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 222 | /* remember attr prefix, name, and get its content */ |
| 223 | prefix = xmlctx->prefix; |
| 224 | prefix_len = xmlctx->prefix_len; |
| 225 | name = xmlctx->name; |
| 226 | name_len = xmlctx->name_len; |
| 227 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 228 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 229 | |
Michal Vasko | e137fc4 | 2021-07-22 11:53:13 +0200 | [diff] [blame] | 230 | /* handle special "xml" attribute prefix */ |
| 231 | if ((prefix_len == 3) && !strncmp(prefix, "xml", 3)) { |
| 232 | name = prefix; |
| 233 | name_len += 1 + prefix_len; |
| 234 | prefix = NULL; |
| 235 | prefix_len = 0; |
| 236 | } |
| 237 | |
| 238 | /* find namespace of the attribute, if any */ |
| 239 | ns = NULL; |
| 240 | if (prefix_len) { |
| 241 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
| 242 | if (!ns) { |
aPiecek | 77a4f50 | 2024-07-03 08:34:48 +0200 | [diff] [blame] | 243 | lydxml_log_namespace_err(xmlctx, prefix, prefix_len, name, name_len); |
Michal Vasko | e137fc4 | 2021-07-22 11:53:13 +0200 | [diff] [blame] | 244 | ret = LY_EVALID; |
| 245 | goto cleanup; |
| 246 | } |
| 247 | } |
| 248 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 249 | /* get value prefixes */ |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 250 | val_prefix_data = NULL; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 251 | LY_CHECK_GOTO(ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 252 | &xmlctx->ns, &format, &val_prefix_data), cleanup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 253 | |
| 254 | /* attr2 is always changed to the created attribute */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 255 | ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL, |
Michal Vasko | e5e49e9 | 2022-02-01 13:15:08 +0100 | [diff] [blame] | 256 | ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, |
| 257 | LYD_HINT_DATA); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 258 | LY_CHECK_GOTO(ret, cleanup); |
| 259 | |
| 260 | if (!*attr) { |
| 261 | *attr = attr2; |
| 262 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 263 | |
| 264 | /* next attribute */ |
| 265 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | cleanup: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 269 | if (ret) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 270 | lyd_free_attr_siblings(xmlctx->ctx, *attr); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 271 | *attr = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 272 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 273 | return ret; |
| 274 | } |
| 275 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 276 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 277 | lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list) |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 278 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 279 | LY_ERR ret = LY_SUCCESS, r; |
| 280 | enum LYXML_PARSER_STATUS next; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 281 | struct ly_set key_set = {0}; |
| 282 | const struct lysc_node *snode; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 283 | uint32_t i, parents_count; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 284 | |
| 285 | assert(list && (list->nodetype == LYS_LIST)); |
| 286 | |
| 287 | /* get all keys into a set (keys do not have if-features or anything) */ |
| 288 | snode = NULL; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 289 | while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 290 | ret = ly_set_add(&key_set, (void *)snode, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 291 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Michal Vasko | 12d809c | 2021-03-03 16:34:32 +0100 | [diff] [blame] | 294 | /* remember parent count */ |
| 295 | parents_count = xmlctx->elements.count; |
| 296 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 297 | while (xmlctx->status == LYXML_ELEMENT) { |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 298 | /* find key definition */ |
| 299 | for (i = 0; i < key_set.count; ++i) { |
| 300 | snode = (const struct lysc_node *)key_set.objs[i]; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 301 | if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) { |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 305 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 306 | |
| 307 | /* skip attributes */ |
| 308 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 309 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 310 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 311 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 314 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 315 | if (i < key_set.count) { |
| 316 | /* validate the value */ |
Michal Vasko | 583b464 | 2023-05-25 10:39:34 +0200 | [diff] [blame] | 317 | r = ly_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 318 | if (!r) { |
| 319 | /* key with a valid value, remove from the set */ |
| 320 | ly_set_rm_index(&key_set, i, NULL); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 324 | /* parser next */ |
| 325 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 326 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 327 | /* skip any children, resursively */ |
Michal Vasko | 12d809c | 2021-03-03 16:34:32 +0100 | [diff] [blame] | 328 | while (xmlctx->status == LYXML_ELEMENT) { |
| 329 | while (parents_count < xmlctx->elements.count) { |
| 330 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 331 | } |
| 332 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 333 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 334 | } |
| 335 | |
| 336 | /* parser next, but do not parse closing element of the list because it would remove its namespaces */ |
| 337 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 338 | LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup); |
| 339 | if (next != LYXML_ELEM_CLOSE) { |
| 340 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 341 | } |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | if (key_set.count) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 345 | /* some keys are missing/did not validate */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 346 | ret = LY_ENOT; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | cleanup: |
| 350 | ly_set_erase(&key_set, NULL); |
| 351 | return ret; |
| 352 | } |
| 353 | |
Michal Vasko | 5c24ed1 | 2021-06-09 09:27:32 +0200 | [diff] [blame] | 354 | /** |
| 355 | * @brief Skip an element with all its descendants. |
| 356 | * |
| 357 | * @param[in] xmlctx XML parser context. |
| 358 | * @return LY_ERR value. |
| 359 | */ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 360 | static LY_ERR |
| 361 | lydxml_data_skip(struct lyxml_ctx *xmlctx) |
| 362 | { |
| 363 | uint32_t parents_count; |
| 364 | |
| 365 | /* remember current number of parents */ |
| 366 | parents_count = xmlctx->elements.count; |
aPiecek | 9cdb9e6 | 2021-05-18 09:46:20 +0200 | [diff] [blame] | 367 | assert(parents_count); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 368 | |
| 369 | /* skip after the content */ |
| 370 | while (xmlctx->status != LYXML_ELEM_CONTENT) { |
| 371 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 372 | } |
| 373 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 374 | |
| 375 | /* skip all children elements, recursively, if any */ |
aPiecek | 9cdb9e6 | 2021-05-18 09:46:20 +0200 | [diff] [blame] | 376 | while (parents_count <= xmlctx->elements.count) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 377 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 378 | } |
| 379 | |
| 380 | /* close element */ |
| 381 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 382 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 383 | |
| 384 | return LY_SUCCESS; |
| 385 | } |
| 386 | |
Michal Vasko | 5c24ed1 | 2021-06-09 09:27:32 +0200 | [diff] [blame] | 387 | /** |
| 388 | * @brief Check that the current element can be parsed as a data node. |
| 389 | * |
| 390 | * @param[in] lydctx XML data parser context. |
| 391 | * @param[in,out] snode Found schema node, set to NULL if data node cannot be created. |
| 392 | * @return LY_ERR value. |
| 393 | */ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 394 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 395 | lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 396 | { |
Michal Vasko | 8a8fde0 | 2024-10-21 15:17:46 +0200 | [diff] [blame] | 397 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 398 | struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx; |
Michal Vasko | 8a8fde0 | 2024-10-21 15:17:46 +0200 | [diff] [blame] | 399 | uint32_t *prev_lo, temp_lo = 0; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 400 | |
Radek IÅ¡a | 3930fd7 | 2021-03-08 10:48:40 +0100 | [diff] [blame] | 401 | if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
| 402 | /* only checks specific to opaque nodes */ |
| 403 | return LY_SUCCESS; |
| 404 | } |
| 405 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 406 | if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) { |
| 407 | /* nothing to check */ |
| 408 | return LY_SUCCESS; |
| 409 | } |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 410 | |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 411 | assert(xmlctx->elements.count); |
| 412 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 413 | /* backup parser */ |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 414 | LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx)); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 415 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 416 | /* skip attributes */ |
| 417 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
| 418 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
| 419 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
| 420 | } |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 421 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 422 | if ((*snode)->nodetype & LYD_NODE_TERM) { |
| 423 | /* value may not be valid in which case we parse it as an opaque node */ |
Michal Vasko | 8a8fde0 | 2024-10-21 15:17:46 +0200 | [diff] [blame] | 424 | prev_lo = ly_temp_log_options(&temp_lo); |
| 425 | r = ly_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA); |
| 426 | ly_temp_log_options(prev_lo); |
| 427 | if (r) { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 428 | LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, (int)xmlctx->value_len, |
Michal Vasko | d0237d4 | 2021-07-12 14:49:46 +0200 | [diff] [blame] | 429 | xmlctx->value); |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 430 | *snode = NULL; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 431 | } |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 432 | } else if ((*snode)->nodetype == LYS_LIST) { |
| 433 | /* skip content */ |
| 434 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 435 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 436 | if (lydxml_check_list(xmlctx, *snode)) { |
| 437 | /* invalid list, parse as opaque if it missing/has invalid some keys */ |
Michal Vasko | d0237d4 | 2021-07-12 14:49:46 +0200 | [diff] [blame] | 438 | LOGVRB("Parsing opaque list node \"%s\" with missing/invalid keys.", (*snode)->name); |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 439 | *snode = NULL; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 440 | } |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 441 | } else { |
Radek IÅ¡a | 3930fd7 | 2021-03-08 10:48:40 +0100 | [diff] [blame] | 442 | /* if there is a non-WS value, it cannot be parsed as an inner node */ |
| 443 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 444 | if (!xmlctx->ws_only) { |
| 445 | *snode = NULL; |
| 446 | } |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 449 | restore: |
| 450 | /* restore parser */ |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 451 | lyxml_ctx_restore(xmlctx, &pxmlctx); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 452 | return ret; |
| 453 | } |
| 454 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 455 | /** |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 456 | * @brief Get sensible data hints for an opaque node. |
| 457 | * |
| 458 | * @param[in] name Node name. |
| 459 | * @param[in] name_len Length of @p name. |
| 460 | * @param[in] value Node value. |
| 461 | * @param[in] value_len Length of @p value. |
| 462 | * @param[in] first Node first sibling. |
Michal Vasko | ce2e07c | 2022-12-01 11:08:52 +0100 | [diff] [blame] | 463 | * @param[in] ns Node module namespace, NULL for no namespace. |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 464 | * @param[out] hints Data hints to use. |
| 465 | * @param[out] anchor Anchor to insert after in case of a list. |
| 466 | */ |
| 467 | static void |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 468 | lydxml_get_hints_opaq(const char *name, size_t name_len, const char *value, size_t value_len, const struct lyd_node *first, |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 469 | const char *ns, uint32_t *hints, struct lyd_node **anchor) |
| 470 | { |
| 471 | struct lyd_node_opaq *opaq; |
| 472 | char *ptr; |
Jan Kundrát | 863b18b | 2024-07-06 20:55:23 +0200 | [diff] [blame] | 473 | /* this needs to be at least 64bit, and it "should not" be an explicit int64_t |
| 474 | * because the code calls strtoll later on, which "might" return a bigger type */ |
| 475 | long long num; |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 476 | |
| 477 | *hints = 0; |
| 478 | *anchor = NULL; |
| 479 | |
| 480 | if (!value_len) { |
Michal Vasko | 6235e15 | 2023-08-07 13:39:13 +0200 | [diff] [blame] | 481 | /* no value but it may also be zero-length string */ |
| 482 | *hints |= LYD_VALHINT_EMPTY | LYD_VALHINT_STRING; |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 483 | } else if (!strncmp(value, "true", value_len) || !strncmp(value, "false", value_len)) { |
| 484 | /* boolean value */ |
| 485 | *hints |= LYD_VALHINT_BOOLEAN; |
| 486 | } else { |
Jan Kundrát | 863b18b | 2024-07-06 20:55:23 +0200 | [diff] [blame] | 487 | num = strtoll(value, &ptr, 10); |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 488 | if ((unsigned)(ptr - value) == value_len) { |
| 489 | /* number value */ |
| 490 | *hints |= LYD_VALHINT_DECNUM; |
Jan Kundrát | 863b18b | 2024-07-06 20:55:23 +0200 | [diff] [blame] | 491 | if ((num < INT32_MIN) || (num > UINT32_MAX)) { |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 492 | /* large number */ |
| 493 | *hints |= LYD_VALHINT_NUM64; |
| 494 | } |
| 495 | } else { |
| 496 | /* string value */ |
| 497 | *hints |= LYD_VALHINT_STRING; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (!first) { |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | /* search backwards to find the last instance */ |
| 506 | do { |
| 507 | first = first->prev; |
| 508 | if (first->schema) { |
| 509 | continue; |
| 510 | } |
| 511 | |
| 512 | opaq = (struct lyd_node_opaq *)first; |
| 513 | assert(opaq->format == LY_VALUE_XML); |
Michal Vasko | ce2e07c | 2022-12-01 11:08:52 +0100 | [diff] [blame] | 514 | if (!ly_strncmp(opaq->name.name, name, name_len) && |
zhangtaog | b348a8b | 2024-02-21 15:43:30 +0800 | [diff] [blame] | 515 | ((ns && opaq->name.module_ns && !strcmp(opaq->name.module_ns, ns)) || (!ns && !opaq->name.module_ns))) { |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 516 | if (opaq->value && opaq->value[0]) { |
| 517 | /* leaf-list nodes */ |
| 518 | opaq->hints |= LYD_NODEHINT_LEAFLIST; |
| 519 | *hints |= LYD_NODEHINT_LEAFLIST; |
| 520 | } else { |
| 521 | /* list nodes */ |
| 522 | opaq->hints |= LYD_NODEHINT_LIST; |
| 523 | *hints |= LYD_NODEHINT_LIST; |
| 524 | } |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 525 | *anchor = (struct lyd_node *)first; |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 526 | break; |
| 527 | } |
| 528 | } while (first->prev->next); |
| 529 | } |
| 530 | |
| 531 | /** |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 532 | * @brief Get schema node for the current element. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 533 | * |
| 534 | * @param[in] lydctx XML data parser context. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 535 | * @param[in] parent Parsed parent data node, if any. |
| 536 | * @param[in] prefix Element prefix, if any. |
| 537 | * @param[in] prefix_len Length of @p prefix. |
| 538 | * @param[in] name Element name. |
| 539 | * @param[in] name_len Length of @p name. |
| 540 | * @param[out] snode Found schema node, NULL if no suitable was found. |
| 541 | * @param[out] ext Extension instance that provided @p snode, if any. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 542 | * @return LY_SUCCESS on success; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 543 | * @return LY_ERR on error. |
| 544 | */ |
| 545 | static LY_ERR |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 546 | lydxml_subtree_get_snode(struct lyd_xml_ctx *lydctx, const struct lyd_node *parent, const char *prefix, size_t prefix_len, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 547 | const char *name, size_t name_len, const struct lysc_node **snode, struct lysc_ext_instance **ext) |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 548 | { |
| 549 | LY_ERR r; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 550 | struct lyxml_ctx *xmlctx; |
| 551 | const struct ly_ctx *ctx; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 552 | const struct lyxml_ns *ns; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 553 | struct lys_module *mod; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 554 | uint32_t getnext_opts; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 555 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 556 | xmlctx = lydctx->xmlctx; |
| 557 | ctx = xmlctx->ctx; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 558 | getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 559 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 560 | *snode = NULL; |
| 561 | *ext = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 562 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 563 | /* get current namespace */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 564 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 565 | if (!ns) { |
Michal Vasko | ce2e07c | 2022-12-01 11:08:52 +0100 | [diff] [blame] | 566 | if (lydctx->int_opts & LYD_INTOPT_ANY) { |
| 567 | goto unknown_module; |
| 568 | } |
aPiecek | 77a4f50 | 2024-07-03 08:34:48 +0200 | [diff] [blame] | 569 | lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 570 | return LY_EVALID; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 571 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 572 | |
| 573 | /* get the element module, use parent context if possible because of extensions */ |
| 574 | mod = ly_ctx_get_module_implemented_ns(parent ? LYD_CTX(parent) : ctx, ns->uri); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 575 | if (!mod) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 576 | /* check for extension data */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 577 | r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, name_len, |
| 578 | snode, ext); |
| 579 | if (r != LY_ENOT) { |
| 580 | /* success or error */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 581 | return r; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Michal Vasko | ce2e07c | 2022-12-01 11:08:52 +0100 | [diff] [blame] | 584 | unknown_module: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 585 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 586 | if (ns) { |
| 587 | LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri); |
| 588 | } else if (prefix_len) { |
| 589 | LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%.*s\" in the context.", (int)prefix_len, prefix); |
| 590 | } else { |
| 591 | LOGVAL(ctx, LYVE_REFERENCE, "No default namespace in the context."); |
| 592 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 593 | return LY_EVALID; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 594 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 595 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 596 | } |
| 597 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 598 | /* get the schema node */ |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 599 | if (!parent && lydctx->ext) { |
| 600 | *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts); |
| 601 | } else { |
| 602 | /* try to find parent schema node even if it is an opaque node (not connected to the parent) */ |
| 603 | *snode = lys_find_child(lyd_parser_node_schema(parent), mod, name, name_len, 0, getnext_opts); |
| 604 | } |
| 605 | if (!*snode) { |
| 606 | /* check for extension data */ |
| 607 | r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, |
| 608 | name_len, snode, ext); |
| 609 | if (r != LY_ENOT) { |
| 610 | /* success or error */ |
| 611 | return r; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 612 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 613 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 614 | /* unknown data node */ |
| 615 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
| 616 | if (parent) { |
| 617 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.", |
| 618 | (int)name_len, name, LYD_NAME(parent)); |
| 619 | } else if (lydctx->ext) { |
| 620 | if (lydctx->ext->argument) { |
| 621 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.", |
| 622 | (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 623 | } else { |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 624 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.", |
| 625 | (int)name_len, name, lydctx->ext->def->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 626 | } |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 627 | } else { |
| 628 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.", |
| 629 | (int)name_len, name, mod->name); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 630 | } |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 631 | return LY_EVALID; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 632 | } |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 633 | return LY_SUCCESS; |
| 634 | } else { |
| 635 | /* check that schema node is valid and can be used */ |
| 636 | LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode)); |
| 637 | LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode)); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 638 | } |
| 639 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 640 | return LY_SUCCESS; |
| 641 | } |
| 642 | |
| 643 | /** |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 644 | * @brief Parse an XML opque node. |
| 645 | * |
| 646 | * @param[in] lydctx XML YANG data parser context. |
| 647 | * @param[in] sibling Existing sibling node, if any. |
| 648 | * @param[in] prefix Parsed node prefix. |
| 649 | * @param[in] prefix_len Length of @p prefix. |
| 650 | * @param[in] name Parsed node name. |
| 651 | * @param[in] name_len Length of @p name. |
| 652 | * @param[out] insert_anchor Optional anchor node for inserting this node. |
| 653 | * @param[out] node Created node. |
| 654 | * @return LY_ERR value. |
| 655 | */ |
| 656 | static LY_ERR |
| 657 | lydxml_subtree_opaq(struct lyd_xml_ctx *lydctx, const struct lyd_node *sibling, const char *prefix, uint32_t prefix_len, |
| 658 | const char *name, uint32_t name_len, struct lyd_node **insert_anchor, struct lyd_node **node) |
| 659 | { |
| 660 | LY_ERR rc = LY_SUCCESS; |
| 661 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 662 | struct lyd_node_opaq *opaq; |
| 663 | const char *ns_uri, *value = NULL; |
| 664 | size_t value_len; |
| 665 | ly_bool ws_only, dynamic = 0; |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 666 | const struct lyxml_ns *ns; |
| 667 | uint32_t hints; |
| 668 | void *val_prefix_data = NULL; |
| 669 | LY_VALUE_FORMAT format; |
| 670 | |
| 671 | assert(lydctx->parse_opts & LYD_PARSE_OPAQ); |
| 672 | |
| 673 | *node = NULL; |
| 674 | |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 675 | /* remember the value */ |
| 676 | value = xmlctx->value; |
| 677 | value_len = xmlctx->value_len; |
| 678 | ws_only = xmlctx->ws_only; |
| 679 | dynamic = xmlctx->dynamic; |
| 680 | if (dynamic) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 681 | xmlctx->dynamic = 0; |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 682 | } |
| 683 | |
Michal Vasko | 535d21c | 2023-08-09 10:41:44 +0200 | [diff] [blame] | 684 | /* get value prefixes, if any */ |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 685 | rc = ly_store_prefix_data(xmlctx->ctx, value, value_len, LY_VALUE_XML, &xmlctx->ns, &format, &val_prefix_data); |
Michal Vasko | 535d21c | 2023-08-09 10:41:44 +0200 | [diff] [blame] | 686 | LY_CHECK_GOTO(rc, cleanup); |
| 687 | |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 688 | /* get NS again, it may have been backed up and restored */ |
| 689 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
| 690 | ns_uri = ns ? ns->uri : NULL; |
| 691 | |
| 692 | /* get best-effort node hints */ |
| 693 | lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, sibling, ns_uri, &hints, insert_anchor); |
| 694 | |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 695 | /* create the node without value */ |
| 696 | rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0, NULL, 0, |
| 697 | NULL, format, NULL, hints, node); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 698 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 699 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 700 | assert(*node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 701 | LOG_LOCSET(NULL, *node); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 702 | |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 703 | /* parser next */ |
| 704 | rc = lyxml_ctx_next(xmlctx); |
| 705 | LY_CHECK_GOTO(rc, cleanup); |
| 706 | |
| 707 | /* process children */ |
| 708 | while (xmlctx->status == LYXML_ELEMENT) { |
| 709 | rc = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); |
| 710 | LY_CHECK_GOTO(rc, cleanup); |
| 711 | } |
| 712 | |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 713 | /* update the value */ |
| 714 | opaq = (struct lyd_node_opaq *)*node; |
| 715 | if (opaq->child) { |
| 716 | if (!ws_only) { |
| 717 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(opaq)); |
| 718 | rc = LY_EVALID; |
| 719 | goto cleanup; |
| 720 | } |
| 721 | } else if (value_len) { |
| 722 | lydict_remove(xmlctx->ctx, opaq->value); |
| 723 | if (dynamic) { |
| 724 | LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup); |
| 725 | dynamic = 0; |
| 726 | } else { |
| 727 | LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /* always store val_prefix_data because the format requires them */ |
| 732 | assert(!opaq->val_prefix_data); |
| 733 | opaq->val_prefix_data = val_prefix_data; |
| 734 | val_prefix_data = NULL; |
| 735 | |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 736 | cleanup: |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 737 | if (*node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 738 | LOG_LOCBACK(0, 1); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 739 | } |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 740 | ly_free_prefix_data(format, val_prefix_data); |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 741 | if (dynamic) { |
| 742 | free((char *)value); |
| 743 | } |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 744 | if (rc) { |
| 745 | lyd_free_tree(*node); |
| 746 | *node = NULL; |
| 747 | } |
| 748 | return rc; |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * @brief Parse an XML leaf/leaf-list node. |
| 753 | * |
| 754 | * @param[in] lydctx XML YANG data parser context. |
| 755 | * @param[in] parent Parent node, if any. |
| 756 | * @param[in] snode Schema node of the new node. |
| 757 | * @param[out] node Created node. |
| 758 | * @return LY_ERR value. |
| 759 | */ |
| 760 | static LY_ERR |
| 761 | lydxml_subtree_term(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, const struct lysc_node *snode, |
| 762 | struct lyd_node **node) |
| 763 | { |
| 764 | LY_ERR r, rc = LY_SUCCESS; |
| 765 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
| 766 | struct lyd_node *anchor; |
| 767 | |
| 768 | *node = NULL; |
| 769 | |
| 770 | /* create node */ |
| 771 | r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, |
| 772 | LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, node); |
| 773 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 774 | |
| 775 | if (*node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 776 | LOG_LOCSET(NULL, *node); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 777 | } |
| 778 | |
Michal Vasko | 58a1a70 | 2023-03-01 13:45:38 +0100 | [diff] [blame] | 779 | if (*node && parent && (snode->flags & LYS_KEY)) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 780 | /* check the key order, the anchor must never be a key */ |
| 781 | anchor = lyd_insert_get_next_anchor(lyd_child(parent), *node); |
| 782 | if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) { |
| 783 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
| 784 | LOGVAL(xmlctx->ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", snode->name); |
| 785 | r = LY_EVALID; |
| 786 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 787 | } else { |
| 788 | LOGWRN(xmlctx->ctx, "Invalid position of the key \"%s\" in a list.", snode->name); |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /* parser next */ |
| 794 | r = lyxml_ctx_next(xmlctx); |
| 795 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 796 | |
| 797 | /* no children expected */ |
| 798 | if (xmlctx->status == LYXML_ELEMENT) { |
| 799 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.", |
| 800 | (int)xmlctx->name_len, xmlctx->name, snode->name); |
| 801 | r = LY_EVALID; |
| 802 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 803 | } |
| 804 | |
| 805 | cleanup: |
| 806 | if (*node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 807 | LOG_LOCBACK(0, 1); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 808 | } |
| 809 | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
| 810 | lyd_free_tree(*node); |
| 811 | *node = NULL; |
| 812 | } |
| 813 | return rc; |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * @brief Parse an XML inner node. |
| 818 | * |
| 819 | * @param[in] lydctx XML YANG data parser context. |
| 820 | * @param[in] snode Schema node of the new node. |
| 821 | * @param[in] ext Extension instance of @p snode, if any. |
| 822 | * @param[out] node Created node. |
| 823 | * @return LY_ERR value. |
| 824 | */ |
| 825 | static LY_ERR |
| 826 | lydxml_subtree_inner(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext, |
| 827 | struct lyd_node **node) |
| 828 | { |
| 829 | LY_ERR r, rc = LY_SUCCESS; |
| 830 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
| 831 | uint32_t prev_parse_opts = lydctx->parse_opts; |
| 832 | |
| 833 | *node = NULL; |
| 834 | |
| 835 | if (!xmlctx->ws_only) { |
| 836 | /* value in inner node */ |
| 837 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.", |
| 838 | (int)xmlctx->value_len, xmlctx->value, snode->name); |
| 839 | r = LY_EVALID; |
| 840 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 841 | } |
| 842 | |
| 843 | /* create node */ |
| 844 | rc = lyd_create_inner(snode, node); |
| 845 | LY_CHECK_GOTO(rc, cleanup); |
| 846 | |
Michal Vasko | 58a1a70 | 2023-03-01 13:45:38 +0100 | [diff] [blame] | 847 | assert(*node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 848 | LOG_LOCSET(NULL, *node); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 849 | |
| 850 | /* parser next */ |
| 851 | rc = lyxml_ctx_next(xmlctx); |
| 852 | LY_CHECK_GOTO(rc, cleanup); |
| 853 | |
| 854 | if (ext) { |
| 855 | /* only parse these extension data and validate afterwards */ |
| 856 | lydctx->parse_opts |= LYD_PARSE_ONLY; |
| 857 | } |
| 858 | |
| 859 | /* process children */ |
| 860 | while (xmlctx->status == LYXML_ELEMENT) { |
| 861 | r = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); |
| 862 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 863 | } |
| 864 | |
| 865 | /* restore options */ |
| 866 | lydctx->parse_opts = prev_parse_opts; |
| 867 | |
| 868 | if (snode->nodetype == LYS_LIST) { |
| 869 | /* check all keys exist */ |
Michal Vasko | 0af8370 | 2024-10-08 13:42:19 +0200 | [diff] [blame] | 870 | r = lyd_parser_check_keys(*node); |
Michal Vasko | 202d816 | 2023-03-01 14:42:19 +0100 | [diff] [blame] | 871 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 872 | } |
| 873 | |
Michal Vasko | 4001573 | 2023-11-20 13:48:15 +0100 | [diff] [blame] | 874 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY) && !rc) { |
| 875 | /* new node validation, autodelete CANNOT occur (it can if multi-error), all nodes are new */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 876 | r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL); |
| 877 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 878 | |
| 879 | /* add any missing default children */ |
| 880 | r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types, |
| 881 | &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL); |
| 882 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 883 | } |
| 884 | |
| 885 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 886 | /* rememeber the RPC/action/notification */ |
| 887 | lydctx->op_node = *node; |
| 888 | } |
| 889 | |
| 890 | cleanup: |
| 891 | if (*node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 892 | LOG_LOCBACK(0, 1); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 893 | } |
| 894 | lydctx->parse_opts = prev_parse_opts; |
Michal Vasko | 202d816 | 2023-03-01 14:42:19 +0100 | [diff] [blame] | 895 | if (rc && ((*node && !(*node)->hash) || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
| 896 | /* list without keys is unusable or an error */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 897 | lyd_free_tree(*node); |
| 898 | *node = NULL; |
| 899 | } |
| 900 | return rc; |
| 901 | } |
| 902 | |
| 903 | /** |
| 904 | * @brief Parse an XML anyxml/anydata node. |
| 905 | * |
| 906 | * @param[in] lydctx XML YANG data parser context. |
| 907 | * @param[in] snode Schema node of the new node. |
| 908 | * @param[in] ext Extension instance of @p snode, if any. |
| 909 | * @param[out] node Created node. |
| 910 | * @return LY_ERR value. |
| 911 | */ |
| 912 | static LY_ERR |
| 913 | lydxml_subtree_any(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext, |
| 914 | struct lyd_node **node) |
| 915 | { |
| 916 | LY_ERR r, rc = LY_SUCCESS; |
| 917 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
| 918 | uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts; |
| 919 | struct lyd_node *child = NULL; |
| 920 | char *val = NULL; |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 921 | ly_bool log_node = 0; |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 922 | |
| 923 | *node = NULL; |
| 924 | |
| 925 | if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) { |
| 926 | /* value in anydata node, we expect a tree */ |
| 927 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.", |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 928 | xmlctx->value_len < 20 ? (int)xmlctx->value_len : 20, xmlctx->value, snode->name); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 929 | r = LY_EVALID; |
| 930 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 931 | } |
| 932 | |
| 933 | if (!xmlctx->ws_only) { |
| 934 | /* use an arbitrary text value for anyxml */ |
| 935 | val = strndup(xmlctx->value, xmlctx->value_len); |
| 936 | LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup); |
| 937 | |
| 938 | /* parser next */ |
| 939 | r = lyxml_ctx_next(xmlctx); |
| 940 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 941 | |
| 942 | /* create node */ |
| 943 | r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node); |
| 944 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 945 | val = NULL; |
| 946 | } else { |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 947 | /* create node */ |
| 948 | r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node); |
| 949 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 950 | |
| 951 | assert(*node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 952 | LOG_LOCSET(NULL, *node); |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 953 | log_node = 1; |
| 954 | |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 955 | /* parser next */ |
| 956 | r = lyxml_ctx_next(xmlctx); |
| 957 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 958 | |
| 959 | /* update options so that generic data can be parsed */ |
| 960 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
| 961 | lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0); |
| 962 | lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS; |
| 963 | |
| 964 | /* parse any data tree */ |
| 965 | while (xmlctx->status == LYXML_ELEMENT) { |
| 966 | r = lydxml_subtree_r(lydctx, NULL, &child, NULL); |
| 967 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 968 | } |
| 969 | |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 970 | /* assign the data tree */ |
| 971 | ((struct lyd_node_any *)*node)->value.tree = child; |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 972 | child = NULL; |
| 973 | } |
| 974 | |
| 975 | cleanup: |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 976 | if (log_node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 977 | LOG_LOCBACK(0, 1); |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 978 | } |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 979 | lydctx->parse_opts = prev_parse_opts; |
| 980 | lydctx->int_opts = prev_int_opts; |
| 981 | free(val); |
| 982 | lyd_free_tree(child); |
| 983 | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
| 984 | lyd_free_tree(*node); |
| 985 | *node = NULL; |
| 986 | } |
| 987 | return rc; |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * @brief Parse an XML subtree, recursively. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 992 | * |
| 993 | * @param[in] lydctx XML YANG data parser context. |
| 994 | * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements. |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 995 | * @param[in,out] first_p Pointer to the first (@p parent or top-level) child. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 996 | * @param[in,out] parsed Optional set to add all the parsed siblings into. |
| 997 | * @return LY_ERR value. |
| 998 | */ |
| 999 | static LY_ERR |
| 1000 | lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
| 1001 | { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1002 | LY_ERR r, rc = LY_SUCCESS; |
| 1003 | const char *prefix, *name; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1004 | size_t prefix_len, name_len; |
| 1005 | struct lyxml_ctx *xmlctx; |
| 1006 | const struct ly_ctx *ctx; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1007 | struct lyd_meta *meta = NULL; |
| 1008 | struct lyd_attr *attr = NULL; |
Michal Vasko | 4a1e3e8 | 2023-09-05 08:45:01 +0200 | [diff] [blame] | 1009 | const struct lysc_node *snode = NULL; |
| 1010 | struct lysc_ext_instance *ext = NULL; |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1011 | uint32_t orig_parse_opts; |
| 1012 | struct lyd_node *node = NULL, *insert_anchor = NULL; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1013 | ly_bool parse_subtree; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1014 | |
| 1015 | assert(parent || first_p); |
| 1016 | |
| 1017 | xmlctx = lydctx->xmlctx; |
| 1018 | ctx = xmlctx->ctx; |
| 1019 | |
| 1020 | parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0; |
| 1021 | /* all descendants should be parsed */ |
| 1022 | lydctx->parse_opts &= ~LYD_PARSE_SUBTREE; |
| 1023 | orig_parse_opts = lydctx->parse_opts; |
| 1024 | |
| 1025 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1026 | |
| 1027 | /* remember element prefix and name */ |
| 1028 | prefix = xmlctx->prefix; |
| 1029 | prefix_len = xmlctx->prefix_len; |
| 1030 | name = xmlctx->name; |
| 1031 | name_len = xmlctx->name_len; |
| 1032 | |
| 1033 | /* parser next */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1034 | rc = lyxml_ctx_next(xmlctx); |
| 1035 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1036 | |
Michal Vasko | 4a1e3e8 | 2023-09-05 08:45:01 +0200 | [diff] [blame] | 1037 | if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && name_len && !prefix_len && |
| 1038 | !ly_strncmp("eventTime", name, name_len)) { |
| 1039 | /* parse eventTime, create node */ |
| 1040 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 1041 | rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, |
| 1042 | "urn:ietf:params:xml:ns:netconf:notification:1.0", 47, xmlctx->value, |
| 1043 | xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA, &node); |
| 1044 | LY_CHECK_GOTO(rc, cleanup); |
| 1045 | |
| 1046 | /* validate the value */ |
| 1047 | r = lyd_parser_notif_eventtime_validate(node); |
| 1048 | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
| 1049 | |
| 1050 | /* parser next */ |
| 1051 | r = lyxml_ctx_next(xmlctx); |
| 1052 | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
| 1053 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1054 | LOGVAL(ctx, LYVE_DATA, "Unexpected notification \"eventTime\" node children."); |
| 1055 | rc = LY_EVALID; |
| 1056 | lyd_free_tree(node); |
| 1057 | goto cleanup; |
| 1058 | } |
| 1059 | |
| 1060 | goto node_parsed; |
| 1061 | } |
| 1062 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1063 | /* get the schema node */ |
Michal Vasko | 202d816 | 2023-03-01 14:42:19 +0100 | [diff] [blame] | 1064 | r = lydxml_subtree_get_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext); |
| 1065 | if (r) { |
| 1066 | rc = r; |
| 1067 | if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) { |
| 1068 | /* skip the invalid data */ |
| 1069 | if ((r = lydxml_data_skip(xmlctx))) { |
| 1070 | rc = r; |
| 1071 | } |
| 1072 | } |
| 1073 | goto cleanup; |
| 1074 | } else if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 1075 | LOGVRB("Skipping parsing of unknown node \"%.*s\".", (int)name_len, name); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1076 | |
| 1077 | /* skip element with children */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1078 | rc = lydxml_data_skip(xmlctx); |
| 1079 | goto cleanup; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1080 | } |
| 1081 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1082 | /* create metadata/attributes */ |
| 1083 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 1084 | if (snode) { |
aPiecek | f410c2e | 2024-06-20 13:09:07 +0200 | [diff] [blame] | 1085 | r = lydxml_metadata(lydctx, snode, &meta); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1086 | } else { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1087 | assert(lydctx->parse_opts & LYD_PARSE_OPAQ); |
aPiecek | f410c2e | 2024-06-20 13:09:07 +0200 | [diff] [blame] | 1088 | r = lydxml_attrs(xmlctx, &attr); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1089 | } |
| 1090 | } |
aPiecek | f410c2e | 2024-06-20 13:09:07 +0200 | [diff] [blame] | 1091 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1092 | |
| 1093 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 1094 | if (!snode) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1095 | /* opaque */ |
| 1096 | r = lydxml_subtree_opaq(lydctx, parent ? lyd_child(parent) : *first_p, prefix, prefix_len, name, name_len, |
| 1097 | &insert_anchor, &node); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1098 | } else if (snode->nodetype & LYD_NODE_TERM) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1099 | /* term */ |
| 1100 | r = lydxml_subtree_term(lydctx, parent, snode, &node); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1101 | } else if (snode->nodetype & LYD_NODE_INNER) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1102 | /* inner */ |
| 1103 | r = lydxml_subtree_inner(lydctx, snode, ext, &node); |
Michal Vasko | 7c6f33f | 2023-02-10 09:40:11 +0100 | [diff] [blame] | 1104 | } else { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1105 | /* any */ |
Michal Vasko | 7c6f33f | 2023-02-10 09:40:11 +0100 | [diff] [blame] | 1106 | assert(snode->nodetype & LYD_NODE_ANY); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1107 | r = lydxml_subtree_any(lydctx, snode, ext, &node); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1108 | } |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1109 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
Michal Vasko | 1524f1a | 2023-03-01 09:36:34 +0100 | [diff] [blame] | 1110 | |
Michal Vasko | 4a1e3e8 | 2023-09-05 08:45:01 +0200 | [diff] [blame] | 1111 | node_parsed: |
Michal Vasko | 58a1a70 | 2023-03-01 13:45:38 +0100 | [diff] [blame] | 1112 | if (node && snode) { |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1113 | /* add/correct flags */ |
Michal Vasko | 0af8370 | 2024-10-08 13:42:19 +0200 | [diff] [blame] | 1114 | r = lyd_parser_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext); |
Michal Vasko | 567d703 | 2023-02-13 08:53:31 +0100 | [diff] [blame] | 1115 | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1116 | |
Michal Vasko | eba2311 | 2022-08-26 08:35:41 +0200 | [diff] [blame] | 1117 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 1118 | /* store for ext instance node validation, if needed */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1119 | r = lyd_validate_node_ext(node, &lydctx->ext_node); |
| 1120 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
Michal Vasko | eba2311 | 2022-08-26 08:35:41 +0200 | [diff] [blame] | 1121 | } |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | /* parser next */ |
| 1125 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1126 | if (!parse_subtree) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1127 | r = lyxml_ctx_next(xmlctx); |
Michal Vasko | 567d703 | 2023-02-13 08:53:31 +0100 | [diff] [blame] | 1128 | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1129 | } |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1130 | |
Michal Vasko | 58a1a70 | 2023-03-01 13:45:38 +0100 | [diff] [blame] | 1131 | LY_CHECK_GOTO(!node, cleanup); |
| 1132 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1133 | /* add metadata/attributes */ |
| 1134 | if (snode) { |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1135 | lyd_insert_meta(node, meta, 0); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1136 | meta = NULL; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1137 | } else { |
| 1138 | lyd_insert_attr(node, attr); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1139 | attr = NULL; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | /* insert, keep first pointer correct */ |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 1143 | if (insert_anchor) { |
| 1144 | lyd_insert_after(insert_anchor, node); |
| 1145 | } else if (ext) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1146 | r = lyplg_ext_insert(parent, node); |
Michal Vasko | 567d703 | 2023-02-13 08:53:31 +0100 | [diff] [blame] | 1147 | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1148 | } else { |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 1149 | lyd_insert_node(parent, first_p, node, |
| 1150 | lydctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1151 | } |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1152 | while (!parent && (*first_p)->prev->next) { |
| 1153 | *first_p = (*first_p)->prev; |
| 1154 | } |
| 1155 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1156 | /* rememeber a successfully parsed node */ |
| 1157 | if (parsed) { |
| 1158 | ly_set_add(parsed, node, 1, NULL); |
| 1159 | } |
| 1160 | |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1161 | cleanup: |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1162 | lydctx->parse_opts = orig_parse_opts; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1163 | lyd_free_meta_siblings(meta); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1164 | lyd_free_attr_siblings(ctx, attr); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1165 | return rc; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1166 | } |
| 1167 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1168 | /** |
| 1169 | * @brief Parse a specific XML element into an opaque node. |
| 1170 | * |
| 1171 | * @param[in] xmlctx XML parser context. |
| 1172 | * @param[in] name Name of the element. |
| 1173 | * @param[in] uri URI of the element. |
| 1174 | * @param[in] value Whether a value is expected in the element. |
| 1175 | * @param[out] evnp Parsed envelope (opaque node). |
| 1176 | * @return LY_SUCCESS on success. |
| 1177 | * @return LY_ENOT if the specified element did not match. |
| 1178 | * @return LY_ERR value on error. |
| 1179 | */ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1180 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1181 | lydxml_envelope(struct lyxml_ctx *xmlctx, const char *name, const char *uri, ly_bool value, struct lyd_node **envp) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1182 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1183 | LY_ERR rc = LY_SUCCESS; |
| 1184 | const struct lyxml_ns *ns; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1185 | struct lyd_attr *attr = NULL; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1186 | const char *prefix; |
| 1187 | size_t prefix_len; |
| 1188 | |
Michal Vasko | f048ba5 | 2023-06-13 10:40:43 +0200 | [diff] [blame] | 1189 | if (xmlctx->status != LYXML_ELEMENT) { |
| 1190 | /* nothing to parse */ |
| 1191 | return LY_ENOT; |
| 1192 | } |
| 1193 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1194 | if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) { |
| 1195 | /* not the expected element */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1196 | return LY_ENOT; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | prefix = xmlctx->prefix; |
| 1200 | prefix_len = xmlctx->prefix_len; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1201 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1202 | if (!ns) { |
aPiecek | 77a4f50 | 2024-07-03 08:34:48 +0200 | [diff] [blame] | 1203 | lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1204 | return LY_EVALID; |
| 1205 | } else if (strcmp(ns->uri, uri)) { |
| 1206 | /* different namespace */ |
| 1207 | return LY_ENOT; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 1211 | |
| 1212 | /* create attributes */ |
| 1213 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 1214 | LY_CHECK_RET(lydxml_attrs(xmlctx, &attr)); |
| 1215 | } |
| 1216 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1217 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 1218 | if (!value && !xmlctx->ws_only) { |
| 1219 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1220 | (int)xmlctx->value_len, xmlctx->value, name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1221 | rc = LY_EVALID; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1222 | goto cleanup; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1223 | } |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1224 | |
| 1225 | /* create node */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1226 | rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1227 | xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1228 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1229 | |
| 1230 | /* assign atributes */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1231 | ((struct lyd_node_opaq *)(*envp))->attr = attr; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1232 | attr = NULL; |
| 1233 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1234 | /* parser next element */ |
| 1235 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1236 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1237 | cleanup: |
| 1238 | lyd_free_attr_siblings(xmlctx->ctx, attr); |
| 1239 | if (rc) { |
| 1240 | lyd_free_tree(*envp); |
| 1241 | *envp = NULL; |
| 1242 | } |
| 1243 | return rc; |
| 1244 | } |
| 1245 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1246 | LY_ERR |
| 1247 | lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 1248 | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts, |
| 1249 | struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p) |
| 1250 | { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1251 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1252 | struct lyd_xml_ctx *lydctx; |
Michal Vasko | 6459269 | 2023-06-12 13:50:11 +0200 | [diff] [blame] | 1253 | ly_bool parsed_data_nodes = 0, close_elem = 0; |
| 1254 | struct lyd_node *act = NULL; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1255 | enum LYXML_PARSER_STATUS status; |
| 1256 | |
| 1257 | assert(ctx && in && lydctx_p); |
| 1258 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
| 1259 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
| 1260 | |
| 1261 | /* init context */ |
| 1262 | lydctx = calloc(1, sizeof *lydctx); |
| 1263 | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
| 1264 | LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup); |
| 1265 | lydctx->parse_opts = parse_opts; |
| 1266 | lydctx->val_opts = val_opts; |
| 1267 | lydctx->int_opts = int_opts; |
| 1268 | lydctx->free = lyd_xml_ctx_free; |
| 1269 | lydctx->ext = ext; |
| 1270 | |
| 1271 | /* find the operation node if it exists already */ |
| 1272 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
| 1273 | |
Michal Vasko | 6459269 | 2023-06-12 13:50:11 +0200 | [diff] [blame] | 1274 | if ((int_opts & LYD_INTOPT_RPC) && (int_opts & LYD_INTOPT_ACTION)) { |
| 1275 | /* can be either, try to parse "action" */ |
| 1276 | if (!lydxml_envelope(lydctx->xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &act)) { |
| 1277 | close_elem = 1; |
| 1278 | int_opts &= ~LYD_INTOPT_RPC; |
| 1279 | } |
| 1280 | } |
| 1281 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1282 | /* parse XML data */ |
| 1283 | while (lydctx->xmlctx->status == LYXML_ELEMENT) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1284 | r = lydxml_subtree_r(lydctx, parent, first_p, parsed); |
| 1285 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 1286 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1287 | parsed_data_nodes = 1; |
| 1288 | |
| 1289 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1290 | break; |
| 1291 | } |
| 1292 | } |
| 1293 | |
Michal Vasko | 6459269 | 2023-06-12 13:50:11 +0200 | [diff] [blame] | 1294 | /* close an opened element */ |
| 1295 | if (close_elem) { |
| 1296 | if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1297 | assert(lydctx->xmlctx->status == LYXML_ELEMENT); |
| 1298 | LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".", |
| 1299 | (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name); |
| 1300 | rc = LY_EVALID; |
| 1301 | goto cleanup; |
| 1302 | } |
| 1303 | |
| 1304 | LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup); |
| 1305 | } |
| 1306 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1307 | /* check final state */ |
| 1308 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) { |
| 1309 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1310 | r = LY_EVALID; |
| 1311 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1312 | } |
| 1313 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
| 1314 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1315 | r = LY_EVALID; |
| 1316 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | if (!parsed_data_nodes) { |
| 1320 | /* no data nodes were parsed */ |
| 1321 | lydctx->op_node = NULL; |
| 1322 | } |
| 1323 | |
| 1324 | if (parse_opts & LYD_PARSE_SUBTREE) { |
| 1325 | /* check for a sibling element */ |
| 1326 | assert(subtree_sibling); |
| 1327 | if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) { |
| 1328 | *subtree_sibling = 1; |
| 1329 | } else { |
| 1330 | *subtree_sibling = 0; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | cleanup: |
| 1335 | /* there should be no unres stored if validation should be skipped */ |
| 1336 | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count && |
| 1337 | !lydctx->node_when.count)); |
| 1338 | |
Michal Vasko | 6459269 | 2023-06-12 13:50:11 +0200 | [diff] [blame] | 1339 | lyd_free_tree(act); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1340 | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1341 | lyd_xml_ctx_free((struct lyd_ctx *)lydctx); |
| 1342 | } else { |
| 1343 | *lydctx_p = (struct lyd_ctx *)lydctx; |
| 1344 | |
| 1345 | /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */ |
| 1346 | lyxml_ctx_free(lydctx->xmlctx); |
| 1347 | lydctx->xmlctx = NULL; |
| 1348 | } |
| 1349 | return rc; |
| 1350 | } |
| 1351 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1352 | /** |
| 1353 | * @brief Parse all expected non-data XML elements of a NETCONF rpc message. |
| 1354 | * |
| 1355 | * @param[in] xmlctx XML parser context. |
| 1356 | * @param[out] evnp Parsed envelope(s) (opaque node). |
| 1357 | * @param[out] int_opts Internal options for parsing the rest of YANG data. |
| 1358 | * @param[out] close_elem Number of parsed opened elements that need to be closed. |
| 1359 | * @return LY_SUCCESS on success. |
| 1360 | * @return LY_ERR value on error. |
| 1361 | */ |
| 1362 | static LY_ERR |
| 1363 | lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem) |
| 1364 | { |
| 1365 | LY_ERR rc = LY_SUCCESS, r; |
| 1366 | struct lyd_node *child; |
| 1367 | |
| 1368 | assert(envp && !*envp); |
| 1369 | |
| 1370 | /* parse "rpc" */ |
| 1371 | r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1372 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 1373 | |
| 1374 | /* parse "action", if any */ |
| 1375 | r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child); |
| 1376 | if (r == LY_SUCCESS) { |
| 1377 | /* insert */ |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 1378 | lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_DEFAULT); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1379 | |
| 1380 | /* NETCONF action */ |
| 1381 | *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION; |
| 1382 | *close_elem = 2; |
| 1383 | } else if (r == LY_ENOT) { |
| 1384 | /* NETCONF RPC */ |
| 1385 | *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC; |
| 1386 | *close_elem = 1; |
| 1387 | } else { |
| 1388 | rc = r; |
| 1389 | goto cleanup; |
| 1390 | } |
| 1391 | |
| 1392 | cleanup: |
| 1393 | if (rc) { |
| 1394 | lyd_free_tree(*envp); |
| 1395 | *envp = NULL; |
| 1396 | } |
| 1397 | return rc; |
| 1398 | } |
| 1399 | |
| 1400 | /** |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1401 | * @brief Parse an XML element as an opaque node subtree. |
| 1402 | * |
| 1403 | * @param[in] xmlctx XML parser context. |
| 1404 | * @param[in] parent Parent to append nodes to. |
| 1405 | * @return LY_ERR value. |
| 1406 | */ |
| 1407 | static LY_ERR |
| 1408 | lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent) |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1409 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1410 | LY_ERR rc = LY_SUCCESS; |
| 1411 | const struct lyxml_ns *ns; |
| 1412 | struct lyd_attr *attr = NULL; |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 1413 | struct lyd_node *node = NULL; |
| 1414 | struct lyd_node_opaq *opaq; |
| 1415 | const char *name, *prefix, *value = NULL; |
| 1416 | size_t name_len, prefix_len, value_len; |
| 1417 | ly_bool ws_only, dynamic = 0; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1418 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1419 | assert(xmlctx->status == LYXML_ELEMENT); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1420 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1421 | name = xmlctx->name; |
| 1422 | name_len = xmlctx->name_len; |
| 1423 | prefix = xmlctx->prefix; |
| 1424 | prefix_len = xmlctx->prefix_len; |
| 1425 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
| 1426 | if (!ns) { |
aPiecek | 77a4f50 | 2024-07-03 08:34:48 +0200 | [diff] [blame] | 1427 | lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1428 | return LY_EVALID; |
| 1429 | } |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1430 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1431 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1432 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1433 | /* create attributes */ |
| 1434 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 1435 | LY_CHECK_RET(lydxml_attrs(xmlctx, &attr)); |
| 1436 | } |
| 1437 | |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 1438 | /* remember the value */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1439 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 1440 | value = xmlctx->value; |
| 1441 | value_len = xmlctx->value_len; |
| 1442 | ws_only = xmlctx->ws_only; |
| 1443 | dynamic = xmlctx->dynamic; |
| 1444 | if (dynamic) { |
| 1445 | xmlctx->dynamic = 0; |
| 1446 | } |
| 1447 | |
| 1448 | /* create the node without value */ |
| 1449 | rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), NULL, 0, NULL, |
| 1450 | LY_VALUE_XML, NULL, 0, &node); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1451 | LY_CHECK_GOTO(rc, cleanup); |
| 1452 | |
| 1453 | /* assign atributes */ |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 1454 | ((struct lyd_node_opaq *)node)->attr = attr; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1455 | attr = NULL; |
| 1456 | |
| 1457 | /* parser next element */ |
| 1458 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
| 1459 | |
| 1460 | /* parse all the descendants */ |
| 1461 | while (xmlctx->status == LYXML_ELEMENT) { |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 1462 | rc = lydxml_opaq_r(xmlctx, node); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1463 | LY_CHECK_GOTO(rc, cleanup); |
| 1464 | } |
| 1465 | |
| 1466 | /* insert */ |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 1467 | lyd_insert_node(parent, NULL, node, LYD_INSERT_NODE_LAST); |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 1468 | |
| 1469 | /* update the value */ |
| 1470 | opaq = (struct lyd_node_opaq *)node; |
| 1471 | if (opaq->child) { |
| 1472 | if (!ws_only) { |
| 1473 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(node)); |
| 1474 | rc = LY_EVALID; |
| 1475 | goto cleanup; |
| 1476 | } |
| 1477 | } else if (value_len) { |
| 1478 | lydict_remove(xmlctx->ctx, opaq->value); |
| 1479 | if (dynamic) { |
| 1480 | LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup); |
| 1481 | dynamic = 0; |
| 1482 | } else { |
| 1483 | LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup); |
| 1484 | } |
| 1485 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1486 | |
| 1487 | cleanup: |
| 1488 | lyd_free_attr_siblings(xmlctx->ctx, attr); |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 1489 | if (dynamic) { |
| 1490 | free((char *)value); |
| 1491 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1492 | if (rc) { |
Michal Vasko | a16dbb4 | 2023-08-15 15:20:25 +0200 | [diff] [blame] | 1493 | lyd_free_tree(node); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1494 | } |
| 1495 | return rc; |
| 1496 | } |
| 1497 | |
| 1498 | /** |
| 1499 | * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message. |
| 1500 | * |
| 1501 | * @param[in] xmlctx XML parser context. |
| 1502 | * @param[in] parent Parent to append nodes to. |
| 1503 | * @return LY_ERR value. |
| 1504 | */ |
| 1505 | static LY_ERR |
| 1506 | lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent) |
| 1507 | { |
| 1508 | LY_ERR r; |
| 1509 | struct lyd_node *child, *iter; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1510 | ly_bool no_dup; |
| 1511 | |
| 1512 | /* there must be some child */ |
| 1513 | if (xmlctx->status == LYXML_ELEM_CLOSE) { |
| 1514 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\"."); |
| 1515 | return LY_EVALID; |
| 1516 | } |
| 1517 | |
| 1518 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1519 | child = NULL; |
| 1520 | |
| 1521 | /* |
| 1522 | * session-id |
| 1523 | */ |
| 1524 | r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1525 | if (r == LY_SUCCESS) { |
| 1526 | no_dup = 1; |
| 1527 | goto check_child; |
| 1528 | } else if (r != LY_ENOT) { |
| 1529 | goto error; |
| 1530 | } |
| 1531 | |
| 1532 | /* |
| 1533 | * bad-attribute |
| 1534 | */ |
| 1535 | r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1536 | if (r == LY_SUCCESS) { |
| 1537 | no_dup = 1; |
| 1538 | goto check_child; |
| 1539 | } else if (r != LY_ENOT) { |
| 1540 | goto error; |
| 1541 | } |
| 1542 | |
| 1543 | /* |
| 1544 | * bad-element |
| 1545 | */ |
| 1546 | r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1547 | if (r == LY_SUCCESS) { |
| 1548 | no_dup = 1; |
| 1549 | goto check_child; |
| 1550 | } else if (r != LY_ENOT) { |
| 1551 | goto error; |
| 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | * bad-namespace |
| 1556 | */ |
| 1557 | r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1558 | if (r == LY_SUCCESS) { |
| 1559 | no_dup = 1; |
| 1560 | goto check_child; |
| 1561 | } else if (r != LY_ENOT) { |
| 1562 | goto error; |
| 1563 | } |
| 1564 | |
| 1565 | if (r == LY_ENOT) { |
| 1566 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1567 | |
Michal Vasko | 845ba39 | 2022-06-16 07:52:13 +0200 | [diff] [blame] | 1568 | /* custom elements, parse all the siblings */ |
| 1569 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1570 | LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error); |
| 1571 | LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error); |
| 1572 | } |
Michal Vasko | 6398eaf | 2022-01-10 10:12:30 +0100 | [diff] [blame] | 1573 | continue; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | check_child: |
| 1577 | /* check for duplicates */ |
| 1578 | if (no_dup) { |
| 1579 | LY_LIST_FOR(lyd_child(parent), iter) { |
| 1580 | if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) && |
| 1581 | (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) { |
| 1582 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".", |
| 1583 | ((struct lyd_node_opaq *)child)->name.name); |
| 1584 | r = LY_EVALID; |
| 1585 | goto error; |
| 1586 | } |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | /* finish child parsing */ |
| 1591 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1592 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1593 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1594 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1595 | r = LY_EVALID; |
| 1596 | goto error; |
| 1597 | } |
| 1598 | LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error); |
| 1599 | |
| 1600 | /* insert */ |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 1601 | lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | return LY_SUCCESS; |
| 1605 | |
| 1606 | error: |
| 1607 | lyd_free_tree(child); |
| 1608 | return r; |
| 1609 | } |
| 1610 | |
| 1611 | /** |
| 1612 | * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message. |
| 1613 | * |
| 1614 | * @param[in] xmlctx XML parser context. |
| 1615 | * @param[in] parent Parent to append nodes to. |
| 1616 | * @return LY_ERR value. |
| 1617 | */ |
| 1618 | static LY_ERR |
| 1619 | lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent) |
| 1620 | { |
| 1621 | LY_ERR r; |
| 1622 | struct lyd_node *child, *iter; |
| 1623 | const char *val; |
| 1624 | ly_bool no_dup; |
| 1625 | |
| 1626 | /* there must be some child */ |
| 1627 | if (xmlctx->status == LYXML_ELEM_CLOSE) { |
| 1628 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\"."); |
| 1629 | return LY_EVALID; |
| 1630 | } |
| 1631 | |
| 1632 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1633 | child = NULL; |
| 1634 | |
| 1635 | /* |
| 1636 | * error-type |
| 1637 | */ |
| 1638 | r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1639 | if (r == LY_SUCCESS) { |
| 1640 | val = ((struct lyd_node_opaq *)child)->value; |
| 1641 | if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) { |
| 1642 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val, |
| 1643 | ((struct lyd_node_opaq *)child)->name.name); |
| 1644 | r = LY_EVALID; |
| 1645 | goto error; |
| 1646 | } |
| 1647 | |
| 1648 | no_dup = 1; |
| 1649 | goto check_child; |
| 1650 | } else if (r != LY_ENOT) { |
| 1651 | goto error; |
| 1652 | } |
| 1653 | |
| 1654 | /* |
| 1655 | * error-tag |
| 1656 | */ |
| 1657 | r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1658 | if (r == LY_SUCCESS) { |
| 1659 | val = ((struct lyd_node_opaq *)child)->value; |
| 1660 | if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") && |
| 1661 | strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") && |
| 1662 | strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") && |
| 1663 | strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") && |
| 1664 | strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") && |
| 1665 | strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") && |
| 1666 | strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) { |
| 1667 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val, |
| 1668 | ((struct lyd_node_opaq *)child)->name.name); |
| 1669 | r = LY_EVALID; |
| 1670 | goto error; |
| 1671 | } |
| 1672 | |
| 1673 | no_dup = 1; |
| 1674 | goto check_child; |
| 1675 | } else if (r != LY_ENOT) { |
| 1676 | goto error; |
| 1677 | } |
| 1678 | |
| 1679 | /* |
| 1680 | * error-severity |
| 1681 | */ |
| 1682 | r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1683 | if (r == LY_SUCCESS) { |
| 1684 | val = ((struct lyd_node_opaq *)child)->value; |
| 1685 | if (strcmp(val, "error") && strcmp(val, "warning")) { |
| 1686 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val, |
| 1687 | ((struct lyd_node_opaq *)child)->name.name); |
| 1688 | r = LY_EVALID; |
| 1689 | goto error; |
| 1690 | } |
| 1691 | |
| 1692 | no_dup = 1; |
| 1693 | goto check_child; |
| 1694 | } else if (r != LY_ENOT) { |
| 1695 | goto error; |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * error-app-tag |
| 1700 | */ |
| 1701 | r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1702 | if (r == LY_SUCCESS) { |
| 1703 | no_dup = 1; |
| 1704 | goto check_child; |
| 1705 | } else if (r != LY_ENOT) { |
| 1706 | goto error; |
| 1707 | } |
| 1708 | |
| 1709 | /* |
| 1710 | * error-path |
| 1711 | */ |
| 1712 | r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1713 | if (r == LY_SUCCESS) { |
| 1714 | no_dup = 1; |
| 1715 | goto check_child; |
| 1716 | } else if (r != LY_ENOT) { |
| 1717 | goto error; |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | * error-message |
| 1722 | */ |
| 1723 | r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1724 | if (r == LY_SUCCESS) { |
| 1725 | no_dup = 1; |
| 1726 | goto check_child; |
| 1727 | } else if (r != LY_ENOT) { |
| 1728 | goto error; |
| 1729 | } |
| 1730 | |
| 1731 | /* error-info */ |
| 1732 | r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child); |
| 1733 | if (r == LY_SUCCESS) { |
| 1734 | /* parse all the descendants */ |
| 1735 | LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error); |
| 1736 | |
| 1737 | no_dup = 0; |
| 1738 | goto check_child; |
| 1739 | } else if (r != LY_ENOT) { |
| 1740 | goto error; |
| 1741 | } |
| 1742 | |
| 1743 | if (r == LY_ENOT) { |
| 1744 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1745 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1746 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1747 | r = LY_EVALID; |
| 1748 | goto error; |
| 1749 | } |
| 1750 | |
| 1751 | check_child: |
| 1752 | /* check for duplicates */ |
| 1753 | if (no_dup) { |
| 1754 | LY_LIST_FOR(lyd_child(parent), iter) { |
| 1755 | if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) && |
| 1756 | (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) { |
| 1757 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".", |
| 1758 | ((struct lyd_node_opaq *)child)->name.name); |
| 1759 | r = LY_EVALID; |
| 1760 | goto error; |
| 1761 | } |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | /* finish child parsing */ |
| 1766 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1767 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1768 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1769 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1770 | r = LY_EVALID; |
| 1771 | goto error; |
| 1772 | } |
| 1773 | LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error); |
| 1774 | |
| 1775 | /* insert */ |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 1776 | lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | return LY_SUCCESS; |
| 1780 | |
| 1781 | error: |
| 1782 | lyd_free_tree(child); |
| 1783 | return r; |
| 1784 | } |
| 1785 | |
| 1786 | /** |
| 1787 | * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message. |
| 1788 | * |
| 1789 | * @param[in] xmlctx XML parser context. |
| 1790 | * @param[out] evnp Parsed envelope(s) (opaque node). |
| 1791 | * @param[out] int_opts Internal options for parsing the rest of YANG data. |
| 1792 | * @param[out] close_elem Number of parsed opened elements that need to be closed. |
| 1793 | * @return LY_SUCCESS on success. |
| 1794 | * @return LY_ERR value on error. |
| 1795 | */ |
| 1796 | static LY_ERR |
| 1797 | lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem) |
| 1798 | { |
| 1799 | LY_ERR rc = LY_SUCCESS, r; |
| 1800 | struct lyd_node *child = NULL; |
| 1801 | const char *parsed_elem = NULL; |
| 1802 | |
| 1803 | assert(envp && !*envp); |
| 1804 | |
| 1805 | /* parse "rpc-reply" */ |
| 1806 | r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1807 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 1808 | |
| 1809 | /* there must be some child */ |
| 1810 | if (xmlctx->status == LYXML_ELEM_CLOSE) { |
| 1811 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\"."); |
| 1812 | rc = LY_EVALID; |
Michal Vasko | cf770e2 | 2020-08-12 13:21:43 +0200 | [diff] [blame] | 1813 | goto cleanup; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1814 | } |
| 1815 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1816 | /* try to parse "ok" */ |
| 1817 | r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child); |
| 1818 | if (r == LY_SUCCESS) { |
| 1819 | /* insert */ |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 1820 | lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1821 | |
| 1822 | /* finish child parsing */ |
| 1823 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1824 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1825 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1826 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1827 | rc = LY_EVALID; |
| 1828 | goto cleanup; |
| 1829 | } |
| 1830 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
| 1831 | |
| 1832 | /* success */ |
| 1833 | parsed_elem = "ok"; |
| 1834 | goto finish; |
| 1835 | } else if (r != LY_ENOT) { |
| 1836 | rc = r; |
| 1837 | goto cleanup; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1838 | } |
| 1839 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1840 | /* try to parse all "rpc-error" elements */ |
| 1841 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1842 | r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child); |
| 1843 | if (r == LY_ENOT) { |
| 1844 | break; |
| 1845 | } else if (r) { |
| 1846 | rc = r; |
| 1847 | goto cleanup; |
| 1848 | } |
| 1849 | |
| 1850 | /* insert */ |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 1851 | lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1852 | |
| 1853 | /* parse all children of "rpc-error" */ |
| 1854 | LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup); |
| 1855 | |
| 1856 | /* finish child parsing */ |
| 1857 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 1858 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
| 1859 | |
| 1860 | parsed_elem = "rpc-error"; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1861 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1862 | |
| 1863 | finish: |
| 1864 | if (parsed_elem) { |
| 1865 | /* NETCONF rpc-reply with no data */ |
| 1866 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1867 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1868 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1869 | (int)xmlctx->name_len, xmlctx->name, parsed_elem); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1870 | rc = LY_EVALID; |
| 1871 | goto cleanup; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | /* NETCONF rpc-reply */ |
Michal Vasko | 1d991fd | 2021-07-09 13:14:40 +0200 | [diff] [blame] | 1876 | *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1877 | *close_elem = 1; |
| 1878 | |
| 1879 | cleanup: |
| 1880 | if (rc) { |
| 1881 | lyd_free_tree(*envp); |
| 1882 | *envp = NULL; |
| 1883 | } |
| 1884 | return rc; |
| 1885 | } |
| 1886 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1887 | LY_ERR |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1888 | lyd_parse_xml_netconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1889 | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1890 | struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p) |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1891 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1892 | LY_ERR rc = LY_SUCCESS; |
| 1893 | struct lyd_xml_ctx *lydctx; |
Michal Vasko | 4a1e3e8 | 2023-09-05 08:45:01 +0200 | [diff] [blame] | 1894 | struct lyd_node *node; |
Michal Vasko | 2ca9f9e | 2021-07-02 09:21:36 +0200 | [diff] [blame] | 1895 | uint32_t i, int_opts = 0, close_elem = 0; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1896 | ly_bool parsed_data_nodes = 0; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1897 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1898 | assert(ctx && in && lydctx_p); |
| 1899 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
| 1900 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1901 | assert(!(parse_opts & LYD_PARSE_SUBTREE)); |
| 1902 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1903 | /* init context */ |
| 1904 | lydctx = calloc(1, sizeof *lydctx); |
| 1905 | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
| 1906 | LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup); |
| 1907 | lydctx->parse_opts = parse_opts; |
| 1908 | lydctx->val_opts = val_opts; |
| 1909 | lydctx->free = lyd_xml_ctx_free; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1910 | lydctx->ext = ext; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1911 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1912 | switch (data_type) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1913 | case LYD_TYPE_RPC_NETCONF: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1914 | assert(!parent); |
Michal Vasko | d71c2d4 | 2022-04-29 09:50:44 +0200 | [diff] [blame] | 1915 | rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem); |
| 1916 | if (rc == LY_ENOT) { |
| 1917 | LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace."); |
| 1918 | } |
| 1919 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1920 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1921 | case LYD_TYPE_NOTIF_NETCONF: |
| 1922 | assert(!parent); |
Michal Vasko | 4a1e3e8 | 2023-09-05 08:45:01 +0200 | [diff] [blame] | 1923 | |
| 1924 | /* parse "notification" */ |
| 1925 | rc = lydxml_envelope(lydctx->xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp); |
Michal Vasko | d71c2d4 | 2022-04-29 09:50:44 +0200 | [diff] [blame] | 1926 | if (rc == LY_ENOT) { |
| 1927 | LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace."); |
| 1928 | } |
| 1929 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 4a1e3e8 | 2023-09-05 08:45:01 +0200 | [diff] [blame] | 1930 | |
| 1931 | /* NETCONF notification */ |
| 1932 | int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME; |
| 1933 | close_elem = 1; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1934 | break; |
| 1935 | case LYD_TYPE_REPLY_NETCONF: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1936 | assert(parent); |
Michal Vasko | d71c2d4 | 2022-04-29 09:50:44 +0200 | [diff] [blame] | 1937 | rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem); |
| 1938 | if (rc == LY_ENOT) { |
| 1939 | LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace."); |
| 1940 | } |
| 1941 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1942 | break; |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 1943 | case LYD_TYPE_RPC_RESTCONF: |
| 1944 | assert(parent); |
| 1945 | |
| 1946 | /* parse "input" */ |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 1947 | rc = lydxml_envelope(lydctx->xmlctx, "input", lyd_node_module(parent)->ns, 0, envp); |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 1948 | if (rc == LY_ENOT) { |
| 1949 | LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"input\" object or in incorrect namespace."); |
| 1950 | } |
| 1951 | LY_CHECK_GOTO(rc, cleanup); |
| 1952 | |
| 1953 | int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION; |
| 1954 | close_elem = 1; |
| 1955 | break; |
| 1956 | case LYD_TYPE_REPLY_RESTCONF: |
| 1957 | assert(parent); |
| 1958 | |
| 1959 | /* parse "output" */ |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 1960 | rc = lydxml_envelope(lydctx->xmlctx, "output", lyd_node_module(parent)->ns, 0, envp); |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 1961 | if (rc == LY_ENOT) { |
| 1962 | LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"output\" object or in incorrect namespace."); |
| 1963 | } |
| 1964 | LY_CHECK_GOTO(rc, cleanup); |
| 1965 | |
| 1966 | int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY; |
| 1967 | close_elem = 1; |
| 1968 | break; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1969 | default: |
| 1970 | LOGINT(ctx); |
| 1971 | rc = LY_EINT; |
| 1972 | goto cleanup; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1973 | } |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1974 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1975 | lydctx->int_opts = int_opts; |
| 1976 | |
| 1977 | /* find the operation node if it exists already */ |
| 1978 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
| 1979 | |
| 1980 | /* parse XML data */ |
| 1981 | while (lydctx->xmlctx->status == LYXML_ELEMENT) { |
| 1982 | LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup); |
| 1983 | parsed_data_nodes = 1; |
| 1984 | |
| 1985 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1986 | break; |
| 1987 | } |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1990 | /* close all opened elements */ |
| 1991 | for (i = 0; i < close_elem; ++i) { |
| 1992 | if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1993 | assert(lydctx->xmlctx->status == LYXML_ELEMENT); |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1994 | LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".", |
| 1995 | (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1996 | rc = LY_EVALID; |
| 1997 | goto cleanup; |
| 1998 | } |
| 1999 | |
| 2000 | LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup); |
Michal Vasko | 4189c0f | 2020-08-13 09:05:22 +0200 | [diff] [blame] | 2001 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 2002 | |
| 2003 | /* check final state */ |
| 2004 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) { |
| 2005 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
| 2006 | rc = LY_EVALID; |
| 2007 | goto cleanup; |
| 2008 | } |
Michal Vasko | c939fdd | 2022-01-03 11:35:13 +0100 | [diff] [blame] | 2009 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 2010 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
| 2011 | rc = LY_EVALID; |
| 2012 | goto cleanup; |
| 2013 | } |
Michal Vasko | 4a1e3e8 | 2023-09-05 08:45:01 +0200 | [diff] [blame] | 2014 | if (int_opts & LYD_INTOPT_EVENTTIME) { |
| 2015 | /* parse as a child of the envelope */ |
| 2016 | node = (*first_p)->prev; |
| 2017 | if (node->schema) { |
| 2018 | LOGVAL(ctx, LYVE_DATA, "Missing notification \"eventTime\" node."); |
| 2019 | rc = LY_EVALID; |
| 2020 | goto cleanup; |
| 2021 | } else { |
| 2022 | /* can be the only opaque node and an operation had to be parsed */ |
| 2023 | assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next); |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 2024 | lyd_unlink(node); |
Michal Vasko | 4a1e3e8 | 2023-09-05 08:45:01 +0200 | [diff] [blame] | 2025 | assert(*envp); |
| 2026 | lyd_insert_child(*envp, node); |
| 2027 | } |
| 2028 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 2029 | |
| 2030 | if (!parsed_data_nodes) { |
| 2031 | /* no data nodes were parsed */ |
| 2032 | lydctx->op_node = NULL; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 2036 | /* there should be no unres stored if validation should be skipped */ |
| 2037 | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count && |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2038 | !lydctx->node_when.count)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 2039 | |
| 2040 | if (rc) { |
| 2041 | lyd_xml_ctx_free((struct lyd_ctx *)lydctx); |
| 2042 | } else { |
| 2043 | *lydctx_p = (struct lyd_ctx *)lydctx; |
| 2044 | |
| 2045 | /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */ |
| 2046 | lyxml_ctx_free(lydctx->xmlctx); |
| 2047 | lydctx->xmlctx = NULL; |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 2048 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 2049 | return rc; |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 2050 | } |