2 * Copyright (c) 1996, 1998-2005, 2007-2010
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 */
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
64 #include "interfaces.h"
69 # include "emul/fnmatch.h"
70 #endif /* HAVE_FNMATCH */
76 char **Argv, **NewArgv;
78 struct interface *interfaces;
79 struct sudo_user sudo_user;
80 struct passwd *list_pw;
81 extern int parse_error;
87 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
88 extern char *malloc_options;
94 int print_alias __P((void *, void *));
95 void dump_sudoers __P((void));
96 void print_defaults __P((void));
97 void print_privilege __P((struct privilege *));
98 void print_userspecs __P((void));
99 void usage __P((void)) __attribute__((__noreturn__));
100 void set_runasgr __P((char *));
101 void set_runaspw __P((char *));
103 extern void setgrfile __P((const char *));
104 extern void setgrent __P((void));
105 extern void endgrent __P((void));
106 extern struct group *getgrent __P((void));
107 extern struct group *getgrnam __P((const char *));
108 extern struct group *getgrgid __P((gid_t));
109 extern void setpwfile __P((const char *));
110 extern void setpwent __P((void));
111 extern void endpwent __P((void));
112 extern struct passwd *getpwent __P((void));
113 extern struct passwd *getpwnam __P((const char *));
114 extern struct passwd *getpwuid __P((uid_t));
122 struct privilege *priv;
124 char *p, *grfile, *pwfile, *runas_group, *runas_user;
125 char hbuf[MAXHOSTNAMELEN + 1];
126 int ch, dflag, rval, matched;
128 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
129 malloc_options = "AFGJPR";
139 grfile = pwfile = runas_group = runas_user = NULL;
140 while ((ch = getopt(argc, argv, "dg:G:h:p:u:")) != -1) {
152 runas_group = optarg;
170 /* Set group/passwd file and init the cache. */
181 if ((sudo_user.pw = sudo_getpwnam("nobody")) == NULL)
182 errorx(1, "no passwd entry for nobody!");
183 user_cmnd = user_base = "true";
185 if ((sudo_user.pw = sudo_getpwnam(*argv)) == NULL)
186 errorx(1, "no passwd entry for %s!", *argv);
188 if ((p = strrchr(user_cmnd, '/')) != NULL)
191 user_base = user_cmnd;
195 if (user_host == NULL) {
196 if (gethostname(hbuf, sizeof(hbuf)) != 0)
197 error(1, "gethostname");
198 hbuf[sizeof(hbuf) - 1] = '\0';
201 if ((p = strchr(user_host, '.'))) {
203 user_shost = estrdup(user_host);
206 user_shost = user_host;
209 /* Fill in user_args from NewArgv. */
214 for (size = 0, from = NewArgv + 1; *from; from++)
215 size += strlen(*from) + 1;
217 user_args = (char *) emalloc(size);
218 for (to = user_args, from = NewArgv + 1; *from; from++) {
219 n = strlcpy(to, *from, size - (to - user_args));
220 if (n >= size - (to - user_args))
221 errorx(1, "internal error, init_vars() overflow");
228 /* Initialize default values. */
231 /* Load ip addr/mask for each interface. */
234 /* Allocate space for data structures in the parser. */
235 init_parser("sudoers", 0);
237 if (yyparse() != 0 || parse_error)
238 (void) fputs("Does not parse", stdout);
240 (void) fputs("Parses OK", stdout);
242 if (!update_defaults(SETDEF_ALL))
243 (void) fputs(" (problem with defaults entries)", stdout);
247 * Set runas passwd/group entries based on command line or sudoers.
248 * Note that if runas_group was specified without runas_user we
249 * defer setting runas_pw so the match routines know to ignore it.
251 if (runas_group != NULL) {
252 set_runasgr(runas_group);
253 if (runas_user != NULL)
254 set_runaspw(runas_user);
256 set_runaspw(runas_user ? runas_user : def_runas_default);
259 (void) putchar('\n');
265 /* This loop must match the one in sudoers_lookup() */
266 printf("\nEntries for user %s:\n", user_name);
268 tq_foreach_rev(&userspecs, us) {
269 if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
271 tq_foreach_rev(&us->privileges, priv) {
273 print_privilege(priv); /* XXX */
275 if (hostlist_matches(&priv->hostlist) == ALLOW) {
276 puts("\thost matched");
277 tq_foreach_rev(&priv->cmndlist, cs) {
278 if (runaslist_matches(&cs->runasuserlist,
279 &cs->runasgrouplist) == ALLOW) {
280 puts("\trunas matched");
281 rval = cmnd_matches(cs->cmnd);
284 printf("\tcmnd %s\n", rval == ALLOW ? "allowed" :
285 rval == DENY ? "denied" : "unmatched");
289 puts("\thost unmatched");
292 printf("\nCommand %s\n", matched == ALLOW ? "allowed" :
293 matched == DENY ? "denied" : "unmatched");
303 if ((runas_pw = sudo_getpwuid(atoi(user + 1))) == NULL)
304 runas_pw = sudo_fakepwnam(user, runas_gr ? runas_gr->gr_gid : 0);
306 if ((runas_pw = sudo_getpwnam(user)) == NULL)
307 errorx(1, "unknown user: %s", user);
316 if ((runas_gr = sudo_getgrgid(atoi(group + 1))) == NULL)
317 runas_gr = sudo_fakegrnam(group);
319 if ((runas_gr = sudo_getgrnam(group)) == NULL)
320 errorx(1, "unknown group: %s", group);
338 const struct passwd *pw;
340 return (pw->pw_passwd);
350 open_sudoers(path, isdir, keepopen)
355 return(fopen(path, "r"));
385 struct sudo_command *c;
390 fputs("ALL", stdout);
391 else if (m->type != COMMAND)
392 fputs(m->name, stdout);
394 c = (struct sudo_command *) m->name;
395 printf("%s%s%s", c->cmnd, c->args ? " " : "",
396 c->args ? c->args : "");
406 tq_foreach_fwd(&defaults, d) {
407 (void) fputs("Defaults", stdout);
422 tq_foreach_fwd(&d->binding, m) {
423 if (m != tq_first(&d->binding))
427 printf("\t%s%s", d->op == FALSE ? "!" : "", d->var);
428 if (d->val != NULL) {
429 printf("%c%s", d->op == TRUE ? '=' : d->op, d->val);
439 struct alias *a = (struct alias *)v1;
441 struct sudo_command *c;
445 (void) printf("Host_Alias\t%s = ", a->name);
448 (void) printf("Cmnd_Alias\t%s = ", a->name);
451 (void) printf("User_Alias\t%s = ", a->name);
454 (void) printf("Runas_Alias\t%s = ", a->name);
457 tq_foreach_fwd(&a->members, m) {
458 if (m != tq_first(&a->members))
460 if (m->type == COMMAND) {
461 c = (struct sudo_command *) m->name;
462 printf("%s%s%s", c->cmnd, c->args ? " " : "",
463 c->args ? c->args : "");
465 fputs(m->name, stdout);
472 print_privilege(priv)
473 struct privilege *priv;
480 for (p = priv; p != NULL; p = p->next) {
482 fputs(" : ", stdout);
483 tq_foreach_fwd(&p->hostlist, m) {
484 if (m != tq_first(&p->hostlist))
488 fputs(" = ", stdout);
489 tags.nopasswd = tags.noexec = UNSPEC;
490 tq_foreach_fwd(&p->cmndlist, cs) {
491 if (cs != tq_first(&p->cmndlist))
493 /* XXX - runasgrouplist too */
494 if (!tq_empty(&cs->runasuserlist)) {
496 tq_foreach_fwd(&cs->runasuserlist, m) {
497 if (m != tq_first(&cs->runasuserlist))
505 printf("ROLE=%s ", cs->role);
507 printf("TYPE=%s ", cs->type);
508 #endif /* HAVE_SELINUX */
509 if (cs->tags.nopasswd != UNSPEC && cs->tags.nopasswd != tags.nopasswd)
510 printf("%sPASSWD: ", cs->tags.nopasswd ? "NO" : "");
511 if (cs->tags.noexec != UNSPEC && cs->tags.noexec != tags.noexec)
512 printf("%sEXEC: ", cs->tags.noexec ? "NO" : "");
513 print_member(cs->cmnd);
514 memcpy(&tags, &cs->tags, sizeof(tags));
525 tq_foreach_fwd(&userspecs, us) {
526 tq_foreach_fwd(&us->users, m) {
527 if (m != tq_first(&us->users))
532 print_privilege(us->privileges.first); /* XXX */
543 alias_apply(print_alias, NULL);
552 (void) fprintf(stderr, "usage: %s [-d] [-G grfile] [-g group] [-h host] [-p pwfile] [-u user] <user> <command> [args]\n", getprogname());