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