2 * Copyright (c) 1996, 1998-2005, 2007-2009
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 */
44 # ifdef HAVE_STRINGS_H
47 #endif /* HAVE_STRING_H */
50 #endif /* HAVE_UNISTD_H */
53 #endif /* HAVE_FNMATCH */
54 #ifdef HAVE_NETGROUP_H
55 # include <netgroup.h>
56 #endif /* HAVE_NETGROUP_H */
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
65 #include "interfaces.h"
70 # include "emul/fnmatch.h"
71 #endif /* HAVE_FNMATCH */
77 char **Argv, **NewArgv;
79 struct interface *interfaces;
80 struct sudo_user sudo_user;
81 struct passwd *list_pw;
82 extern int parse_error;
84 /* passwd/group redirection for pwutil.c */
85 void (*my_setgrent) __P((void)) = setgrent;
86 void (*my_endgrent) __P((void)) = endgrent;
87 struct group *(*my_getgrnam) __P((const char *)) = getgrnam;
88 struct group *(*my_getgrgid) __P((gid_t)) = getgrgid;
89 void (*my_setpwent) __P((void)) = setpwent;
90 void (*my_endpwent) __P((void)) = endpwent;
91 struct passwd *(*my_getpwnam) __P((const char *)) = getpwnam;
92 struct passwd *(*my_getpwuid) __P((uid_t)) = getpwuid;
98 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
99 extern char *malloc_options;
105 int print_alias __P((void *, void *));
106 void dump_sudoers __P((void));
107 void print_defaults __P((void));
108 void print_privilege __P((struct privilege *));
109 void print_userspecs __P((void));
110 void usage __P((void)) __attribute__((__noreturn__));
111 void set_runasgr __P((char *));
112 void set_runaspw __P((char *));
114 extern void ts_setgrfile __P((const char *));
115 extern void ts_setgrent __P((void));
116 extern void ts_endgrent __P((void));
117 extern struct group *ts_getgrent __P((void));
118 extern struct group *ts_getgrnam __P((const char *));
119 extern struct group *ts_getgrgid __P((gid_t));
120 extern void ts_setpwfile __P((const char *));
121 extern void ts_setpwent __P((void));
122 extern void ts_endpwent __P((void));
123 extern struct passwd *ts_getpwent __P((void));
124 extern struct passwd *ts_getpwnam __P((const char *));
125 extern struct passwd *ts_getpwuid __P((uid_t));
133 struct privilege *priv;
135 char *p, *grfile, *pwfile, *runas_group, *runas_user;
136 char hbuf[MAXHOSTNAMELEN + 1];
137 int ch, dflag, rval, matched;
139 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
140 malloc_options = "AFGJPR";
150 grfile = pwfile = runas_group = runas_user = NULL;
151 while ((ch = getopt(argc, argv, "dg:G:h:p:u:")) != -1) {
163 runas_group = optarg;
181 /* Set group/passwd file and init the cache. */
183 my_setgrent = ts_setgrent;
184 my_endgrent = ts_endgrent;
185 my_getgrnam = ts_getgrnam;
186 my_getgrgid = ts_getgrgid;
187 ts_setgrfile(grfile);
190 my_setpwent = ts_setpwent;
191 my_endpwent = ts_endpwent;
192 my_getpwnam = ts_getpwnam;
193 my_getpwuid = ts_getpwuid;
194 ts_setpwfile(pwfile);
202 if ((sudo_user.pw = sudo_getpwnam("nobody")) == NULL)
203 errorx(1, "no passwd entry for nobody!");
204 user_cmnd = user_base = "true";
206 if ((sudo_user.pw = sudo_getpwnam(*argv)) == NULL)
207 errorx(1, "no passwd entry for %s!", *argv);
209 if ((p = strrchr(user_cmnd, '/')) != NULL)
212 user_base = user_cmnd;
216 if (user_host == NULL) {
217 if (gethostname(hbuf, sizeof(hbuf)) != 0)
218 error(1, "gethostname");
219 hbuf[sizeof(hbuf) - 1] = '\0';
222 if ((p = strchr(user_host, '.'))) {
224 user_shost = estrdup(user_host);
227 user_shost = user_host;
230 /* Fill in user_args from NewArgv. */
235 for (size = 0, from = NewArgv + 1; *from; from++)
236 size += strlen(*from) + 1;
238 user_args = (char *) emalloc(size);
239 for (to = user_args, from = NewArgv + 1; *from; from++) {
240 n = strlcpy(to, *from, size - (to - user_args));
241 if (n >= size - (to - user_args))
242 errorx(1, "internal error, init_vars() overflow");
249 /* Initialize default values. */
252 /* Load ip addr/mask for each interface. */
255 /* Allocate space for data structures in the parser. */
256 init_parser("sudoers", 0);
258 if (yyparse() != 0 || parse_error)
259 (void) fputs("Does not parse", stdout);
261 (void) fputs("Parses OK", stdout);
263 if (!update_defaults(SETDEF_ALL))
264 (void) fputs(" (problem with defaults entries)", stdout);
268 * Set runas passwd/group entries based on command line or sudoers.
269 * Note that if runas_group was specified without runas_user we
270 * defer setting runas_pw so the match routines know to ignore it.
272 if (runas_group != NULL) {
273 set_runasgr(runas_group);
274 if (runas_user != NULL)
275 set_runaspw(runas_user);
277 set_runaspw(runas_user ? runas_user : def_runas_default);
280 (void) putchar('\n');
286 /* This loop must match the one in sudoers_lookup() */
287 printf("\nEntries for user %s:\n", user_name);
289 tq_foreach_rev(&userspecs, us) {
290 if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
292 tq_foreach_rev(&us->privileges, priv) {
294 print_privilege(priv); /* XXX */
296 if (hostlist_matches(&priv->hostlist) == ALLOW) {
297 puts("\thost matched");
298 tq_foreach_rev(&priv->cmndlist, cs) {
299 if (runaslist_matches(&cs->runasuserlist,
300 &cs->runasgrouplist) == ALLOW) {
301 puts("\trunas matched");
302 rval = cmnd_matches(cs->cmnd);
305 printf("\tcmnd %s\n", rval == ALLOW ? "allowed" :
306 rval == DENY ? "denied" : "unmatched");
310 puts("\thost unmatched");
313 printf("\nCommand %s\n", matched == ALLOW ? "allowed" :
314 matched == DENY ? "denied" : "unmatched");
324 if ((runas_pw = sudo_getpwuid(atoi(user + 1))) == NULL)
325 runas_pw = sudo_fakepwnam(user, runas_gr ? runas_gr->gr_gid : 0);
327 if ((runas_pw = sudo_getpwnam(user)) == NULL)
328 errorx(1, "unknown user: %s", user);
337 if ((runas_gr = sudo_getgrgid(atoi(group + 1))) == NULL)
338 runas_gr = sudo_fakegrnam(group);
340 if ((runas_gr = sudo_getgrnam(group)) == NULL)
341 errorx(1, "unknown group: %s", group);
359 const struct passwd *pw;
361 return (pw->pw_passwd);
371 open_sudoers(path, isdir, keepopen)
376 return(fopen(path, "r"));
406 struct sudo_command *c;
411 fputs("ALL", stdout);
412 else if (m->type != COMMAND)
413 fputs(m->name, stdout);
415 c = (struct sudo_command *) m->name;
416 printf("%s%s%s", c->cmnd, c->args ? " " : "",
417 c->args ? c->args : "");
427 tq_foreach_fwd(&defaults, d) {
428 (void) fputs("Defaults", stdout);
443 tq_foreach_fwd(&d->binding, m) {
444 if (m != tq_first(&d->binding))
448 printf("\t%s%s", d->op == FALSE ? "!" : "", d->var);
449 if (d->val != NULL) {
450 printf("%c%s", d->op == TRUE ? '=' : d->op, d->val);
460 struct alias *a = (struct alias *)v1;
462 struct sudo_command *c;
466 (void) printf("Host_Alias\t%s = ", a->name);
469 (void) printf("Cmnd_Alias\t%s = ", a->name);
472 (void) printf("User_Alias\t%s = ", a->name);
475 (void) printf("Runas_Alias\t%s = ", a->name);
478 tq_foreach_fwd(&a->members, m) {
479 if (m != tq_first(&a->members))
481 if (m->type == COMMAND) {
482 c = (struct sudo_command *) m->name;
483 printf("%s%s%s", c->cmnd, c->args ? " " : "",
484 c->args ? c->args : "");
486 fputs(m->name, stdout);
493 print_privilege(priv)
494 struct privilege *priv;
501 for (p = priv; p != NULL; p = p->next) {
503 fputs(" : ", stdout);
504 tq_foreach_fwd(&p->hostlist, m) {
505 if (m != tq_first(&p->hostlist))
509 fputs(" = ", stdout);
510 tags.nopasswd = tags.noexec = UNSPEC;
511 tq_foreach_fwd(&p->cmndlist, cs) {
512 if (cs != tq_first(&p->cmndlist))
514 /* XXX - runasgrouplist too */
515 if (!tq_empty(&cs->runasuserlist)) {
517 tq_foreach_fwd(&cs->runasuserlist, m) {
518 if (m != tq_first(&cs->runasuserlist))
526 printf("ROLE=%s ", cs->role);
528 printf("TYPE=%s ", cs->type);
529 #endif /* HAVE_SELINUX */
530 if (cs->tags.nopasswd != UNSPEC && cs->tags.nopasswd != tags.nopasswd)
531 printf("%sPASSWD: ", cs->tags.nopasswd ? "NO" : "");
532 if (cs->tags.noexec != UNSPEC && cs->tags.noexec != tags.noexec)
533 printf("%sEXEC: ", cs->tags.noexec ? "NO" : "");
534 print_member(cs->cmnd);
535 memcpy(&tags, &cs->tags, sizeof(tags));
546 tq_foreach_fwd(&userspecs, us) {
547 tq_foreach_fwd(&us->users, m) {
548 if (m != tq_first(&us->users))
553 print_privilege(us->privileges.first); /* XXX */
564 alias_apply(print_alias, NULL);
573 (void) fprintf(stderr, "usage: %s [-d] [-G grfile] [-g group] [-h host] [-p pwfile] [-u user] <user> <command> [args]\n", getprogname());