summaryrefslogtreecommitdiffstats
path: root/plugins/sudoers/check_aliases.c
blob: 1b3d030098a5c2c37e032449d69beeb772c40f95 (plain)
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
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 2004-2005, 2007-2018, 2021-2022
 *	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
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "sudoers.h"
#include <gram.h>

struct alias_warned {
    SLIST_ENTRY(alias_warned) entries;
    const char *name;
};
SLIST_HEAD(alias_warned_list, alias_warned);

static bool alias_warnx(const char *file, int line, int column, bool strict, bool quiet, const char *fmt, ...) sudo_printflike(6, 7);

static bool
alias_warned(struct alias_warned_list *warned, char *name)
{
    struct alias_warned *w;
    debug_decl(alias_warned, SUDOERS_DEBUG_ALIAS);

    SLIST_FOREACH(w, warned, entries) {
	if (strcmp(w->name, name) == 0)
	    debug_return_bool(true);
    }

    debug_return_bool(false);
}

static void
alias_warned_add(struct alias_warned_list *warned, char *name)
{
    struct alias_warned *w;
    debug_decl(alias_warned_add, SUDOERS_DEBUG_ALIAS);

    w = malloc(sizeof(*w));
    if (w != NULL) {
	w->name = name;
	SLIST_INSERT_HEAD(warned, w, entries);
    }

    debug_return;
}

static bool
alias_warnx(const char *file, int line, int column, bool strict, bool quiet,
    const char *fmt, ...)
{
    bool ret = true;
    va_list ap;
    debug_decl(alias_warnx, SUDOERS_DEBUG_ALIAS);

    if (strict && sudoers_error_hook != NULL) {
	va_start(ap, fmt);
	ret = sudoers_error_hook(file, line, column, fmt, ap);
	va_end(ap);
    }

    if (!quiet) {
	int oldlocale;
	char *errstr;

	sudoers_setlocale(SUDOERS_LOCALE_USER, &oldlocale);
	va_start(ap, fmt);
	if (vasprintf(&errstr, _(fmt), ap) == -1) {
	    errstr = NULL;
	    ret = false;
	} else if (line > 0) {
	    sudo_printf(SUDO_CONV_ERROR_MSG, _("%s:%d:%d: %s\n"), file,
		line, column, errstr);
	} else {
	    sudo_printf(SUDO_CONV_ERROR_MSG, _("%s: %s\n"), file, errstr);
	}
	va_end(ap);
	sudoers_setlocale(oldlocale, NULL);

	free(errstr);
    }

    debug_return_bool(ret);
}

static int
check_alias(struct sudoers_parse_tree *parse_tree,
    struct alias_warned_list *warned, char *name, int type,
    char *file, int line, int column, bool strict, bool quiet)
{
    struct member *m;
    struct alias *a;
    int errors = 0;
    debug_decl(check_alias, SUDOERS_DEBUG_ALIAS);

    if ((a = alias_get(parse_tree, name, type)) != NULL) {
	/* check alias contents */
	TAILQ_FOREACH(m, &a->members, entries) {
	    if (m->type != ALIAS)
		continue;
	    errors += check_alias(parse_tree, warned, m->name, type,
		a->file, a->line, a->column, strict, quiet);
	}
	alias_put(a);
    } else {
	if (!alias_warned(warned, name)) {
	    if (errno == ELOOP) {
		alias_warnx(file, line, column, strict, quiet,
		    N_("cycle in %s \"%s\""), alias_type_to_string(type), name);
	    } else {
		alias_warnx(file, line, column, strict, quiet,
		    N_("%s \"%s\" referenced but not defined"),
		    alias_type_to_string(type), name);
	    }
	    alias_warned_add(warned, name);
	}
	errors++;
    }

    debug_return_int(errors);
}

/*
 * Iterate through the sudoers datastructures looking for undefined
 * aliases or unused aliases.
 * In strict mode, returns the number of errors, else 0.
 */
int
check_aliases(struct sudoers_parse_tree *parse_tree, bool strict, bool quiet,
    int (*cb_unused)(struct sudoers_parse_tree *, struct alias *, void *))
{
    struct alias_warned_list warned = SLIST_HEAD_INITIALIZER(warned);
    struct rbtree *used_aliases;
    struct alias_warned *w;
    struct cmndspec *cs;
    struct member *m;
    struct privilege *priv;
    struct userspec *us;
    int errors = 0;
    debug_decl(check_aliases, SUDOERS_DEBUG_ALIAS);

    used_aliases = alloc_aliases();
    if (used_aliases == NULL) {
	sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
	debug_return_int(-1);
    }

    /* Forward check. */
    TAILQ_FOREACH(us, &parse_tree->userspecs, entries) {
	TAILQ_FOREACH(m, &us->users, entries) {
	    if (m->type == ALIAS) {
		errors += check_alias(parse_tree, &warned, m->name, USERALIAS,
		    us->file, us->line, us->column, strict, quiet);
	    }
	}
	TAILQ_FOREACH(priv, &us->privileges, entries) {
	    TAILQ_FOREACH(m, &priv->hostlist, entries) {
		if (m->type == ALIAS) {
		    errors += check_alias(parse_tree, &warned, m->name, HOSTALIAS,
			us->file, us->line, us->column, strict, quiet);
		}
	    }
	    TAILQ_FOREACH(cs, &priv->cmndlist, entries) {
		if (cs->runasuserlist != NULL) {
		    TAILQ_FOREACH(m, cs->runasuserlist, entries) {
			if (m->type == ALIAS) {
			    errors += check_alias(parse_tree, &warned, m->name, RUNASALIAS,
				us->file, us->line, us->column, strict, quiet);
			}
		    }
		}
		if (cs->runasgrouplist != NULL) {
		    TAILQ_FOREACH(m, cs->runasgrouplist, entries) {
			if (m->type == ALIAS) {
			    errors += check_alias(parse_tree, &warned, m->name, RUNASALIAS,
				us->file, us->line, us->column, strict, quiet);
			}
		    }
		}
		if ((m = cs->cmnd)->type == ALIAS) {
		    errors += check_alias(parse_tree, &warned, m->name, CMNDALIAS,
			us->file, us->line, us->column, strict, quiet);
		}
	    }
	}
    }
    while ((w = SLIST_FIRST(&warned)) != NULL) {
	SLIST_REMOVE_HEAD(&warned, entries);
	free(w);
    }

    /* Reverse check (destructive) */
    if (!alias_find_used(parse_tree, used_aliases))
	errors++;
    free_aliases(used_aliases);

    /* If all aliases were referenced we will have an empty tree. */
    if (!no_aliases(parse_tree))
	alias_apply(parse_tree, cb_unused, &quiet);

    debug_return_int(strict ? errors : 0);
}