blob: aa0d0e808b849b3ae5cfb47a8d52b1f1fe470ed3 [file] [log] [blame]
Radek Krejci0935f412019-08-20 16:15:18 +02001/**
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file nacm.c
Radek Krejci0935f412019-08-20 16:15:18 +02003 * @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 Krejci0935f412019-08-20 16:15:18 +020014
Radek Krejci883355a2021-03-11 11:54:41 +010015#include <stdint.h>
Radek Krejci0935f412019-08-20 16:15:18 +020016#include <stdlib.h>
Radek Krejci883355a2021-03-11 11:54:41 +010017#include <string.h>
Radek Krejci0935f412019-08-20 16:15:18 +020018
Radek Krejci883355a2021-03-11 11:54:41 +010019#include "libyang.h"
Radek Krejci0935f412019-08-20 16:15:18 +020020#include "plugins_exts.h"
Radek Krejci5f9a3672021-03-05 21:35:22 +010021
Michal Vaskof1ab44f2020-10-22 08:58:32 +020022struct 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 */
30static LY_ERR
31nacm_inherit_clb(struct lysc_node *node, void *data, ly_bool *dfs_continue)
32{
Radek Krejci859a15a2021-03-05 20:56:59 +010033 LY_ERR ret;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020034 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 Krejci859a15a2021-03-05 20:56:59 +010050 LY_ARRAY_NEW_GOTO(node->module->ctx, node->exts, inherited, ret, emem);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020051
52 inherited->def = lysc_ext_dup(arg->c_ext->def);
53 inherited->parent = node;
Radek Krejciab430862021-03-02 20:13:40 +010054 inherited->parent_stmt = lys_nodetype2stmt(node->nodetype);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020055 if (arg->c_ext->argument) {
Radek Krejci859a15a2021-03-05 20:56:59 +010056 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 Vaskof1ab44f2020-10-22 08:58:32 +020062 }
63 /* TODO duplicate extension instances */
64 inherited->data = arg->c_ext->data;
65 }
66
67 return LY_SUCCESS;
Radek Krejci859a15a2021-03-05 20:56:59 +010068
69emem:
Radek Krejci0b013302021-03-29 15:22:32 +020070 lyplg_ext_log(arg->c_ext, LY_LLERR, LY_EMEM, NULL, "Memory allocation failed (%s()).", __func__);
Radek Krejci859a15a2021-03-05 20:56:59 +010071 return ret;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020072}
73
Radek Krejci0935f412019-08-20 16:15:18 +020074/**
75 * @brief Compile NAMC's extension instances.
76 *
Radek Krejci0b013302021-03-29 15:22:32 +020077 * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
Radek Krejci0935f412019-08-20 16:15:18 +020078 */
Radek Krejci3e6632f2021-03-22 22:08:21 +010079static LY_ERR
Radek Krejci0935f412019-08-20 16:15:18 +020080nacm_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
81{
Radek Krejci859a15a2021-03-05 20:56:59 +010082 LY_ERR ret;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020083 struct lysc_node *parent = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +020084 LY_ARRAY_COUNT_TYPE u;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020085 struct nacm_dfs_arg dfs_arg;
Radek Krejci0935f412019-08-20 16:15:18 +020086
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 Vasko22df3f02020-08-24 13:29:22 +020092 c_ext->data = (void *)&nacm_deny_write;
Radek Krejci0935f412019-08-20 16:15:18 +020093 } else if (!strcmp(c_ext->def->name, "default-deny-all")) {
Michal Vasko22df3f02020-08-24 13:29:22 +020094 c_ext->data = (void *)&nacm_deny_all;
Radek Krejci0935f412019-08-20 16:15:18 +020095 } else {
96 return LY_EINT;
97 }
98
99 /* check that the extension is instantiated at an allowed place - data node */
Radek Krejciab430862021-03-02 20:13:40 +0100100 if (!LY_STMT_IS_NODE(c_ext->parent_stmt)) {
Radek Krejci0b013302021-03-29 15:22:32 +0200101 lyplg_ext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
Radek Krejci5f9a3672021-03-05 21:35:22 +0100102 "Extension %s is allowed only in a data nodes, but it is placed in \"%s\" statement.",
Radek Krejciab430862021-03-02 20:13:40 +0100103 p_ext->name, ly_stmt2str(c_ext->parent_stmt));
Radek Krejci0bfc6f92021-02-09 13:13:13 +0100104 return LY_ENOT;
Radek Krejci0935f412019-08-20 16:15:18 +0200105 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200106 parent = (struct lysc_node *)c_ext->parent;
Michal Vasko69730152020-10-09 16:30:07 +0200107 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 Krejci0935f412019-08-20 16:15:18 +0200109 /* 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 */
111invalid_parent:
Radek Krejci0b013302021-03-29 15:22:32 +0200112 lyplg_ext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
Michal Vasko69730152020-10-09 16:30:07 +0200113 "Extension %s is not allowed in %s statement.", p_ext->name, lys_nodetype2str(parent->nodetype));
Radek Krejci0bfc6f92021-02-09 13:13:13 +0100114 return LY_ENOT;
Radek Krejci0935f412019-08-20 16:15:18 +0200115 }
Michal Vasko69730152020-10-09 16:30:07 +0200116 if ((c_ext->data == (void *)&nacm_deny_write) && (parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
Radek Krejci0935f412019-08-20 16:15:18 +0200117 goto invalid_parent;
118 }
119 }
120
121 /* check for duplication */
122 LY_ARRAY_FOR(parent->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +0100123 if ((&parent->exts[u] != c_ext) && (parent->exts[u].def->plugin->compile == c_ext->def->plugin->compile)) {
Radek Krejci0935f412019-08-20 16:15:18 +0200124 /* duplication of a NACM extension on a single node
Radek Krejci3e6632f2021-03-22 22:08:21 +0100125 * We check for all NACM plugins since we want to catch even the situation that there is default-deny-all
Radek Krejci0935f412019-08-20 16:15:18 +0200126 * AND default-deny-write */
127 if (parent->exts[u].def == c_ext->def) {
Radek Krejci0b013302021-03-29 15:22:32 +0200128 lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
Radek Krejci5f9a3672021-03-05 21:35:22 +0100129 "Extension %s is instantiated multiple times.", p_ext->name);
Radek Krejci0935f412019-08-20 16:15:18 +0200130 } else {
Radek Krejci0b013302021-03-29 15:22:32 +0200131 lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
Radek Krejci5f9a3672021-03-05 21:35:22 +0100132 "Extension nacm:default-deny-write is mixed with nacm:default-deny-all.");
Radek Krejci0935f412019-08-20 16:15:18 +0200133 }
134 return LY_EVALID;
135 }
136 }
137
138 /* inherit the extension instance to all the children nodes */
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200139 dfs_arg.c_ext = c_ext;
140 dfs_arg.parent = parent;
Radek Krejci859a15a2021-03-05 20:56:59 +0100141 ret = lysc_tree_dfs_full(parent, nacm_inherit_clb, &dfs_arg);
Radek Krejci0935f412019-08-20 16:15:18 +0200142
Radek Krejci859a15a2021-03-05 20:56:59 +0100143 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200144}
145
Radek Krejci0935f412019-08-20 16:15:18 +0200146/**
Radek Krejci3e6632f2021-03-22 22:08:21 +0100147 * @brief Plugin descriptions for the NACM's default-deny-write and default-deny-all extensions
Radek Krejcia6f61e72021-03-24 21:00:19 +0100148 *
149 * Note that external plugins are supposed to use:
150 *
151 * LYPLG_EXTENSIONS = {
Radek Krejci0935f412019-08-20 16:15:18 +0200152 */
Radek Krejci3e6632f2021-03-22 22:08:21 +0100153const 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 Krejci3e6632f2021-03-22 22:08:21 +0100161 .plugin.sprinter = NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +0100162 .plugin.free = NULL,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200163 .plugin.snode = NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +0100164 .plugin.validate = NULL
Radek Krejci3e6632f2021-03-22 22:08:21 +0100165 }, {
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 Krejci3e6632f2021-03-22 22:08:21 +0100172 .plugin.sprinter = NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +0100173 .plugin.free = NULL,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200174 .plugin.snode = NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +0100175 .plugin.validate = NULL
Radek Krejci3e6632f2021-03-22 22:08:21 +0100176 }, {
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 Krejci3e6632f2021-03-22 22:08:21 +0100183 .plugin.sprinter = NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +0100184 .plugin.free = NULL,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200185 .plugin.snode = NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +0100186 .plugin.validate = NULL
Radek Krejci3e6632f2021-03-22 22:08:21 +0100187 }, {
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 Krejci3e6632f2021-03-22 22:08:21 +0100194 .plugin.sprinter = NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +0100195 .plugin.free = NULL,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200196 .plugin.snode = NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +0100197 .plugin.validate = NULL
Radek Krejci3e6632f2021-03-22 22:08:21 +0100198 },
199 {0} /* terminating zeroed item */
Radek Krejci0935f412019-08-20 16:15:18 +0200200};