Imported Upstream version 1.7.2p6
[debian/sudo] / testsudoers.c
1 /*
2  * Copyright (c) 1996, 1998-2005, 2007-2009
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 #else
44 # ifdef HAVE_STRINGS_H
45 #  include <strings.h>
46 # endif
47 #endif /* HAVE_STRING_H */
48 #ifdef HAVE_UNISTD_H
49 # include <unistd.h>
50 #endif /* HAVE_UNISTD_H */
51 #ifdef HAVE_FNMATCH
52 # include <fnmatch.h>
53 #endif /* HAVE_FNMATCH */
54 #ifdef HAVE_NETGROUP_H
55 # include <netgroup.h>
56 #endif /* HAVE_NETGROUP_H */
57 #include <ctype.h>
58 #include <pwd.h>
59 #include <grp.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
62 #include <netdb.h>
63
64 #include "sudo.h"
65 #include "interfaces.h"
66 #include "parse.h"
67 #include <gram.h>
68
69 #ifndef HAVE_FNMATCH
70 # include "emul/fnmatch.h"
71 #endif /* HAVE_FNMATCH */
72
73 /*
74  * Globals
75  */
76 int  Argc, NewArgc;
77 char **Argv, **NewArgv;
78 int num_interfaces;
79 struct interface *interfaces;
80 struct sudo_user sudo_user;
81 struct passwd *list_pw;
82 extern int parse_error;
83
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;
93
94 /* For getopt(3) */
95 extern char *optarg;
96 extern int optind;
97
98 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
99 extern char *malloc_options;
100 #endif
101 #ifdef YYDEBUG
102 extern int yydebug;
103 #endif
104
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 *));
113
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));
126
127 int
128 main(argc, argv)
129     int argc;
130     char **argv;
131 {
132     struct cmndspec *cs;
133     struct privilege *priv;
134     struct userspec *us;
135     char *p, *grfile, *pwfile, *runas_group, *runas_user;
136     char hbuf[MAXHOSTNAMELEN + 1];
137     int ch, dflag, rval, matched;
138
139 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
140     malloc_options = "AFGJPR";
141 #endif
142 #ifdef YYDEBUG
143     yydebug = 1;
144 #endif
145
146     Argv = argv;
147     Argc = argc;
148
149     dflag = 0;
150     grfile = pwfile = runas_group = runas_user = NULL;
151     while ((ch = getopt(argc, argv, "dg:G:h:p:u:")) != -1) {
152         switch (ch) {
153             case 'd':
154                 dflag = 1;
155                 break;
156             case 'h':
157                 user_host = optarg;
158                 break;
159             case 'G':
160                 grfile = optarg;
161                 break;
162             case 'g':
163                 runas_group = optarg;
164                 break;
165             case 'p':
166                 pwfile = optarg;
167                 break;
168             case 'u':
169                 runas_user = optarg;
170                 break;
171             default:
172                 usage();
173                 break;
174         }
175     }
176     argc -= optind;
177     argv += optind;
178     NewArgc = argc;
179     NewArgv = argv;
180
181     /* Set group/passwd file and init the cache. */
182     if (grfile) {
183         my_setgrent = ts_setgrent;
184         my_endgrent = ts_endgrent;
185         my_getgrnam = ts_getgrnam;
186         my_getgrgid = ts_getgrgid;
187         ts_setgrfile(grfile);
188     }
189     if (pwfile) {
190         my_setpwent = ts_setpwent;
191         my_endpwent = ts_endpwent;
192         my_getpwnam = ts_getpwnam;
193         my_getpwuid = ts_getpwuid;
194         ts_setpwfile(pwfile);
195     }
196     sudo_setpwent();
197     sudo_setgrent();
198
199     if (argc < 2) {
200         if (!dflag)
201             usage();
202         if ((sudo_user.pw = sudo_getpwnam("nobody")) == NULL)
203             errorx(1, "no passwd entry for nobody!");
204         user_cmnd = user_base = "true";
205     } else {
206         if ((sudo_user.pw = sudo_getpwnam(*argv)) == NULL)
207             errorx(1, "no passwd entry for %s!", *argv);
208         user_cmnd = *++argv;
209         if ((p = strrchr(user_cmnd, '/')) != NULL)
210             user_base = p + 1;
211         else
212             user_base = user_cmnd;
213         NewArgc -= 2;
214     }
215
216     if (user_host == NULL) {
217         if (gethostname(hbuf, sizeof(hbuf)) != 0)
218             error(1, "gethostname");
219         hbuf[sizeof(hbuf) - 1] = '\0';
220         user_host = hbuf;
221     }
222     if ((p = strchr(user_host, '.'))) {
223         *p = '\0';
224         user_shost = estrdup(user_host);
225         *p = '.';
226     } else {
227         user_shost = user_host;
228     }
229
230     /* Fill in user_args from NewArgv. */
231     if (NewArgc > 1) {
232         char *to, **from;
233         size_t size, n;
234
235         for (size = 0, from = NewArgv + 1; *from; from++)
236             size += strlen(*from) + 1;
237
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");
243             to += n;
244             *to++ = ' ';
245         }
246         *--to = '\0';
247     }
248
249     /* Initialize default values. */
250     init_defaults();
251
252     /* Load ip addr/mask for each interface. */
253     load_interfaces();
254
255     /* Allocate space for data structures in the parser. */
256     init_parser("sudoers", 0);
257
258     if (yyparse() != 0 || parse_error)
259         (void) fputs("Does not parse", stdout);
260     else
261         (void) fputs("Parses OK", stdout);
262
263     if (!update_defaults(SETDEF_ALL))
264         (void) fputs(" (problem with defaults entries)", stdout);
265     puts(".");
266
267     /*
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.
271      */
272     if (runas_group != NULL) {
273         set_runasgr(runas_group);
274         if (runas_user != NULL)
275             set_runaspw(runas_user);
276     } else
277         set_runaspw(runas_user ? runas_user : def_runas_default);
278
279     if (dflag) {
280         (void) putchar('\n');
281         dump_sudoers();
282         if (argc < 2)
283             exit(0);
284     }
285
286     /* This loop must match the one in sudoers_lookup() */
287     printf("\nEntries for user %s:\n", user_name);
288     matched = UNSPEC;
289     tq_foreach_rev(&userspecs, us) {
290         if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
291             continue;
292         tq_foreach_rev(&us->privileges, priv) {
293             putchar('\n');
294             print_privilege(priv); /* XXX */
295             putchar('\n');
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);
303                         if (rval != UNSPEC)
304                             matched = rval;
305                         printf("\tcmnd  %s\n", rval == ALLOW ? "allowed" :
306                             rval == DENY ? "denied" : "unmatched");
307                     }
308                 }
309             } else
310                 puts("\thost  unmatched");
311         }
312     }
313     printf("\nCommand %s\n", matched == ALLOW ? "allowed" :
314         matched == DENY ? "denied" : "unmatched");
315
316     exit(0);
317 }
318
319 void
320 set_runaspw(user)
321     char *user;
322 {
323     if (*user == '#') {
324         if ((runas_pw = sudo_getpwuid(atoi(user + 1))) == NULL)
325             runas_pw = sudo_fakepwnam(user, runas_gr ? runas_gr->gr_gid : 0);
326     } else {
327         if ((runas_pw = sudo_getpwnam(user)) == NULL)
328             errorx(1, "unknown user: %s", user);
329     }
330 }
331
332 void
333 set_runasgr(group)
334     char *group;
335 {
336     if (*group == '#') {
337         if ((runas_gr = sudo_getgrgid(atoi(group + 1))) == NULL)
338             runas_gr = sudo_fakegrnam(group);
339     } else {
340         if ((runas_gr = sudo_getgrnam(group)) == NULL)
341             errorx(1, "unknown group: %s", group);
342     }
343 }
344
345 void
346 sudo_setspent()
347 {
348     return;
349 }
350
351 void
352 sudo_endspent()
353 {
354     return;
355 }
356
357 char *
358 sudo_getepw(pw)
359     const struct passwd *pw;
360 {
361     return (pw->pw_passwd);
362 }
363
364 void
365 set_fqdn()
366 {
367     return;
368 }
369
370 FILE *
371 open_sudoers(path, isdir, keepopen)
372     const char *path;
373     int isdir;
374     int *keepopen;
375 {
376     return(fopen(path, "r"));
377 }
378
379 void
380 init_envtables()
381 {
382     return;
383 }
384
385 int
386 set_perms(perm)
387     int perm;
388 {
389     return(1);
390 }
391
392 void
393 cleanup(gotsignal)
394     int gotsignal;
395 {
396     if (!gotsignal) {
397         sudo_endpwent();
398         sudo_endgrent();
399     }
400 }
401
402 void
403 print_member(m)    
404     struct member *m;
405 {
406     struct sudo_command *c;
407
408     if (m->negated)
409         putchar('!');
410     if (m->name == NULL)
411         fputs("ALL", stdout);
412     else if (m->type != COMMAND)
413         fputs(m->name, stdout);
414     else {
415         c = (struct sudo_command *) m->name;
416         printf("%s%s%s", c->cmnd, c->args ? " " : "",
417             c->args ? c->args : "");
418     }
419 }
420
421 void
422 print_defaults()
423 {
424     struct defaults *d;
425     struct member *m;
426
427     tq_foreach_fwd(&defaults, d) {
428         (void) fputs("Defaults", stdout);
429         switch (d->type) {
430             case DEFAULTS_HOST:
431                 putchar('@');
432                 break;
433             case DEFAULTS_USER:
434                 putchar(':');
435                 break;
436             case DEFAULTS_RUNAS:
437                 putchar('>');
438                 break;
439             case DEFAULTS_CMND:
440                 putchar('!');
441                 break;
442         }
443         tq_foreach_fwd(&d->binding, m) {
444             if (m != tq_first(&d->binding))
445                 putchar(',');
446             print_member(m);
447         }
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);
451         }
452         putchar('\n');
453     }
454 }
455
456 int
457 print_alias(v1, v2)
458     void *v1, *v2;
459 {
460     struct alias *a = (struct alias *)v1;
461     struct member *m;
462     struct sudo_command *c;
463
464     switch (a->type) {
465         case HOSTALIAS:
466             (void) printf("Host_Alias\t%s = ", a->name);
467             break;
468         case CMNDALIAS:
469             (void) printf("Cmnd_Alias\t%s = ", a->name);
470             break;
471         case USERALIAS:
472             (void) printf("User_Alias\t%s = ", a->name);
473             break;
474         case RUNASALIAS:
475             (void) printf("Runas_Alias\t%s = ", a->name);
476             break;
477     }
478     tq_foreach_fwd(&a->members, m) {
479         if (m != tq_first(&a->members))
480             fputs(", ", stdout);
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 : "");
485         } else
486             fputs(m->name, stdout);
487     }
488     putchar('\n');
489     return(0);
490 }
491
492 void
493 print_privilege(priv)
494     struct privilege *priv;
495 {
496     struct cmndspec *cs;
497     struct member *m;
498     struct privilege *p;
499     struct cmndtag tags;
500
501     for (p = priv; p != NULL; p = p->next) {
502         if (p != priv)
503             fputs(" : ", stdout);
504         tq_foreach_fwd(&p->hostlist, m) {
505             if (m != tq_first(&p->hostlist))
506                 fputs(", ", stdout);
507             print_member(m);
508         }
509         fputs(" = ", stdout);
510         tags.nopasswd = tags.noexec = UNSPEC;
511         tq_foreach_fwd(&p->cmndlist, cs) {
512             if (cs != tq_first(&p->cmndlist))
513                 fputs(", ", stdout);
514             /* XXX - runasgrouplist too */
515             if (!tq_empty(&cs->runasuserlist)) {
516                 fputs("(", stdout);
517                 tq_foreach_fwd(&cs->runasuserlist, m) {
518                     if (m != tq_first(&cs->runasuserlist))
519                         fputs(", ", stdout);
520                     print_member(m);
521                 }
522                 fputs(") ", stdout);
523             }
524 #ifdef HAVE_SELINUX
525             if (cs->role)
526                 printf("ROLE=%s ", cs->role);
527             if (cs->type)
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));
536         }
537     }
538 }
539
540 void
541 print_userspecs()
542 {
543     struct member *m;
544     struct userspec *us;
545
546     tq_foreach_fwd(&userspecs, us) {
547         tq_foreach_fwd(&us->users, m) {
548             if (m != tq_first(&us->users))
549                 fputs(", ", stdout);
550             print_member(m);
551         }
552         putchar('\t');
553         print_privilege(us->privileges.first); /* XXX */
554         putchar('\n');
555     }
556 }
557
558 void
559 dump_sudoers()
560 {
561     print_defaults();
562
563     putchar('\n');
564     alias_apply(print_alias, NULL);
565
566     putchar('\n');
567     print_userspecs();
568 }
569
570 void
571 usage()
572 {
573     (void) fprintf(stderr, "usage: %s [-d] [-G grfile] [-g group] [-h host] [-p pwfile] [-u user] <user> <command> [args]\n", getprogname());
574     exit(1);
575 }