the WHATSNEW file is gone
[debian/sudo] / visudo.c
index 0310d84d4cd28f511f91ce4bc146218e468eb376..ab8d58744e258899cf59ee97631d6f2ada935e6d 100644 (file)
--- a/visudo.c
+++ b/visudo.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (c) 1996, 1998-2005 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1996, 1998-2005, 2007-2010
+ *     Todd C. Miller <Todd.Miller@courtesan.com>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -33,6 +34,7 @@
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/stat.h>
+#include <sys/socket.h>
 #include <sys/time.h>
 #ifndef __TANDEM
 # include <sys/file.h>
 #endif /* STDC_HEADERS */
 #ifdef HAVE_STRING_H
 # include <string.h>
-#else
-# ifdef HAVE_STRINGS_H
-#  include <strings.h>
-# endif
 #endif /* HAVE_STRING_H */
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif /* HAVE_STRINGS_H */
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif /* HAVE_UNISTD_H */
-#ifdef HAVE_ERR_H
-# include <err.h>
-#else
-# include "emul/err.h"
-#endif /* HAVE_ERR_H */
 #include <ctype.h>
 #include <pwd.h>
-#if TIME_WITH_SYS_TIME
-# include <time.h>
-#endif
+#include <grp.h>
 #include <signal.h>
 #include <errno.h>
 #include <fcntl.h>
-#ifndef HAVE_TIMESPEC
-# include <emul/timespec.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#if TIME_WITH_SYS_TIME
+# include <time.h>
+#endif
+#ifdef __STDC__
+# include <stdarg.h>
+#else
+# include <varargs.h>
 #endif
 
 #include "sudo.h"
-#include "version.h"
-
-#ifndef lint
-__unused static const char rcsid[] = "$Sudo: visudo.c,v 1.166.2.10 2007/09/01 13:39:13 millert Exp $";
-#endif /* lint */
+#include "interfaces.h"
+#include "parse.h"
+#include "redblack.h"
+#include <gram.h>
 
 struct sudoersfile {
+    struct sudoersfile *prev, *next;
     char *path;
     char *tpath;
     int fd;
-    off_t orig_size;
-    struct timespec orig_mtim;
+    int modified;
+    int doedit;
 };
+TQ_DECLARE(sudoersfile);
 
 /*
  * Function prototypes
  */
-static void usage              __P((void)) __attribute__((__noreturn__));
-static char whatnow            __P((void));
-static RETSIGTYPE Exit         __P((int));
-static void edit_sudoers       __P((struct sudoersfile *, char *, char *, int));
-static void visudo             __P((struct sudoersfile *, char *, char *));
-static void setup_signals      __P((void));
-static void install_sudoers    __P((struct sudoersfile *, int));
-static int check_syntax                __P(());
-static int run_command         __P((char *, char **));
+static RETSIGTYPE quit         __P((int));
 static char *get_args          __P((char *));
 static char *get_editor                __P((char **));
-static FILE *open_sudoers      __P((struct sudoersfile *));
-
-int command_matches            __P((char *, char *));
-int addr_matches               __P((char *));
-int hostname_matches           __P((char *, char *, char *));
-int netgr_matches              __P((char *, char *, char *, char *));
-int usergr_matches             __P((char *, char *, struct passwd *));
-int userpw_matches             __P((char *, char *, struct passwd *));
-void init_parser               __P((void));
-void yyerror                   __P((char *));
-void yyrestart                 __P((FILE *));
+static void get_hostname       __P((void));
+static char whatnow            __P((void));
+static int check_aliases       __P((int, int));
+static int check_syntax                __P((char *, int, int));
+static int edit_sudoers                __P((struct sudoersfile *, char *, char *, int));
+static int install_sudoers     __P((struct sudoersfile *, int));
+static int print_unused                __P((void *, void *));
+static int reparse_sudoers     __P((char *, char *, int, int));
+static int run_command         __P((char *, char **));
+static void print_selfref      __P((char *, int, int, int));
+static void print_undefined    __P((char *, int, int, int));
+static void setup_signals      __P((void));
+static void usage              __P((void)) __attribute__((__noreturn__));
+
+extern void yyerror            __P((const char *));
+extern void yyrestart          __P((FILE *));
 
 /*
  * External globals exported by the parser
  */
+extern struct rbtree *aliases;
 extern FILE *yyin;
-extern int errorlineno;
-extern int pedantic;
-extern int quiet;
-
+extern char *sudoers, *errorfile;
+extern int errorlineno, parse_error;
 /* For getopt(3) */
 extern char *optarg;
 extern int optind;
@@ -130,26 +129,27 @@ extern int optind;
 /*
  * Globals
  */
+int Argc;
 char **Argv;
+int num_interfaces;
+struct interface *interfaces;
 struct sudo_user sudo_user;
