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> |
| 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 | */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 14 | |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 15 | #include <stdint.h> |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 16 | #include <stdlib.h> |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 17 | #include <string.h> |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 19 | #include "libyang.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 20 | #include "plugins_exts.h" |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 21 | |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 22 | struct nacm_dfs_arg { |
| 23 | struct lysc_ext_instance *c_ext; |
| 24 | struct lysc_node *parent; |
| 25 | }; |
| 26 | |
| 27 | /** |
| 28 | * @brief DFS callback implementation for inheriting the NACM extension. |
| 29 | */ |
| 30 | static LY_ERR |
| 31 | nacm_inherit_clb(struct lysc_node *node, void *data, ly_bool *dfs_continue) |
| 32 | { |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 33 | LY_ERR ret; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 34 | struct nacm_dfs_arg *arg = data; |
| 35 | struct lysc_ext_instance *inherited; |
| 36 | LY_ARRAY_COUNT_TYPE u; |
| 37 | |
| 38 | /* ignore the parent from which we inherit and input/output nodes */ |
| 39 | if ((node != arg->parent) && !(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 40 | /* check that the node does not have its own NACM extension instance */ |
| 41 | LY_ARRAY_FOR(node->exts, u) { |
| 42 | if (node->exts[u].def == arg->c_ext->def) { |
| 43 | /* the child already have its own NACM flag, so skip the subtree */ |
| 44 | *dfs_continue = 1; |
| 45 | return LY_SUCCESS; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /* duplicate this one to inherit it to the child */ |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 50 | LY_ARRAY_NEW_GOTO(node->module->ctx, node->exts, inherited, ret, emem); |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 51 | |
| 52 | inherited->def = lysc_ext_dup(arg->c_ext->def); |
| 53 | inherited->parent = node; |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 54 | inherited->parent_stmt = lys_nodetype2stmt(node->nodetype); |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 55 | if (arg->c_ext->argument) { |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 56 | LY_ERR ret; |
| 57 | |
| 58 | if ((ret = lydict_insert(node->module->ctx, arg->c_ext->argument, strlen(arg->c_ext->argument), |
| 59 | &inherited->argument))) { |
| 60 | return ret; |
| 61 | } |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 62 | } |
| 63 | /* TODO duplicate extension instances */ |
| 64 | inherited->data = arg->c_ext->data; |
| 65 | } |
| 66 | |
| 67 | return LY_SUCCESS; |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 68 | |
| 69 | emem: |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 70 | lyplg_ext_log(arg->c_ext, LY_LLERR, LY_EMEM, NULL, "Memory allocation failed (%s()).", __func__); |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 71 | return ret; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 74 | /** |
| 75 | * @brief Compile NAMC's extension instances. |
| 76 | * |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 77 | * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 78 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 79 | static LY_ERR |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 80 | nacm_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext) |
| 81 | { |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 82 | LY_ERR ret; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 83 | struct lysc_node *parent = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 84 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 85 | struct nacm_dfs_arg dfs_arg; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 86 | |
| 87 | static const uint8_t nacm_deny_all = 1; |
| 88 | static const uint8_t nacm_deny_write = 2; |
| 89 | |
| 90 | /* store the NACM flag */ |
| 91 | if (!strcmp(c_ext->def->name, "default-deny-write")) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 92 | c_ext->data = (void *)&nacm_deny_write; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 93 | } else if (!strcmp(c_ext->def->name, "default-deny-all")) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 94 | c_ext->data = (void *)&nacm_deny_all; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 95 | } else { |
| 96 | return LY_EINT; |
| 97 | } |
| 98 | |
| 99 | /* check that the extension is instantiated at an allowed place - data node */ |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 100 | if (!LY_STMT_IS_NODE(c_ext->parent_stmt)) { |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 101 | lyplg_ext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx), |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 102 | "Extension %s is allowed only in a data nodes, but it is placed in \"%s\" statement.", |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 103 | p_ext->name, ly_stmt2str(c_ext->parent_stmt)); |
Radek Krejci | 0bfc6f9 | 2021-02-09 13:13:13 +0100 | [diff] [blame] | 104 | return LY_ENOT; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 105 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 106 | parent = (struct lysc_node *)c_ext->parent; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 107 | if (!(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_ANYDATA | |
| 108 | LYS_CASE | LYS_RPC | LYS_ACTION | LYS_NOTIF))) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 109 | /* note LYS_AUGMENT and LYS_USES is not in the list since they are not present in the compiled tree. Instead, libyang |
| 110 | * passes all their extensions to their children nodes */ |
| 111 | invalid_parent: |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 112 | lyplg_ext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 113 | "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] | 114 | return LY_ENOT; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 115 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 116 | 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] | 117 | goto invalid_parent; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* check for duplication */ |
| 122 | LY_ARRAY_FOR(parent->exts, u) { |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 123 | if ((&parent->exts[u] != c_ext) && (parent->exts[u].def->plugin->compile == c_ext->def->plugin->compile)) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 124 | /* duplication of a NACM extension on a single node |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 125 | * 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] | 126 | * AND default-deny-write */ |
| 127 | if (parent->exts[u].def == c_ext->def) { |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 128 | lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 129 | "Extension %s is instantiated multiple times.", p_ext->name); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 130 | } else { |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 131 | lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 132 | "Extension nacm:default-deny-write is mixed with nacm:default-deny-all."); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 133 | } |
| 134 | return LY_EVALID; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* inherit the extension instance to all the children nodes */ |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 139 | dfs_arg.c_ext = c_ext; |
| 140 | dfs_arg.parent = parent; |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 141 | ret = lysc_tree_dfs_full(parent, nacm_inherit_clb, &dfs_arg); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 142 | |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 143 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 146 | /** |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 147 | * @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] | 148 | * |
| 149 | * Note that external plugins are supposed to use: |
| 150 | * |
| 151 | * LYPLG_EXTENSIONS = { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 152 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 153 | const struct lyplg_ext_record plugins_nacm[] = { |
| 154 | { |
| 155 | .module = "ietf-netconf-acm", |
| 156 | .revision = "2012-02-22", |
| 157 | .name = "default-deny-write", |
| 158 | |
| 159 | .plugin.id = "libyang 2 - NACM, version 1", |
| 160 | .plugin.compile = &nacm_compile, |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 161 | .plugin.sprinter = NULL, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 162 | .plugin.free = NULL, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame^] | 163 | .plugin.snode = NULL, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 164 | .plugin.validate = NULL |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 165 | }, { |
| 166 | .module = "ietf-netconf-acm", |
| 167 | .revision = "2018-02-14", |
| 168 | .name = "default-deny-write", |
| 169 | |
| 170 | .plugin.id = "libyang 2 - NACM, version 1", |
| 171 | .plugin.compile = &nacm_compile, |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 172 | .plugin.sprinter = NULL, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 173 | .plugin.free = NULL, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame^] | 174 | .plugin.snode = NULL, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 175 | .plugin.validate = NULL |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 176 | }, { |
| 177 | .module = "ietf-netconf-acm", |
| 178 | .revision = "2012-02-22", |
| 179 | .name = "default-deny-all", |
| 180 | |
| 181 | .plugin.id = "libyang 2 - NACM, version 1", |
| 182 | .plugin.compile = &nacm_compile, |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 183 | .plugin.sprinter = NULL, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 184 | .plugin.free = NULL, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame^] | 185 | .plugin.snode = NULL, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 186 | .plugin.validate = NULL |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 187 | }, { |
| 188 | .module = "ietf-netconf-acm", |
| 189 | .revision = "2018-02-14", |
| 190 | .name = "default-deny-all", |
| 191 | |
| 192 | .plugin.id = "libyang 2 - NACM, version 1", |
| 193 | .plugin.compile = &nacm_compile, |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 194 | .plugin.sprinter = NULL, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 195 | .plugin.free = NULL, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame^] | 196 | .plugin.snode = NULL, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 197 | .plugin.validate = NULL |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 198 | }, |
| 199 | {0} /* terminating zeroed item */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 200 | }; |