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>
63 #include "tsgetgrpw.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;
88 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
89 extern char *malloc_options;
95 int print_alias __P((void *, void *));
96 void dump_sudoers __P((void));
97 void print_defaults __P((void));
98 void print_privilege __P((struct privilege *));
99 void print_userspecs __P((void));
100 void usage __P((void)) __attribute__((__noreturn__));
101 void set_runasgr __P((char *));
102 void set_runaspw __P((char *));
110 struct privilege *priv;
112 char *p, *grfile, *pwfile, *runas_group, *runas_user;
113 char hbuf[MAXHOSTNAMELEN + 1];
114 int match, host_match, runas_match, cmnd_match;
117 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
118 malloc_options = "AFGJPR";
128 grfile = pwfile = runas_group = runas_user = NULL;
129 while ((ch = getopt(argc, argv, "dg:G:h:p:u:")) != -1) {
141 runas_group = optarg;
159 /* Set group/passwd file and init the cache. */
170 if ((sudo_user.pw = sudo_getpwnam("root")) == NULL)
171 errorx(1, "no passwd entry for root!");
172 user_cmnd = user_base = "true";
174 if ((sudo_user.pw = sudo_getpwnam(*argv)) == NULL)
175 errorx(1, "no passwd entry for %s!", *argv);
177 if ((p = strrchr(user_cmnd, '/')) != NULL)
180 user_base = user_cmnd;
184 if (user_host == NULL) {
185 if (gethostname(hbuf, sizeof(hbuf)) != 0)
186 error(1, "gethostname");
187 hbuf[sizeof(hbuf) - 1] = '\0';
190 if ((p = strchr(user_host, '.'))) {
192 user_shost = estrdup(user_host);
195 user_shost = user_host;
198 /* Fill in user_args from NewArgv. */
203 for (size = 0, from = NewArgv + 1; *from; from++)
204 size += strlen(*from) + 1;
206 user_args = (char *) emalloc(size);
207 for (to = user_args, from = NewArgv + 1; *from; from++) {
208 n = strlcpy(to, *from, size - (to - user_args));
209 if (n >= size - (to - user_args))
210 errorx(1, "internal error, init_vars() overflow");
217 /* Initialize default values. */
220 /* Load ip addr/mask for each interface. */
223 /* Allocate space for data structures in the parser. */
224 init_parser("sudoers", 0);
226 if (yyparse() != 0 || parse_error) {
228 (void) fputs("Does not parse", stdout);
230 (void) fputs("Parses OK", stdout);
233 if (!update_defaults(SETDEF_ALL))
234 (void) fputs(" (problem with defaults entries)", stdout);
238 * Set runas passwd/group entries based on command line or sudoers.
239 * Note that if runas_group was specified without runas_user we
240 * defer setting runas_pw so the match routines know to ignore it.
242 if (runas_group != NULL) {
243 set_runasgr(runas_group);
244 if (runas_user != NULL)
245 set_runaspw(runas_user);
247 set_runaspw(runas_user ? runas_user : def_runas_default);
250 (void) putchar('\n');
253 exit(parse_error ? 1 : 0);
256 /* This loop must match the one in sudo_file_lookup() */
257 printf("\nEntries for user %s:\n", user_name);
259 tq_foreach_rev(&userspecs, us) {
260 if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
262 tq_foreach_rev(&us->privileges, priv) {
264 print_privilege(priv); /* XXX */
266 host_match = hostlist_matches(&priv->hostlist);
267 if (host_match == ALLOW) {
268 puts("\thost matched");
269 tq_foreach_rev(&priv->cmndlist, cs) {
270 runas_match = runaslist_matches(&cs->runasuserlist,
271 &cs->runasgrouplist);
272 if (runas_match == ALLOW) {
273 puts("\trunas matched");
274 cmnd_match = cmnd_matches(cs->cmnd);
275 if (cmnd_match != UNSPEC)
277 printf("\tcmnd %s\n", match == ALLOW ? "allowed" :
278 match == DENY ? "denied" : "unmatched");
282 puts("\thost unmatched");
285 printf("\nCommand %s\n", match == ALLOW ? "allowed" :
286 match == DENY ? "denied" : "unmatched");
290 * 0 - parsed OK and command matched.
292 * 2 - command not matched
297 exit(match == ALLOW ? 0 : match + 3);
305 if ((runas_pw = sudo_getpwuid(atoi(user + 1))) == NULL)
306 runas_pw = sudo_fakepwnam(user, runas_gr ? runas_gr->gr_gid : 0);
308 if ((runas_pw = sudo_getpwnam(user)) == NULL)
309 errorx(1, "unknown user: %s", user);
318 if ((runas_gr = sudo_getgrgid(atoi(group + 1))) == NULL)
319 runas_gr = sudo_fakegrnam(group);
321 if ((runas_gr = sudo_getgrnam(group)) == NULL)
322 errorx(1, "unknown group: %s", group);
340 const struct passwd *pw;
342 return pw->pw_passwd;
352 open_sudoers(path, isdir, keepopen)
357 return fopen(path, "r");
387 struct sudo_command *c;
392 fputs("ALL", stdout);
393 else if (m->type != COMMAND)
394 fputs(m->name, stdout);
396 c = (struct sudo_command *) m->name;
397 printf("%s%s%s", c->cmnd, c->args ? " " : "",
398 c->args ? c->args : "");
408 tq_foreach_fwd(&defaults, d) {
409 (void) fputs("Defaults", stdout);
424 tq_foreach_fwd(&d->binding, m) {
425 if (m != tq_first(&d->binding))
429 printf("\t%s%s", d->op == FALSE ? "!" : "", d->var);
430 if (d->val != NULL) {
431 printf("%c%s", d->op == TRUE ? '=' : d->op, d->val);
441 struct alias *a = (struct alias *)v1;
443 struct sudo_command *c;
447 (void) printf("Host_Alias\t%s = ", a->name);
450 (void) printf("Cmnd_Alias\t%s = ", a->name);
453 (void) printf("User_Alias\t%s = ", a->name);
456 (void) printf("Runas_Alias\t%s = ", a->name);
459 tq_foreach_fwd(&a->members, m) {
460 if (m != tq_first(&a->members))
462 if (m->type == COMMAND) {
463 c = (struct sudo_command *) m->name;
464 printf("%s%s%s", c->cmnd, c->args ? " " : "",
465 c->args ? c->args : "");
466 } else if (m->type == ALL) {
467 fputs("ALL", stdout);
469 fputs(m->name, stdout);
477 print_privilege(priv)
478 struct privilege *priv;
485 for (p = priv; p != NULL; p = p->next) {
487 fputs(" : ", stdout);
488 tq_foreach_fwd(&p->hostlist, m) {
489 if (m != tq_first(&p->hostlist))
493 fputs(" = ", stdout);
494 tags.nopasswd = tags.noexec = UNSPEC;
495 tq_foreach_fwd(&p->cmndlist, cs) {
496 if (cs != tq_first(&p->cmndlist))
498 if (!tq_empty(&cs->runasuserlist) || !tq_empty(&cs->runasgrouplist)) {
500 if (!tq_empty(&cs->runasuserlist)) {
501 tq_foreach_fwd(&cs->runasuserlist, m) {
502 if (m != tq_first(&cs->runasuserlist))
506 } else if (tq_empty(&cs->runasgrouplist)) {
507 fputs(def_runas_default, stdout);
509 fputs(sudo_user.pw->pw_name, stdout);
511 if (!tq_empty(&cs->runasgrouplist)) {
512 fputs(" : ", stdout);
513 tq_foreach_fwd(&cs->runasgrouplist, m) {
514 if (m != tq_first(&cs->runasgrouplist))
523 printf("ROLE=%s ", cs->role);
525 printf("TYPE=%s ", cs->type);
526 #endif /* HAVE_SELINUX */
527 if (cs->tags.nopasswd != UNSPEC && cs->tags.nopasswd != tags.nopasswd)
528 printf("%sPASSWD: ", cs->tags.nopasswd ? "NO" : "");
529 if (cs->tags.noexec != UNSPEC && cs->tags.noexec != tags.noexec)
530 printf("%sEXEC: ", cs->tags.noexec ? "NO" : "");
531 print_member(cs->cmnd);
532 memcpy(&tags, &cs->tags, sizeof(tags));
543 tq_foreach_fwd(&userspecs, us) {
544 tq_foreach_fwd(&us->users, m) {
545 if (m != tq_first(&us->users))
550 print_privilege(us->privileges.first); /* XXX */
561 alias_apply(print_alias, NULL);
570 (void) fprintf(stderr, "usage: %s [-d] [-G grfile] [-g group] [-h host] [-p pwfile] [-u user] <user> <command> [args]\n", getprogname());