-int Argc, parse_error = FALSE;
-static struct sudoersfile sudoers;
+struct passwd *list_pw;
+static struct sudoersfile_list sudoerslist;
+static struct rbtree *alias_freelist;
 
 int
 main(argc, argv)
     int argc;
     char **argv;
 {
-    char *args, *editor;
-    int ch, checkonly, n, oldperms;
-
-    /* Initialize sudoers struct. */
-    sudoers.path = _PATH_SUDOERS;
-    sudoers.tpath = _PATH_SUDOERS_TMP;
-    sudoers.fd = -1;
-
-    /* Warn about aliases that are used before being defined. */
-    pedantic = 1;
+    struct sudoersfile *sp;
+    char *args, *editor, *sudoers_path;
+    int ch, checkonly, quiet, strict, oldperms;
+#if defined(SUDO_DEVEL) && defined(__OpenBSD__)
+    extern char *malloc_options;
+    malloc_options = "AFGJPR";
+#endif
 
     Argv = argv;
     if ((Argc = argc) < 1)
@@ -158,22 +158,22 @@ main(argc, argv)
     /*
      * Arg handling.
      */
-    checkonly = oldperms = FALSE;
+    checkonly = oldperms = quiet = strict = FALSE;
+    sudoers_path = _PATH_SUDOERS;
     while ((ch = getopt(argc, argv, "Vcf:sq")) != -1) {
        switch (ch) {
            case 'V':
-               (void) printf("%s version %s\n", getprogname(), version);
+               (void) printf("%s version %s\n", getprogname(), PACKAGE_VERSION);
                exit(0);
            case 'c':
                checkonly++;            /* check mode */
                break;
-           case 'f':                   /* sudoers file path */
-               sudoers.path = optarg;
-               easprintf(&sudoers.tpath, "%s.tmp", optarg);
+           case 'f':
+               sudoers_path = optarg;  /* sudoers file path */
                oldperms = TRUE;
                break;
            case 's':
-               pedantic++;             /* strict mode */
+               strict++;               /* strict mode */
                break;
            case 'q':
                quiet++;                /* quiet mode */
@@ -187,59 +187,118 @@ main(argc, argv)
     if (argc)
        usage();
 
+    sudo_setpwent();
+    sudo_setgrent();
+
     /* Mock up a fake sudo_user struct. */
-    user_host = user_shost = user_cmnd = "";
-    if ((sudo_user.pw = getpwuid(getuid())) == NULL)
-       errx(1, "you don't exist in the passwd database");
+    user_cmnd = "";
+    if ((sudo_user.pw = sudo_getpwuid(getuid())) == NULL)
+       errorx(1, "you don't exist in the passwd database");
+    get_hostname();
 
     /* Setup defaults data structures. */
     init_defaults();
 
     if (checkonly)
-       exit(check_syntax());
+       exit(check_syntax(sudoers_path, quiet, strict));
 
     /*
-     * Open and parse the existing sudoers file(s) in quiet mode to highlight
-     * any existing errors and to pull in editor and env_editor conf values.
-     */  
-    if ((yyin = open_sudoers(&sudoers)) == NULL)
-       err(1, "%s", sudoers.path);
-    n = quiet;
-    quiet = 1;
-    init_parser();
+     * Parse the existing sudoers file(s) in quiet mode to highlight any
+     * existing errors and to pull in editor and env_editor conf values.
+     */
+    if ((yyin = open_sudoers(sudoers_path, TRUE, NULL)) == NULL) {
+       error(1, "%s", sudoers_path);
+    }
+    init_parser(sudoers_path, 0);
     yyparse();
-    parse_error = FALSE;
-    quiet = n;
+    (void) update_defaults(SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER);
 
-    /* Edit sudoers, check for parse errors and re-edit on failure. */
     editor = get_editor(&args);
-    visudo(&sudoers, editor, args);
 
-    /* Install the new sudoers file. */
-    install_sudoers(&sudoers, oldperms);
+    /* Install signal handlers to clean up temp files if we are killed. */
+    setup_signals();
+
+    /* Edit the sudoers file(s) */
+    tq_foreach_fwd(&sudoerslist, sp) {
+       if (!sp->doedit)
+           continue;
+       if (sp != tq_first(&sudoerslist)) {
+           printf("press return to edit %s: ", sp->path);
+           while ((ch = getchar()) != EOF && ch != '\n')
+                   continue;
+       }
+       edit_sudoers(sp, editor, args, -1);
+    }
+
+    /* Check edited files for a parse error and re-edit any that fail. */
+    reparse_sudoers(editor, args, strict, quiet);
+
+    /* Install the sudoers temp files. */
+    tq_foreach_fwd(&sudoerslist, sp) {
+       if (!sp->modified)
+           (void) unlink(sp->tpath);
+       else
+           (void) install_sudoers(sp, oldperms);
+    }
 
     exit(0);
 }
 
 /*
- * Edit the sudoers file.
+ * Edit each sudoers file.
  * Returns TRUE on success, else FALSE.
  */
-static void
+static int
 edit_sudoers(sp, editor, args, lineno)
     struct sudoersfile *sp;
     char *editor, *args;
     int lineno;
 {
+    int tfd;                           /* sudoers temp file descriptor */
+    int modified;                      /* was the file modified? */
     int ac;                            /* argument count */
     char **av;                         /* argument vector for run_command */
     char *cp;                          /* scratch char pointer */
+    char buf[PATH_MAX*2];              /* buffer used for copying files */
     char linestr[64];                  /* string version of lineno */
-    struct timespec ts1, ts2;          /* time before and after edit */
+    struct timeval tv, tv1, tv2;       /* time before and after edit */
+    struct timeval orig_mtim;          /* starting mtime of sudoers file */
+    off_t orig_size;                   /* starting size of sudoers file */
+    ssize_t nread;                     /* number of bytes read */
     struct stat sb;                    /* stat buffer */
 
-    /* Make timestamp on temp file match original. */
-    (void) touch(-1, sp->tpath, &sp->orig_mtim);
+#ifdef HAVE_FSTAT
+    if (fstat(sp->fd, &sb) == -1)
+#else
+    if (stat(sp->path, &sb) == -1)
+#endif
+       error(1, "can't stat %s", sp->path);
+    orig_size = sb.st_size;
+    mtim_get(&sb, &orig_mtim);
+
+    /* Create the temp file if needed and set timestamp. */
+    if (sp->tpath == NULL) {
+       easprintf(&sp->tpath, "%s.tmp", sp->path);
+       tfd = open(sp->tpath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+       if (tfd < 0)
+           error(1, "%s", sp->tpath);
+
+       /* Copy sp->path -> sp->tpath and reset the mtime. */
+       if (orig_size != 0) {
+           (void) lseek(sp->fd, (off_t)0, SEEK_SET);
+           while ((nread = read(sp->fd, buf, sizeof(buf))) > 0)
+               if (write(tfd, buf, nread) != nread)
+                   error(1, "write error");
+
+           /* Add missing newline at EOF if needed. */
+           if (nread > 0 && buf[nread - 1] != '\n') {
+               buf[0] = '\n';
+               write(tfd, buf, 1);
+           }
+       }
+       (void) close(tfd);
+    }
+    (void) touch(-1, sp->tpath, &orig_mtim);
 
     /* Find the length of the argument vector */
     ac = 3 + (lineno > 0);
@@ -281,89 +340,96 @@ edit_sudoers(sp, editor, args, lineno)
      *  XPG4 specifies that vi's exit value is a function of the
      *  number of errors during editing (?!?!).
      */
-    gettime(&ts1);
+    gettime(&tv1);
     if (run_command(editor, av) != -1) {
-       gettime(&ts2);
+       gettime(&tv2);
        /*
         * Sanity checks.
         */
        if (stat(sp->tpath, &sb) < 0) {
-           warnx("cannot stat temporary file (%s), %s unchanged",
+           warningx("cannot stat temporary file (%s), %s unchanged",
                sp->tpath, sp->path);
-           Exit(-1);
+           return(FALSE);
        }
-       if (sb.st_size == 0) {
-           warnx("zero length temporary file (%s), %s unchanged",
+       if (sb.st_size == 0 && orig_size != 0) {
+           warningx("zero length temporary file (%s), %s unchanged",
                sp->tpath, sp->path);
-           Exit(-1);
+           sp->modified = TRUE;
+           return(FALSE);
        }
     } else {
-       warnx("editor (%s) failed, %s unchanged", editor, sp->path);
-       Exit(-1);
+       warningx("editor (%s) failed, %s unchanged", editor, sp->path);
+       return(FALSE);
     }
 
-    /* Check to see if the user changed the file. */
-    if (sp->orig_size == sb.st_size &&
-       sp->orig_mtim.tv_sec == mtim_getsec(sb) &&
-       sp->orig_mtim.tv_nsec == mtim_getnsec(sb)) {
+    /* Set modified bit if use changed the file. */
+    modified = TRUE;
+    mtim_get(&sb, &tv);
+    if (orig_size == sb.st_size && timevalcmp(&orig_mtim, &tv, ==)) {
        /*
         * If mtime and size match but the user spent no measurable
         * time in the editor we can't tell if the file was changed.
         */
-#ifdef HAVE_TIMESPECSUB2
-       timespecsub(&ts1, &ts2);
-#else
-       timespecsub(&ts1, &ts2, &ts2);
-#endif
-       if (timespecisset(&ts2)) {
-           warnx("%s unchanged", sp->tpath);
-           Exit(0);
-       }
+       timevalsub(&tv1, &tv2);
+       if (timevalisset(&tv2))
+           modified = FALSE;
     }
+
+    /*
+     * If modified in this edit session, mark as modified.
+     */
+    if (modified)
+       sp->modified = modified;
+    else
+       warningx("%s unchanged", sp->tpath);
+
+    return(TRUE);
 }
 
 /*
  * Parse sudoers after editing and re-edit any ones that caused a parse error.
  * Returns TRUE on success, else FALSE.
  */
-static void
-visudo(sp, editor, args)
-    struct sudoersfile *sp;
+static int
+reparse_sudoers(editor, args, strict, quiet)
     char *editor, *args;
+    int strict, quiet;
 {
+    struct sudoersfile *sp, *last;
+    FILE *fp;
     int ch;
 
     /*
-     * Parse the edited sudoers file and do sanity checking
+     * Parse the edited sudoers files and do sanity checking
      */
     do {
-       edit_sudoers(sp, editor, args, errorlineno);
-
-       yyin = fopen(sp->tpath, "r+");
-       if (yyin == NULL) {
-           warnx("can't re-open temporary file (%s), %s unchanged.",
+       sp = tq_first(&sudoerslist);
+       last = tq_last(&sudoerslist);
+       fp = fopen(sp->tpath, "r+");
+       if (fp == NULL)
+           errorx(1, "can't re-open temporary file (%s), %s unchanged.",
                sp->tpath, sp->path);
-           Exit(-1);
-       }
-
-       /* Add missing newline at EOF if needed. */
-       if (fseek(yyin, -1, SEEK_END) == 0 && (ch = fgetc(yyin)) != '\n')
-           fputc('\n', yyin);
-       rewind(yyin);
 
        /* Clean slate for each parse */
-       user_runas = NULL;
        init_defaults();
-       init_parser();
+       init_parser(sp->path, quiet);
 
        /* Parse the sudoers temp file */
-       yyrestart(yyin);
-       if (yyparse() && parse_error != TRUE) {
-           warnx("unabled to parse temporary file (%s), unknown error",
+       yyrestart(fp);
+       if (yyparse() && !parse_error) {
+           warningx("unabled to parse temporary file (%s), unknown error",
                sp->tpath);
            parse_error = TRUE;
+           errorfile = sp->path;
        }
        fclose(yyin);
+       if (!parse_error) {
+           if (!update_defaults(SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER) ||
+               check_aliases(strict, quiet) != 0) {
+               parse_error = TRUE;
+               errorfile = sp->path;
+           }
+       }
 
        /*
         * Got an error, prompt the user for what to do now
@@ -372,18 +438,40 @@ visudo(sp, editor, args)
            switch (whatnow()) {
                case 'Q' :      parse_error = FALSE;    /* ignore parse error */
                                break;
-               case 'x' :      Exit(0);
+               case 'x' :      cleanup(0);
+                               exit(0);
                                break;
            }
        }
+       if (parse_error) {
+           /* Edit file with the parse error */
+           tq_foreach_fwd(&sudoerslist, sp) {
+               if (errorfile == NULL || strcmp(sp->path, errorfile) == 0) {
+                   edit_sudoers(sp, editor, args, errorlineno);
+                   break;
+               }
+           }
+           if (sp == NULL)
+               errorx(1, "internal error, can't find %s in list!", sudoers);
+       }
+
+       /* If any new #include directives were added, edit them too. */
+       for (sp = last->next; sp != NULL; sp = sp->next) {
+           printf("press return to edit %s: ", sp->path);
+           while ((ch = getchar()) != EOF && ch != '\n')
+                   continue;
+           edit_sudoers(sp, editor, args, errorlineno);
+       }
     } while (parse_error);
+
+    return(TRUE);
 }
 
 /*
  * Set the owner and mode on a sudoers temp file and
  * move it into place.  Returns TRUE on success, else FALSE.
  */
-static void
+static int
 install_sudoers(sp, oldperms)
     struct sudoersfile *sp;
     int oldperms;
@@ -401,18 +489,19 @@ install_sudoers(sp, oldperms)
 #else
        if (stat(sp->path, &sb) == -1)
 #endif
-           err(1, "can't stat %s", sp->path);
+           error(1, "can't stat %s", sp->path);
        (void) chown(sp->tpath, sb.st_uid, sb.st_gid);
        (void) chmod(sp->tpath, sb.st_mode & 0777);
     } else {
        if (chown(sp->tpath, SUDOERS_UID, SUDOERS_GID) != 0) {
-           warn("unable to set (uid, gid) of %s to (%d, %d)",
+           warning("unable to set (uid, gid) of %s to (%d, %d)",
                sp->tpath, SUDOERS_UID, SUDOERS_GID);
-           Exit(-1);
+           return(FALSE);
        }
        if (chmod(sp->tpath, SUDOERS_MODE) != 0) {
-           warn("unable to change mode of %s to 0%o", sp->tpath, SUDOERS_MODE);
-           Exit(-1);
+           warning("unable to change mode of %s to 0%o", sp->tpath,
+               SUDOERS_MODE);
+           return(FALSE);
        }
     }
 
@@ -421,10 +510,13 @@ install_sudoers(sp, oldperms)
      * rename(2)'d to sp->path.  If the rename(2) fails we try using
      * mv(1) in case sp->tpath and sp->path are on different file systems.
      */
-    if (rename(sp->tpath, sp->path) != 0) {
+    if (rename(sp->tpath, sp->path) == 0) {
+       efree(sp->tpath);
+       sp->tpath = NULL;
+    } else {
        if (errno == EXDEV) {
            char *av[4];
-           warnx("%s and %s not on the same file system, using mv to rename",
+           warningx("%s and %s not on the same file system, using mv to rename",
              sp->tpath, sp->path);
 
            /* Build up argument vector for the command */
@@ -438,98 +530,64 @@ install_sudoers(sp, oldperms)
 
            /* And run it... */
            if (run_command(_PATH_MV, av)) {
-               warnx("command failed: '%s %s %s', %s unchanged",
+               warningx("command failed: '%s %s %s', %s unchanged",
                    _PATH_MV, sp->tpath, sp->path, sp->path);
-               Exit(-1);
+               (void) unlink(sp->tpath);
+               efree(sp->tpath);
+               sp->tpath = NULL;
+               return(FALSE);
            }
+           efree(sp->tpath);
+           sp->tpath = NULL;
        } else {
-           warn("error renaming %s, %s unchanged", sp->tpath, sp->path);
-           Exit(-1);
+           warning("error renaming %s, %s unchanged", sp->tpath, sp->path);
+           (void) unlink(sp->tpath);
+           return(FALSE);
        }
     }
-}
-
-/*
- * Dummy *_matches routines.
- * These exist to allow us to use the same parser as sudo(8).
- */
-int
-command_matches(path, sudoers_args)
-    char *path;
-    char *sudoers_args;
-{
-    return(TRUE);
-}
-
-int
-addr_matches(n)
-    char *n;
-{
     return(TRUE);
 }
 
-int
-hostname_matches(s, l, p)
-    char *s, *l, *p;
-{
-    return(TRUE);
-}
-
-int
-usergr_matches(g, u, pw)
-    char *g, *u;
-    struct passwd *pw;
+/* STUB */
+void
+set_fqdn()
 {
-    return(TRUE);
+    return;
 }
 
-int
-userpw_matches(s, u, pw)
-    char *s, *u;
-    struct passwd *pw;
+/* STUB */
+void
+init_envtables()
 {
-    return(TRUE);
+    return;
 }
 
+/* STUB */
 int
-netgr_matches(n, h, sh, u)
-    char *n, *h, *sh, *u;
+user_is_exempt()
 {
-    return(TRUE);
+    return(FALSE);
 }
 
+/* STUB */
 void
-set_fqdn()
+sudo_setspent()
 {
     return;
 }
 
-int
-set_runaspw(user)
-    char *user;
-{
-    extern int sudolineno, used_runas;
-
-    if (used_runas) {
-       (void) fprintf(stderr,
-           "%s: runas_default set after old value is in use near line %d\n",
-           pedantic > 1 ? "Error" : "Warning", sudolineno);
-       if (pedantic > 1)
-           yyerror(NULL);
-    }
-    return(TRUE);
-}
-
-int
-user_is_exempt()
+/* STUB */
+void
+sudo_endspent()
 {
-    return(TRUE);
+    return;
 }
 
-void
-init_envtables()
+char *
+sudo_getepw(pw)
+    const struct passwd *pw;
 {
-    return;
+    return (pw->pw_passwd);
 }
 
 /*
@@ -575,9 +633,10 @@ setup_signals()
        /*
         * Setup signal handlers to cleanup nicely.
         */
+       zero_bytes(&sa, sizeof(sa));
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = SA_RESTART;
-       sa.sa_handler = Exit;
+       sa.sa_handler = quit;
        (void) sigaction(SIGTERM, &sa, NULL);
        (void) sigaction(SIGHUP, &sa, NULL);
        (void) sigaction(SIGINT, &sa, NULL);
@@ -590,122 +649,148 @@ run_command(path, argv)
     char **argv;
 {
     int status;
-    pid_t pid;
-    sigset_t set, oset;
-
-    (void) sigemptyset(&set);
-    (void) sigaddset(&set, SIGCHLD);
-    (void) sigprocmask(SIG_BLOCK, &set, &oset);
+    pid_t pid, rv;
 
     switch (pid = fork()) {
        case -1:
-           warn("unable to run %s", path);
-           Exit(-1);
+           error(1, "unable to run %s", path);
            break;      /* NOTREACHED */
        case 0:
-           (void) sigprocmask(SIG_SETMASK, &oset, NULL);
-           endpwent();
+           sudo_endpwent();
+           sudo_endgrent();
            closefrom(STDERR_FILENO + 1);
            execv(path, argv);
-           warn("unable to run %s", path);
+           warning("unable to run %s", path);
            _exit(127);
            break;      /* NOTREACHED */
     }
 
+    do {
 #ifdef sudo_waitpid
-    pid = sudo_waitpid(pid, &status, 0);
+       rv = sudo_waitpid(pid, &status, 0);
 #else
-    pid = wait(&status);
+       rv = wait(&status);
 #endif
+    } while (rv == -1 && errno == EINTR);
 
-    (void) sigprocmask(SIG_SETMASK, &oset, NULL);
-
-    if (pid == -1 || !WIFEXITED(status))
+    if (rv == -1 || !WIFEXITED(status))
        return(-1);
     return(WEXITSTATUS(status));
 }
 
 static int
-check_syntax()
+check_syntax(sudoers_path, quiet, strict)
+    char *sudoers_path;
+    int quiet;
+    int strict;
 {
+    struct stat sb;
+    int error;
 
-    if ((yyin = fopen(sudoers.path, "r")) == NULL) {
+    if ((yyin = fopen(sudoers_path, "r")) == NULL) {
        if (!quiet)
-           warn("unable to open %s", sudoers.path);
+           warning("unable to open %s", sudoers_path);
        exit(1);
     }
-    init_parser();
-    if (yyparse() && parse_error != TRUE) {
+    init_parser(sudoers_path, quiet);
+    if (yyparse() && !parse_error) {
        if (!quiet)
-           warnx("failed to parse %s file, unknown error", sudoers.path);
+           warningx("failed to parse %s file, unknown error", sudoers_path);
        parse_error = TRUE;
+       errorfile = sudoers_path;
     }
-    if (!quiet){
-       if (parse_error)
-           (void) printf("parse error in %s near line %d\n", sudoers.path,
-               errorlineno);
-       else
-           (void) printf("%s file parsed OK\n", sudoers.path);
+    if (!parse_error && check_aliases(strict, quiet) != 0) {
+       parse_error = TRUE;
+       errorfile = sudoers_path;
     }
-
-    return(parse_error == TRUE);
-}
-
-static FILE *
-open_sudoers(sp)
-    struct sudoersfile *sp;
-{
-    struct stat sb;
-    ssize_t nread;
-    FILE *fp;
-    char buf[PATH_MAX*2];
-    int tfd;
-
-    /* Open and lock sudoers. */
-    sp->fd = open(sp->path, O_RDWR | O_CREAT, SUDOERS_MODE);
-    if (sp->fd == -1)
-       err(1, "%s", sp->path);
-    if (!lock_file(sp->fd, SUDO_TLOCK))
-       errx(1, "%s busy, try again later", sp->path);
-    if ((fp = fdopen(sp->fd, "r")) == NULL)
-       err(1, "%s", sp->path);
-
-    /* Stash sudoers size and mtime. */
+    error = parse_error;
+    if (!quiet) {
+       if (parse_error) {
+           if (errorlineno != -1)
+               (void) printf("parse error in %s near line %d\n", errorfile,
+                   errorlineno);
+           else
+               (void) printf("parse error in %s\n", errorfile);
+       } else {
+           (void) printf("%s: parsed OK\n", sudoers_path);
+       }
+    }
+    /* Check mode and owner in strict mode. */
 #ifdef HAVE_FSTAT
-    if (fstat(sp->fd, &sb) == -1)
+    if (strict && fstat(fileno(yyin), &sb) == 0)
 #else
-    if (stat(sp->path, &sb) == -1)
+    if (strict && stat(sudoers_path, &sb) == 0)
 #endif
-       err(1, "can't stat %s", sp->path);
-    sp->orig_size = sb.st_size;
-    sp->orig_mtim.tv_sec = mtim_getsec(sb);
-    sp->orig_mtim.tv_nsec = mtim_getnsec(sb);
-
-    /* Create the temp file. */
-    tfd = open(sp->tpath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
-    if (tfd < 0)
-       err(1, "%s", sp->tpath);
+    {
+       if (sb.st_uid != SUDOERS_UID || sb.st_gid != SUDOERS_GID) {
+           error = TRUE;
+           if (!quiet) {
+               fprintf(stderr, "%s: wrong owner (uid, gid) should be (%d, %d)\n",
+                   sudoers_path, SUDOERS_UID, SUDOERS_GID);
+               }
+       }
+       if ((sb.st_mode & 07777) != SUDOERS_MODE) {
+           error = TRUE;
+           if (!quiet) {
+               fprintf(stderr, "%s: bad permissions, should be mode 0%o\n",
+                   sudoers_path, SUDOERS_MODE);
+           }
+       }
+    }
 
-    /* Install signal handlers to clean up temp file if we are killed. */
-    setup_signals();
+    return(error);
+}
 
-    /* Copy sp->path -> sp->tpath. */
-    if (sp->orig_size != 0) {
-       while ((nread = read(sp->fd, buf, sizeof(buf))) > 0)
-           if (write(tfd, buf, nread) != nread) {
-               warn("write error");
-               Exit(-1);
-           }
+/*
+ * Used to open (and lock) the initial sudoers file and to also open
+ * any subsequent files #included via a callback from the parser.
+ */
+FILE *
+open_sudoers(path, doedit, keepopen)
+    const char *path;
+    int doedit;
+    int *keepopen;
+{
+    struct sudoersfile *entry;
+    FILE *fp;
 
-       /* Add missing newline at EOF if needed. */
-       if (nread > 0 && buf[nread - 1] != '\n') {
-           buf[0] = '\n';
-           write(tfd, buf, 1);
+    /* Check for existing entry */
+    tq_foreach_fwd(&sudoerslist, entry) {
+       if (strcmp(path, entry->path) == 0)
+           break;
+    }
+    if (entry == NULL) {
+       entry = emalloc(sizeof(*entry));
+       entry->path = estrdup(path);
+       entry->modified = 0;
+       entry->prev = entry;
+       entry->next = NULL;
+       entry->fd = open(entry->path, O_RDWR | O_CREAT, SUDOERS_MODE);
+       entry->tpath = NULL;
+       entry->doedit = doedit;
+       if (entry->fd == -1) {
+           warning("%s", entry->path);
+           efree(entry);
+           return(NULL);
+       }
+       if (!lock_file(entry->fd, SUDO_TLOCK))
+           errorx(1, "%s busy, try again later", entry->path);
+       if ((fp = fdopen(entry->fd, "r")) == NULL)
+           error(1, "%s", entry->path);
+       tq_append(&sudoerslist, entry);
+    } else {
+       /* Already exists, open .tmp version if there is one. */
+       if (entry->tpath != NULL) {
+           if ((fp = fopen(entry->tpath, "r")) == NULL)
+               error(1, "%s", entry->tpath);
+       } else {
+           if ((fp = fdopen(entry->fd, "r")) == NULL)
+               error(1, "%s", entry->path);
+           rewind(fp);
        }
     }
-    (void) close(tfd);
-    rewind(fp);
-
+    if (keepopen != NULL)
+       *keepopen = TRUE;
     return(fp);
 }
 
@@ -728,13 +813,12 @@ get_editor(args)
        UserEditor = NULL;
     else if (UserEditor) {
        UserEditorArgs = get_args(UserEditor);
-       if (find_path(UserEditor, &Editor, NULL, getenv("PATH")) == FOUND) {
+       if (find_path(UserEditor, &Editor, NULL, getenv("PATH"), 0) == FOUND) {
            UserEditor = Editor;
        } else {
            if (def_env_editor) {
                /* If we are honoring $EDITOR this is a fatal error. */
-               warnx("specified editor (%s) doesn't exist!", UserEditor);
-               Exit(-1);
+               errorx(1, "specified editor (%s) doesn't exist!", UserEditor);
            } else {
                /* Otherwise, just ignore $EDITOR. */
                UserEditor = NULL;
@@ -757,8 +841,7 @@ get_editor(args)
 
        if (stat(UserEditor, &user_editor_sb) != 0) {
            /* Should never happen since we already checked above. */
-           warn("unable to stat editor (%s)", UserEditor);
-           Exit(-1);
+           error(1, "unable to stat editor (%s)", UserEditor);
        }
        EditorPath = estrdup(def_editor);
        Editor = strtok(EditorPath, ":");
@@ -805,10 +888,8 @@ get_editor(args)
        } while ((Editor = strtok(NULL, ":")));
 
        /* Bleah, none of the editors existed! */
-       if (Editor == NULL || *Editor == '\0') {
-           warnx("no editor found (editor path = %s)", def_editor);
-           Exit(-1);
-       }
+       if (Editor == NULL || *Editor == '\0')
+           errorx(1, "no editor found (editor path = %s)", def_editor);
     }
     *args = EditorArgs;
     return(Editor);
@@ -835,24 +916,261 @@ get_args(cmnd)
 }
 
 /*
- * Unlink the sudoers temp file (if it exists) and exit.
- * Used in place of a normal exit() and as a signal handler.
- * A positive parameter indicates we were called as a signal handler.
+ * Look up the hostname and set user_host and user_shost.
  */
-static RETSIGTYPE
-Exit(sig)
-    int sig;
+static void
+get_hostname()
 {
-#define        emsg     " exiting due to signal.\n"
+    char *p, thost[MAXHOSTNAMELEN + 1];
 
-    (void) unlink(sudoers.tpath);
+    if (gethostname(thost, sizeof(thost)) != 0) {
+       user_host = user_shost = "localhost";
+       return;
+    }
+    thost[sizeof(thost) - 1] = '\0';
+    user_host = estrdup(thost);
 
-    if (sig > 0) {
-       write(STDERR_FILENO, getprogname(), strlen(getprogname()));
-       write(STDERR_FILENO, emsg, sizeof(emsg) - 1);
-       _exit(sig);
+    if ((p = strchr(user_host, '.'))) {
+       *p = '\0';
+       user_shost = estrdup(user_host);
+       *p = '.';
+    } else {
+       user_shost = user_host;
+    }
+}
+
+static int
+alias_remove_recursive(name, type, strict, quiet)
+    char *name;
+    int type;
+    int strict;
+    int quiet;
+{
+    struct member *m;
+    struct alias *a;
+    int error = 0;
+
+    if ((a = alias_find(name, type)) != NULL) {
+       tq_foreach_fwd(&a->members, m) {
+           if (m->type == ALIAS) {
+               if (strcmp(name, m->name) == 0) {
+                   print_selfref(m->name, type, strict, quiet);
+                   error = 1;
+               } else {
+                   if (!alias_remove_recursive(m->name, type, strict, quiet))
+                       error = 1;
+               }
+           }
+       }
     }
-    exit(-sig);
+    alias_seqno++;
+    a = alias_remove(name, type);
+    if (a)
+       rbinsert(alias_freelist, a);
+    return(error);
+}
+
+/*
+ * Iterate through the sudoers datastructures looking for undefined
+ * aliases or unused aliases.
+ */
+static int
+check_aliases(strict, quiet)
+    int strict;
+    int quiet;
+{
+    struct cmndspec *cs;
+    struct member *m, *binding;
+    struct privilege *priv;
+    struct userspec *us;
+    struct defaults *d;
+    int atype, error = 0;
+
+    alias_freelist = rbcreate(alias_compare);
+
+    /* Forward check. */
+    tq_foreach_fwd(&userspecs, us) {
+       tq_foreach_fwd(&us->users, m) {
+           if (m->type == ALIAS) {
+               alias_seqno++;
+               if (alias_find(m->name, USERALIAS) == NULL) {
+                   print_undefined(m->name, USERALIAS, strict, quiet);
+                   error++;
+               }
+           }
+       }
+       tq_foreach_fwd(&us->privileges, priv) {
+           tq_foreach_fwd(&priv->hostlist, m) {
+               if (m->type == ALIAS) {
+                   alias_seqno++;
+                   if (alias_find(m->name, HOSTALIAS) == NULL) {
+                       print_undefined(m->name, HOSTALIAS, strict, quiet);
+                       error++;
+                   }
+               }
+           }
+           tq_foreach_fwd(&priv->cmndlist, cs) {
+               tq_foreach_fwd(&cs->runasuserlist, m) {
+                   if (m->type == ALIAS) {
+                       alias_seqno++;
+                       if (alias_find(m->name, RUNASALIAS) == NULL) {
+                           print_undefined(m->name, RUNASALIAS, strict, quiet);
+                           error++;
+                       }
+                   }
+               }
+               if ((m = cs->cmnd)->type == ALIAS) {
+                   alias_seqno++;
+                   if (alias_find(m->name, CMNDALIAS) == NULL) {
+                       print_undefined(m->name, CMNDALIAS, strict, quiet);
+                       error++;
+                   }
+               }
+           }
+       }
+    }
+
+    /* Reverse check (destructive) */
+    tq_foreach_fwd(&userspecs, us) {
+       tq_foreach_fwd(&us->users, m) {
+           if (m->type == ALIAS) {
+               if (!alias_remove_recursive(m->name, USERALIAS, strict, quiet))
+                   error++;
+           }
+       }
+       tq_foreach_fwd(&us->privileges, priv) {
+           tq_foreach_fwd(&priv->hostlist, m) {
+               if (m->type == ALIAS)
+                   if (!alias_remove_recursive(m->name, HOSTALIAS, strict, 
+                       quiet))
+                       error++;
+           }
+           tq_foreach_fwd(&priv->cmndlist, cs) {
+               tq_foreach_fwd(&cs->runasuserlist, m) {
+                   if (m->type == ALIAS)
+                       if (!alias_remove_recursive(m->name, RUNASALIAS,
+                           strict, quiet))
+                           error++;
+               }
+               if ((m = cs->cmnd)->type == ALIAS)
+                   if (!alias_remove_recursive(m->name, CMNDALIAS, strict,
+                       quiet))
+                       error++;
+           }
+       }
+    }
+    tq_foreach_fwd(&defaults, d) {
+       switch (d->type) {
+           case DEFAULTS_HOST:
+               atype = HOSTALIAS;
+               break;
+           case DEFAULTS_USER:
+               atype = USERALIAS;
+               break;
+           case DEFAULTS_RUNAS:
+               atype = RUNASALIAS;
+               break;
+           case DEFAULTS_CMND:
+               atype = CMNDALIAS;
+               break;
+           default:
+               continue; /* not an alias */
+       }
+       tq_foreach_fwd(&d->binding, binding) {
+           for (m = binding; m != NULL; m = m->next) {
+               if (m->type == ALIAS)
+                   if (!alias_remove_recursive(m->name, atype, strict, quiet))
+                       error++;
+           }
+       }
+    }
+    rbdestroy(alias_freelist, alias_free);
+
+    /* If all aliases were referenced we will have an empty tree. */
+    if (!no_aliases() && !quiet)
+       alias_apply(print_unused, strict ? "Error" : "Warning");
+
+    return (strict ? error : 0);
+}
+
+static void
+print_undefined(name, type, strict, quiet)
+    char *name;
+    int type;
+    int strict;
+    int quiet;
+{
+    if (!quiet) {
+       warningx("%s: %s_Alias `%s' referenced but not defined",
+           strict ? "Error" : "Warning",
+           type == HOSTALIAS ? "Host" : type == CMNDALIAS ? "Cmnd" :
+           type == USERALIAS ? "User" : type == RUNASALIAS ? "Runas" :
+           "Unknown", name);
+    }
+}
+
+static void
+print_selfref(name, type, strict, quiet)
+    char *name;
+    int type;
+    int strict;
+    int quiet;
+{
+    if (!quiet) {
+       warningx("%s: %s_Alias `%s' references self",
+           strict ? "Error" : "Warning",
+           type == HOSTALIAS ? "Host" : type == CMNDALIAS ? "Cmnd" :
+           type == USERALIAS ? "User" : type == RUNASALIAS ? "Runas" :
+           "Unknown", name);
+    }
+}
+
+static int
+print_unused(v1, v2)
+    void *v1;
+    void *v2;
+{
+    struct alias *a = (struct alias *)v1;
+    char *prefix = (char *)v2;
+
+    warningx("%s: unused %s_Alias %s", prefix,
+       a->type == HOSTALIAS ? "Host" : a->type == CMNDALIAS ? "Cmnd" :
+       a->type == USERALIAS ? "User" : a->type == RUNASALIAS ? "Runas" :
+       "Unknown", a->name);
+    return(0);
+}
+
+/*
+ * Unlink any sudoers temp files that remain.
+ */
+void
+cleanup(gotsignal)
+    int gotsignal;
+{
+    struct sudoersfile *sp;
+
+    tq_foreach_fwd(&sudoerslist, sp) {
+       if (sp->tpath != NULL)
+           (void) unlink(sp->tpath);
+    }
+    if (!gotsignal) {
+       sudo_endpwent();
+       sudo_endgrent();
+    }
+}
+
+/*
+ * Unlink sudoers temp files (if any) and exit.
+ */
+static RETSIGTYPE
+quit(signo)
+    int signo;
+{
+    cleanup(signo);
+#define        emsg     " exiting due to signal.\n"
+    write(STDERR_FILENO, getprogname(), strlen(getprogname()));
+    write(STDERR_FILENO, emsg, sizeof(emsg) - 1);
+    _exit(signo);
 }
 
 static void