Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1 | /** |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file nacm.c |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5 | * @brief libyang extension plugin - NACM (RFC 6536) |
| 6 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 7 | * Copyright (c) 2019 - 2022 CESNET, z.s.p.o. |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +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 | */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 15 | |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 16 | #include <stdint.h> |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 17 | #include <stdlib.h> |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 18 | #include <string.h> |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 19 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 20 | #include "compat.h" |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 21 | #include "libyang.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 22 | #include "plugins_exts.h" |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 23 | |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 24 | struct nacm_dfs_arg { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 25 | struct lysc_ext_instance *ext; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 26 | struct lysc_node *parent; |
| 27 | }; |
| 28 | |
| 29 | /** |
| 30 | * @brief DFS callback implementation for inheriting the NACM extension. |
| 31 | */ |
| 32 | static LY_ERR |
| 33 | nacm_inherit_clb(struct lysc_node *node, void *data, ly_bool *dfs_continue) |
| 34 | { |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 35 | LY_ERR ret; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 36 | struct nacm_dfs_arg *arg = data; |
| 37 | struct lysc_ext_instance *inherited; |
| 38 | LY_ARRAY_COUNT_TYPE u; |
| 39 | |
| 40 | /* ignore the parent from which we inherit and input/output nodes */ |
| 41 | if ((node != arg->parent) && !(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 42 | /* check that the node does not have its own NACM extension instance */ |
| 43 | LY_ARRAY_FOR(node->exts, u) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 44 | if (node->exts[u].def == arg->ext->def) { |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 45 | /* the child already have its own NACM flag, so skip the subtree */ |
| 46 | *dfs_continue = 1; |
| 47 | return LY_SUCCESS; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /* duplicate this one to inherit it to the child */ |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 52 | LY_ARRAY_NEW_GOTO(node->module->ctx, node->exts, inherited, ret, emem); |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 53 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 54 | inherited->def = arg->ext->def; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 55 | inherited->parent = node; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 56 | inherited->parent_stmt = lyplg_ext_nodetype2stmt(node->nodetype); |
| 57 | if (arg->ext->argument) { |
| 58 | if ((ret = lydict_insert(node->module->ctx, arg->ext->argument, 0, &inherited->argument))) { |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 59 | return ret; |
| 60 | } |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 61 | } |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 62 | /* copy the pointer to the static variables */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 63 | inherited->compiled = arg->ext->compiled; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | return LY_SUCCESS; |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 67 | |
| 68 | emem: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 69 | lyplg_ext_compile_log(NULL, arg->ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 70 | return ret; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 73 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 74 | * @brief Parse NACM extension instances. |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 75 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 76 | * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 77 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 78 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 79 | nacm_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 80 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 81 | struct lysp_node *parent = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 82 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 83 | |
| 84 | /* check that the extension is instantiated at an allowed place - data node */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 85 | if (!(ext->parent_stmt & LY_STMT_NODE_MASK)) { |
| 86 | lyplg_ext_parse_log(pctx, ext, LY_LLWRN, 0, "Extension %s is allowed only in a data nodes, but it is placed in " |
| 87 | "\"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt)); |
Radek Krejci | 0bfc6f9 | 2021-02-09 13:13:13 +0100 | [diff] [blame] | 88 | return LY_ENOT; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | parent = ext->parent; |
| 92 | if (!(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_ANYDATA | |
| 93 | LYS_CASE | LYS_RPC | LYS_ACTION | LYS_NOTIF)) || (!strcmp(strchr(ext->name, ':') + 1, "default-deny-write") && |
| 94 | (parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))) { |
| 95 | /* note LYS_AUGMENT and LYS_USES is not in the list since they are not present in the compiled tree. Instead, libyang |
| 96 | * passes all their extensions to their children nodes */ |
| 97 | lyplg_ext_parse_log(pctx, ext, LY_LLWRN, 0, "Extension %s is not allowed in %s statement.", ext->name, |
| 98 | lys_nodetype2str(parent->nodetype)); |
| 99 | return LY_ENOT; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* check for duplication */ |
| 103 | LY_ARRAY_FOR(parent->exts, u) { |
lePici | cd7e4ad | 2023-07-04 13:26:41 +0200 | [diff] [blame] | 104 | if ((&parent->exts[u] != ext) && parent->exts[u].record && !strcmp(parent->exts[u].record->plugin.id, ext->record->plugin.id)) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 105 | /* duplication of a NACM extension on a single node |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 106 | * We check for all NACM plugins since we want to catch even the situation that there is default-deny-all |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 107 | * AND default-deny-write */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 108 | if (parent->exts[u].name == ext->name) { |
| 109 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 110 | } else { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 111 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 112 | "Extension nacm:default-deny-write is mixed with nacm:default-deny-all."); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 113 | } |
| 114 | return LY_EVALID; |
| 115 | } |
| 116 | } |
| 117 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 118 | return LY_SUCCESS; |
| 119 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 120 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 121 | /** |
| 122 | * @brief Compile NACM extension instances. |
| 123 | * |
| 124 | * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. |
| 125 | */ |
| 126 | static LY_ERR |
| 127 | nacm_compile(struct lysc_ctx *UNUSED(cctx), const struct lysp_ext_instance *UNUSED(extp), struct lysc_ext_instance *ext) |
| 128 | { |
| 129 | struct nacm_dfs_arg dfs_arg; |
| 130 | |
| 131 | static const uint8_t nacm_deny_all = 1; |
| 132 | static const uint8_t nacm_deny_write = 2; |
| 133 | |
| 134 | /* store the NACM flag */ |
| 135 | if (!strcmp(ext->def->name, "default-deny-write")) { |
| 136 | ext->compiled = (void *)&nacm_deny_write; |
| 137 | } else if (!strcmp(ext->def->name, "default-deny-all")) { |
| 138 | ext->compiled = (void *)&nacm_deny_all; |
| 139 | } else { |
| 140 | return LY_EINT; |
| 141 | } |
| 142 | |
| 143 | /* inherit the extension instance to all the children nodes */ |
| 144 | dfs_arg.ext = ext; |
| 145 | dfs_arg.parent = ext->parent; |
| 146 | return lysc_tree_dfs_full(ext->parent, nacm_inherit_clb, &dfs_arg); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 149 | /** |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 150 | * @brief Plugin descriptions for the NACM's default-deny-write and default-deny-all extensions |
Radek Krejci | a6f61e7 | 2021-03-24 21:00:19 +0100 | [diff] [blame] | 151 | * |
| 152 | * Note that external plugins are supposed to use: |
| 153 | * |
| 154 | * LYPLG_EXTENSIONS = { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 155 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 156 | const struct lyplg_ext_record plugins_nacm[] = { |
| 157 | { |
| 158 | .module = "ietf-netconf-acm", |
| 159 | .revision = "2012-02-22", |
| 160 | .name = "default-deny-write", |
| 161 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 162 | .plugin.id = "ly2 NACM v1", |
| 163 | .plugin.parse = nacm_parse, |
| 164 | .plugin.compile = nacm_compile, |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 165 | .plugin.printer_info = NULL, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 166 | .plugin.printer_ctree = NULL, |
| 167 | .plugin.printer_ptree = NULL, |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 168 | .plugin.node = NULL, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 169 | .plugin.snode = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 170 | .plugin.validate = NULL, |
| 171 | .plugin.pfree = NULL, |
| 172 | .plugin.cfree = NULL |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 173 | }, { |
| 174 | .module = "ietf-netconf-acm", |
| 175 | .revision = "2018-02-14", |
| 176 | .name = "default-deny-write", |
| 177 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 178 | .plugin.id = "ly2 NACM v1", |
| 179 | .plugin.parse = nacm_parse, |
| 180 | .plugin.compile = nacm_compile, |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 181 | .plugin.printer_info = NULL, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 182 | .plugin.printer_ctree = NULL, |
| 183 | .plugin.printer_ptree = NULL, |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 184 | .plugin.node = NULL, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 185 | .plugin.snode = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 186 | .plugin.validate = NULL, |
| 187 | .plugin.pfree = NULL, |
| 188 | .plugin.cfree = NULL |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 189 | }, { |
| 190 | .module = "ietf-netconf-acm", |
| 191 | .revision = "2012-02-22", |
| 192 | .name = "default-deny-all", |
| 193 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 194 | .plugin.id = "ly2 NACM v1", |
| 195 | .plugin.parse = nacm_parse, |
| 196 | .plugin.compile = nacm_compile, |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 197 | .plugin.printer_info = NULL, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 198 | .plugin.printer_ctree = NULL, |
| 199 | .plugin.printer_ptree = NULL, |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 200 | .plugin.node = NULL, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 201 | .plugin.snode = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 202 | .plugin.validate = NULL, |
| 203 | .plugin.pfree = NULL, |
| 204 | .plugin.cfree = NULL |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 205 | }, { |
| 206 | .module = "ietf-netconf-acm", |
| 207 | .revision = "2018-02-14", |
| 208 | .name = "default-deny-all", |
| 209 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 210 | .plugin.id = "ly2 NACM v1", |
| 211 | .plugin.parse = nacm_parse, |
| 212 | .plugin.compile = nacm_compile, |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 213 | .plugin.printer_info = NULL, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 214 | .plugin.printer_ctree = NULL, |
| 215 | .plugin.printer_ptree = NULL, |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 216 | .plugin.node = NULL, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 217 | .plugin.snode = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 218 | .plugin.validate = NULL, |
| 219 | .plugin.pfree = NULL, |
| 220 | .plugin.cfree = NULL |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 221 | }, |
| 222 | {0} /* terminating zeroed item */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 223 | }; |