2
* Copyright (c) 1996, 1998-2005, 2007-2011
3
* Todd C. Miller <Todd.Miller@courtesan.com>
5
* Permission to use, copy, modify, and distribute this software for any
6
* purpose with or without fee is hereby granted, provided that the above
7
* copyright notice and this permission notice appear in all copies.
9
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
17
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
* Sponsored in part by the Defense Advanced Research Projects
20
* Agency (DARPA) and Air Force Research Laboratory, Air Force
21
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
28
#include <sys/param.h>
29
#include <sys/types.h>
31
#include <sys/socket.h>
40
#endif /* STDC_HEADERS */
43
#endif /* HAVE_STRING_H */
46
#endif /* HAVE_STRINGS_H */
49
#endif /* HAVE_UNISTD_H */
52
#endif /* HAVE_FNMATCH */
53
#ifdef HAVE_NETGROUP_H
54
# include <netgroup.h>
55
#endif /* HAVE_NETGROUP_H */
58
#include <netinet/in.h>
59
#include <arpa/inet.h>
62
#include "tsgetgrpw.h"
64
#include "interfaces.h"
69
# include "compat/fnmatch.h"
70
#endif /* HAVE_FNMATCH */
75
int print_alias(void *, void *);
76
void dump_sudoers(void);
77
void print_defaults(void);
78
void print_privilege(struct privilege *);
79
void print_userspecs(void);
80
void usage(void) __attribute__((__noreturn__));
82
static void set_runaspw(const char *);
83
static void set_runasgr(const char *);
84
static int cb_runas_default(const char *);
85
static int testsudoers_printf(int msg_type, const char *fmt, ...);
86
static int testsudoers_print(const char *msg);
88
extern void setgrfile(const char *);
89
extern void setgrent(void);
90
extern void endgrent(void);
91
extern struct group *getgrent(void);
92
extern struct group *getgrnam(const char *);
93
extern struct group *getgrgid(gid_t);
94
extern void setpwfile(const char *);
95
extern void setpwent(void);
96
extern void endpwent(void);
97
extern struct passwd *getpwent(void);
98
extern struct passwd *getpwnam(const char *);
99
extern struct passwd *getpwuid(uid_t);
101
extern int (*trace_print)(const char *msg);
106
struct interface *interfaces;
107
struct sudo_user sudo_user;
108
struct passwd *list_pw;
109
static char *runas_group, *runas_user;
110
extern int parse_error;
111
sudo_printf_t sudo_printf = testsudoers_printf;
117
#if defined(SUDO_DEVEL) && defined(__OpenBSD__)
118
extern char *malloc_options;
125
main(int argc, char *argv[])
128
struct privilege *priv;
130
char *p, *grfile, *pwfile;
131
char hbuf[MAXHOSTNAMELEN + 1];
132
int match, host_match, runas_match, cmnd_match;
135
#if defined(SUDO_DEVEL) && defined(__OpenBSD__)
136
malloc_options = "AFGJPR";
142
#if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
143
setprogname(argc > 0 ? argv[0] : "testsudoers");
147
grfile = pwfile = NULL;
148
while ((ch = getopt(argc, argv, "dg:G:h:p:tu:")) != -1) {
160
runas_group = optarg;
166
trace_print = testsudoers_print;
179
/* Set group/passwd file and init the cache. */
191
user_cmnd = user_base = "true";
195
if ((p = strrchr(user_cmnd, '/')) != NULL)
198
user_base = user_cmnd;
201
if ((sudo_user.pw = sudo_getpwnam(user_name)) == NULL)
202
errorx(1, _("unknown user: %s"), user_name);
204
if (user_host == NULL) {
205
if (gethostname(hbuf, sizeof(hbuf)) != 0)
206
error(1, "gethostname");
207
hbuf[sizeof(hbuf) - 1] = '\0';
210
if ((p = strchr(user_host, '.'))) {
212
user_shost = estrdup(user_host);
215
user_shost = user_host;
218
/* Fill in user_args from argv. */
223
for (size = 0, from = argv + 1; *from; from++)
224
size += strlen(*from) + 1;
226
user_args = (char *) emalloc(size);
227
for (to = user_args, from = argv + 1; *from; from++) {
228
n = strlcpy(to, *from, size - (to - user_args));
229
if (n >= size - (to - user_args))
230
errorx(1, _("internal error, init_vars() overflow"));
237
/* Initialize default values. */
240
/* Set runas callback. */
241
sudo_defs_table[I_RUNAS_DEFAULT].callback = cb_runas_default;
243
/* Load ip addr/mask for each interface. */
244
if (get_net_ifs(&p) > 0)
247
/* Allocate space for data structures in the parser. */
248
init_parser("sudoers", 0);
250
if (yyparse() != 0 || parse_error) {
252
(void) fputs("Does not parse", stdout);
254
(void) fputs("Parses OK", stdout);
257
if (!update_defaults(SETDEF_ALL))
258
(void) fputs(" (problem with defaults entries)", stdout);
261
if (def_group_plugin && group_plugin_load(def_group_plugin) != TRUE)
262
def_group_plugin = NULL;
265
* Set runas passwd/group entries based on command line or sudoers.
266
* Note that if runas_group was specified without runas_user we
267
* defer setting runas_pw so the match routines know to ignore it.
269
if (runas_group != NULL) {
270
set_runasgr(runas_group);
271
if (runas_user != NULL)
272
set_runaspw(runas_user);
274
set_runaspw(runas_user ? runas_user : def_runas_default);
277
(void) putchar('\n');
280
exit(parse_error ? 1 : 0);
283
/* This loop must match the one in sudo_file_lookup() */
284
printf("\nEntries for user %s:\n", user_name);
286
tq_foreach_rev(&userspecs, us) {
287
if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
289
tq_foreach_rev(&us->privileges, priv) {
291
print_privilege(priv); /* XXX */
293
host_match = hostlist_matches(&priv->hostlist);
294
if (host_match == ALLOW) {
295
puts("\thost matched");
296
tq_foreach_rev(&priv->cmndlist, cs) {
297
runas_match = runaslist_matches(&cs->runasuserlist,
298
&cs->runasgrouplist);
299
if (runas_match == ALLOW) {
300
puts("\trunas matched");
301
cmnd_match = cmnd_matches(cs->cmnd);
302
if (cmnd_match != UNSPEC)
304
printf("\tcmnd %s\n", match == ALLOW ? "allowed" :
305
match == DENY ? "denied" : "unmatched");
309
puts(_("\thost unmatched"));
312
puts(match == ALLOW ? _("\nCommand allowed") :
313
match == DENY ? _("\nCommand denied") : _("\nCommand unmatched"));
317
* 0 - parsed OK and command matched.
319
* 2 - command not matched
324
exit(match == ALLOW ? 0 : match + 3);
328
set_runaspw(const char *user)
330
if (runas_pw != NULL)
333
if ((runas_pw = sudo_getpwuid(atoi(user + 1))) == NULL)
334
runas_pw = sudo_fakepwnam(user, runas_gr ? runas_gr->gr_gid : 0);
336
if ((runas_pw = sudo_getpwnam(user)) == NULL)
337
errorx(1, _("unknown user: %s"), user);
342
set_runasgr(const char *group)
344
if (runas_gr != NULL)
347
if ((runas_gr = sudo_getgrgid(atoi(group + 1))) == NULL)
348
runas_gr = sudo_fakegrnam(group);
350
if ((runas_gr = sudo_getgrnam(group)) == NULL)
351
errorx(1, _("unknown group: %s"), group);
356
* Callback for runas_default sudoers setting.
359
cb_runas_default(const char *user)
361
/* Only reset runaspw if user didn't specify one. */
362
if (!runas_user && !runas_group)
386
open_sudoers(const char *path, int isdir, int *keepopen)
388
return fopen(path, "r");
409
cleanup(int gotsignal)
418
print_member(struct member *m)
420
struct sudo_command *c;
425
fputs("ALL", stdout);
426
else if (m->type != COMMAND)
427
fputs(m->name, stdout);
429
c = (struct sudo_command *) m->name;
430
printf("%s%s%s", c->cmnd, c->args ? " " : "",
431
c->args ? c->args : "");
441
tq_foreach_fwd(&defaults, d) {
442
(void) fputs("Defaults", stdout);
457
tq_foreach_fwd(&d->binding, m) {
458
if (m != tq_first(&d->binding))
462
printf("\t%s%s", d->op == FALSE ? "!" : "", d->var);
463
if (d->val != NULL) {
464
printf("%c%s", d->op == TRUE ? '=' : d->op, d->val);
471
print_alias(void *v1, void *v2)
473
struct alias *a = (struct alias *)v1;
475
struct sudo_command *c;
479
(void) printf("Host_Alias\t%s = ", a->name);
482
(void) printf("Cmnd_Alias\t%s = ", a->name);
485
(void) printf("User_Alias\t%s = ", a->name);
488
(void) printf("Runas_Alias\t%s = ", a->name);
491
tq_foreach_fwd(&a->members, m) {
492
if (m != tq_first(&a->members))
494
if (m->type == COMMAND) {
495
c = (struct sudo_command *) m->name;
496
printf("%s%s%s", c->cmnd, c->args ? " " : "",
497
c->args ? c->args : "");
498
} else if (m->type == ALL) {
499
fputs("ALL", stdout);
501
fputs(m->name, stdout);
509
print_privilege(struct privilege *priv)
516
for (p = priv; p != NULL; p = p->next) {
518
fputs(" : ", stdout);
519
tq_foreach_fwd(&p->hostlist, m) {
520
if (m != tq_first(&p->hostlist))
524
fputs(" = ", stdout);
525
tags.nopasswd = tags.noexec = UNSPEC;
526
tq_foreach_fwd(&p->cmndlist, cs) {
527
if (cs != tq_first(&p->cmndlist))
529
if (!tq_empty(&cs->runasuserlist) || !tq_empty(&cs->runasgrouplist)) {
531
if (!tq_empty(&cs->runasuserlist)) {
532
tq_foreach_fwd(&cs->runasuserlist, m) {
533
if (m != tq_first(&cs->runasuserlist))
537
} else if (tq_empty(&cs->runasgrouplist)) {
538
fputs(def_runas_default, stdout);
540
fputs(sudo_user.pw->pw_name, stdout);
542
if (!tq_empty(&cs->runasgrouplist)) {
543
fputs(" : ", stdout);
544
tq_foreach_fwd(&cs->runasgrouplist, m) {
545
if (m != tq_first(&cs->runasgrouplist))
554
printf("ROLE=%s ", cs->role);
556
printf("TYPE=%s ", cs->type);
557
#endif /* HAVE_SELINUX */
558
if (cs->tags.nopasswd != UNSPEC && cs->tags.nopasswd != tags.nopasswd)
559
printf("%sPASSWD: ", cs->tags.nopasswd ? "NO" : "");
560
if (cs->tags.noexec != UNSPEC && cs->tags.noexec != tags.noexec)
561
printf("%sEXEC: ", cs->tags.noexec ? "NO" : "");
562
print_member(cs->cmnd);
563
memcpy(&tags, &cs->tags, sizeof(tags));
569
print_userspecs(void)
574
tq_foreach_fwd(&userspecs, us) {
575
tq_foreach_fwd(&us->users, m) {
576
if (m != tq_first(&us->users))
581
print_privilege(us->privileges.first); /* XXX */
587
testsudoers_printf(int msg_type, const char *fmt, ...)
593
case SUDO_CONV_INFO_MSG:
596
case SUDO_CONV_ERROR_MSG:
605
vfprintf(fp, fmt, ap);
617
alias_apply(print_alias, NULL);
623
static int testsudoers_print(const char *msg)
625
return fputs(msg, stderr);
631
(void) fprintf(stderr, "usage: %s [-dt] [-G grfile] [-g group] [-h host] [-p pwfile] [-u user] <user> <command> [args]\n", getprogname());