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__));
81 void set_runasgr(char *);
82 void set_runaspw(char *);
84 static int testsudoers_printf(int msg_type, const char *fmt, ...);
85 static int testsudoers_print(const char *msg);
87 extern void setgrfile(const char *);
88 extern void setgrent(void);
89 extern void endgrent(void);
90 extern struct group *getgrent(void);
91 extern struct group *getgrnam(const char *);
92 extern struct group *getgrgid(gid_t);
93 extern void setpwfile(const char *);
94 extern void setpwent(void);
95 extern void endpwent(void);
96 extern struct passwd *getpwent(void);
97 extern struct passwd *getpwnam(const char *);
98 extern struct passwd *getpwuid(uid_t);
100 extern int (*trace_print)(const char *msg);
105 struct interface *interfaces;
106 struct sudo_user sudo_user;
107 struct passwd *list_pw;
108 extern int parse_error;
109 sudo_printf_t sudo_printf = testsudoers_printf;
115 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
116 extern char *malloc_options;
123 main(int argc, char *argv[])
126 struct privilege *priv;
128 char *p, *grfile, *pwfile, *runas_group, *runas_user;
129 char hbuf[MAXHOSTNAMELEN + 1];
130 int match, host_match, runas_match, cmnd_match;
133 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
134 malloc_options = "AFGJPR";
140 #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
141 setprogname(argc > 0 ? argv[0] : "testsudoers");
145 grfile = pwfile = runas_group = runas_user = NULL;
146 while ((ch = getopt(argc, argv, "dg:G:h:p:tu:")) != -1) {
158 runas_group = optarg;
164 trace_print = testsudoers_print;
177 /* Set group/passwd file and init the cache. */
189 user_cmnd = user_base = "true";
193 if ((p = strrchr(user_cmnd, '/')) != NULL)
196 user_base = user_cmnd;
199 if ((sudo_user.pw = sudo_getpwnam(user_name)) == NULL)
200 errorx(1, "no passwd entry for %s!", user_name);
202 if (user_host == NULL) {
203 if (gethostname(hbuf, sizeof(hbuf)) != 0)
204 error(1, "gethostname");
205 hbuf[sizeof(hbuf) - 1] = '\0';
208 if ((p = strchr(user_host, '.'))) {
210 user_shost = estrdup(user_host);
213 user_shost = user_host;
216 /* Fill in user_args from argv. */
221 for (size = 0, from = argv + 1; *from; from++)
222 size += strlen(*from) + 1;
224 user_args = (char *) emalloc(size);
225 for (to = user_args, from = argv + 1; *from; from++) {
226 n = strlcpy(to, *from, size - (to - user_args));
227 if (n >= size - (to - user_args))
228 errorx(1, "internal error, init_vars() overflow");
235 /* Initialize default values. */
238 /* Load ip addr/mask for each interface. */
239 if (get_net_ifs(&p) > 0)
242 /* Allocate space for data structures in the parser. */
243 init_parser("sudoers", 0);
245 if (yyparse() != 0 || parse_error) {
247 (void) fputs("Does not parse", stdout);
249 (void) fputs("Parses OK", stdout);
252 if (!update_defaults(SETDEF_ALL))
253 (void) fputs(" (problem with defaults entries)", stdout);
256 if (def_group_plugin && group_plugin_load(def_group_plugin) != TRUE)
257 def_group_plugin = NULL;
260 * Set runas passwd/group entries based on command line or sudoers.
261 * Note that if runas_group was specified without runas_user we
262 * defer setting runas_pw so the match routines know to ignore it.
264 if (runas_group != NULL) {
265 set_runasgr(runas_group);
266 if (runas_user != NULL)
267 set_runaspw(runas_user);
269 set_runaspw(runas_user ? runas_user : def_runas_default);
272 (void) putchar('\n');
275 exit(parse_error ? 1 : 0);
278 /* This loop must match the one in sudo_file_lookup() */
279 printf("\nEntries for user %s:\n", user_name);
281 tq_foreach_rev(&userspecs, us) {
282 if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
284 tq_foreach_rev(&us->privileges, priv) {
286 print_privilege(priv); /* XXX */
288 host_match = hostlist_matches(&priv->hostlist);
289 if (host_match == ALLOW) {
290 puts("\thost matched");
291 tq_foreach_rev(&priv->cmndlist, cs) {
292 runas_match = runaslist_matches(&cs->runasuserlist,
293 &cs->runasgrouplist);
294 if (runas_match == ALLOW) {
295 puts("\trunas matched");
296 cmnd_match = cmnd_matches(cs->cmnd);
297 if (cmnd_match != UNSPEC)
299 printf("\tcmnd %s\n", match == ALLOW ? "allowed" :
300 match == DENY ? "denied" : "unmatched");
304 puts("\thost unmatched");
307 printf("\nCommand %s\n", match == ALLOW ? "allowed" :
308 match == DENY ? "denied" : "unmatched");
312 * 0 - parsed OK and command matched.
314 * 2 - command not matched
319 exit(match == ALLOW ? 0 : match + 3);
323 set_runaspw(char *user)
326 if ((runas_pw = sudo_getpwuid(atoi(user + 1))) == NULL)
327 runas_pw = sudo_fakepwnam(user, runas_gr ? runas_gr->gr_gid : 0);
329 if ((runas_pw = sudo_getpwnam(user)) == NULL)
330 errorx(1, "unknown user: %s", user);
335 set_runasgr(char *group)
338 if ((runas_gr = sudo_getgrgid(atoi(group + 1))) == NULL)
339 runas_gr = sudo_fakegrnam(group);
341 if ((runas_gr = sudo_getgrnam(group)) == NULL)
342 errorx(1, "unknown group: %s", group);
365 open_sudoers(const char *path, int isdir, int *keepopen)
367 return fopen(path, "r");
388 cleanup(int gotsignal)
397 print_member(struct member *m)
399 struct sudo_command *c;
404 fputs("ALL", stdout);
405 else if (m->type != COMMAND)
406 fputs(m->name, stdout);
408 c = (struct sudo_command *) m->name;
409 printf("%s%s%s", c->cmnd, c->args ? " " : "",
410 c->args ? c->args : "");
420 tq_foreach_fwd(&defaults, d) {
421 (void) fputs("Defaults", stdout);
436 tq_foreach_fwd(&d->binding, m) {
437 if (m != tq_first(&d->binding))
441 printf("\t%s%s", d->op == FALSE ? "!" : "", d->var);
442 if (d->val != NULL) {
443 printf("%c%s", d->op == TRUE ? '=' : d->op, d->val);
450 print_alias(void *v1, void *v2)
452 struct alias *a = (struct alias *)v1;
454 struct sudo_command *c;
458 (void) printf("Host_Alias\t%s = ", a->name);
461 (void) printf("Cmnd_Alias\t%s = ", a->name);
464 (void) printf("User_Alias\t%s = ", a->name);
467 (void) printf("Runas_Alias\t%s = ", a->name);
470 tq_foreach_fwd(&a->members, m) {
471 if (m != tq_first(&a->members))
473 if (m->type == COMMAND) {
474 c = (struct sudo_command *) m->name;
475 printf("%s%s%s", c->cmnd, c->args ? " " : "",
476 c->args ? c->args : "");
477 } else if (m->type == ALL) {
478 fputs("ALL", stdout);
480 fputs(m->name, stdout);
488 print_privilege(struct privilege *priv)
495 for (p = priv; p != NULL; p = p->next) {
497 fputs(" : ", stdout);
498 tq_foreach_fwd(&p->hostlist, m) {
499 if (m != tq_first(&p->hostlist))
503 fputs(" = ", stdout);
504 tags.nopasswd = tags.noexec = UNSPEC;
505 tq_foreach_fwd(&p->cmndlist, cs) {
506 if (cs != tq_first(&p->cmndlist))
508 if (!tq_empty(&cs->runasuserlist) || !tq_empty(&cs->runasgrouplist)) {
510 if (!tq_empty(&cs->runasuserlist)) {
511 tq_foreach_fwd(&cs->runasuserlist, m) {
512 if (m != tq_first(&cs->runasuserlist))
516 } else if (tq_empty(&cs->runasgrouplist)) {
517 fputs(def_runas_default, stdout);
519 fputs(sudo_user.pw->pw_name, stdout);
521 if (!tq_empty(&cs->runasgrouplist)) {
522 fputs(" : ", stdout);
523 tq_foreach_fwd(&cs->runasgrouplist, m) {
524 if (m != tq_first(&cs->runasgrouplist))
533 printf("ROLE=%s ", cs->role);
535 printf("TYPE=%s ", cs->type);
536 #endif /* HAVE_SELINUX */
537 if (cs->tags.nopasswd != UNSPEC && cs->tags.nopasswd != tags.nopasswd)
538 printf("%sPASSWD: ", cs->tags.nopasswd ? "NO" : "");
539 if (cs->tags.noexec != UNSPEC && cs->tags.noexec != tags.noexec)
540 printf("%sEXEC: ", cs->tags.noexec ? "NO" : "");
541 print_member(cs->cmnd);
542 memcpy(&tags, &cs->tags, sizeof(tags));
548 print_userspecs(void)
553 tq_foreach_fwd(&userspecs, us) {
554 tq_foreach_fwd(&us->users, m) {
555 if (m != tq_first(&us->users))
560 print_privilege(us->privileges.first); /* XXX */
566 testsudoers_printf(int msg_type, const char *fmt, ...)
572 case SUDO_CONV_INFO_MSG:
575 case SUDO_CONV_ERROR_MSG:
584 vfprintf(fp, fmt, ap);
596 alias_apply(print_alias, NULL);
602 static int testsudoers_print(const char *msg)
604 return fputs(msg, stderr);
610 (void) fprintf(stderr, "usage: %s [-dt] [-G grfile] [-g group] [-h host] [-p pwfile] [-u user] <user> <command> [args]\n", getprogname());