blob: 9cd1f3de5d32644adfa932449202c4e1df040c8e [file] [log] [blame]
Radek Krejci0935f412019-08-20 16:15:18 +02001/**
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 Krejci0935f412019-08-20 16:15:18 +020014
15#include <stdlib.h>
16
17#include "plugins_exts.h"
Radek Krejci5f9a3672021-03-05 21:35:22 +010018
19#include "dict.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010020#include "tree_edit.h"
Radek Krejci0935f412019-08-20 16:15:18 +020021#include "tree_schema.h"
22
23/**
24 * @brief Storage for ID used to check plugin API version compatibility.
25 * Ignored here in the internal plugin.
26LYEXT_VERSION_CHECK
27 */
28
Michal Vaskof1ab44f2020-10-22 08:58:32 +020029struct nacm_dfs_arg {
30 struct lysc_ext_instance *c_ext;
31 struct lysc_node *parent;
32};
33
34/**
35 * @brief DFS callback implementation for inheriting the NACM extension.
36 */
37static LY_ERR
38nacm_inherit_clb(struct lysc_node *node, void *data, ly_bool *dfs_continue)
39{
Radek Krejci859a15a2021-03-05 20:56:59 +010040 LY_ERR ret;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020041 struct nacm_dfs_arg *arg = data;
42 struct lysc_ext_instance *inherited;
43 LY_ARRAY_COUNT_TYPE u;
44
45 /* ignore the parent from which we inherit and input/output nodes */
46 if ((node != arg->parent) && !(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
47 /* check that the node does not have its own NACM extension instance */
48 LY_ARRAY_FOR(node->exts, u) {
49 if (node->exts[u].def == arg->c_ext->def) {
50 /* the child already have its own NACM flag, so skip the subtree */
51 *dfs_continue = 1;
52 return LY_SUCCESS;
53 }
54 }
55
56 /* duplicate this one to inherit it to the child */
Radek Krejci859a15a2021-03-05 20:56:59 +010057 LY_ARRAY_NEW_GOTO(node->module->ctx, node->exts, inherited, ret, emem);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020058
59 inherited->def = lysc_ext_dup(arg->c_ext->def);
60 inherited->parent = node;
Radek Krejciab430862021-03-02 20:13:40 +010061 inherited->parent_stmt = lys_nodetype2stmt(node->nodetype);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020062 if (arg->c_ext->argument) {
Radek Krejci859a15a2021-03-05 20:56:59 +010063 LY_ERR ret;
64
65 if ((ret = lydict_insert(node->module->ctx, arg->c_ext->argument, strlen(arg->c_ext->argument),
66 &inherited->argument))) {
67 return ret;
68 }
Michal Vaskof1ab44f2020-10-22 08:58:32 +020069 }
70 /* TODO duplicate extension instances */
71 inherited->data = arg->c_ext->data;
72 }
73
74 return LY_SUCCESS;
Radek Krejci859a15a2021-03-05 20:56:59 +010075
76emem:
77 lyext_log(arg->c_ext, LY_LLERR, LY_EMEM, NULL, "Memory allocation failed (%s()).", __func__);
78 return ret;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020079}
80
Radek Krejci0935f412019-08-20 16:15:18 +020081/**
82 * @brief Compile NAMC's extension instances.
83 *
84 * Implementation of lyext_clb_compile callback set as lyext_plugin::compile.
85 */
86LY_ERR
87nacm_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
88{
Radek Krejci859a15a2021-03-05 20:56:59 +010089 LY_ERR ret;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020090 struct lysc_node *parent = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +020091 LY_ARRAY_COUNT_TYPE u;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020092 struct nacm_dfs_arg dfs_arg;
Radek Krejci0935f412019-08-20 16:15:18 +020093
94 static const uint8_t nacm_deny_all = 1;
95 static const uint8_t nacm_deny_write = 2;
96
97 /* store the NACM flag */
98 if (!strcmp(c_ext->def->name, "default-deny-write")) {
Michal Vasko22df3f02020-08-24 13:29:22 +020099 c_ext->data = (void *)&nacm_deny_write;
Radek Krejci0935f412019-08-20 16:15:18 +0200100 } else if (!strcmp(c_ext->def->name, "default-deny-all")) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200101 c_ext->data = (void *)&nacm_deny_all;
Radek Krejci0935f412019-08-20 16:15:18 +0200102 } else {
103 return LY_EINT;
104 }
105
106 /* check that the extension is instantiated at an allowed place - data node */
Radek Krejciab430862021-03-02 20:13:40 +0100107 if (!LY_STMT_IS_NODE(c_ext->parent_stmt)) {
Radek Krejci5f9a3672021-03-05 21:35:22 +0100108 lyext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
109 "Extension %s is allowed only in a data nodes, but it is placed in \"%s\" statement.",
Radek Krejciab430862021-03-02 20:13:40 +0100110 p_ext->name, ly_stmt2str(c_ext->parent_stmt));
Radek Krejci0bfc6f92021-02-09 13:13:13 +0100111 return LY_ENOT;
Radek Krejci0935f412019-08-20 16:15:18 +0200112 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200113 parent = (struct lysc_node *)c_ext->parent;
Michal Vasko69730152020-10-09 16:30:07 +0200114 if (!(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_ANYDATA |
115 LYS_CASE | LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
Radek Krejci0935f412019-08-20 16:15:18 +0200116 /* note LYS_AUGMENT and LYS_USES is not in the list since they are not present in the compiled tree. Instead, libyang
117 * passes all their extensions to their children nodes */
118invalid_parent:
Radek Krejci5f9a3672021-03-05 21:35:22 +0100119 lyext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
Michal Vasko69730152020-10-09 16:30:07 +0200120 "Extension %s is not allowed in %s statement.", p_ext->name, lys_nodetype2str(parent->nodetype));
Radek Krejci0bfc6f92021-02-09 13:13:13 +0100121 return LY_ENOT;
Radek Krejci0935f412019-08-20 16:15:18 +0200122 }
Michal Vasko69730152020-10-09 16:30:07 +0200123 if ((c_ext->data == (void *)&nacm_deny_write) && (parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
Radek Krejci0935f412019-08-20 16:15:18 +0200124 goto invalid_parent;
125 }
126 }
127
128 /* check for duplication */
129 LY_ARRAY_FOR(parent->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +0200130 if ((&parent->exts[u] != c_ext) && (parent->exts[u].def->plugin == c_ext->def->plugin)) {
Radek Krejci0935f412019-08-20 16:15:18 +0200131 /* duplication of a NACM extension on a single node
132 * We check plugin since we want to catch even the situation that there is default-deny-all
133 * AND default-deny-write */
134 if (parent->exts[u].def == c_ext->def) {
Radek Krejci5f9a3672021-03-05 21:35:22 +0100135 lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
136 "Extension %s is instantiated multiple times.", p_ext->name);
Radek Krejci0935f412019-08-20 16:15:18 +0200137 } else {
Radek Krejci5f9a3672021-03-05 21:35:22 +0100138 lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
139 "Extension nacm:default-deny-write is mixed with nacm:default-deny-all.");
Radek Krejci0935f412019-08-20 16:15:18 +0200140 }
141 return LY_EVALID;
142 }
143 }
144
145 /* inherit the extension instance to all the children nodes */
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200146 dfs_arg.c_ext = c_ext;
147 dfs_arg.parent = parent;
Radek Krejci859a15a2021-03-05 20:56:59 +0100148 ret = lysc_tree_dfs_full(parent, nacm_inherit_clb, &dfs_arg);
Radek Krejci0935f412019-08-20 16:15:18 +0200149
Radek Krejci859a15a2021-03-05 20:56:59 +0100150 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200151}
152
Radek Krejci0935f412019-08-20 16:15:18 +0200153/**
154 * @brief Plugin for the NACM's default-deny-write and default-deny-all extensions
155 */
156struct lyext_plugin nacm_plugin = {
157 .id = "libyang 2 - NACM, version 1",
158 .compile = &nacm_compile,
159 .validate = NULL,
Radek Krejciadcf63d2021-02-09 10:21:18 +0100160 .sprinter = NULL,
Radek Krejci0935f412019-08-20 16:15:18 +0200161 .free = NULL
162};