blob: 209fb3212adf8ba5656b8c3a92574d2b83212043 [file] [log] [blame]
Radek Krejcib1c12512015-08-11 11:22:04 +02001/**
2 * @file validation.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Data tree validation functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcib1c12512015-08-11 11:22:04 +020013 */
14
Radek Krejcieab784a2015-08-27 09:56:53 +020015#include <assert.h>
Radek Krejcib1c12512015-08-11 11:22:04 +020016#include <stdlib.h>
Michal Vaskocf024702015-10-08 15:01:42 +020017#include <string.h>
Radek Krejcib1c12512015-08-11 11:22:04 +020018
Radek Krejcieab784a2015-08-27 09:56:53 +020019#include "common.h"
Michal Vaskocf024702015-10-08 15:01:42 +020020#include "validation.h"
Radek Krejcib1c12512015-08-11 11:22:04 +020021#include "libyang.h"
Michal Vaskocf024702015-10-08 15:01:42 +020022#include "xpath.h"
Radek Krejcicf509982015-12-15 09:22:44 +010023#include "parser.h"
Michal Vaskocf024702015-10-08 15:01:42 +020024#include "resolve.h"
25#include "tree_internal.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020026#include "xml_internal.h"
Radek Krejcib1c12512015-08-11 11:22:04 +020027
Radek Krejcib45b3082016-09-09 16:08:51 +020028static int
Radek Krejci48464ed2016-03-17 15:44:09 +010029lyv_keys(const struct lyd_node *list)
Radek Krejcib1c12512015-08-11 11:22:04 +020030{
Radek Krejci702deb82016-03-09 12:37:57 +010031 struct lyd_node *child;
32 struct lys_node_list *schema = (struct lys_node_list *)list->schema; /* shortcut */
Radek Krejcib1c12512015-08-11 11:22:04 +020033 int i;
34
Radek Krejci702deb82016-03-09 12:37:57 +010035 for (i = 0, child = list->child; i < schema->keys_size; i++, child = child->next) {
36 if (!child || child->schema != (struct lys_node *)schema->keys[i]) {
37 /* key not found on the correct place */
Michal Vasko53b7da02018-02-13 15:28:42 +010038 LOGVAL(schema->module->ctx, LYE_MISSELEM, LY_VLOG_LYD, list, schema->keys[i]->name, schema->name);
Radek Krejci702deb82016-03-09 12:37:57 +010039 for ( ; child; child = child->next) {
40 if (child->schema == (struct lys_node *)schema->keys[i]) {
Michal Vasko53b7da02018-02-13 15:28:42 +010041 LOGVAL(schema->module->ctx, LYE_SPEC, LY_VLOG_LYD, child, "Invalid position of the key element.");
Radek Krejci702deb82016-03-09 12:37:57 +010042 break;
43 }
Radek Krejcib1c12512015-08-11 11:22:04 +020044 }
Michal Vasko53b7da02018-02-13 15:28:42 +010045 return 1;
Radek Krejcib1c12512015-08-11 11:22:04 +020046 }
47 }
Michal Vasko53b7da02018-02-13 15:28:42 +010048 return 0;
Radek Krejcib1c12512015-08-11 11:22:04 +020049}
Radek Krejcieab784a2015-08-27 09:56:53 +020050
51int
Michal Vasko805729f2021-02-12 14:17:07 +010052lyv_data_context(struct lyd_node *node, int options, int check_node_order, struct unres_data *unres)
Radek Krejcieab784a2015-08-27 09:56:53 +020053{
Radek Krejci6baaa9a2016-02-23 16:07:12 +010054 const struct lys_node *siter = NULL;
Michal Vaskoe48303c2019-12-03 14:03:54 +010055 struct lys_node *sparent, *op;
Radek Krejcib1f318b2016-08-22 16:18:37 +020056 struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node;
Michal Vasko53b7da02018-02-13 15:28:42 +010057 struct ly_ctx *ctx = node->schema->module->ctx;
Radek Krejci4a49bdf2016-01-12 17:17:01 +010058
Michal Vaskocf024702015-10-08 15:01:42 +020059 assert(node);
Radek Krejci03b71f72016-03-16 11:10:09 +010060 assert(unres);
Radek Krejcieab784a2015-08-27 09:56:53 +020061
62 /* check if the node instance is enabled by if-feature */
Michal Vaskocf024702015-10-08 15:01:42 +020063 if (lys_is_disabled(node->schema, 2)) {
Michal Vasko53b7da02018-02-13 15:28:42 +010064 LOGVAL(ctx, LYE_INELEM, LY_VLOG_LYD, node, node->schema->name);
65 return 1;
Michal Vaskocf024702015-10-08 15:01:42 +020066 }
67
Michal Vaskoe48303c2019-12-03 14:03:54 +010068 /* find (nested) operation node */
Michal Vasko1640f922020-03-04 08:18:05 +010069 for (op = node->schema; op && !(op->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); op = lys_parent(op));
Michal Vaskoe48303c2019-12-03 14:03:54 +010070
71 if (!(options & (LYD_OPT_NOTIF_FILTER | LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG))
Michal Vaskof54f97f2019-12-13 11:44:02 +010072 && (!(options & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF)) || op)) {
Michal Vasko0bc2c672018-12-18 13:14:33 +010073 if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
74 /* if union with leafref/intsid, leafref itself (invalid) or instance-identifier, store the node for later resolving */
75 if ((((struct lys_node_leaf *)leaf->schema)->type.base == LY_TYPE_UNION)
76 && ((struct lys_node_leaf *)leaf->schema)->type.info.uni.has_ptr_type) {
77 if (unres_data_add(unres, (struct lyd_node *)node, UNRES_UNION)) {
78 return 1;
79 }
Michal Vasko1544f482020-01-03 14:16:55 +010080 } else if (((struct lys_node_leaf *)leaf->schema)->type.base == LY_TYPE_LEAFREF) {
Michal Vaskod0826ca2018-12-19 16:31:10 +010081 /* always retry validation on unres leafrefs, if again not possible, the correct flags should
82 * be set and the leafref will be kept unresolved */
83 leaf->value_flags &= ~LY_VALUE_UNRES;
Michal Vaskod0826ca2018-12-19 16:31:10 +010084
Michal Vasko0bc2c672018-12-18 13:14:33 +010085 if (unres_data_add(unres, (struct lyd_node *)node, UNRES_LEAFREF)) {
86 return 1;
87 }
88 } else if (((struct lys_node_leaf *)leaf->schema)->type.base == LY_TYPE_INST) {
89 if (unres_data_add(unres, (struct lyd_node *)node, UNRES_INSTID)) {
90 return 1;
91 }
Radek Krejci0b7704f2016-03-18 12:16:14 +010092 }
Michal Vasko0d182ba2015-10-09 09:29:14 +020093 }
Michal Vaskoef644d22020-12-09 19:36:04 +010094 }
Radek Krejci0b7704f2016-03-18 12:16:14 +010095
Michal Vaskoef644d22020-12-09 19:36:04 +010096 /* check all relevant when conditions */
97 if (!(options & (LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG))) {
Michal Vasko0bc2c672018-12-18 13:14:33 +010098 if (node->when_status & LYD_WHEN) {
Michal Vaskoef644d22020-12-09 19:36:04 +010099 if ((options & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF | LYD_OPT_NOTIF_FILTER)) && !op) {
100 /* we are validating an operation but are still on its parents (nested operation), parse them as trusted */
101 node->when_status |= LYD_WHEN_TRUE;
102 } else if (options & LYD_OPT_TRUSTED) {
Michal Vasko1f394492020-01-30 09:37:04 +0100103 node->when_status |= LYD_WHEN_TRUE;
104 } else if (unres_data_add(unres, (struct lyd_node *)node, UNRES_WHEN)) {
Michal Vasko0bc2c672018-12-18 13:14:33 +0100105 return 1;
106 }
Michal Vasko0d182ba2015-10-09 09:29:14 +0200107 }
Radek Krejcieab784a2015-08-27 09:56:53 +0200108 }
109
110 /* check for (non-)presence of status data in edit-config data */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100111 if ((options & (LYD_OPT_EDIT | LYD_OPT_GETCONFIG | LYD_OPT_CONFIG)) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100112 LOGVAL(ctx, LYE_INELEM, LY_VLOG_LYD, node, node->schema->name);
113 return 1;
Radek Krejcieab784a2015-08-27 09:56:53 +0200114 }
115
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100116 /* check elements order in case of RPC's input and output */
Michal Vasko805729f2021-02-12 14:17:07 +0100117 if (check_node_order && !(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER)) &&
118 (options & (LYD_OPT_RPC | LYD_OPT_RPCREPLY)) && (node->validity & LYD_VAL_MAND) && op) {
Michal Vasko15e0bab2016-02-24 13:58:21 +0100119 if ((node->prev != node) && node->prev->next) {
Michal Vaskof1f278d2018-10-11 12:07:21 +0200120 /* find schema data parent */
121 for (sparent = lys_parent(node->schema);
122 sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE));
123 sparent = lys_parent(sparent));
124 for (siter = lys_getnext(node->schema, sparent, lyd_node_module(node), 0);
Radek Krejci6baaa9a2016-02-23 16:07:12 +0100125 siter;
Michal Vaskof1f278d2018-10-11 12:07:21 +0200126 siter = lys_getnext(siter, sparent, lyd_node_module(node), 0)) {
Radek Krejci6baaa9a2016-02-23 16:07:12 +0100127 if (siter == node->prev->schema) {
128 /* data predecessor has the schema node after
129 * the schema node of the data node being checked */
Michal Vasko53b7da02018-02-13 15:28:42 +0100130 LOGVAL(ctx, LYE_INORDER, LY_VLOG_LYD, node, node->schema->name, siter->name);
131 return 1;
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100132 }
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100133 }
134
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100135 }
136 }
137
Michal Vasko53b7da02018-02-13 15:28:42 +0100138 return 0;
Radek Krejcieab784a2015-08-27 09:56:53 +0200139}
Michal Vaskocf024702015-10-08 15:01:42 +0200140
Michal Vasko185b5272018-09-13 14:26:12 +0200141/*
142 * actions (cb_data):
143 * 0 - compare all uniques
144 * n - compare n-th unique
145 */
146static int
147lyv_list_uniq_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data)
148{
149 struct ly_ctx *ctx;
150 struct lys_node_list *slist;
151 struct lyd_node *diter, *first, *second;
152 const char *val1, *val2;
153 char *path1, *path2, *uniq_str;
154 uint16_t idx_uniq;
155 int i, j, r, action;
156
157 assert(val1_p && val2_p);
158
159 first = *((struct lyd_node **)val1_p);
160 second = *((struct lyd_node **)val2_p);
161 action = (intptr_t)cb_data;
162
163 assert(first && (first->schema->nodetype == LYS_LIST));
164 assert(second && (second->schema == first->schema));
165
166 ctx = first->schema->module->ctx;
167
168 slist = (struct lys_node_list *)first->schema;
169
170 /* compare unique leaves */
171 if (action > 0) {
172 i = action - 1;
173 if (i < slist->unique_size) {
174 goto uniquecheck;
175 }
176 }
177 for (i = 0; i < slist->unique_size; i++) {
178uniquecheck:
179 for (j = 0; j < slist->unique[i].expr_size; j++) {
180 /* first */
181 diter = resolve_data_descendant_schema_nodeid(slist->unique[i].expr[j], first->child);
182 if (diter) {
183 val1 = ((struct lyd_node_leaf_list *)diter)->value_str;
184 } else {
185 /* use default value */
186 if (lyd_get_unique_default(slist->unique[i].expr[j], first, &val1)) {
187 return 1;
188 }
189 }
190
191 /* second */
192 diter = resolve_data_descendant_schema_nodeid(slist->unique[i].expr[j], second->child);
193 if (diter) {
194 val2 = ((struct lyd_node_leaf_list *)diter)->value_str;
195 } else {
196 /* use default value */
197 if (lyd_get_unique_default(slist->unique[i].expr[j], second, &val2)) {
198 return 1;
199 }
200 }
201
202 if (!val1 || !val2 || !ly_strequal(val1, val2, 1)) {
203 /* values differ or either one is not set */
204 break;
205 }
206 }
207 if (j && (j == slist->unique[i].expr_size)) {
208 /* all unique leafs are the same in this set, create this nice error */
209 ly_vlog_build_path(LY_VLOG_LYD, first, &path1, 0, 0);
210 ly_vlog_build_path(LY_VLOG_LYD, second, &path2, 0, 0);
211
212 /* use buffer to rebuild the unique string */
213 uniq_str = malloc(1024);
214 idx_uniq = 0;
215 for (j = 0; j < slist->unique[i].expr_size; ++j) {
216 if (j) {
217 uniq_str[idx_uniq++] = ' ';
218 }
219 r = lyd_build_relative_data_path(lys_node_module((struct lys_node *)slist), first,
220 slist->unique[i].expr[j], &uniq_str[idx_uniq]);
221 if (r == -1) {
222 goto unique_errmsg_cleanup;
223 }
224 idx_uniq += r;
225 }
226
227 LOGVAL(ctx, LYE_NOUNIQ, LY_VLOG_LYD, second, uniq_str, path1, path2);
228unique_errmsg_cleanup:
229 free(path1);
230 free(path2);
231 free(uniq_str);
232 return 1;
233 }
234
235 if (action > 0) {
236 /* done */
237 return 0;
238 }
239 }
240
241 return 0;
242}
243
Radek Krejci63b79c82016-08-10 10:09:33 +0200244int
Michal Vasko185b5272018-09-13 14:26:12 +0200245lyv_data_unique(struct lyd_node *list)
246{
247 struct lyd_node *diter;
248 struct ly_set *set;
249 uint32_t i, j, n = 0;
250 int ret = 0;
251 uint32_t hash, u, usize = 0;
252 struct hash_table **uniqtables = NULL;
253 const char *id;
254 char *path;
255 struct lys_node_list *slist;
256 struct ly_ctx *ctx = list->schema->module->ctx;
257
258 if (!(list->validity & LYD_VAL_UNIQUE)) {
259 /* validated sa part of another instance validation */
260 return 0;
261 }
262
263 slist = (struct lys_node_list *)list->schema;
264
265 /* get all list instances */
266 if (ly_vlog_build_path(LY_VLOG_LYD, list, &path, 0, 1)) {
267 return -1;
268 }
269 set = lyd_find_path(list, path);
270 free(path);
271 if (!set) {
272 return -1;
273 }
274
275 for (i = 0; i < set->number; ++i) {
276 /* remove the flag */
277 set->set.d[i]->validity &= ~LYD_VAL_UNIQUE;
278 }
279
280 if (set->number == 2) {
281 /* simple comparison */
282 if (lyv_list_uniq_equal(&set->set.d[0], &set->set.d[1], 0, (void *)0)) {
283 /* instance duplication */
284 ly_set_free(set);
285 return 1;
286 }
287 } else if (set->number > 2) {
288 /* use hashes for comparison */
289 /* first, allocate the table, the size depends on number of items in the set */
290 for (u = 31; u > 0; u--) {
291 usize = set->number << u;
292 usize = usize >> u;
293 if (usize == set->number) {
294 break;
295 }
296 }
297 if (u == 0) {
298 LOGINT(ctx);
299 ret = -1;
300 goto cleanup;
301 } else {
302 u = 32 - u;
303 usize = 1 << u;
304 }
305
306 n = slist->unique_size;
307 uniqtables = malloc(n * sizeof *uniqtables);
308 if (!uniqtables) {
309 LOGMEM(ctx);
310 ret = -1;
311 n = 0;
312 goto cleanup;
313 }
314 for (j = 0; j < n; j++) {
315 uniqtables[j] = lyht_new(usize, sizeof(struct lyd_node *), lyv_list_uniq_equal, (void *)(j + 1L), 0);
316 if (!uniqtables[j]) {
317 LOGMEM(ctx);
318 ret = -1;
319 goto cleanup;
320 }
321 }
322
323 for (u = 0; u < set->number; u++) {
324 /* loop for unique - get the hash for the instances */
325 for (j = 0; j < n; j++) {
326 id = NULL;
327 for (i = hash = 0; i < slist->unique[j].expr_size; i++) {
328 diter = resolve_data_descendant_schema_nodeid(slist->unique[j].expr[i], set->set.d[u]->child);
329 if (diter) {
330 id = ((struct lyd_node_leaf_list *)diter)->value_str;
331 } else {
332 /* use default value */
333 if (lyd_get_unique_default(slist->unique[j].expr[i], set->set.d[u], &id)) {
334 ret = -1;
335 goto cleanup;
336 }
337 }
338 if (!id) {
339 /* unique item not present nor has default value */
340 break;
341 }
342 hash = dict_hash_multi(hash, id, strlen(id));
343 }
344 if (!id) {
345 /* skip this list instance since its unique set is incomplete */
346 continue;
347 }
348
349 /* finish the hash value */
350 hash = dict_hash_multi(hash, NULL, 0);
351
352 /* insert into the hashtable */
353 if (lyht_insert(uniqtables[j], &set->set.d[u], hash, NULL)) {
354 ret = 1;
355 goto cleanup;
356 }
357 }
358 }
359 }
360
361cleanup:
362 ly_set_free(set);
363 for (j = 0; j < n; j++) {
364 if (!uniqtables[j]) {
365 /* failed when allocating uniquetables[j], following j are not allocated */
366 break;
367 }
368 lyht_free(uniqtables[j]);
369 }
370 free(uniqtables);
371
372 return ret;
373}
374
375static int
376lyv_list_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
377{
378 struct ly_ctx *ctx;
379 struct lys_node_list *slist;
380 const struct lys_node *snode = NULL;
381 struct lyd_node *diter, *first, *second;
382 const char *val1, *val2;
383 int i;
384
385 assert(val1_p && val2_p);
386
387 first = *((struct lyd_node **)val1_p);
388 second = *((struct lyd_node **)val2_p);
389
390 assert(first && (first->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)));
391 assert(second && (second->schema == first->schema));
392
393 ctx = first->schema->module->ctx;
394
395 switch (first->schema->nodetype) {
396 case LYS_LEAFLIST:
Michal Vaskob8ad9522021-04-09 13:52:29 +0200397 if (!(first->schema->flags & LYS_CONFIG_W) && (first->schema->module->version >= LYS_VERSION_1_1)) {
Michal Vaskod4f65112021-02-12 11:57:10 +0100398 /* same values are allowed for non-configuration data */
Michal Vasko185b5272018-09-13 14:26:12 +0200399 return 0;
400 }
401 /* compare values */
402 if (ly_strequal(((struct lyd_node_leaf_list *)first)->value_str,
403 ((struct lyd_node_leaf_list *)second)->value_str, 1)) {
404 LOGVAL(ctx, LYE_DUPLEAFLIST, LY_VLOG_LYD, second, second->schema->name,
405 ((struct lyd_node_leaf_list *)second)->value_str);
406 return 1;
407 }
408 return 0;
409 case LYS_LIST:
410 slist = (struct lys_node_list *)first->schema;
411
412 /* compare keys */
413 if (!slist->keys_size) {
414 /* status lists without keys */
415 return 0;
416 } else {
417 for (i = 0; i < slist->keys_size; i++) {
418 snode = (struct lys_node *)slist->keys[i];
419 val1 = val2 = NULL;
420 LY_TREE_FOR(first->child, diter) {
421 if (diter->schema == snode) {
422 val1 = ((struct lyd_node_leaf_list *)diter)->value_str;
423 break;
424 }
425 }
426 LY_TREE_FOR(second->child, diter) {
427 if (diter->schema == snode) {
428 val2 = ((struct lyd_node_leaf_list *)diter)->value_str;
429 break;
430 }
431 }
432 if (!ly_strequal(val1, val2, 1)) {
433 return 0;
434 }
435 }
436 }
437
438 LOGVAL(ctx, LYE_DUPLIST, LY_VLOG_LYD, second, second->schema->name);
439 return 1;
440
441 default:
442 LOGINT(ctx);
443 return 1;
444 }
445}
446
447int
448lyv_data_dup(struct lyd_node *node, struct lyd_node *start)
Radek Krejci63b79c82016-08-10 10:09:33 +0200449{
450 struct lyd_node *diter, *key;
Radek Krejci63b79c82016-08-10 10:09:33 +0200451 struct ly_set *set;
Michal Vasko185b5272018-09-13 14:26:12 +0200452 int i, ret = 0;
Michal Vasko6c810702018-03-14 16:23:21 +0100453 uint32_t hash, u, usize = 0;
Michal Vasko185b5272018-09-13 14:26:12 +0200454 struct hash_table *keystable = NULL;
Radek Krejci63b79c82016-08-10 10:09:33 +0200455 const char *id;
Michal Vasko53b7da02018-02-13 15:28:42 +0100456 struct ly_ctx *ctx = node->schema->module->ctx;
Radek Krejci63b79c82016-08-10 10:09:33 +0200457
458 /* get the first list/leaflist instance sibling */
459 if (!start) {
460 start = lyd_first_sibling(node);
461 }
462
463 /* check uniqueness of the list/leaflist instances (compare values) */
464 set = ly_set_new();
465 for (diter = start; diter; diter = diter->next) {
466 if (diter->schema != node->schema) {
467 /* check only instances of the same list/leaflist */
468 continue;
469 }
470
471 /* remove the flag */
Michal Vasko185b5272018-09-13 14:26:12 +0200472 diter->validity &= ~LYD_VAL_DUP;
Radek Krejci63b79c82016-08-10 10:09:33 +0200473
474 /* store for comparison */
475 ly_set_add(set, diter, LY_SET_OPT_USEASLIST);
476 }
477
478 if (set->number == 2) {
479 /* simple comparison */
Michal Vasko185b5272018-09-13 14:26:12 +0200480 if (lyv_list_equal(&set->set.d[0], &set->set.d[1], 0, 0)) {
Radek Krejci63b79c82016-08-10 10:09:33 +0200481 /* instance duplication */
482 ly_set_free(set);
Michal Vasko53b7da02018-02-13 15:28:42 +0100483 return 1;
Radek Krejci63b79c82016-08-10 10:09:33 +0200484 }
485 } else if (set->number > 2) {
486 /* use hashes for comparison */
487 /* first, allocate the table, the size depends on number of items in the set */
488 for (u = 31; u > 0; u--) {
489 usize = set->number << u;
490 usize = usize >> u;
491 if (usize == set->number) {
492 break;
493 }
494 }
495 if (u == 0) {
Michal Vasko6c810702018-03-14 16:23:21 +0100496 LOGINT(ctx);
Michal Vaskod60a1a32018-05-23 16:31:22 +0200497 ret = 1;
Michal Vasko185b5272018-09-13 14:26:12 +0200498 goto cleanup;
Radek Krejci63b79c82016-08-10 10:09:33 +0200499 } else {
500 u = 32 - u;
501 usize = 1 << u;
Radek Krejci63b79c82016-08-10 10:09:33 +0200502 }
Michal Vasko419fce02018-03-21 11:55:09 +0100503 keystable = lyht_new(usize, sizeof(struct lyd_node *), lyv_list_equal, 0, 0);
Radek Krejci63b79c82016-08-10 10:09:33 +0200504 if (!keystable) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100505 LOGMEM(ctx);
506 ret = 1;
Michal Vasko185b5272018-09-13 14:26:12 +0200507 goto cleanup;
Radek Krejci63b79c82016-08-10 10:09:33 +0200508 }
509
510 for (u = 0; u < set->number; u++) {
511 /* get the hash for the instance - keys */
512 if (node->schema->nodetype == LYS_LEAFLIST) {
513 id = ((struct lyd_node_leaf_list *)set->set.d[u])->value_str;
514 hash = dict_hash_multi(0, id, strlen(id));
515 } else { /* LYS_LIST */
516 for (hash = i = 0, key = set->set.d[u]->child;
517 i < ((struct lys_node_list *)set->set.d[u]->schema)->keys_size;
518 i++, key = key->next) {
519 id = ((struct lyd_node_leaf_list *)key)->value_str;
520 hash = dict_hash_multi(hash, id, strlen(id));
521 }
522 }
523 /* finish the hash value */
Michal Vasko6c810702018-03-14 16:23:21 +0100524 hash = dict_hash_multi(hash, NULL, 0);
Radek Krejci63b79c82016-08-10 10:09:33 +0200525
526 /* insert into the hashtable */
David Sedlák83b1faf2018-08-09 10:52:04 +0200527 if (lyht_insert(keystable, &set->set.d[u], hash, NULL)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100528 ret = 1;
Michal Vasko185b5272018-09-13 14:26:12 +0200529 goto cleanup;
Radek Krejci63b79c82016-08-10 10:09:33 +0200530 }
531 }
532 }
533
Michal Vasko185b5272018-09-13 14:26:12 +0200534cleanup:
Radek Krejci63b79c82016-08-10 10:09:33 +0200535 ly_set_free(set);
Michal Vasko6c810702018-03-14 16:23:21 +0100536 lyht_free(keystable);
Radek Krejci63b79c82016-08-10 10:09:33 +0200537
538 return ret;
539}
540
Michal Vasko91ff4592017-05-24 15:41:49 +0200541static struct lys_type *
542find_orig_type(struct lys_type *par_type, LY_DATA_TYPE base_type)
543{
544 struct lys_type *type, *prev_type, *tmp_type;
545 int found;
546
547 /* go through typedefs */
548 for (type = par_type; type->der->type.der; type = &type->der->type);
549
550 if (type->base == base_type) {
551 /* we have the result */
552 return type;
Michal Vasko101658e2018-06-05 15:05:54 +0200553 } else if ((type->base == LY_TYPE_LEAFREF) && !(type->value_flags & LY_VALUE_UNRES)) {
Michal Vasko70bf8e52018-03-26 11:32:33 +0200554 /* go through the leafref */
555 assert(type->info.lref.target);
556 return find_orig_type(&((struct lys_node_leaf *)type->info.lref.target)->type, base_type);
Michal Vasko91ff4592017-05-24 15:41:49 +0200557 } else if (type->base == LY_TYPE_UNION) {
558 /* go through all the union types */
559 prev_type = NULL;
560 found = 0;
561 while ((prev_type = lyp_get_next_union_type(type, prev_type, &found))) {
562 tmp_type = find_orig_type(prev_type, base_type);
563 if (tmp_type) {
564 return tmp_type;
565 }
566 found = 0;
567 }
568 }
569
570 /* not found */
571 return NULL;
572}
573
PavolVican556559e2017-12-12 13:39:36 +0100574static int
575lyv_extension(struct lys_ext_instance **ext, uint8_t size, struct lyd_node *node)
576{
577 uint i;
578
579 for (i = 0; i < size; ++i) {
580 if ((ext[i]->flags & LYEXT_OPT_VALID) && ext[i]->def->plugin->valid_data) {
581 if (ext[i]->def->plugin->valid_data(ext[i], node)) {
582 return EXIT_FAILURE;
583 }
584 }
585 }
586 return 0;
587}
588
589static int
590lyv_type_extension(struct lyd_node_leaf_list *leaf, struct lys_type *type, int first_type)
591{
592 struct lyd_node *node = (struct lyd_node *)leaf;
593 unsigned int i;
594
595 switch (type->base) {
596 case LY_TYPE_ENUM:
597 if (first_type && lyv_extension(leaf->value.enm->ext, leaf->value.enm->ext_size, node)) {
598 return EXIT_FAILURE;
599 }
600 break;
601 case LY_TYPE_STRING:
602 if (type->info.str.length &&
603 lyv_extension(type->info.str.length->ext, type->info.str.length->ext_size, node)) {
604 return EXIT_FAILURE;
605 }
606 for(i = 0; i < type->info.str.pat_count; ++i) {
607 if (lyv_extension(type->info.str.patterns[i].ext, type->info.str.patterns[i].ext_size, node)) {
608 return EXIT_FAILURE;
609 }
610 }
611 break;
612 case LY_TYPE_DEC64:
613 if (type->info.dec64.range &&
614 lyv_extension(type->info.dec64.range->ext, type->info.dec64.range->ext_size, node)) {
615 return EXIT_FAILURE;
616 }
617 break;
618 case LY_TYPE_INT8:
619 case LY_TYPE_INT16:
620 case LY_TYPE_INT32:
621 case LY_TYPE_INT64:
622 case LY_TYPE_UINT8:
623 case LY_TYPE_UINT16:
624 case LY_TYPE_UINT32:
625 case LY_TYPE_UINT64:
626 if (type->info.num.range &&
627 lyv_extension(type->info.num.range->ext, type->info.num.range->ext_size, node)) {
628 return EXIT_FAILURE;
629 }
630 break;
631 case LY_TYPE_BITS:
632 if (first_type) {
633 /* get the count of bits */
634 type = find_orig_type(&((struct lys_node_leaf *) leaf->schema)->type, LY_TYPE_BITS);
635 for (i = 0; i < type->info.bits.count; ++i) {
636 if (!leaf->value.bit[i]) {
637 continue;
638 }
639 if (lyv_extension(leaf->value.bit[i]->ext, leaf->value.bit[i]->ext_size, node)) {
640 return EXIT_FAILURE;
641 }
642 }
643 }
644 break;
645 case LY_TYPE_UNION:
646 for (i = 0; i < type->info.uni.count; ++i) {
647 if (type->info.uni.types[i].base == leaf->value_type) {
648 break;
649 }
650 }
651 if (i < type->info.uni.count &&
652 lyv_type_extension(leaf, &type->info.uni.types[i], first_type)) {
653 return EXIT_FAILURE;
654 }
655 break;
656 default:
657 break;
658 }
659
660
661 if (lyv_extension(type->ext, type->ext_size, node)) {
662 return EXIT_FAILURE;
663 }
664
665 while (type->der->type.der) {
666 type = &type->der->type;
Michal Vasko1bdfd432018-03-09 09:30:19 +0100667 if ((type->parent->flags & LYS_VALID_EXT)) {
668 if (lyv_type_extension(leaf, type, 0) || lyv_extension(type->parent->ext, type->parent->ext_size, node)) {
PavolVican556559e2017-12-12 13:39:36 +0100669 return EXIT_FAILURE;
670 }
671 }
672 }
673
674 return EXIT_SUCCESS;
675}
676
Radek Krejcieab784a2015-08-27 09:56:53 +0200677int
Radek Krejci48464ed2016-03-17 15:44:09 +0100678lyv_data_content(struct lyd_node *node, int options, struct unres_data *unres)
Radek Krejcieab784a2015-08-27 09:56:53 +0200679{
PavolVican832f5432018-02-21 00:54:45 +0100680 const struct lys_node *schema, *siter, *parent;
Radek Krejcid788a522016-07-25 14:57:38 +0200681 struct lyd_node *diter, *start = NULL;
Radek Krejcicf509982015-12-15 09:22:44 +0100682 struct lys_ident *ident;
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100683 struct lys_tpdf *tpdf;
Radek Krejcie6a71b52016-08-10 15:11:16 +0200684 struct lys_type *type = NULL;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200685 struct lyd_node_leaf_list *leaf;
Radek Krejcidce5f972017-09-12 15:47:49 +0200686 unsigned int i, j = 0;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200687 uint8_t iff_size;
688 struct lys_iffeature *iff;
689 const char *id, *idname;
Michal Vasko53b7da02018-02-13 15:28:42 +0100690 struct ly_ctx *ctx;
Radek Krejcieab784a2015-08-27 09:56:53 +0200691
692 assert(node);
693 assert(node->schema);
Radek Krejci0b7704f2016-03-18 12:16:14 +0100694 assert(unres);
Radek Krejcieab784a2015-08-27 09:56:53 +0200695
696 schema = node->schema; /* shortcut */
Michal Vasko53b7da02018-02-13 15:28:42 +0100697 ctx = schema->module->ctx;
Radek Krejcieab784a2015-08-27 09:56:53 +0200698
Michal Vasko1e1be2f2019-04-16 15:52:35 +0200699 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION))) {
700 for (diter = node->parent; diter; diter = diter->parent) {
701 if (diter->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) {
702 break;
703 }
704 }
Michal Vaskof54f97f2019-12-13 11:44:02 +0100705 if (!diter && (options & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF))) {
Michal Vasko1e1be2f2019-04-16 15:52:35 +0200706 /* validating parent of a nested notification/action, skip most checks */
707 options |= LYD_OPT_TRUSTED;
708 }
709 }
710
Michal Vasko091dd842017-02-08 14:07:32 +0100711 if (node->validity & LYD_VAL_MAND) {
712 if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER))) {
713 /* check presence and correct order of all keys in case of list */
714 if (schema->nodetype == LYS_LIST && !(options & (LYD_OPT_GET | LYD_OPT_GETCONFIG))) {
715 if (lyv_keys(node)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100716 return 1;
Radek Krejcieab784a2015-08-27 09:56:53 +0200717 }
Radek Krejcieab784a2015-08-27 09:56:53 +0200718 }
Radek Krejcieab784a2015-08-27 09:56:53 +0200719
Michal Vasko091dd842017-02-08 14:07:32 +0100720 if (schema->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) {
721 /* check number of instances (similar to list uniqueness) for non-list nodes */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100722
Michal Vasko091dd842017-02-08 14:07:32 +0100723 /* find duplicity */
724 start = lyd_first_sibling(node);
725 for (diter = start; diter; diter = diter->next) {
726 if (diter->schema == schema && diter != node) {
PavolVican832f5432018-02-21 00:54:45 +0100727 parent = lys_parent(schema);
Michal Vasko53b7da02018-02-13 15:28:42 +0100728 LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, node, schema->name,
PavolVican832f5432018-02-21 00:54:45 +0100729 parent ? (parent->nodetype == LYS_EXT) ? ((struct lys_ext_instance *)parent)->arg_value : parent->name : "data tree");
Michal Vasko53b7da02018-02-13 15:28:42 +0100730 return 1;
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100731 }
732 }
Michal Vasko091dd842017-02-08 14:07:32 +0100733 }
734
735 if (options & LYD_OPT_OBSOLETE) {
736 /* status - of the node's schema node itself and all its parents that
737 * cannot have their own instance (like a choice statement) */
738 siter = node->schema;
739 do {
740 if (((siter->flags & LYS_STATUS_MASK) == LYS_STATUS_OBSLT) && (options & LYD_OPT_OBSOLETE)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100741 LOGVAL(ctx, LYE_OBSDATA, LY_VLOG_LYD, node, schema->name);
742 return 1;
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100743 }
Michal Vasko091dd842017-02-08 14:07:32 +0100744 siter = lys_parent(siter);
745 } while (siter && !(siter->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)));
746
747 /* status of the identity value */
748 if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
749 if (options & LYD_OPT_OBSOLETE) {
750 /* check that we are not instantiating obsolete type */
751 tpdf = ((struct lys_node_leaf *)node->schema)->type.der;
752 while (tpdf) {
753 if ((tpdf->flags & LYS_STATUS_MASK) == LYS_STATUS_OBSLT) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100754 LOGVAL(ctx, LYE_OBSTYPE, LY_VLOG_LYD, node, schema->name, tpdf->name);
755 return 1;
Michal Vasko091dd842017-02-08 14:07:32 +0100756 }
757 tpdf = tpdf->type.der;
758 }
759 }
760 if (((struct lyd_node_leaf_list *)node)->value_type == LY_TYPE_IDENT) {
761 ident = ((struct lyd_node_leaf_list *)node)->value.ident;
762 if (lyp_check_status(schema->flags, schema->module, schema->name,
763 ident->flags, ident->module, ident->name, NULL)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100764 LOGPATH(ctx, LY_VLOG_LYD, node);
765 return 1;
Michal Vasko091dd842017-02-08 14:07:32 +0100766 }
767 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100768 }
769 }
770 }
771
PavolVican556559e2017-12-12 13:39:36 +0100772 /* check validation function for extension */
Michal Vasko1bdfd432018-03-09 09:30:19 +0100773 if (schema->flags & LYS_VALID_EXT) {
PavolVican556559e2017-12-12 13:39:36 +0100774 // check extension in node
775 if (lyv_extension(schema->ext, schema->ext_size, node)) {
776 return EXIT_FAILURE;
777 }
778
779 if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
780 type = &((struct lys_node_leaf *) schema)->type;
781 leaf = (struct lyd_node_leaf_list *) node;
782 if (lyv_type_extension(leaf, type, 1)) {
783 return EXIT_FAILURE;
784 }
785 }
PavolVican556559e2017-12-12 13:39:36 +0100786 }
787
Radek Krejcid788a522016-07-25 14:57:38 +0200788 /* remove the flag */
789 node->validity &= ~LYD_VAL_MAND;
790 }
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100791
Michal Vasko4d1e7b02018-08-08 16:28:02 +0200792 if (schema->nodetype & (LYS_LIST | LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION)) {
Michal Vasko0c59e822018-09-13 09:32:02 +0200793 siter = NULL;
794 while ((siter = lys_getnext(siter, schema, NULL, 0))) {
Michal Vasko4d1e7b02018-08-08 16:28:02 +0200795 if (siter->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
796 LY_TREE_FOR(node->child, diter) {
Michal Vasko185b5272018-09-13 14:26:12 +0200797 if (diter->schema == siter && (diter->validity & LYD_VAL_DUP)) {
Michal Vasko4d1e7b02018-08-08 16:28:02 +0200798 /* skip key uniqueness check in case of get/get-config data */
799 if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_GET | LYD_OPT_GETCONFIG))) {
Michal Vasko185b5272018-09-13 14:26:12 +0200800 if (lyv_data_dup(diter, node->child)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100801 return 1;
Radek Krejci63b79c82016-08-10 10:09:33 +0200802 }
Michal Vasko4d1e7b02018-08-08 16:28:02 +0200803 } else {
804 /* always remove the flag */
Michal Vasko185b5272018-09-13 14:26:12 +0200805 diter->validity &= ~LYD_VAL_DUP;
Radek Krejci63b79c82016-08-10 10:09:33 +0200806 }
Michal Vasko4d1e7b02018-08-08 16:28:02 +0200807 /* all schema instances checked, continue with another schema node */
808 break;
Radek Krejci63b79c82016-08-10 10:09:33 +0200809 }
Radek Krejcica7efb72016-01-18 13:06:01 +0100810 }
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100811 }
812 }
Radek Krejcid788a522016-07-25 14:57:38 +0200813 }
814
Michal Vasko185b5272018-09-13 14:26:12 +0200815 if (node->validity & LYD_VAL_UNIQUE) {
Michal Vasko02340262018-11-30 09:11:52 +0100816 if (options & LYD_OPT_TRUSTED) {
817 /* just remove flag */
818 node->validity &= ~LYD_VAL_UNIQUE;
819 } else {
820 /* check the unique constraint at the end (once the parsing is done) */
821 if (unres_data_add(unres, node, UNRES_UNIQ_LEAVES)) {
822 return 1;
823 }
Michal Vasko185b5272018-09-13 14:26:12 +0200824 }
825 }
826
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200827 if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
828 /* since feature can be enabled/disabled, do this check despite the validity flag,
829 * - check if the type value (enum, bit, identity) is disabled via feature */
830 leaf = (struct lyd_node_leaf_list *)node;
831 switch (leaf->value_type) {
832 case LY_TYPE_BITS:
833 id = "Bit";
834 /* get the count of bits */
Michal Vasko91ff4592017-05-24 15:41:49 +0200835 type = find_orig_type(&((struct lys_node_leaf *)leaf->schema)->type, LY_TYPE_BITS);
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200836 for (j = iff_size = 0; j < type->info.bits.count; j++) {
837 if (!leaf->value.bit[j]) {
838 continue;
839 }
840 idname = leaf->value.bit[j]->name;
841 iff_size = leaf->value.bit[j]->iffeature_size;
842 iff = leaf->value.bit[j]->iffeature;
843 break;
844nextbit:
845 iff_size = 0;
846 }
847 break;
848 case LY_TYPE_ENUM:
849 id = "Enum";
850 idname = leaf->value_str;
851 iff_size = leaf->value.enm->iffeature_size;
852 iff = leaf->value.enm->iffeature;
853 break;
854 case LY_TYPE_IDENT:
855 id = "Identity";
856 idname = leaf->value_str;
857 iff_size = leaf->value.ident->iffeature_size;
858 iff = leaf->value.ident->iffeature;
859 break;
860 default:
861 iff_size = 0;
862 break;
863 }
864
865 if (iff_size) {
866 for (i = 0; i < iff_size; i++) {
867 if (!resolve_iffeature(&iff[i])) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100868 LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, node, leaf->value_str, schema->name);
Michal Vasko83cea222020-09-11 15:50:43 +0200869 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "%s \"%s\" is disabled by its %d. if-feature condition.",
870 id, idname, i);
Michal Vasko53b7da02018-02-13 15:28:42 +0100871 return 1;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200872 }
873 }
874 if (leaf->value_type == LY_TYPE_BITS) {
875 goto nextbit;
876 }
877 }
878 }
879
Michal Vaskoad2e44a2017-01-03 10:31:35 +0100880 /* check must conditions */
881 if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER | LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG))) {
Michal Vasko4b5b6252016-10-11 12:18:00 +0000882 i = resolve_applies_must(node);
Michal Vasko53b7da02018-02-13 15:28:42 +0100883 if ((i & 0x1) && unres_data_add(unres, node, UNRES_MUST)) {
884 return 1;
Michal Vasko4b5b6252016-10-11 12:18:00 +0000885 }
Michal Vasko53b7da02018-02-13 15:28:42 +0100886 if ((i & 0x2) && unres_data_add(unres, node, UNRES_MUST_INOUT)) {
887 return 1;
Michal Vasko4b5b6252016-10-11 12:18:00 +0000888 }
Michal Vaskobf19d252015-10-08 15:39:17 +0200889 }
890
Michal Vasko53b7da02018-02-13 15:28:42 +0100891 return 0;
892}
893
894int
895lyv_multicases(struct lyd_node *node, struct lys_node *schemanode, struct lyd_node **first_sibling,
896 int autodelete, struct lyd_node *nodel)
897{
898 struct lys_node *sparent, *schoice, *scase, *saux;
899 struct lyd_node *next, *iter;
900 assert(node || schemanode);
901
902 if (!schemanode) {
903 schemanode = node->schema;
904 }
905
Michal Vaskoa1abd3c2019-04-23 15:58:26 +0200906 for (sparent = lys_parent(schemanode); sparent && (sparent->nodetype == LYS_USES); sparent = lys_parent(sparent));
Michal Vasko53b7da02018-02-13 15:28:42 +0100907 if (!sparent || !(sparent->nodetype & (LYS_CHOICE | LYS_CASE))) {
908 /* node is not under any choice */
909 return 0;
910 } else if (!first_sibling || !(*first_sibling)) {
911 /* nothing to check */
912 return 0;
913 }
914
915 /* remember which case to skip in which choice */
916 if (sparent->nodetype == LYS_CHOICE) {
917 schoice = sparent;
918 scase = schemanode;
919 } else {
920 schoice = lys_parent(sparent);
921 scase = sparent;
922 }
923
924autodelete:
925 /* remove all nodes from other cases than 'sparent' */
926 LY_TREE_FOR_SAFE(*first_sibling, next, iter) {
927 if (schemanode == iter->schema) {
928 continue;
929 }
930
Michal Vaskoa1abd3c2019-04-23 15:58:26 +0200931 for (sparent = lys_parent(iter->schema); sparent && (sparent->nodetype == LYS_USES); sparent = lys_parent(sparent));
Michal Vasko53b7da02018-02-13 15:28:42 +0100932 if (sparent && ((sparent->nodetype == LYS_CHOICE && sparent == schoice) /* another implicit case */
933 || (sparent->nodetype == LYS_CASE && sparent != scase && lys_parent(sparent) == schoice)) /* another case */
934 ) {
935 if (autodelete) {
936 if (iter == nodel) {
937 LOGVAL(schemanode->module->ctx, LYE_MCASEDATA, LY_VLOG_LYD, iter, schoice->name);
938 return 2;
939 }
940 if (iter == *first_sibling) {
941 *first_sibling = next;
942 }
943 lyd_free(iter);
944 } else {
Michal Vaskoaf8ec362018-03-28 09:08:09 +0200945 LOGVAL(schemanode->module->ctx, LYE_MCASEDATA, LY_VLOG_LYD, iter, schoice->name);
Michal Vasko53b7da02018-02-13 15:28:42 +0100946 return 1;
947 }
948 }
949 }
950
951 if (*first_sibling && (saux = lys_parent(schoice)) && (saux->nodetype & LYS_CASE)) {
952 /* go recursively in case of nested choices */
953 schoice = lys_parent(saux);
954 scase = saux;
955 goto autodelete;
956 }
957
958 return 0;
Radek Krejcieab784a2015-08-27 09:56:53 +0200959}