Annotation of embedaddon/sudo/plugins/sudoers/testsudoers.c, revision 1.1.1.2
1.1 misho 1: /*
1.1.1.2 ! misho 2: * Copyright (c) 1996, 1998-2005, 2007-2012
1.1 misho 3: * Todd C. Miller <Todd.Miller@courtesan.com>
4: *
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.
8: *
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.
18: *
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.
22: */
23:
24: #define _SUDO_MAIN
25:
26: #include <config.h>
27:
28: #include <sys/param.h>
29: #include <sys/types.h>
30: #include <sys/stat.h>
31: #include <sys/socket.h>
32: #include <stdio.h>
33: #ifdef STDC_HEADERS
34: # include <stdlib.h>
35: # include <stddef.h>
36: #else
37: # ifdef HAVE_STDLIB_H
38: # include <stdlib.h>
39: # endif
40: #endif /* STDC_HEADERS */
41: #ifdef HAVE_STRING_H
42: # include <string.h>
43: #endif /* HAVE_STRING_H */
44: #ifdef HAVE_STRINGS_H
45: # include <strings.h>
46: #endif /* HAVE_STRINGS_H */
47: #ifdef HAVE_UNISTD_H
48: # include <unistd.h>
49: #endif /* HAVE_UNISTD_H */
50: #ifdef HAVE_FNMATCH
51: # include <fnmatch.h>
1.1.1.2 ! misho 52: #else
! 53: # include "compat/fnmatch.h"
1.1 misho 54: #endif /* HAVE_FNMATCH */
55: #ifdef HAVE_NETGROUP_H
56: # include <netgroup.h>
57: #endif /* HAVE_NETGROUP_H */
58: #include <ctype.h>
59: #include <errno.h>
60: #include <netinet/in.h>
61: #include <arpa/inet.h>
62: #include <netdb.h>
1.1.1.2 ! misho 63: #ifdef HAVE_SETLOCALE
! 64: # include <locale.h>
! 65: #endif
1.1 misho 66:
67: #include "tsgetgrpw.h"
68: #include "sudoers.h"
69: #include "interfaces.h"
70: #include "parse.h"
1.1.1.2 ! misho 71: #include "sudo_conf.h"
1.1 misho 72: #include <gram.h>
73:
74: /*
75: * Function Prototypes
76: */
77: int print_alias(void *, void *);
78: void dump_sudoers(void);
79: void print_defaults(void);
80: void print_privilege(struct privilege *);
81: void print_userspecs(void);
82: void usage(void) __attribute__((__noreturn__));
83: void cleanup(int);
84: static void set_runaspw(const char *);
85: static void set_runasgr(const char *);
86: static int cb_runas_default(const char *);
87: static int testsudoers_printf(int msg_type, const char *fmt, ...);
88: static int testsudoers_print(const char *msg);
89:
90: extern void setgrfile(const char *);
91: extern void setgrent(void);
92: extern void endgrent(void);
93: extern struct group *getgrent(void);
94: extern struct group *getgrnam(const char *);
95: extern struct group *getgrgid(gid_t);
96: extern void setpwfile(const char *);
97: extern void setpwent(void);
98: extern void endpwent(void);
99: extern struct passwd *getpwent(void);
100: extern struct passwd *getpwnam(const char *);
101: extern struct passwd *getpwuid(uid_t);
102:
103: extern int (*trace_print)(const char *msg);
104:
105: /*
106: * Globals
107: */
108: struct interface *interfaces;
109: struct sudo_user sudo_user;
110: struct passwd *list_pw;
111: static char *runas_group, *runas_user;
1.1.1.2 ! misho 112: extern int errorlineno;
! 113: extern bool parse_error;
! 114: extern char *errorfile;
1.1 misho 115: sudo_printf_t sudo_printf = testsudoers_printf;
1.1.1.2 ! misho 116: sudo_conv_t sudo_conv; /* NULL in non-plugin */
1.1 misho 117:
118: /* For getopt(3) */
119: extern char *optarg;
120: extern int optind;
121:
122: #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
123: extern char *malloc_options;
124: #endif
125: #ifdef YYDEBUG
126: extern int yydebug;
127: #endif
128:
129: int
130: main(int argc, char *argv[])
131: {
132: struct cmndspec *cs;
133: struct privilege *priv;
134: struct userspec *us;
135: char *p, *grfile, *pwfile;
136: char hbuf[MAXHOSTNAMELEN + 1];
137: int match, host_match, runas_match, cmnd_match;
1.1.1.2 ! misho 138: int ch, dflag, exitcode = 0;
! 139: debug_decl(main, SUDO_DEBUG_MAIN)
1.1 misho 140:
141: #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
142: malloc_options = "AFGJPR";
143: #endif
144: #ifdef YYDEBUG
145: yydebug = 1;
146: #endif
147:
148: #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
149: setprogname(argc > 0 ? argv[0] : "testsudoers");
150: #endif
151:
1.1.1.2 ! misho 152: #ifdef HAVE_SETLOCALE
! 153: setlocale(LC_ALL, "");
! 154: #endif
! 155: bindtextdomain("sudoers", LOCALEDIR); /* XXX - should have own domain */
! 156: textdomain("sudoers");
! 157:
! 158: /* Read sudo.conf. */
! 159: sudo_conf_read();
! 160:
1.1 misho 161: dflag = 0;
162: grfile = pwfile = NULL;
163: while ((ch = getopt(argc, argv, "dg:G:h:p:tu:")) != -1) {
164: switch (ch) {
165: case 'd':
166: dflag = 1;
167: break;
168: case 'h':
169: user_host = optarg;
170: break;
171: case 'G':
172: grfile = optarg;
173: break;
174: case 'g':
175: runas_group = optarg;
176: break;
177: case 'p':
178: pwfile = optarg;
179: break;
180: case 't':
181: trace_print = testsudoers_print;
182: break;
183: case 'u':
184: runas_user = optarg;
185: break;
186: default:
187: usage();
188: break;
189: }
190: }
191: argc -= optind;
192: argv += optind;
193:
194: /* Set group/passwd file and init the cache. */
195: if (grfile)
196: setgrfile(grfile);
197: if (pwfile)
198: setpwfile(pwfile);
199: sudo_setpwent();
200: sudo_setgrent();
201:
202: if (argc < 2) {
203: if (!dflag)
204: usage();
1.1.1.2 ! misho 205: user_name = argc ? *argv++ : "root";
1.1 misho 206: user_cmnd = user_base = "true";
1.1.1.2 ! misho 207: argc = 0;
1.1 misho 208: } else {
1.1.1.2 ! misho 209: user_name = *argv++;
! 210: user_cmnd = *argv++;
1.1 misho 211: if ((p = strrchr(user_cmnd, '/')) != NULL)
212: user_base = p + 1;
213: else
214: user_base = user_cmnd;
215: argc -= 2;
216: }
217: if ((sudo_user.pw = sudo_getpwnam(user_name)) == NULL)
218: errorx(1, _("unknown user: %s"), user_name);
219:
220: if (user_host == NULL) {
221: if (gethostname(hbuf, sizeof(hbuf)) != 0)
222: error(1, "gethostname");
223: hbuf[sizeof(hbuf) - 1] = '\0';
224: user_host = hbuf;
225: }
226: if ((p = strchr(user_host, '.'))) {
227: *p = '\0';
228: user_shost = estrdup(user_host);
229: *p = '.';
230: } else {
231: user_shost = user_host;
232: }
233:
234: /* Fill in user_args from argv. */
235: if (argc > 0) {
236: char *to, **from;
237: size_t size, n;
238:
1.1.1.2 ! misho 239: for (size = 0, from = argv; *from; from++)
1.1 misho 240: size += strlen(*from) + 1;
241:
242: user_args = (char *) emalloc(size);
1.1.1.2 ! misho 243: for (to = user_args, from = argv; *from; from++) {
1.1 misho 244: n = strlcpy(to, *from, size - (to - user_args));
245: if (n >= size - (to - user_args))
246: errorx(1, _("internal error, init_vars() overflow"));
247: to += n;
248: *to++ = ' ';
249: }
250: *--to = '\0';
251: }
252:
253: /* Initialize default values. */
254: init_defaults();
255:
256: /* Set runas callback. */
257: sudo_defs_table[I_RUNAS_DEFAULT].callback = cb_runas_default;
258:
259: /* Load ip addr/mask for each interface. */
260: if (get_net_ifs(&p) > 0)
261: set_interfaces(p);
262:
263: /* Allocate space for data structures in the parser. */
264: init_parser("sudoers", 0);
265:
266: if (yyparse() != 0 || parse_error) {
1.1.1.2 ! misho 267: parse_error = true;
! 268: if (errorlineno != -1)
! 269: (void) printf("Parse error in %s near line %d",
! 270: errorfile, errorlineno);
! 271: else
! 272: (void) printf("Parse error in %s", errorfile);
1.1 misho 273: } else {
274: (void) fputs("Parses OK", stdout);
275: }
276:
277: if (!update_defaults(SETDEF_ALL))
278: (void) fputs(" (problem with defaults entries)", stdout);
279: puts(".");
280:
1.1.1.2 ! misho 281: if (def_group_plugin && group_plugin_load(def_group_plugin) != true)
1.1 misho 282: def_group_plugin = NULL;
283:
284: /*
285: * Set runas passwd/group entries based on command line or sudoers.
286: * Note that if runas_group was specified without runas_user we
287: * defer setting runas_pw so the match routines know to ignore it.
288: */
289: if (runas_group != NULL) {
290: set_runasgr(runas_group);
291: if (runas_user != NULL)
292: set_runaspw(runas_user);
293: } else
294: set_runaspw(runas_user ? runas_user : def_runas_default);
295:
296: if (dflag) {
297: (void) putchar('\n');
298: dump_sudoers();
1.1.1.2 ! misho 299: if (argc < 2) {
! 300: exitcode = parse_error ? 1 : 0;
! 301: goto done;
! 302: }
1.1 misho 303: }
304:
305: /* This loop must match the one in sudo_file_lookup() */
306: printf("\nEntries for user %s:\n", user_name);
307: match = UNSPEC;
308: tq_foreach_rev(&userspecs, us) {
309: if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
310: continue;
311: tq_foreach_rev(&us->privileges, priv) {
312: putchar('\n');
313: print_privilege(priv); /* XXX */
314: putchar('\n');
315: host_match = hostlist_matches(&priv->hostlist);
316: if (host_match == ALLOW) {
317: puts("\thost matched");
318: tq_foreach_rev(&priv->cmndlist, cs) {
319: runas_match = runaslist_matches(&cs->runasuserlist,
320: &cs->runasgrouplist);
321: if (runas_match == ALLOW) {
322: puts("\trunas matched");
323: cmnd_match = cmnd_matches(cs->cmnd);
324: if (cmnd_match != UNSPEC)
325: match = cmnd_match;
326: printf("\tcmnd %s\n", match == ALLOW ? "allowed" :
327: match == DENY ? "denied" : "unmatched");
328: }
329: }
330: } else
331: puts(_("\thost unmatched"));
332: }
333: }
334: puts(match == ALLOW ? _("\nCommand allowed") :
335: match == DENY ? _("\nCommand denied") : _("\nCommand unmatched"));
336:
337: /*
338: * Exit codes:
339: * 0 - parsed OK and command matched.
340: * 1 - parse error
341: * 2 - command not matched
342: * 3 - command denied
343: */
1.1.1.2 ! misho 344: exitcode = parse_error ? 1 : (match == ALLOW ? 0 : match + 3);
! 345: done:
! 346: sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode);
! 347: exit(exitcode);
1.1 misho 348: }
349:
350: static void
351: set_runaspw(const char *user)
352: {
1.1.1.2 ! misho 353: debug_decl(main, SUDO_DEBUG_UTIL)
! 354:
1.1 misho 355: if (runas_pw != NULL)
356: pw_delref(runas_pw);
357: if (*user == '#') {
358: if ((runas_pw = sudo_getpwuid(atoi(user + 1))) == NULL)
359: runas_pw = sudo_fakepwnam(user, runas_gr ? runas_gr->gr_gid : 0);
360: } else {
361: if ((runas_pw = sudo_getpwnam(user)) == NULL)
362: errorx(1, _("unknown user: %s"), user);
363: }
1.1.1.2 ! misho 364:
! 365: debug_return;
1.1 misho 366: }
367:
368: static void
369: set_runasgr(const char *group)
370: {
1.1.1.2 ! misho 371: debug_decl(main, SUDO_DEBUG_UTIL)
! 372:
1.1 misho 373: if (runas_gr != NULL)
374: gr_delref(runas_gr);
375: if (*group == '#') {
376: if ((runas_gr = sudo_getgrgid(atoi(group + 1))) == NULL)
377: runas_gr = sudo_fakegrnam(group);
378: } else {
379: if ((runas_gr = sudo_getgrnam(group)) == NULL)
380: errorx(1, _("unknown group: %s"), group);
381: }
1.1.1.2 ! misho 382:
! 383: debug_return;
1.1 misho 384: }
385:
386: /*
387: * Callback for runas_default sudoers setting.
388: */
389: static int
390: cb_runas_default(const char *user)
391: {
392: /* Only reset runaspw if user didn't specify one. */
393: if (!runas_user && !runas_group)
394: set_runaspw(user);
1.1.1.2 ! misho 395: return true;
1.1 misho 396: }
397:
398: void
399: sudo_setspent(void)
400: {
401: return;
402: }
403:
404: void
405: sudo_endspent(void)
406: {
407: return;
408: }
409:
410: void
411: set_fqdn(void)
412: {
413: return;
414: }
415:
416: FILE *
1.1.1.2 ! misho 417: open_sudoers(const char *path, bool doedit, bool *keepopen)
1.1 misho 418: {
1.1.1.2 ! misho 419: debug_decl(open_sudoers, SUDO_DEBUG_UTIL)
! 420:
! 421: debug_return_ptr(fopen(path, "r"));
1.1 misho 422: }
423:
424: void
425: init_envtables(void)
426: {
427: return;
428: }
429:
430: int
431: set_perms(int perm)
432: {
433: return 1;
434: }
435:
436: void
437: restore_perms(void)
438: {
439: }
440:
441: void
442: cleanup(int gotsignal)
443: {
444: if (!gotsignal) {
445: sudo_endpwent();
446: sudo_endgrent();
447: }
448: }
449:
450: void
451: print_member(struct member *m)
452: {
453: struct sudo_command *c;
1.1.1.2 ! misho 454: debug_decl(print_member, SUDO_DEBUG_UTIL)
1.1 misho 455:
456: if (m->negated)
457: putchar('!');
458: if (m->name == NULL)
459: fputs("ALL", stdout);
460: else if (m->type != COMMAND)
461: fputs(m->name, stdout);
462: else {
463: c = (struct sudo_command *) m->name;
464: printf("%s%s%s", c->cmnd, c->args ? " " : "",
465: c->args ? c->args : "");
466: }
1.1.1.2 ! misho 467:
! 468: debug_return;
1.1 misho 469: }
470:
471: void
472: print_defaults(void)
473: {
474: struct defaults *d;
475: struct member *m;
1.1.1.2 ! misho 476: debug_decl(print_member, SUDO_DEBUG_UTIL)
1.1 misho 477:
478: tq_foreach_fwd(&defaults, d) {
479: (void) fputs("Defaults", stdout);
480: switch (d->type) {
481: case DEFAULTS_HOST:
482: putchar('@');
483: break;
484: case DEFAULTS_USER:
485: putchar(':');
486: break;
487: case DEFAULTS_RUNAS:
488: putchar('>');
489: break;
490: case DEFAULTS_CMND:
491: putchar('!');
492: break;
493: }
494: tq_foreach_fwd(&d->binding, m) {
495: if (m != tq_first(&d->binding))
496: putchar(',');
497: print_member(m);
498: }
1.1.1.2 ! misho 499: printf("\t%s%s", d->op == false ? "!" : "", d->var);
1.1 misho 500: if (d->val != NULL) {
1.1.1.2 ! misho 501: printf("%c%s", d->op == true ? '=' : d->op, d->val);
1.1 misho 502: }
503: putchar('\n');
504: }
1.1.1.2 ! misho 505:
! 506: debug_return;
1.1 misho 507: }
508:
509: int
510: print_alias(void *v1, void *v2)
511: {
512: struct alias *a = (struct alias *)v1;
513: struct member *m;
514: struct sudo_command *c;
1.1.1.2 ! misho 515: debug_decl(print_alias, SUDO_DEBUG_UTIL)
1.1 misho 516:
517: switch (a->type) {
518: case HOSTALIAS:
519: (void) printf("Host_Alias\t%s = ", a->name);
520: break;
521: case CMNDALIAS:
522: (void) printf("Cmnd_Alias\t%s = ", a->name);
523: break;
524: case USERALIAS:
525: (void) printf("User_Alias\t%s = ", a->name);
526: break;
527: case RUNASALIAS:
528: (void) printf("Runas_Alias\t%s = ", a->name);
529: break;
530: }
531: tq_foreach_fwd(&a->members, m) {
532: if (m != tq_first(&a->members))
533: fputs(", ", stdout);
534: if (m->type == COMMAND) {
535: c = (struct sudo_command *) m->name;
536: printf("%s%s%s", c->cmnd, c->args ? " " : "",
537: c->args ? c->args : "");
538: } else if (m->type == ALL) {
539: fputs("ALL", stdout);
540: } else {
541: fputs(m->name, stdout);
542: }
543: }
544: putchar('\n');
1.1.1.2 ! misho 545: debug_return_int(0);
1.1 misho 546: }
547:
548: void
549: print_privilege(struct privilege *priv)
550: {
551: struct cmndspec *cs;
552: struct member *m;
553: struct privilege *p;
554: struct cmndtag tags;
1.1.1.2 ! misho 555: debug_decl(print_privilege, SUDO_DEBUG_UTIL)
1.1 misho 556:
557: for (p = priv; p != NULL; p = p->next) {
558: if (p != priv)
559: fputs(" : ", stdout);
560: tq_foreach_fwd(&p->hostlist, m) {
561: if (m != tq_first(&p->hostlist))
562: fputs(", ", stdout);
563: print_member(m);
564: }
565: fputs(" = ", stdout);
566: tags.nopasswd = tags.noexec = UNSPEC;
567: tq_foreach_fwd(&p->cmndlist, cs) {
568: if (cs != tq_first(&p->cmndlist))
569: fputs(", ", stdout);
570: if (!tq_empty(&cs->runasuserlist) || !tq_empty(&cs->runasgrouplist)) {
571: fputs("(", stdout);
572: if (!tq_empty(&cs->runasuserlist)) {
573: tq_foreach_fwd(&cs->runasuserlist, m) {
574: if (m != tq_first(&cs->runasuserlist))
575: fputs(", ", stdout);
576: print_member(m);
577: }
578: } else if (tq_empty(&cs->runasgrouplist)) {
579: fputs(def_runas_default, stdout);
580: } else {
581: fputs(sudo_user.pw->pw_name, stdout);
582: }
583: if (!tq_empty(&cs->runasgrouplist)) {
584: fputs(" : ", stdout);
585: tq_foreach_fwd(&cs->runasgrouplist, m) {
586: if (m != tq_first(&cs->runasgrouplist))
587: fputs(", ", stdout);
588: print_member(m);
589: }
590: }
591: fputs(") ", stdout);
592: }
593: #ifdef HAVE_SELINUX
594: if (cs->role)
595: printf("ROLE=%s ", cs->role);
596: if (cs->type)
597: printf("TYPE=%s ", cs->type);
598: #endif /* HAVE_SELINUX */
599: if (cs->tags.nopasswd != UNSPEC && cs->tags.nopasswd != tags.nopasswd)
600: printf("%sPASSWD: ", cs->tags.nopasswd ? "NO" : "");
601: if (cs->tags.noexec != UNSPEC && cs->tags.noexec != tags.noexec)
602: printf("%sEXEC: ", cs->tags.noexec ? "NO" : "");
603: print_member(cs->cmnd);
604: memcpy(&tags, &cs->tags, sizeof(tags));
605: }
606: }
1.1.1.2 ! misho 607: debug_return;
1.1 misho 608: }
609:
610: void
611: print_userspecs(void)
612: {
613: struct member *m;
614: struct userspec *us;
1.1.1.2 ! misho 615: debug_decl(print_userspecs, SUDO_DEBUG_UTIL)
1.1 misho 616:
617: tq_foreach_fwd(&userspecs, us) {
618: tq_foreach_fwd(&us->users, m) {
619: if (m != tq_first(&us->users))
620: fputs(", ", stdout);
621: print_member(m);
622: }
623: putchar('\t');
624: print_privilege(us->privileges.first); /* XXX */
625: putchar('\n');
626: }
1.1.1.2 ! misho 627: debug_return;
1.1 misho 628: }
629:
630: static int
631: testsudoers_printf(int msg_type, const char *fmt, ...)
632: {
633: va_list ap;
634: FILE *fp;
1.1.1.2 ! misho 635: debug_decl(testsudoers_printf, SUDO_DEBUG_UTIL)
1.1 misho 636:
637: switch (msg_type) {
638: case SUDO_CONV_INFO_MSG:
639: fp = stdout;
640: break;
641: case SUDO_CONV_ERROR_MSG:
642: fp = stderr;
643: break;
644: default:
645: errno = EINVAL;
1.1.1.2 ! misho 646: debug_return_int(-1);
1.1 misho 647: }
648:
649: va_start(ap, fmt);
650: vfprintf(fp, fmt, ap);
651: va_end(ap);
652:
1.1.1.2 ! misho 653: debug_return_int(0);
1.1 misho 654: }
655:
656: void
657: dump_sudoers(void)
658: {
1.1.1.2 ! misho 659: debug_decl(dump_sudoers, SUDO_DEBUG_UTIL)
! 660:
1.1 misho 661: print_defaults();
662:
663: putchar('\n');
664: alias_apply(print_alias, NULL);
665:
666: putchar('\n');
667: print_userspecs();
1.1.1.2 ! misho 668:
! 669: debug_return;
1.1 misho 670: }
671:
672: static int testsudoers_print(const char *msg)
673: {
674: return fputs(msg, stderr);
675: }
676:
677: void
678: usage(void)
679: {
680: (void) fprintf(stderr, "usage: %s [-dt] [-G grfile] [-g group] [-h host] [-p pwfile] [-u user] <user> <command> [args]\n", getprogname());
681: exit(1);
682: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>