Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame^] | 1 | /** |
| 2 | * @file plugins_exts_nacm.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang extension plugin - NACM (RFC 6536) |
| 5 | * |
| 6 | * Copyright (c) 2019 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * 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 |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | #include "common.h" |
| 15 | |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include "plugins_exts.h" |
| 19 | #include "tree_schema.h" |
| 20 | |
| 21 | /** |
| 22 | * @brief Storage for ID used to check plugin API version compatibility. |
| 23 | * Ignored here in the internal plugin. |
| 24 | LYEXT_VERSION_CHECK |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * @brief Compile NAMC's extension instances. |
| 29 | * |
| 30 | * Implementation of lyext_clb_compile callback set as lyext_plugin::compile. |
| 31 | */ |
| 32 | LY_ERR |
| 33 | nacm_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext) |
| 34 | { |
| 35 | struct lysc_node *parent = NULL, *iter; |
| 36 | struct lysc_ext_instance *inherited; |
| 37 | unsigned int u; |
| 38 | |
| 39 | static const uint8_t nacm_deny_all = 1; |
| 40 | static const uint8_t nacm_deny_write = 2; |
| 41 | |
| 42 | /* store the NACM flag */ |
| 43 | if (!strcmp(c_ext->def->name, "default-deny-write")) { |
| 44 | c_ext->data = (void*)&nacm_deny_write; |
| 45 | } else if (!strcmp(c_ext->def->name, "default-deny-all")) { |
| 46 | c_ext->data = (void*)&nacm_deny_all; |
| 47 | } else { |
| 48 | return LY_EINT; |
| 49 | } |
| 50 | |
| 51 | /* check that the extension is instantiated at an allowed place - data node */ |
| 52 | if (c_ext->parent_type != LYEXT_PAR_NODE) { |
| 53 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is allowed only in a data nodes, but it is placed in \"%s\" statement.", |
| 54 | p_ext->name, lyext_parent2str(c_ext->parent_type)); |
| 55 | return LY_EVALID; |
| 56 | } else { |
| 57 | parent = (struct lysc_node*)c_ext->parent; |
| 58 | if (!(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_ANYDATA | LYS_CASE | LYS_ACTION | LYS_NOTIF))) { |
| 59 | /* note LYS_AUGMENT and LYS_USES is not in the list since they are not present in the compiled tree. Instead, libyang |
| 60 | * passes all their extensions to their children nodes */ |
| 61 | invalid_parent: |
| 62 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, |
| 63 | "Extension %s is not allowed in %s statement.", p_ext->name, lys_nodetype2str(parent->nodetype)); |
| 64 | return LY_EVALID; |
| 65 | } |
| 66 | if (c_ext->data == (void*)&nacm_deny_write && (parent->nodetype & (LYS_ACTION | LYS_NOTIF))) { |
| 67 | goto invalid_parent; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /* check for duplication */ |
| 72 | LY_ARRAY_FOR(parent->exts, u) { |
| 73 | if (&parent->exts[u] != c_ext && parent->exts[u].def->plugin == c_ext->def->plugin) { |
| 74 | /* duplication of a NACM extension on a single node |
| 75 | * We check plugin since we want to catch even the situation that there is default-deny-all |
| 76 | * AND default-deny-write */ |
| 77 | if (parent->exts[u].def == c_ext->def) { |
| 78 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name); |
| 79 | } else { |
| 80 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension nacm:default-deny-write is mixed with nacm:default-deny-all."); |
| 81 | } |
| 82 | return LY_EVALID; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /* inherit the extension instance to all the children nodes */ |
| 87 | LYSC_TREE_DFS_BEGIN(parent, iter) { |
| 88 | if (iter != parent) { /* ignore the parent from which we inherit */ |
| 89 | /* check that the node does not have its own NACM extension instance */ |
| 90 | LY_ARRAY_FOR(iter->exts, u) { |
| 91 | if (iter->exts[u].def == c_ext->def) { |
| 92 | /* the child already have its own NACM flag, so skip the subtree */ |
| 93 | LYSC_TREE_DFS_continue = 1; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | if (!LYSC_TREE_DFS_continue) { |
| 98 | /* duplicate this one to inherit it to the child */ |
| 99 | LY_ARRAY_NEW_RET(cctx->ctx, iter->exts, inherited, LY_EMEM); |
| 100 | |
| 101 | inherited->def = c_ext->def; |
| 102 | inherited->parent = iter; |
| 103 | inherited->parent_type = LYEXT_PAR_NODE; |
| 104 | if (c_ext->argument) { |
| 105 | inherited->argument = lydict_insert(cctx->ctx, c_ext->argument, strlen(c_ext->argument)); |
| 106 | } |
| 107 | /* TODO duplicate extension instances */ |
| 108 | inherited->data = c_ext->data; |
| 109 | } |
| 110 | } |
| 111 | LYSC_TREE_DFS_END(parent, iter) |
| 112 | } |
| 113 | |
| 114 | return LY_SUCCESS; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * @brief Plugin for the NACM's default-deny-write and default-deny-all extensions |
| 120 | */ |
| 121 | struct lyext_plugin nacm_plugin = { |
| 122 | .id = "libyang 2 - NACM, version 1", |
| 123 | .compile = &nacm_compile, |
| 124 | .validate = NULL, |
| 125 | .free = NULL |
| 126 | }; |