1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2004-2005, 2007-2021, 2023
* Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*/
/*
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sudoers.h>
#include <redblack.h>
#include <gram.h>
/*
* Comparison function for the red-black tree.
* Aliases are sorted by name with the type used as a tie-breaker.
*/
static int
alias_compare(const void *v1, const void *v2)
{
const struct alias *a1 = (const struct alias *)v1;
const struct alias *a2 = (const struct alias *)v2;
int res;
debug_decl(alias_compare, SUDOERS_DEBUG_ALIAS);
if (a1 == NULL)
res = -1;
else if (a2 == NULL)
res = 1;
else if ((res = strcmp(a1->name, a2->name)) == 0)
res = a1->type - a2->type;
debug_return_int(res);
}
/*
* Search the tree for an alias with the specified name and type.
* Returns a pointer to the alias structure or NULL if not found.
* Caller is responsible for calling alias_put() on the returned
* alias to mark it as unused.
*/
struct alias *
alias_get(const struct sudoers_parse_tree *parse_tree, const char *name,
short type)
{
struct alias key;
struct rbnode *node;
struct alias *a = NULL;
debug_decl(alias_get, SUDOERS_DEBUG_ALIAS);
if (parse_tree->aliases == NULL)
debug_return_ptr(NULL);
key.name = (char *)name;
key.type = type;
if ((node = rbfind(parse_tree->aliases, &key)) != NULL) {
/*
* Check whether this alias is already in use.
* If so, we've detected a loop. If not, set the flag,
* which the caller should clear with a call to alias_put().
*/
a = node->data;
if (a->used) {
errno = ELOOP;
debug_return_ptr(NULL);
}
a->used = true;
} else {
errno = ENOENT;
}
debug_return_ptr(a);
}
/*
* Clear the "used" flag in an alias once the caller is done with it.
*/
void
alias_put(struct alias *a)
{
debug_decl(alias_put, SUDOERS_DEBUG_ALIAS);
a->used = false;
debug_return;
}
/*
* Add an alias to the aliases redblack tree.
* Note that "file" must be a reference-counted string.
* Returns true on success and false on failure, setting errno.
*/
bool
alias_add(struct sudoers_parse_tree *parse_tree, char *name,
short type, char *file, int line, int column,
struct member *members)
{
struct alias *a;
debug_decl(alias_add, SUDOERS_DEBUG_ALIAS);
if (parse_tree->aliases == NULL) {
if ((parse_tree->aliases = alloc_aliases()) == NULL)
debug_return_bool(false);
}
a = calloc(1, sizeof(*a));
if (a == NULL)
debug_return_bool(false);
/* Only set elements used by alias_compare() in case there is a dupe. */
a->name = name;
a->type = type;
switch (rbinsert(parse_tree->aliases, a, NULL)) {
case 1:
free(a);
errno = EEXIST;
debug_return_bool(false);
case -1:
free(a);
debug_return_bool(false);
}
/*
* It is now safe to fill in the rest of the alias. We do this last
* since it modifies "file" (adds a ref) and "members" (tailq conversion).
*/
/* a->used = false; */
a->file = sudo_rcstr_addref(file);
a->line = line;
a->column = column;
HLTQ_TO_TAILQ(&a->members, members, entries);
debug_return_bool(true);
}
/*
* Closure to adapt 2-arg rbapply() to 3-arg alias_apply().
*/
struct alias_apply_closure {
struct sudoers_parse_tree *parse_tree;
int (*func)(struct sudoers_parse_tree *, struct alias *, void *);
void *cookie;
};
/* Adapt rbapply() to alias_apply() calling convention. */
static int
alias_apply_func(void *v1, void *v2)
{
struct alias *a = v1;
struct alias_apply_closure *closure = v2;
return closure->func(closure->parse_tree, a, closure->cookie);
}
/*
* Apply a function to each alias entry and pass in a cookie.
*/
bool
alias_apply(struct sudoers_parse_tree *parse_tree,
int (*func)(struct sudoers_parse_tree *, struct alias *, void *),
void *cookie)
{
struct alias_apply_closure closure;
bool ret = true;
debug_decl(alias_apply, SUDOERS_DEBUG_ALIAS);
if (parse_tree->aliases != NULL) {
closure.parse_tree = parse_tree;
closure.func = func;
closure.cookie = cookie;
if (rbapply(parse_tree->aliases, alias_apply_func, &closure, inorder) != 0)
ret = false;
}
debug_return_bool(ret);
}
/*
* Returns true if there are no aliases in the parse_tree, else false.
*/
bool
no_aliases(const struct sudoers_parse_tree *parse_tree)
{
debug_decl(no_aliases, SUDOERS_DEBUG_ALIAS);
debug_return_bool(parse_tree->aliases == NULL ||
rbisempty(parse_tree->aliases));
}
/*
* Free memory used by an alias struct and its members.
*/
void
alias_free(void *v)
{
struct alias *a = (struct alias *)v;
debug_decl(alias_free, SUDOERS_DEBUG_ALIAS);
if (a != NULL) {
free(a->name);
sudo_rcstr_delref(a->file);
free_members(&a->members);
free(a);
}
debug_return;
}
/*
* Find the named alias, remove it from the tree and return it.
*/
struct alias *
alias_remove(struct sudoers_parse_tree *parse_tree, const char *name,
short type)
{
struct rbnode *node;
struct alias key;
debug_decl(alias_remove, SUDOERS_DEBUG_ALIAS);
if (parse_tree->aliases != NULL) {
key.name = (char *)name;
key.type = type;
if ((node = rbfind(parse_tree->aliases, &key)) != NULL)
debug_return_ptr(rbdelete(parse_tree->aliases, node));
}
errno = ENOENT;
debug_return_ptr(NULL);
}
struct rbtree *
alloc_aliases(void)
{
debug_decl(alloc_aliases, SUDOERS_DEBUG_ALIAS);
debug_return_ptr(rbcreate(alias_compare));
}
void
free_aliases(struct rbtree *aliases)
{
debug_decl(free_aliases, SUDOERS_DEBUG_ALIAS);
if (aliases != NULL)
rbdestroy(aliases, alias_free);
}
const char *
alias_type_to_string(short alias_type)
{
return alias_type == HOSTALIAS ? "Host_Alias" :
alias_type == CMNDALIAS ? "Cmnd_Alias" :
alias_type == USERALIAS ? "User_Alias" :
alias_type == RUNASALIAS ? "Runas_Alias" :
"Invalid_Alias";
}
/*
* Remove the alias of the specified type as well as any other aliases
* referenced by that alias. Stores removed aliases in a freelist.
*/
static bool
alias_remove_recursive(struct sudoers_parse_tree *parse_tree, char *name,
short type, struct rbtree *freelist)
{
struct member *m;
struct alias *a;
bool ret = true;
debug_decl(alias_remove_recursive, SUDOERS_DEBUG_ALIAS);
if ((a = alias_remove(parse_tree, name, type)) != NULL) {
TAILQ_FOREACH(m, &a->members, entries) {
if (m->type == ALIAS) {
if (!alias_remove_recursive(parse_tree, m->name, type, freelist))
ret = false;
}
}
if (rbinsert(freelist, a, NULL) != 0)
ret = false;
}
debug_return_bool(ret);
}
static int
alias_find_used_members(struct sudoers_parse_tree *parse_tree,
struct member_list *members, short atype, struct rbtree *used_aliases)
{
struct member *m;
int errors = 0;
debug_decl(alias_find_used_members, SUDOERS_DEBUG_ALIAS);
if (members != NULL) {
TAILQ_FOREACH(m, members, entries) {
if (m->type != ALIAS)
continue;
if (!alias_remove_recursive(parse_tree, m->name, atype, used_aliases))
errors++;
}
}
debug_return_int(errors);
}
/*
* Move all aliases referenced by userspecs to used_aliases.
*/
bool
alias_find_used(struct sudoers_parse_tree *parse_tree, struct rbtree *used_aliases)
{
struct privilege *priv;
struct userspec *us;
struct cmndspec *cs;
struct defaults *d;
struct member *m;
int errors = 0;
debug_decl(alias_find_used, SUDOERS_DEBUG_ALIAS);
/* Move referenced aliases to used_aliases. */
TAILQ_FOREACH(us, &parse_tree->userspecs, entries) {
errors += alias_find_used_members(parse_tree, &us->users,
USERALIAS, used_aliases);
TAILQ_FOREACH(priv, &us->privileges, entries) {
errors += alias_find_used_members(parse_tree, &priv->hostlist,
HOSTALIAS, used_aliases);
TAILQ_FOREACH(cs, &priv->cmndlist, entries) {
errors += alias_find_used_members(parse_tree, cs->runasuserlist,
RUNASALIAS, used_aliases);
errors += alias_find_used_members(parse_tree, cs->runasgrouplist,
RUNASALIAS, used_aliases);
if ((m = cs->cmnd)->type == ALIAS) {
if (!alias_remove_recursive(parse_tree, m->name, CMNDALIAS,
used_aliases))
errors++;
}
}
}
}
TAILQ_FOREACH(d, &parse_tree->defaults, entries) {
switch (d->type) {
case DEFAULTS_HOST:
errors += alias_find_used_members(parse_tree,
&d->binding->members, HOSTALIAS, used_aliases);
break;
case DEFAULTS_USER:
errors += alias_find_used_members(parse_tree,
&d->binding->members, USERALIAS, used_aliases);
break;
case DEFAULTS_RUNAS:
errors += alias_find_used_members(parse_tree,
&d->binding->members, RUNASALIAS, used_aliases);
break;
case DEFAULTS_CMND:
errors += alias_find_used_members(parse_tree,
&d->binding->members, CMNDALIAS, used_aliases);
break;
default:
break;
}
}
debug_return_bool(errors ? false : true);
}
|