blob: 5be0619dec444fcf89cbdfdb5d0ad2d0afabe561 [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 */
14#include "common.h"
15
16#include <stdlib.h>
17
18#include "plugins_exts.h"
19#include "tree_schema.h"
20
21/**
22 * @brief Storage for ID used to check plugin API version compatibility.
23 * Ignored here in the internal plugin.
24LYEXT_VERSION_CHECK
25 */
26
27/**
28 * @brief Compile NAMC's extension instances.
29 *
30 * Implementation of lyext_clb_compile callback set as lyext_plugin::compile.
31 */
32LY_ERR
33nacm_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
34{
35 struct lysc_node *parent = NULL, *iter;
36 struct lysc_ext_instance *inherited;
Michal Vaskofd69e1d2020-07-03 11:57:17 +020037 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0935f412019-08-20 16:15:18 +020038
39 static const uint8_t nacm_deny_all = 1;
40 static const uint8_t nacm_deny_write = 2;
41
42 /* store the NACM flag */
43 if (!strcmp(c_ext->def->name, "default-deny-write")) {
Michal Vasko22df3f02020-08-24 13:29:22 +020044 c_ext->data = (void *)&nacm_deny_write;
Radek Krejci0935f412019-08-20 16:15:18 +020045 } else if (!strcmp(c_ext->def->name, "default-deny-all")) {
Michal Vasko22df3f02020-08-24 13:29:22 +020046 c_ext->data = (void *)&nacm_deny_all;
Radek Krejci0935f412019-08-20 16:15:18 +020047 } else {
48 return LY_EINT;
49 }
50
51 /* check that the extension is instantiated at an allowed place - data node */
52 if (c_ext->parent_type != LYEXT_PAR_NODE) {
53 lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is allowed only in a data nodes, but it is placed in \"%s\" statement.",
54 p_ext->name, lyext_parent2str(c_ext->parent_type));
55 return LY_EVALID;
56 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +020057 parent = (struct lysc_node *)c_ext->parent;
Michal Vasko1bf09392020-03-27 12:38:10 +010058 if (!(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_ANYDATA
59 | LYS_CASE | LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
Radek Krejci0935f412019-08-20 16:15:18 +020060 /* note LYS_AUGMENT and LYS_USES is not in the list since they are not present in the compiled tree. Instead, libyang
61 * passes all their extensions to their children nodes */
62invalid_parent:
63 lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path,
64 "Extension %s is not allowed in %s statement.", p_ext->name, lys_nodetype2str(parent->nodetype));
65 return LY_EVALID;
66 }
Michal Vasko22df3f02020-08-24 13:29:22 +020067 if (c_ext->data == (void *)&nacm_deny_write && (parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
Radek Krejci0935f412019-08-20 16:15:18 +020068 goto invalid_parent;
69 }
70 }
71
72 /* check for duplication */
73 LY_ARRAY_FOR(parent->exts, u) {
74 if (&parent->exts[u] != c_ext && parent->exts[u].def->plugin == c_ext->def->plugin) {
75 /* duplication of a NACM extension on a single node
76 * We check plugin since we want to catch even the situation that there is default-deny-all
77 * AND default-deny-write */
78 if (parent->exts[u].def == c_ext->def) {
79 lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name);
80 } else {
81 lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension nacm:default-deny-write is mixed with nacm:default-deny-all.");
82 }
83 return LY_EVALID;
84 }
85 }
86
87 /* inherit the extension instance to all the children nodes */
88 LYSC_TREE_DFS_BEGIN(parent, iter) {
89 if (iter != parent) { /* ignore the parent from which we inherit */
90 /* check that the node does not have its own NACM extension instance */
91 LY_ARRAY_FOR(iter->exts, u) {
92 if (iter->exts[u].def == c_ext->def) {
93 /* the child already have its own NACM flag, so skip the subtree */
94 LYSC_TREE_DFS_continue = 1;
95 break;
96 }
97 }
98 if (!LYSC_TREE_DFS_continue) {
99 /* duplicate this one to inherit it to the child */
100 LY_ARRAY_NEW_RET(cctx->ctx, iter->exts, inherited, LY_EMEM);
101
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100102 inherited->def = lysc_ext_dup(c_ext->def);
Radek Krejci0935f412019-08-20 16:15:18 +0200103 inherited->parent = iter;
104 inherited->parent_type = LYEXT_PAR_NODE;
105 if (c_ext->argument) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200106 LY_CHECK_RET(lydict_insert(cctx->ctx, c_ext->argument, strlen(c_ext->argument), &inherited->argument));
Radek Krejci0935f412019-08-20 16:15:18 +0200107 }
108 /* TODO duplicate extension instances */
109 inherited->data = c_ext->data;
110 }
111 }
112 LYSC_TREE_DFS_END(parent, iter)
113 }
114
115 return LY_SUCCESS;
116}
117
Radek Krejci0935f412019-08-20 16:15:18 +0200118/**
119 * @brief Plugin for the NACM's default-deny-write and default-deny-all extensions
120 */
121struct lyext_plugin nacm_plugin = {
122 .id = "libyang 2 - NACM, version 1",
123 .compile = &nacm_compile,
124 .validate = NULL,
125 .free = NULL
126};