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