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 | |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 27 | struct nacm_dfs_arg { |
| 28 | struct lysc_ext_instance *c_ext; |
| 29 | struct lysc_node *parent; |
| 30 | }; |
| 31 | |
| 32 | /** |
| 33 | * @brief DFS callback implementation for inheriting the NACM extension. |
| 34 | */ |
| 35 | static LY_ERR |
| 36 | nacm_inherit_clb(struct lysc_node *node, void *data, ly_bool *dfs_continue) |
| 37 | { |
| 38 | struct nacm_dfs_arg *arg = data; |
| 39 | struct lysc_ext_instance *inherited; |
| 40 | LY_ARRAY_COUNT_TYPE u; |
| 41 | |
| 42 | /* ignore the parent from which we inherit and input/output nodes */ |
| 43 | if ((node != arg->parent) && !(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 44 | /* check that the node does not have its own NACM extension instance */ |
| 45 | LY_ARRAY_FOR(node->exts, u) { |
| 46 | if (node->exts[u].def == arg->c_ext->def) { |
| 47 | /* the child already have its own NACM flag, so skip the subtree */ |
| 48 | *dfs_continue = 1; |
| 49 | return LY_SUCCESS; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /* duplicate this one to inherit it to the child */ |
| 54 | LY_ARRAY_NEW_RET(node->module->ctx, node->exts, inherited, LY_EMEM); |
| 55 | |
| 56 | inherited->def = lysc_ext_dup(arg->c_ext->def); |
| 57 | inherited->parent = node; |
| 58 | inherited->parent_type = LYEXT_PAR_NODE; |
| 59 | if (arg->c_ext->argument) { |
| 60 | LY_CHECK_RET(lydict_insert(node->module->ctx, arg->c_ext->argument, strlen(arg->c_ext->argument), |
| 61 | &inherited->argument)); |
| 62 | } |
| 63 | /* TODO duplicate extension instances */ |
| 64 | inherited->data = arg->c_ext->data; |
| 65 | } |
| 66 | |
| 67 | return LY_SUCCESS; |
| 68 | } |
| 69 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 70 | /** |
| 71 | * @brief Compile NAMC's extension instances. |
| 72 | * |
| 73 | * Implementation of lyext_clb_compile callback set as lyext_plugin::compile. |
| 74 | */ |
| 75 | LY_ERR |
| 76 | nacm_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext) |
| 77 | { |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 78 | struct lysc_node *parent = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 79 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 80 | struct nacm_dfs_arg dfs_arg; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 81 | |
| 82 | static const uint8_t nacm_deny_all = 1; |
| 83 | static const uint8_t nacm_deny_write = 2; |
| 84 | |
| 85 | /* store the NACM flag */ |
| 86 | if (!strcmp(c_ext->def->name, "default-deny-write")) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 87 | c_ext->data = (void *)&nacm_deny_write; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 88 | } else if (!strcmp(c_ext->def->name, "default-deny-all")) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 89 | c_ext->data = (void *)&nacm_deny_all; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 90 | } else { |
| 91 | return LY_EINT; |
| 92 | } |
| 93 | |
| 94 | /* check that the extension is instantiated at an allowed place - data node */ |
| 95 | if (c_ext->parent_type != LYEXT_PAR_NODE) { |
Radek Krejci | 0bfc6f9 | 2021-02-09 13:13:13 +0100 | [diff] [blame^] | 96 | lyext_log(c_ext, LY_LLWRN, 0, cctx->path, "Extension %s is allowed only in a data nodes, but it is placed in \"%s\" statement.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 97 | p_ext->name, lyext_parent2str(c_ext->parent_type)); |
Radek Krejci | 0bfc6f9 | 2021-02-09 13:13:13 +0100 | [diff] [blame^] | 98 | return LY_ENOT; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 99 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 100 | parent = (struct lysc_node *)c_ext->parent; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 101 | if (!(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_ANYDATA | |
| 102 | LYS_CASE | LYS_RPC | LYS_ACTION | LYS_NOTIF))) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 103 | /* note LYS_AUGMENT and LYS_USES is not in the list since they are not present in the compiled tree. Instead, libyang |
| 104 | * passes all their extensions to their children nodes */ |
| 105 | invalid_parent: |
Radek Krejci | 0bfc6f9 | 2021-02-09 13:13:13 +0100 | [diff] [blame^] | 106 | lyext_log(c_ext, LY_LLWRN, 0, cctx->path, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 107 | "Extension %s is not allowed in %s statement.", p_ext->name, lys_nodetype2str(parent->nodetype)); |
Radek Krejci | 0bfc6f9 | 2021-02-09 13:13:13 +0100 | [diff] [blame^] | 108 | return LY_ENOT; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 109 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 110 | if ((c_ext->data == (void *)&nacm_deny_write) && (parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 111 | goto invalid_parent; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* check for duplication */ |
| 116 | LY_ARRAY_FOR(parent->exts, u) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 117 | if ((&parent->exts[u] != c_ext) && (parent->exts[u].def->plugin == c_ext->def->plugin)) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 118 | /* duplication of a NACM extension on a single node |
| 119 | * We check plugin since we want to catch even the situation that there is default-deny-all |
| 120 | * AND default-deny-write */ |
| 121 | if (parent->exts[u].def == c_ext->def) { |
| 122 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name); |
| 123 | } else { |
| 124 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension nacm:default-deny-write is mixed with nacm:default-deny-all."); |
| 125 | } |
| 126 | return LY_EVALID; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* inherit the extension instance to all the children nodes */ |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 131 | dfs_arg.c_ext = c_ext; |
| 132 | dfs_arg.parent = parent; |
| 133 | LY_CHECK_RET(lysc_tree_dfs_full(parent, nacm_inherit_clb, &dfs_arg)); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 134 | |
| 135 | return LY_SUCCESS; |
| 136 | } |
| 137 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 138 | /** |
| 139 | * @brief Plugin for the NACM's default-deny-write and default-deny-all extensions |
| 140 | */ |
| 141 | struct lyext_plugin nacm_plugin = { |
| 142 | .id = "libyang 2 - NACM, version 1", |
| 143 | .compile = &nacm_compile, |
| 144 | .validate = NULL, |
| 145 | .free = NULL |
| 146 | }; |