blob: afec924f823f6bb963dffad6b798556941bc4768 [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
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
Radek Krejci0935f412019-08-20 16:15:18 +020022/**
23 * @brief Storage for ID used to check plugin API version compatibility.
24 * Ignored here in the internal plugin.
25LYEXT_VERSION_CHECK
26 */
27
Michal Vaskof1ab44f2020-10-22 08:58:32 +020028struct 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 */
36static LY_ERR
37nacm_inherit_clb(struct lysc_node *node, void *data, ly_bool *dfs_continue)
38{
Radek Krejci859a15a2021-03-05 20:56:59 +010039 LY_ERR ret;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020040 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 Krejci859a15a2021-03-05 20:56:59 +010056 LY_ARRAY_NEW_GOTO(node->module->ctx, node->exts, inherited, ret, emem);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020057
58 inherited->def = lysc_ext_dup(arg->c_ext->def);
59 inherited->parent = node;
Radek Krejciab430862021-03-02 20:13:40 +010060 inherited->parent_stmt = lys_nodetype2stmt(node->nodetype);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020061 if (arg->c_ext->argument) {
Radek Krejci859a15a2021-03-05 20:56:59 +010062 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 Vaskof1ab44f2020-10-22 08:58:32 +020068 }
69 /* TODO duplicate extension instances */
70 inherited->data = arg->c_ext->data;
71 }
72
73 return LY_SUCCESS;
Radek Krejci859a15a2021-03-05 20:56:59 +010074
75emem:
76 lyext_log(arg->c_ext, LY_LLERR, LY_EMEM, NULL, "Memory allocation failed (%s()).", __func__);
77 return ret;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020078}
79
Radek Krejci0935f412019-08-20 16:15:18 +020080/**
81 * @brief Compile NAMC's extension instances.
82 *
83 * Implementation of lyext_clb_compile callback set as lyext_plugin::compile.
84 */
85LY_ERR
86nacm_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
87{
Radek Krejci859a15a2021-03-05 20:56:59 +010088 LY_ERR ret;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020089 struct lysc_node *parent = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +020090 LY_ARRAY_COUNT_TYPE u;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020091 struct nacm_dfs_arg dfs_arg;
Radek Krejci0935f412019-08-20 16:15:18 +020092
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 Vasko22df3f02020-08-24 13:29:22 +020098 c_ext->data = (void *)&nacm_deny_write;
Radek Krejci0935f412019-08-20 16:15:18 +020099 } else if (!strcmp(c_ext->def->name, "default-deny-all")) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200100 c_ext->data = (void *)&nacm_deny_all;
Radek Krejci0935f412019-08-20 16:15:18 +0200101 } else {
102 return LY_EINT;
103 }
104
105 /* check that the extension is instantiated at an allowed place - data node */
Radek Krejciab430862021-03-02 20:13:40 +0100106 if (!LY_STMT_IS_NODE(c_ext->parent_stmt)) {
Radek Krejci5f9a3672021-03-05 21:35:22 +0100107 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 Krejciab430862021-03-02 20:13:40 +0100109 p_ext->name, ly_stmt2str(c_ext->parent_stmt));
Radek Krejci0bfc6f92021-02-09 13:13:13 +0100110 return LY_ENOT;
Radek Krejci0935f412019-08-20 16:15:18 +0200111 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200112 parent = (struct lysc_node *)c_ext->parent;
Michal Vasko69730152020-10-09 16:30:07 +0200113 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 Krejci0935f412019-08-20 16:15:18 +0200115 /* 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 */
117invalid_parent:
Radek Krejci5f9a3672021-03-05 21:35:22 +0100118 lyext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
Michal Vasko69730152020-10-09 16:30:07 +0200119 "Extension %s is not allowed in %s statement.", p_ext->name, lys_nodetype2str(parent->nodetype));
Radek Krejci0bfc6f92021-02-09 13:13:13 +0100120 return LY_ENOT;
Radek Krejci0935f412019-08-20 16:15:18 +0200121 }
Michal Vasko69730152020-10-09 16:30:07 +0200122 if ((c_ext->data == (void *)&nacm_deny_write) && (parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
Radek Krejci0935f412019-08-20 16:15:18 +0200123 goto invalid_parent;
124 }
125 }
126
127 /* check for duplication */
128 LY_ARRAY_FOR(parent->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +0200129 if ((&parent->exts[u] != c_ext) && (parent->exts[u].def->plugin == c_ext->def->plugin)) {
Radek Krejci0935f412019-08-20 16:15:18 +0200130 /* duplication of a NACM extension on a single node
131 * We check plugin since we want to catch even the situation that there is default-deny-all
132 * AND default-deny-write */
133 if (parent->exts[u].def == c_ext->def) {
Radek Krejci5f9a3672021-03-05 21:35:22 +0100134 lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
135 "Extension %s is instantiated multiple times.", p_ext->name);
Radek Krejci0935f412019-08-20 16:15:18 +0200136 } else {
Radek Krejci5f9a3672021-03-05 21:35:22 +0100137 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 Krejci0935f412019-08-20 16:15:18 +0200139 }
140 return LY_EVALID;
141 }
142 }
143
144 /* inherit the extension instance to all the children nodes */
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200145 dfs_arg.c_ext = c_ext;
146 dfs_arg.parent = parent;
Radek Krejci859a15a2021-03-05 20:56:59 +0100147 ret = lysc_tree_dfs_full(parent, nacm_inherit_clb, &dfs_arg);
Radek Krejci0935f412019-08-20 16:15:18 +0200148
Radek Krejci859a15a2021-03-05 20:56:59 +0100149 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200150}
151
Radek Krejci0935f412019-08-20 16:15:18 +0200152/**
153 * @brief Plugin for the NACM's default-deny-write and default-deny-all extensions
154 */
155struct lyext_plugin nacm_plugin = {
156 .id = "libyang 2 - NACM, version 1",
157 .compile = &nacm_compile,
158 .validate = NULL,
Radek Krejciadcf63d2021-02-09 10:21:18 +0100159 .sprinter = NULL,
Radek Krejci0935f412019-08-20 16:15:18 +0200160 .free = NULL
161};