/src/sudo/plugins/sudoers/check_aliases.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2004-2005, 2007-2018, 2021-2023 |
5 | | * Todd C. Miller <Todd.Miller@sudo.ws> |
6 | | * |
7 | | * Permission to use, copy, modify, and distribute this software for any |
8 | | * purpose with or without fee is hereby granted, provided that the above |
9 | | * copyright notice and this permission notice appear in all copies. |
10 | | * |
11 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
12 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
13 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
14 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
15 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
16 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
17 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
18 | | */ |
19 | | |
20 | | #include <config.h> |
21 | | |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | #include <errno.h> |
26 | | |
27 | | #include <sudoers.h> |
28 | | #include <gram.h> |
29 | | |
30 | | struct alias_warned { |
31 | | SLIST_ENTRY(alias_warned) entries; |
32 | | const char *name; |
33 | | }; |
34 | | SLIST_HEAD(alias_warned_list, alias_warned); |
35 | | |
36 | | static bool |
37 | | alias_warned(struct alias_warned_list *warned, char *name) |
38 | 0 | { |
39 | 0 | struct alias_warned *w; |
40 | 0 | debug_decl(alias_warned, SUDOERS_DEBUG_ALIAS); |
41 | |
|
42 | 0 | SLIST_FOREACH(w, warned, entries) { |
43 | 0 | if (strcmp(w->name, name) == 0) |
44 | 0 | debug_return_bool(true); |
45 | 0 | } |
46 | | |
47 | 0 | debug_return_bool(false); |
48 | 0 | } |
49 | | |
50 | | static void |
51 | | alias_warned_add(struct alias_warned_list *warned, char *name) |
52 | 0 | { |
53 | 0 | struct alias_warned *w; |
54 | 0 | debug_decl(alias_warned_add, SUDOERS_DEBUG_ALIAS); |
55 | |
|
56 | 0 | w = malloc(sizeof(*w)); |
57 | 0 | if (w != NULL) { |
58 | 0 | w->name = name; |
59 | 0 | SLIST_INSERT_HEAD(warned, w, entries); |
60 | 0 | } |
61 | |
|
62 | 0 | debug_return; |
63 | 0 | } |
64 | | |
65 | | static int |
66 | | check_alias(struct sudoers_parse_tree *parse_tree, |
67 | | struct alias_warned_list *warned, char *name, short type, |
68 | | char *file, int line, int column, bool strict, bool quiet) |
69 | 0 | { |
70 | 0 | struct member *m; |
71 | 0 | struct alias *a; |
72 | 0 | int errors = 0; |
73 | 0 | debug_decl(check_alias, SUDOERS_DEBUG_ALIAS); |
74 | |
|
75 | 0 | if ((a = alias_get(parse_tree, name, type)) != NULL) { |
76 | | /* check alias contents */ |
77 | 0 | TAILQ_FOREACH(m, &a->members, entries) { |
78 | 0 | if (m->type != ALIAS) |
79 | 0 | continue; |
80 | 0 | errors += check_alias(parse_tree, warned, m->name, type, |
81 | 0 | a->file, a->line, a->column, strict, quiet); |
82 | 0 | } |
83 | 0 | alias_put(a); |
84 | 0 | } else { |
85 | 0 | if (!alias_warned(warned, name)) { |
86 | 0 | if (errno == ELOOP) { |
87 | 0 | parser_warnx(parse_tree->ctx, file, line, column, strict, quiet, |
88 | 0 | N_("cycle in %s \"%s\""), alias_type_to_string(type), name); |
89 | 0 | } else { |
90 | 0 | parser_warnx(parse_tree->ctx, file, line, column, strict, quiet, |
91 | 0 | N_("%s \"%s\" referenced but not defined"), |
92 | 0 | alias_type_to_string(type), name); |
93 | 0 | } |
94 | 0 | alias_warned_add(warned, name); |
95 | 0 | } |
96 | 0 | errors++; |
97 | 0 | } |
98 | |
|
99 | 0 | debug_return_int(errors); |
100 | 0 | } |
101 | | |
102 | | /* |
103 | | * Iterate through the sudoers datastructures looking for undefined |
104 | | * aliases or unused aliases. |
105 | | * In strict mode, returns the number of errors, else 0. |
106 | | */ |
107 | | int |
108 | | check_aliases(struct sudoers_parse_tree *parse_tree, bool strict, bool quiet, |
109 | | int (*cb_unused)(struct sudoers_parse_tree *, struct alias *, void *)) |
110 | 2 | { |
111 | 2 | struct alias_warned_list warned = SLIST_HEAD_INITIALIZER(warned); |
112 | 2 | struct rbtree *used_aliases; |
113 | 2 | struct alias_warned *w; |
114 | 2 | struct cmndspec *cs; |
115 | 2 | struct member *m; |
116 | 2 | struct privilege *priv; |
117 | 2 | struct userspec *us; |
118 | 2 | int errors = 0; |
119 | 2 | debug_decl(check_aliases, SUDOERS_DEBUG_ALIAS); |
120 | | |
121 | 2 | used_aliases = alloc_aliases(); |
122 | 2 | if (used_aliases == NULL) { |
123 | 0 | sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); |
124 | 0 | debug_return_int(-1); |
125 | 0 | } |
126 | | |
127 | | /* Forward check. */ |
128 | 2 | TAILQ_FOREACH(us, &parse_tree->userspecs, entries) { |
129 | 0 | TAILQ_FOREACH(m, &us->users, entries) { |
130 | 0 | if (m->type == ALIAS) { |
131 | 0 | errors += check_alias(parse_tree, &warned, m->name, USERALIAS, |
132 | 0 | us->file, us->line, us->column, strict, quiet); |
133 | 0 | } |
134 | 0 | } |
135 | 0 | TAILQ_FOREACH(priv, &us->privileges, entries) { |
136 | 0 | TAILQ_FOREACH(m, &priv->hostlist, entries) { |
137 | 0 | if (m->type == ALIAS) { |
138 | 0 | errors += check_alias(parse_tree, &warned, m->name, HOSTALIAS, |
139 | 0 | us->file, us->line, us->column, strict, quiet); |
140 | 0 | } |
141 | 0 | } |
142 | 0 | TAILQ_FOREACH(cs, &priv->cmndlist, entries) { |
143 | 0 | if (cs->runasuserlist != NULL) { |
144 | 0 | TAILQ_FOREACH(m, cs->runasuserlist, entries) { |
145 | 0 | if (m->type == ALIAS) { |
146 | 0 | errors += check_alias(parse_tree, &warned, m->name, RUNASALIAS, |
147 | 0 | us->file, us->line, us->column, strict, quiet); |
148 | 0 | } |
149 | 0 | } |
150 | 0 | } |
151 | 0 | if (cs->runasgrouplist != NULL) { |
152 | 0 | TAILQ_FOREACH(m, cs->runasgrouplist, entries) { |
153 | 0 | if (m->type == ALIAS) { |
154 | 0 | errors += check_alias(parse_tree, &warned, m->name, RUNASALIAS, |
155 | 0 | us->file, us->line, us->column, strict, quiet); |
156 | 0 | } |
157 | 0 | } |
158 | 0 | } |
159 | 0 | if ((m = cs->cmnd)->type == ALIAS) { |
160 | 0 | errors += check_alias(parse_tree, &warned, m->name, CMNDALIAS, |
161 | 0 | us->file, us->line, us->column, strict, quiet); |
162 | 0 | } |
163 | 0 | } |
164 | 0 | } |
165 | 0 | } |
166 | 2 | while ((w = SLIST_FIRST(&warned)) != NULL) { |
167 | 0 | SLIST_REMOVE_HEAD(&warned, entries); |
168 | 0 | free(w); |
169 | 0 | } |
170 | | |
171 | | /* Reverse check (destructive) */ |
172 | 2 | if (!alias_find_used(parse_tree, used_aliases)) |
173 | 0 | errors++; |
174 | 2 | free_aliases(used_aliases); |
175 | | |
176 | | /* If all aliases were referenced we will have an empty tree. */ |
177 | 2 | if (!no_aliases(parse_tree)) { |
178 | 0 | if (!alias_apply(parse_tree, cb_unused, &quiet)) |
179 | 0 | errors++; |
180 | 0 | } |
181 | | |
182 | 2 | debug_return_int(strict ? errors : 0); |
183 | 2 | } |