add doc about interaction with RAMRUN to README.Debian in response to #581393
[debian/sudo] / visudo.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  *
17  * Sponsored in part by the Defense Advanced Research Projects
18  * Agency (DARPA) and Air Force Research Laboratory, Air Force
19  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
20  */
21
22 /*
23  * Lock the sudoers file for safe editing (ala vipw) and check for parse errors.
24  */
25
26 #define _SUDO_MAIN
27
28 #ifdef __TANDEM
29 # include <floss.h>
30 #endif
31
32 #include <config.h>
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 #include <sys/socket.h>
38 #include <sys/time.h>
39 #ifndef __TANDEM
40 # include <sys/file.h>
41 #endif
42 #include <sys/wait.h>
43 #include <stdio.h>
44 #ifdef STDC_HEADERS
45 # include <stdlib.h>
46 # include <stddef.h>
47 #else
48 # ifdef HAVE_STDLIB_H
49 #  include <stdlib.h>
50 # endif
51 #endif /* STDC_HEADERS */
52 #ifdef HAVE_STRING_H
53 # include <string.h>
54 #else
55 # ifdef HAVE_STRINGS_H
56 #  include <strings.h>
57 # endif
58 #endif /* HAVE_STRING_H */
59 #ifdef HAVE_UNISTD_H
60 #include <unistd.h>
61 #endif /* HAVE_UNISTD_H */
62 #include <ctype.h>
63 #include <pwd.h>
64 #include <grp.h>
65 #include <signal.h>
66 #include <errno.h>
67 #include <fcntl.h>
68 #include <netinet/in.h>
69 #include <arpa/inet.h>
70 #include <netdb.h>
71 #if TIME_WITH_SYS_TIME
72 # include <time.h>
73 #endif
74 #ifdef __STDC__
75 # include <stdarg.h>
76 #else
77 # include <varargs.h>
78 #endif
79 #ifndef HAVE_TIMESPEC
80 # include <emul/timespec.h>
81 #endif
82
83 #include "sudo.h"
84 #include "interfaces.h"
85 #include "parse.h"
86 #include "redblack.h"
87 #include <gram.h>
88
89 struct sudoersfile {
90     char *path;
91     char *tpath;
92     int fd;
93     int tfd;
94     int modified;
95     int doedit;
96     struct sudoersfile *next;
97 };
98
99 /*
100  * Function prototypes
101  */
102 static RETSIGTYPE quit          __P((int));
103 static char *get_args           __P((char *));
104 static char *get_editor         __P((char **));
105 static void get_hostname        __P((void));
106 static char whatnow             __P((void));
107 static int check_aliases        __P((int, int));
108 static int check_syntax         __P((char *, int, int));
109 static int edit_sudoers         __P((struct sudoersfile *, char *, char *, int));
110 static int install_sudoers      __P((struct sudoersfile *, int));
111 static int print_unused         __P((void *, void *));
112 static int reparse_sudoers      __P((char *, char *, int, int));
113 static int run_command          __P((char *, char **));
114 static void print_undefined     __P((char *name, int, int, int));
115 static void setup_signals       __P((void));
116 static void usage               __P((void)) __attribute__((__noreturn__));
117
118 extern void yyerror             __P((const char *));
119 extern void yyrestart           __P((FILE *));
120
121 /*
122  * External globals exported by the parser
123  */
124 extern struct rbtree *aliases;
125 extern FILE *yyin;
126 extern char *sudoers, *errorfile;
127 extern int errorlineno, parse_error;
128 /* For getopt(3) */
129 extern char *optarg;
130 extern int optind;
131
132 /*
133  * Globals
134  */
135 int Argc;
136 char **Argv;
137 int num_interfaces;
138 struct interface *interfaces;
139 struct sudo_user sudo_user;
140 struct passwd *list_pw;
141 static struct sudoerslist {
142     struct sudoersfile *first, *last;
143 } sudoerslist;
144 static struct rbtree *alias_freelist;
145
146 int
147 main(argc, argv)
148     int argc;
149     char **argv;
150 {
151     struct sudoersfile *sp;
152     char *args, *editor, *sudoers_path;
153     int ch, checkonly, quiet, strict, oldperms;
154 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
155     extern char *malloc_options;
156     malloc_options = "AFGJPR";
157 #endif
158
159     Argv = argv;
160     if ((Argc = argc) < 1)
161         usage();
162
163     /*
164      * Arg handling.
165      */
166     checkonly = oldperms = quiet = strict = FALSE;
167     sudoers_path = _PATH_SUDOERS;
168     while ((ch = getopt(argc, argv, "Vcf:sq")) != -1) {
169         switch (ch) {
170             case 'V':
171                 (void) printf("%s version %s\n", getprogname(), PACKAGE_VERSION);
172                 exit(0);
173             case 'c':
174                 checkonly++;            /* check mode */
175                 break;
176             case 'f':
177                 sudoers_path = optarg;  /* sudoers file path */
178                 oldperms = TRUE;
179                 break;
180             case 's':
181                 strict++;               /* strict mode */
182                 break;
183             case 'q':
184                 quiet++;                /* quiet mode */
185                 break;
186             default:
187                 usage();
188         }
189     }
190     argc -= optind;
191     argv += optind;
192     if (argc)
193         usage();
194
195     sudo_setpwent();
196     sudo_setgrent();
197
198     /* Mock up a fake sudo_user struct. */
199     user_cmnd = "";
200     if ((sudo_user.pw = sudo_getpwuid(getuid())) == NULL)
201         errorx(1, "you don't exist in the passwd database");
202     get_hostname();
203
204     /* Setup defaults data structures. */
205     init_defaults();
206
207     if (checkonly)
208         exit(check_syntax(sudoers_path, quiet, strict));
209
210     /*
211      * Parse the existing sudoers file(s) in quiet mode to highlight any
212      * existing errors and to pull in editor and env_editor conf values.
213      */
214     if ((yyin = open_sudoers(sudoers_path, TRUE, NULL)) == NULL) {
215         error(1, "%s", sudoers_path);
216     }
217     init_parser(sudoers_path, 0);
218     yyparse();
219     (void) update_defaults(SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER);
220
221     editor = get_editor(&args);
222
223     /* Install signal handlers to clean up temp files if we are killed. */
224     setup_signals();
225
226     /* Edit the sudoers file(s) */
227     tq_foreach_fwd(&sudoerslist, sp) {
228         if (!sp->doedit)
229             continue;
230         if (sp != tq_first(&sudoerslist)) {
231             printf("press return to edit %s: ", sp->path);
232             while ((ch = getchar()) != EOF && ch != '\n')
233                     continue;
234         }
235         edit_sudoers(sp, editor, args, -1);
236     }
237
238     /* Check edited files for a parse error and re-edit any that fail. */
239     reparse_sudoers(editor, args, strict, quiet);
240
241     /* Install the sudoers temp files. */
242     tq_foreach_fwd(&sudoerslist, sp) {
243         if (!sp->modified)
244             (void) unlink(sp->tpath);
245         else
246             (void) install_sudoers(sp, oldperms);
247     }
248
249     exit(0);
250 }
251
252 /*
253  * Edit each sudoers file.
254  * Returns TRUE on success, else FALSE.
255  */
256 static int
257 edit_sudoers(sp, editor, args, lineno)
258     struct sudoersfile *sp;
259     char *editor, *args;
260     int lineno;
261 {
262     int tfd;                            /* sudoers temp file descriptor */
263     int modified;                       /* was the file modified? */
264     int ac;                             /* argument count */
265     char **av;                          /* argument vector for run_command */
266     char *cp;                           /* scratch char pointer */
267     char buf[PATH_MAX*2];               /* buffer used for copying files */
268     char linestr[64];                   /* string version of lineno */
269     struct timespec ts1, ts2;           /* time before and after edit */
270     struct timespec orig_mtim;          /* starting mtime of sudoers file */
271     off_t orig_size;                    /* starting size of sudoers file */
272     ssize_t nread;                      /* number of bytes read */
273     struct stat sb;                     /* stat buffer */
274
275 #ifdef HAVE_FSTAT
276     if (fstat(sp->fd, &sb) == -1)
277 #else
278     if (stat(sp->path, &sb) == -1)
279 #endif
280         error(1, "can't stat %s", sp->path);
281     orig_size = sb.st_size;
282     orig_mtim.tv_sec = mtim_getsec(sb);
283     orig_mtim.tv_nsec = mtim_getnsec(sb);
284
285     /* Create the temp file if needed and set timestamp. */
286     if (sp->tpath == NULL) {
287         easprintf(&sp->tpath, "%s.tmp", sp->path);
288         tfd = open(sp->tpath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
289         if (tfd < 0)
290             error(1, "%s", sp->tpath);
291
292         /* Copy sp->path -> sp->tpath and reset the mtime. */
293         if (orig_size != 0) {
294             (void) lseek(sp->fd, (off_t)0, SEEK_SET);
295             while ((nread = read(sp->fd, buf, sizeof(buf))) > 0)
296                 if (write(tfd, buf, nread) != nread)
297                     error(1, "write error");
298
299             /* Add missing newline at EOF if needed. */
300             if (nread > 0 && buf[nread - 1] != '\n') {
301                 buf[0] = '\n';
302                 write(tfd, buf, 1);
303             }
304         }
305         (void) close(tfd);
306     }
307     (void) touch(-1, sp->tpath, &orig_mtim);
308
309     /* Find the length of the argument vector */
310     ac = 3 + (lineno > 0);
311     if (args) {
312         int wasblank;
313
314         ac++;
315         for (wasblank = FALSE, cp = args; *cp; cp++) {
316             if (isblank((unsigned char) *cp))
317                 wasblank = TRUE;
318             else if (wasblank) {
319                 wasblank = FALSE;
320                 ac++;
321             }
322         }
323     }
324
325     /* Build up argument vector for the command */
326     av = emalloc2(ac, sizeof(char *));
327     if ((av[0] = strrchr(editor, '/')) != NULL)
328         av[0]++;
329     else
330         av[0] = editor;
331     ac = 1;
332     if (lineno > 0) {
333         (void) snprintf(linestr, sizeof(linestr), "+%d", lineno);
334         av[ac++] = linestr;
335     }
336     if (args) {
337         for ((cp = strtok(args, " \t")); cp; (cp = strtok(NULL, " \t")))
338             av[ac++] = cp;
339     }
340     av[ac++] = sp->tpath;
341     av[ac++] = NULL;
342
343     /*
344      * Do the edit:
345      *  We cannot check the editor's exit value against 0 since
346      *  XPG4 specifies that vi's exit value is a function of the
347      *  number of errors during editing (?!?!).
348      */
349     gettime(&ts1);
350     if (run_command(editor, av) != -1) {
351         gettime(&ts2);
352         /*
353          * Sanity checks.
354          */
355         if (stat(sp->tpath, &sb) < 0) {
356             warningx("cannot stat temporary file (%s), %s unchanged",
357                 sp->tpath, sp->path);
358             return(FALSE);
359         }
360         if (sb.st_size == 0 && orig_size != 0) {
361             warningx("zero length temporary file (%s), %s unchanged",
362                 sp->tpath, sp->path);
363             sp->modified = TRUE;
364             return(FALSE);
365         }
366     } else {
367         warningx("editor (%s) failed, %s unchanged", editor, sp->path);
368         return(FALSE);
369     }
370
371     /* Set modified bit if use changed the file. */
372     modified = TRUE;
373     if (orig_size == sb.st_size &&
374         orig_mtim.tv_sec == mtim_getsec(sb) &&
375         orig_mtim.tv_nsec == mtim_getnsec(sb)) {
376         /*
377          * If mtime and size match but the user spent no measurable
378          * time in the editor we can't tell if the file was changed.
379          */
380 #ifdef HAVE_TIMESPECSUB2
381         timespecsub(&ts1, &ts2);
382 #else
383         timespecsub(&ts1, &ts2, &ts2);
384 #endif
385         if (timespecisset(&ts2))
386             modified = FALSE;
387     }
388
389     /*
390      * If modified in this edit session, mark as modified.
391      */
392     if (modified)
393         sp->modified = modified;
394     else
395         warningx("%s unchanged", sp->tpath);
396
397     return(TRUE);
398 }
399
400 /*
401  * Parse sudoers after editing and re-edit any ones that caused a parse error.
402  * Returns TRUE on success, else FALSE.
403  */
404 static int
405 reparse_sudoers(editor, args, strict, quiet)
406     char *editor, *args;
407     int strict, quiet;
408 {
409     struct sudoersfile *sp, *last;
410     FILE *fp;
411     int ch;
412
413     /*
414      * Parse the edited sudoers files and do sanity checking
415      */
416     do {
417         sp = tq_first(&sudoerslist);
418         last = tq_last(&sudoerslist);
419         fp = fopen(sp->tpath, "r+");
420         if (fp == NULL)
421             errorx(1, "can't re-open temporary file (%s), %s unchanged.",
422                 sp->tpath, sp->path);
423
424         /* Clean slate for each parse */
425         init_defaults();
426         init_parser(sp->path, quiet);
427
428         /* Parse the sudoers temp file */
429         yyrestart(fp);
430         if (yyparse() && parse_error != TRUE) {
431             warningx("unabled to parse temporary file (%s), unknown error",
432                 sp->tpath);
433             parse_error = TRUE;
434         }
435         fclose(yyin);
436         if (check_aliases(strict, quiet) != 0)
437             parse_error = TRUE;
438
439         /*
440          * Got an error, prompt the user for what to do now
441          */
442         if (parse_error) {
443             switch (whatnow()) {
444                 case 'Q' :      parse_error = FALSE;    /* ignore parse error */
445                                 break;
446                 case 'x' :      cleanup(0);
447                                 exit(0);
448                                 break;
449             }
450         }
451         if (parse_error) {
452             /* Edit file with the parse error */
453             tq_foreach_fwd(&sudoerslist, sp) {
454                 if (errorfile == NULL || strcmp(sp->path, errorfile) == 0) {
455                     edit_sudoers(sp, editor, args, errorlineno);
456                     break;
457                 }
458             }
459             if (sp == NULL)
460                 errorx(1, "internal error, can't find %s in list!", sudoers);
461         }
462
463         /* If any new #include directives were added, edit them too. */
464         for (sp = last->next; sp != NULL; sp = sp->next) {
465             printf("press return to edit %s: ", sp->path);
466             while ((ch = getchar()) != EOF && ch != '\n')
467                     continue;
468             edit_sudoers(sp, editor, args, errorlineno);
469         }
470     } while (parse_error);
471
472     return(TRUE);
473 }
474
475 /*
476  * Set the owner and mode on a sudoers temp file and
477  * move it into place.  Returns TRUE on success, else FALSE.
478  */
479 static int
480 install_sudoers(sp, oldperms)
481     struct sudoersfile *sp;
482     int oldperms;
483 {
484     struct stat sb;
485
486     /*
487      * Change mode and ownership of temp file so when
488      * we move it to sp->path things are kosher.
489      */
490     if (oldperms) {
491         /* Use perms of the existing file.  */
492 #ifdef HAVE_FSTAT
493         if (fstat(sp->fd, &sb) == -1)
494 #else
495         if (stat(sp->path, &sb) == -1)
496 #endif
497             error(1, "can't stat %s", sp->path);
498         (void) chown(sp->tpath, sb.st_uid, sb.st_gid);
499         (void) chmod(sp->tpath, sb.st_mode & 0777);
500     } else {
501         if (chown(sp->tpath, SUDOERS_UID, SUDOERS_GID) != 0) {
502             warning("unable to set (uid, gid) of %s to (%d, %d)",
503                 sp->tpath, SUDOERS_UID, SUDOERS_GID);
504             return(FALSE);
505         }
506         if (chmod(sp->tpath, SUDOERS_MODE) != 0) {
507             warning("unable to change mode of %s to 0%o", sp->tpath,
508                 SUDOERS_MODE);
509             return(FALSE);
510         }
511     }
512
513     /*
514      * Now that sp->tpath is sane (parses ok) it needs to be
515      * rename(2)'d to sp->path.  If the rename(2) fails we try using
516      * mv(1) in case sp->tpath and sp->path are on different file systems.
517      */
518     if (rename(sp->tpath, sp->path) == 0) {
519         efree(sp->tpath);
520         sp->tpath = NULL;
521     } else {
522         if (errno == EXDEV) {
523             char *av[4];
524             warningx("%s and %s not on the same file system, using mv to rename",
525               sp->tpath, sp->path);
526
527             /* Build up argument vector for the command */
528             if ((av[0] = strrchr(_PATH_MV, '/')) != NULL)
529                 av[0]++;
530             else
531                 av[0] = _PATH_MV;
532             av[1] = sp->tpath;
533             av[2] = sp->path;
534             av[3] = NULL;
535
536             /* And run it... */
537             if (run_command(_PATH_MV, av)) {
538                 warningx("command failed: '%s %s %s', %s unchanged",
539                     _PATH_MV, sp->tpath, sp->path, sp->path);
540                 (void) unlink(sp->tpath);
541                 efree(sp->tpath);
542                 sp->tpath = NULL;
543                 return(FALSE);
544             }
545             efree(sp->tpath);
546             sp->tpath = NULL;
547         } else {
548             warning("error renaming %s, %s unchanged", sp->tpath, sp->path);
549             (void) unlink(sp->tpath);
550             return(FALSE);
551         }
552     }
553     return(TRUE);
554 }
555
556 /* STUB */
557 void
558 set_fqdn()
559 {
560     return;
561 }
562
563 /* STUB */
564 void
565 init_envtables()
566 {
567     return;
568 }
569
570 /* STUB */
571 int
572 user_is_exempt()
573 {
574     return(FALSE);
575 }
576
577 /* STUB */
578 void
579 sudo_setspent()
580 {
581     return;
582 }
583
584 /* STUB */
585 void
586 sudo_endspent()
587 {
588     return;
589 }
590
591 char *
592 sudo_getepw(pw)
593     const struct passwd *pw;
594 {
595     return (pw->pw_passwd);
596 }
597
598 /*
599  * Assuming a parse error occurred, prompt the user for what they want
600  * to do now.  Returns the first letter of their choice.
601  */
602 static char
603 whatnow()
604 {
605     int choice, c;
606
607     for (;;) {
608         (void) fputs("What now? ", stdout);
609         choice = getchar();
610         for (c = choice; c != '\n' && c != EOF;)
611             c = getchar();
612
613         switch (choice) {
614             case EOF:
615                 choice = 'x';
616                 /* FALLTHROUGH */
617             case 'e':
618             case 'x':
619             case 'Q':
620                 return(choice);
621             default:
622                 (void) puts("Options are:");
623                 (void) puts("  (e)dit sudoers file again");
624                 (void) puts("  e(x)it without saving changes to sudoers file");
625                 (void) puts("  (Q)uit and save changes to sudoers file (DANGER!)\n");
626         }
627     }
628 }
629
630 /*
631  * Install signal handlers for visudo.
632  */
633 static void
634 setup_signals()
635 {
636         sigaction_t sa;
637
638         /*
639          * Setup signal handlers to cleanup nicely.
640          */
641         zero_bytes(&sa, sizeof(sa));
642         sigemptyset(&sa.sa_mask);
643         sa.sa_flags = SA_RESTART;
644         sa.sa_handler = quit;
645         (void) sigaction(SIGTERM, &sa, NULL);
646         (void) sigaction(SIGHUP, &sa, NULL);
647         (void) sigaction(SIGINT, &sa, NULL);
648         (void) sigaction(SIGQUIT, &sa, NULL);
649 }
650
651 static int
652 run_command(path, argv)
653     char *path;
654     char **argv;
655 {
656     int status;
657     pid_t pid, rv;
658
659     switch (pid = fork()) {
660         case -1:
661             error(1, "unable to run %s", path);
662             break;      /* NOTREACHED */
663         case 0:
664             sudo_endpwent();
665             sudo_endgrent();
666             closefrom(STDERR_FILENO + 1);
667             execv(path, argv);
668             warning("unable to run %s", path);
669             _exit(127);
670             break;      /* NOTREACHED */
671     }
672
673     do {
674 #ifdef sudo_waitpid
675         rv = sudo_waitpid(pid, &status, 0);
676 #else
677         rv = wait(&status);
678 #endif
679     } while (rv == -1 && errno == EINTR);
680
681     if (rv == -1 || !WIFEXITED(status))
682         return(-1);
683     return(WEXITSTATUS(status));
684 }
685
686 static int
687 check_syntax(sudoers_path, quiet, strict)
688     char *sudoers_path;
689     int quiet;
690     int strict;
691 {
692     struct stat sb;
693     int error;
694
695     if ((yyin = fopen(sudoers_path, "r")) == NULL) {
696         if (!quiet)
697             warning("unable to open %s", sudoers_path);
698         exit(1);
699     }
700     init_parser(sudoers_path, quiet);
701     if (yyparse() && parse_error != TRUE) {
702         if (!quiet)
703             warningx("failed to parse %s file, unknown error", sudoers_path);
704         parse_error = TRUE;
705     }
706     if (!parse_error) {
707         if (check_aliases(strict, quiet) != 0)
708             parse_error = TRUE;
709     }
710     error = parse_error;
711     if (!quiet) {
712         if (parse_error)
713             (void) printf("parse error in %s near line %d\n", errorfile,
714                 errorlineno);
715         else
716             (void) printf("%s: parsed OK\n", sudoers_path);
717     }
718     /* Check mode and owner in strict mode. */
719 #ifdef HAVE_FSTAT
720     if (strict && fstat(fileno(yyin), &sb) == 0)
721 #else
722     if (strict && stat(sudoers_path, &sb) == 0)
723 #endif
724     {
725         if (sb.st_uid != SUDOERS_UID || sb.st_gid != SUDOERS_GID) {
726             error = TRUE;
727             if (!quiet) {
728                 fprintf(stderr, "%s: wrong owner (uid, gid) should be (%d, %d)\n",
729                     sudoers_path, SUDOERS_UID, SUDOERS_GID);
730                 }
731         }
732         if ((sb.st_mode & 07777) != SUDOERS_MODE) {
733             error = TRUE;
734             if (!quiet) {
735                 fprintf(stderr, "%s: bad permissions, should be mode 0%o\n",
736                     sudoers_path, SUDOERS_MODE);
737             }
738         }
739     }
740
741     return(error);
742 }
743
744 /*
745  * Used to open (and lock) the initial sudoers file and to also open
746  * any subsequent files #included via a callback from the parser.
747  */
748 FILE *
749 open_sudoers(path, doedit, keepopen)
750     const char *path;
751     int doedit;
752     int *keepopen;
753 {
754     struct sudoersfile *entry;
755     FILE *fp;
756
757     /* Check for existing entry */
758     tq_foreach_fwd(&sudoerslist, entry) {
759         if (strcmp(path, entry->path) == 0)
760             break;
761     }
762     if (entry == NULL) {
763         entry = emalloc(sizeof(*entry));
764         entry->path = estrdup(path);
765         entry->modified = 0;
766         entry->next = NULL;
767         entry->fd = open(entry->path, O_RDWR | O_CREAT, SUDOERS_MODE);
768         entry->tpath = NULL;
769         entry->tfd = -1;
770         entry->doedit = doedit;
771         if (entry->fd == -1) {
772             warning("%s", entry->path);
773             efree(entry);
774             return(NULL);
775         }
776         if (!lock_file(entry->fd, SUDO_TLOCK))
777             errorx(1, "%s busy, try again later", entry->path);
778         if ((fp = fdopen(entry->fd, "r")) == NULL)
779             error(1, "%s", entry->path);
780         /* XXX - macro here? */
781         if (sudoerslist.last == NULL)
782             sudoerslist.first = sudoerslist.last = entry;
783         else {
784             sudoerslist.last->next = entry;
785             sudoerslist.last = entry;
786         }
787     } else {
788         /* Already exists, open .tmp version if there is one. */
789         if (entry->tpath != NULL) {
790             if ((fp = fopen(entry->tpath, "r")) == NULL)
791                 error(1, "%s", entry->tpath);
792         } else {
793             if ((fp = fdopen(entry->fd, "r")) == NULL)
794                 error(1, "%s", entry->path);
795             rewind(fp);
796         }
797     }
798     if (keepopen != NULL)
799         *keepopen = TRUE;
800     return(fp);
801 }
802
803 static char *
804 get_editor(args)
805     char **args;
806 {
807     char *Editor, *EditorArgs, *EditorPath, *UserEditor, *UserEditorArgs;
808
809     /*
810      * Check VISUAL and EDITOR environment variables to see which editor
811      * the user wants to use (we may not end up using it though).
812      * If the path is not fully-qualified, make it so and check that
813      * the specified executable actually exists.
814      */
815     UserEditorArgs = NULL;
816     if ((UserEditor = getenv("VISUAL")) == NULL || *UserEditor == '\0')
817         UserEditor = getenv("EDITOR");
818     if (UserEditor && *UserEditor == '\0')
819         UserEditor = NULL;
820     else if (UserEditor) {
821         UserEditorArgs = get_args(UserEditor);
822         if (find_path(UserEditor, &Editor, NULL, getenv("PATH")) == FOUND) {
823             UserEditor = Editor;
824         } else {
825             if (def_env_editor) {
826                 /* If we are honoring $EDITOR this is a fatal error. */
827                 errorx(1, "specified editor (%s) doesn't exist!", UserEditor);
828             } else {
829                 /* Otherwise, just ignore $EDITOR. */
830                 UserEditor = NULL;
831             }
832         }
833     }
834
835     /*
836      * See if we can use the user's choice of editors either because
837      * we allow any $EDITOR or because $EDITOR is in the allowable list.
838      */
839     Editor = EditorArgs = EditorPath = NULL;
840     if (def_env_editor && UserEditor) {
841         Editor = UserEditor;
842         EditorArgs = UserEditorArgs;
843     } else if (UserEditor) {
844         struct stat editor_sb;
845         struct stat user_editor_sb;
846         char *base, *userbase;
847
848         if (stat(UserEditor, &user_editor_sb) != 0) {
849             /* Should never happen since we already checked above. */
850             error(1, "unable to stat editor (%s)", UserEditor);
851         }
852         EditorPath = estrdup(def_editor);
853         Editor = strtok(EditorPath, ":");
854         do {
855             EditorArgs = get_args(Editor);
856             /*
857              * Both Editor and UserEditor should be fully qualified but
858              * check anyway...
859              */
860             if ((base = strrchr(Editor, '/')) == NULL)
861                 continue;
862             if ((userbase = strrchr(UserEditor, '/')) == NULL) {
863                 Editor = NULL;
864                 break;
865             }
866             base++, userbase++;
867
868             /*
869              * We compare the basenames first and then use stat to match
870              * for sure.
871              */
872             if (strcmp(base, userbase) == 0) {
873                 if (stat(Editor, &editor_sb) == 0 && S_ISREG(editor_sb.st_mode)
874                     && (editor_sb.st_mode & 0000111) &&
875                     editor_sb.st_dev == user_editor_sb.st_dev &&
876                     editor_sb.st_ino == user_editor_sb.st_ino)
877                     break;
878             }
879         } while ((Editor = strtok(NULL, ":")));
880     }
881
882     /*
883      * Can't use $EDITOR, try each element of def_editor until we
884      * find one that exists, is regular, and is executable.
885      */
886     if (Editor == NULL || *Editor == '\0') {
887         efree(EditorPath);
888         EditorPath = estrdup(def_editor);
889         Editor = strtok(EditorPath, ":");
890         do {
891             EditorArgs = get_args(Editor);
892             if (sudo_goodpath(Editor, NULL))
893                 break;
894         } while ((Editor = strtok(NULL, ":")));
895
896         /* Bleah, none of the editors existed! */
897         if (Editor == NULL || *Editor == '\0')
898             errorx(1, "no editor found (editor path = %s)", def_editor);
899     }
900     *args = EditorArgs;
901     return(Editor);
902 }
903
904 /*
905  * Split out any command line arguments and return them.
906  */
907 static char *
908 get_args(cmnd)
909     char *cmnd;
910 {
911     char *args;
912
913     args = cmnd;
914     while (*args && !isblank((unsigned char) *args))
915         args++;
916     if (*args) {
917         *args++ = '\0';
918         while (*args && isblank((unsigned char) *args))
919             args++;
920     }
921     return(*args ? args : NULL);
922 }
923
924 /*
925  * Look up the hostname and set user_host and user_shost.
926  */
927 static void
928 get_hostname()
929 {
930     char *p, thost[MAXHOSTNAMELEN + 1];
931
932     if (gethostname(thost, sizeof(thost)) != 0) {
933         user_host = user_shost = "localhost";
934         return;
935     }
936     thost[sizeof(thost) - 1] = '\0';
937     user_host = estrdup(thost);
938
939     if ((p = strchr(user_host, '.'))) {
940         *p = '\0';
941         user_shost = estrdup(user_host);
942         *p = '.';
943     } else {
944         user_shost = user_host;
945     }
946 }
947
948 static void
949 alias_remove_recursive(name, type)
950     char *name;
951     int type;
952 {
953     struct member *m;
954     struct alias *a;
955
956     if ((a = alias_find(name, type)) != NULL) {
957         tq_foreach_fwd(&a->members, m) {
958             if (m->type == ALIAS) {
959                 alias_remove_recursive(m->name, type);
960             }
961         }
962     }
963     alias_seqno++;
964     a = alias_remove(name, type);
965     if (a)
966         rbinsert(alias_freelist, a);
967 }
968
969 /*
970  * Iterate through the sudoers datastructures looking for undefined
971  * aliases or unused aliases.
972  */
973 static int
974 check_aliases(strict, quiet)
975     int strict;
976     int quiet;
977 {
978     struct cmndspec *cs;
979     struct member *m, *binding;
980     struct privilege *priv;
981     struct userspec *us;
982     struct defaults *d;
983     int atype, error = 0;
984
985     alias_freelist = rbcreate(alias_compare);
986
987     /* Forward check. */
988     tq_foreach_fwd(&userspecs, us) {
989         tq_foreach_fwd(&us->users, m) {
990             if (m->type == ALIAS) {
991                 alias_seqno++;
992                 if (alias_find(m->name, USERALIAS) == NULL) {
993                     print_undefined(m->name, USERALIAS, strict, quiet);
994                     error++;
995                 }
996             }
997         }
998         tq_foreach_fwd(&us->privileges, priv) {
999             tq_foreach_fwd(&priv->hostlist, m) {
1000                 if (m->type == ALIAS) {
1001                     alias_seqno++;
1002                     if (alias_find(m->name, HOSTALIAS) == NULL) {
1003                         print_undefined(m->name, HOSTALIAS, strict, quiet);
1004                         error++;
1005                     }
1006                 }
1007             }
1008             tq_foreach_fwd(&priv->cmndlist, cs) {
1009                 tq_foreach_fwd(&cs->runasuserlist, m) {
1010                     if (m->type == ALIAS) {
1011                         alias_seqno++;
1012                         if (alias_find(m->name, RUNASALIAS) == NULL) {
1013                             print_undefined(m->name, RUNASALIAS, strict, quiet);
1014                             error++;
1015                         }
1016                     }
1017                 }
1018                 if ((m = cs->cmnd)->type == ALIAS) {
1019                     alias_seqno++;
1020                     if (alias_find(m->name, CMNDALIAS) == NULL) {
1021                         print_undefined(m->name, CMNDALIAS, strict, quiet);
1022                         error++;
1023                     }
1024                 }
1025             }
1026         }
1027     }
1028
1029     /* Reverse check (destructive) */
1030     tq_foreach_fwd(&userspecs, us) {
1031         tq_foreach_fwd(&us->users, m) {
1032             if (m->type == ALIAS) {
1033                 (void) alias_remove_recursive(m->name, USERALIAS);
1034             }
1035         }
1036         tq_foreach_fwd(&us->privileges, priv) {
1037             tq_foreach_fwd(&priv->hostlist, m) {
1038                 if (m->type == ALIAS)
1039                     (void) alias_remove_recursive(m->name, HOSTALIAS);
1040             }
1041             tq_foreach_fwd(&priv->cmndlist, cs) {
1042                 tq_foreach_fwd(&cs->runasuserlist, m) {
1043                     if (m->type == ALIAS)
1044                         (void) alias_remove_recursive(m->name, RUNASALIAS);
1045                 }
1046                 if ((m = cs->cmnd)->type == ALIAS)
1047                     (void) alias_remove_recursive(m->name, CMNDALIAS);
1048             }
1049         }
1050     }
1051     tq_foreach_fwd(&defaults, d) {
1052         switch (d->type) {
1053             case DEFAULTS_HOST:
1054                 atype = HOSTALIAS;
1055                 break;
1056             case DEFAULTS_USER:
1057                 atype = USERALIAS;
1058                 break;
1059             case DEFAULTS_RUNAS:
1060                 atype = RUNASALIAS;
1061                 break;
1062             case DEFAULTS_CMND:
1063                 atype = CMNDALIAS;
1064                 break;
1065             default:
1066                 continue; /* not an alias */
1067         }
1068         tq_foreach_fwd(&d->binding, binding) {
1069             for (m = binding; m != NULL; m = m->next) {
1070                 if (m->type == ALIAS)
1071                     (void) alias_remove_recursive(m->name, atype);
1072             }
1073         }
1074     }
1075     rbdestroy(alias_freelist, alias_free);
1076
1077     /* If all aliases were referenced we will have an empty tree. */
1078     if (no_aliases())
1079         return(0);
1080     if (!quiet) {
1081         alias_apply(print_unused, strict ? "Error" : "Warning");
1082     }
1083     return (strict ? 1 : 0);
1084 }
1085
1086 static void
1087 print_undefined(name, type, strict, quiet)
1088     char *name;
1089     int type;
1090     int strict;
1091     int quiet;
1092 {
1093     if (!quiet) {
1094         warningx("%s: %s_Alias `%s' referenced but not defined",
1095             strict ? "Error" : "Warning",
1096             type == HOSTALIAS ? "Host" : type == CMNDALIAS ? "Cmnd" :
1097             type == USERALIAS ? "User" : type == RUNASALIAS ? "Runas" :
1098             "Unknown", name);
1099     }
1100 }
1101
1102 static int
1103 print_unused(v1, v2)
1104     void *v1;
1105     void *v2;
1106 {
1107     struct alias *a = (struct alias *)v1;
1108     char *prefix = (char *)v2;
1109
1110     warningx("%s: unused %s_Alias %s", prefix,
1111         a->type == HOSTALIAS ? "Host" : a->type == CMNDALIAS ? "Cmnd" :
1112         a->type == USERALIAS ? "User" : a->type == RUNASALIAS ? "Runas" :
1113         "Unknown", a->name);
1114     return(0);
1115 }
1116
1117 /*
1118  * Unlink any sudoers temp files that remain.
1119  */
1120 void
1121 cleanup(gotsignal)
1122     int gotsignal;
1123 {
1124     struct sudoersfile *sp;
1125
1126     tq_foreach_fwd(&sudoerslist, sp) {
1127         if (sp->tpath != NULL)
1128             (void) unlink(sp->tpath);
1129     }
1130     if (!gotsignal) {
1131         sudo_endpwent();
1132         sudo_endgrent();
1133     }
1134 }
1135
1136 /*
1137  * Unlink sudoers temp files (if any) and exit.
1138  */
1139 static RETSIGTYPE
1140 quit(signo)
1141     int signo;
1142 {
1143     cleanup(signo);
1144 #define emsg     " exiting due to signal.\n"
1145     write(STDERR_FILENO, getprogname(), strlen(getprogname()));
1146     write(STDERR_FILENO, emsg, sizeof(emsg) - 1);
1147     _exit(signo);
1148 }
1149
1150 static void
1151 usage()
1152 {
1153     (void) fprintf(stderr, "usage: %s [-c] [-q] [-s] [-V] [-f sudoers]\n",
1154         getprogname());
1155     exit(1);
1156 }