2 * Copyright (c) 1996, 1998-2005, 2007-2011
3 * Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
23 * Lock the sudoers file for safe editing (ala vipw) and check for parse errors.
34 #include <sys/types.h>
35 #include <sys/param.h>
37 #include <sys/socket.h>
40 # include <sys/file.h>
51 #endif /* STDC_HEADERS */
54 #endif /* HAVE_STRING_H */
57 #endif /* HAVE_STRINGS_H */
60 #endif /* HAVE_UNISTD_H */
68 #include <netinet/in.h>
69 #include <arpa/inet.h>
71 #if TIME_WITH_SYS_TIME
79 #include "interfaces.h"
83 #include "sudoers_version.h"
87 struct sudoersfile *prev, *next;
94 TQ_DECLARE(sudoersfile)
99 static void quit(int);
100 static char *get_args(char *);
101 static char *get_editor(char **);
102 static void get_hostname(void);
103 static char whatnow(void);
104 static int check_aliases(int, int);
105 static int check_syntax(char *, int, int);
106 static int edit_sudoers(struct sudoersfile *, char *, char *, int);
107 static int install_sudoers(struct sudoersfile *, int);
108 static int print_unused(void *, void *);
109 static int reparse_sudoers(char *, char *, int, int);
110 static int run_command(char *, char **);
111 static int visudo_printf(int msg_type, const char *fmt, ...);
112 static void setup_signals(void);
113 static void help(void) __attribute__((__noreturn__));
114 static void usage(int);
118 extern void yyerror(const char *);
119 extern void yyrestart(FILE *);
122 * External globals exported by the parser
124 extern struct rbtree *aliases;
126 extern char *sudoers, *errorfile;
127 extern int errorlineno, parse_error;
135 struct interface *interfaces;
136 struct sudo_user sudo_user;
137 struct passwd *list_pw;
138 sudo_printf_t sudo_printf = visudo_printf;
139 static struct sudoersfile_list sudoerslist;
140 static struct rbtree *alias_freelist;
143 main(int argc, char *argv[])
145 struct sudoersfile *sp;
146 char *args, *editor, *sudoers_path;
147 int ch, checkonly, quiet, strict, oldperms;
148 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
149 extern char *malloc_options;
150 malloc_options = "AFGJPR";
153 #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
154 setprogname(argc > 0 ? argv[0] : "visudo");
157 #ifdef HAVE_SETLOCALE
158 setlocale(LC_ALL, "");
160 bindtextdomain("sudoers", LOCALEDIR); /* XXX - should have visudo domain */
161 textdomain("sudoers");
169 checkonly = oldperms = quiet = strict = FALSE;
170 sudoers_path = _PATH_SUDOERS;
171 while ((ch = getopt(argc, argv, "Vcf:sq")) != -1) {
174 (void) printf(_("%s version %s\n"), getprogname(), PACKAGE_VERSION);
175 (void) printf(_("%s grammar version %d\n"), getprogname(), SUDOERS_GRAMMAR_VERSION);
178 checkonly++; /* check mode */
181 sudoers_path = optarg; /* sudoers file path */
188 strict++; /* strict mode */
191 quiet++; /* quiet mode */
205 /* Mock up a fake sudo_user struct. */
207 if ((sudo_user.pw = sudo_getpwuid(getuid())) == NULL)
208 errorx(1, _("you do not exist in the %s database"), "passwd");
211 /* Setup defaults data structures. */
215 exit(check_syntax(sudoers_path, quiet, strict));
218 * Parse the existing sudoers file(s) in quiet mode to highlight any
219 * existing errors and to pull in editor and env_editor conf values.
221 if ((yyin = open_sudoers(sudoers_path, TRUE, NULL)) == NULL) {
222 error(1, "%s", sudoers_path);
224 init_parser(sudoers_path, 0);
226 (void) update_defaults(SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER);
228 editor = get_editor(&args);
230 /* Install signal handlers to clean up temp files if we are killed. */
233 /* Edit the sudoers file(s) */
234 tq_foreach_fwd(&sudoerslist, sp) {
237 if (sp != tq_first(&sudoerslist)) {
238 printf(_("press return to edit %s: "), sp->path);
239 while ((ch = getchar()) != EOF && ch != '\n')
242 edit_sudoers(sp, editor, args, -1);
245 /* Check edited files for a parse error and re-edit any that fail. */
246 reparse_sudoers(editor, args, strict, quiet);
248 /* Install the sudoers temp files. */
249 tq_foreach_fwd(&sudoerslist, sp) {
251 (void) unlink(sp->tpath);
253 (void) install_sudoers(sp, oldperms);
260 * List of editors that support the "+lineno" command line syntax.
261 * If an entry starts with '*' the tail end of the string is matched.
262 * No other wild cards are supported.
264 static char *lineno_editors[] = {
284 * Edit each sudoers file.
285 * Returns TRUE on success, else FALSE.
288 edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno)
290 int tfd; /* sudoers temp file descriptor */
291 int modified; /* was the file modified? */
292 int ac; /* argument count */
293 char **av; /* argument vector for run_command */
294 char *cp; /* scratch char pointer */
295 char buf[PATH_MAX*2]; /* buffer used for copying files */
296 char linestr[64]; /* string version of lineno */
297 struct timeval tv, tv1, tv2; /* time before and after edit */
298 struct timeval orig_mtim; /* starting mtime of sudoers file */
299 off_t orig_size; /* starting size of sudoers file */
300 ssize_t nread; /* number of bytes read */
301 struct stat sb; /* stat buffer */
303 if (fstat(sp->fd, &sb) == -1)
304 error(1, _("unable to stat %s"), sp->path);
305 orig_size = sb.st_size;
306 mtim_get(&sb, &orig_mtim);
308 /* Create the temp file if needed and set timestamp. */
309 if (sp->tpath == NULL) {
310 easprintf(&sp->tpath, "%s.tmp", sp->path);
311 tfd = open(sp->tpath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
313 error(1, "%s", sp->tpath);
315 /* Copy sp->path -> sp->tpath and reset the mtime. */
316 if (orig_size != 0) {
317 (void) lseek(sp->fd, (off_t)0, SEEK_SET);
318 while ((nread = read(sp->fd, buf, sizeof(buf))) > 0)
319 if (write(tfd, buf, nread) != nread)
320 error(1, _("write error"));
322 /* Add missing newline at EOF if needed. */
323 if (nread > 0 && buf[nread - 1] != '\n') {
325 if (write(tfd, buf, 1) != 1)
326 error(1, _("write error"));
331 (void) touch(-1, sp->tpath, &orig_mtim);
333 /* Does the editor support +lineno? */
336 char *editor_base = strrchr(editor, '/');
337 if (editor_base != NULL)
340 editor_base = editor;
341 if (*editor_base == 'r')
344 for (av = lineno_editors; (cp = *av) != NULL; av++) {
345 /* We only handle a leading '*' wildcard. */
347 size_t blen = strlen(editor_base);
348 size_t clen = strlen(++cp);
350 if (strcmp(cp, editor_base + blen - clen) == 0)
353 } else if (strcmp(cp, editor_base) == 0)
356 /* Disable +lineno if editor doesn't support it. */
361 /* Find the length of the argument vector */
362 ac = 3 + (lineno > 0);
367 for (wasblank = FALSE, cp = args; *cp; cp++) {
368 if (isblank((unsigned char) *cp))
377 /* Build up argument vector for the command */
378 av = emalloc2(ac, sizeof(char *));
379 if ((av[0] = strrchr(editor, '/')) != NULL)
385 (void) snprintf(linestr, sizeof(linestr), "+%d", lineno);
389 for ((cp = strtok(args, " \t")); cp; (cp = strtok(NULL, " \t")))
392 av[ac++] = sp->tpath;
397 * We cannot check the editor's exit value against 0 since
398 * XPG4 specifies that vi's exit value is a function of the
399 * number of errors during editing (?!?!).
401 gettimeofday(&tv1, NULL);
402 if (run_command(editor, av) != -1) {
403 gettimeofday(&tv2, NULL);
407 if (stat(sp->tpath, &sb) < 0) {
408 warningx(_("unable to stat temporary file (%s), %s unchanged"),
409 sp->tpath, sp->path);
412 if (sb.st_size == 0 && orig_size != 0) {
413 warningx(_("zero length temporary file (%s), %s unchanged"),
414 sp->tpath, sp->path);
419 warningx(_("editor (%s) failed, %s unchanged"), editor, sp->path);
423 /* Set modified bit if use changed the file. */
426 if (orig_size == sb.st_size && timevalcmp(&orig_mtim, &tv, ==)) {
428 * If mtime and size match but the user spent no measurable
429 * time in the editor we can't tell if the file was changed.
431 timevalsub(&tv1, &tv2);
432 if (timevalisset(&tv2))
437 * If modified in this edit session, mark as modified.
440 sp->modified = modified;
442 warningx(_("%s unchanged"), sp->tpath);
448 * Parse sudoers after editing and re-edit any ones that caused a parse error.
449 * Returns TRUE on success, else FALSE.
452 reparse_sudoers(char *editor, char *args, int strict, int quiet)
454 struct sudoersfile *sp, *last;
459 * Parse the edited sudoers files and do sanity checking
462 sp = tq_first(&sudoerslist);
463 last = tq_last(&sudoerslist);
464 fp = fopen(sp->tpath, "r+");
466 errorx(1, _("unable to re-open temporary file (%s), %s unchanged."),
467 sp->tpath, sp->path);
469 /* Clean slate for each parse */
471 init_parser(sp->path, quiet);
473 /* Parse the sudoers temp file */
475 if (yyparse() && !parse_error) {
476 warningx(_("unabled to parse temporary file (%s), unknown error"),
479 errorfile = sp->path;
483 if (!update_defaults(SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER) ||
484 check_aliases(strict, quiet) != 0) {
486 errorfile = sp->path;
491 * Got an error, prompt the user for what to do now
495 case 'Q' : parse_error = FALSE; /* ignore parse error */
497 case 'x' : cleanup(0);
503 /* Edit file with the parse error */
504 tq_foreach_fwd(&sudoerslist, sp) {
505 if (errorfile == NULL || strcmp(sp->path, errorfile) == 0) {
506 edit_sudoers(sp, editor, args, errorlineno);
511 errorx(1, _("internal error, unable to find %s in list!"),
516 /* If any new #include directives were added, edit them too. */
517 for (sp = last->next; sp != NULL; sp = sp->next) {
518 printf(_("press return to edit %s: "), sp->path);
519 while ((ch = getchar()) != EOF && ch != '\n')
521 edit_sudoers(sp, editor, args, errorlineno);
523 } while (parse_error);
529 * Set the owner and mode on a sudoers temp file and
530 * move it into place. Returns TRUE on success, else FALSE.
533 install_sudoers(struct sudoersfile *sp, int oldperms)
538 * Change mode and ownership of temp file so when
539 * we move it to sp->path things are kosher.
542 /* Use perms of the existing file. */
543 if (fstat(sp->fd, &sb) == -1)
544 error(1, _("unable to stat %s"), sp->path);
545 if (chown(sp->tpath, sb.st_uid, sb.st_gid) != 0) {
546 warning(_("unable to set (uid, gid) of %s to (%u, %u)"),
547 sp->tpath, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid);
549 if (chmod(sp->tpath, sb.st_mode & 0777) != 0) {
550 warning(_("unable to change mode of %s to 0%o"), sp->tpath,
551 (unsigned int)(sb.st_mode & 0777));
554 if (chown(sp->tpath, SUDOERS_UID, SUDOERS_GID) != 0) {
555 warning(_("unable to set (uid, gid) of %s to (%u, %u)"),
556 sp->tpath, SUDOERS_UID, SUDOERS_GID);
559 if (chmod(sp->tpath, SUDOERS_MODE) != 0) {
560 warning(_("unable to change mode of %s to 0%o"), sp->tpath,
567 * Now that sp->tpath is sane (parses ok) it needs to be
568 * rename(2)'d to sp->path. If the rename(2) fails we try using
569 * mv(1) in case sp->tpath and sp->path are on different file systems.
571 if (rename(sp->tpath, sp->path) == 0) {
575 if (errno == EXDEV) {
577 warningx(_("%s and %s not on the same file system, using mv to rename"),
578 sp->tpath, sp->path);
580 /* Build up argument vector for the command */
581 if ((av[0] = strrchr(_PATH_MV, '/')) != NULL)
590 if (run_command(_PATH_MV, av)) {
591 warningx(_("command failed: '%s %s %s', %s unchanged"),
592 _PATH_MV, sp->tpath, sp->path, sp->path);
593 (void) unlink(sp->tpath);
601 warning(_("error renaming %s, %s unchanged"), sp->tpath, sp->path);
602 (void) unlink(sp->tpath);
646 group_plugin_query(const char *user, const char *group, const struct passwd *pw)
652 * Assuming a parse error occurred, prompt the user for what they want
653 * to do now. Returns the first letter of their choice.
661 (void) fputs(_("What now? "), stdout);
663 for (c = choice; c != '\n' && c != EOF;)
675 (void) puts(_("Options are:\n"
676 " (e)dit sudoers file again\n"
677 " e(x)it without saving changes to sudoers file\n"
678 " (Q)uit and save changes to sudoers file (DANGER!)\n"));
684 * Install signal handlers for visudo.
692 * Setup signal handlers to cleanup nicely.
694 zero_bytes(&sa, sizeof(sa));
695 sigemptyset(&sa.sa_mask);
696 sa.sa_flags = SA_RESTART;
697 sa.sa_handler = quit;
698 (void) sigaction(SIGTERM, &sa, NULL);
699 (void) sigaction(SIGHUP, &sa, NULL);
700 (void) sigaction(SIGINT, &sa, NULL);
701 (void) sigaction(SIGQUIT, &sa, NULL);
705 run_command(char *path, char **argv)
710 switch (pid = fork()) {
712 error(1, _("unable to execute %s"), path);
713 break; /* NOTREACHED */
717 closefrom(STDERR_FILENO + 1);
719 warning(_("unable to run %s"), path);
721 break; /* NOTREACHED */
725 rv = waitpid(pid, &status, 0);
726 } while (rv == -1 && errno == EINTR);
728 if (rv == -1 || !WIFEXITED(status))
730 return WEXITSTATUS(status);
734 check_syntax(char *sudoers_path, int quiet, int strict)
739 if (strcmp(sudoers_path, "-") == 0) {
741 sudoers_path = "stdin";
742 } else if ((yyin = fopen(sudoers_path, "r")) == NULL) {
744 warning(_("unable to open %s"), sudoers_path);
747 init_parser(sudoers_path, quiet);
748 if (yyparse() && !parse_error) {
750 warningx(_("failed to parse %s file, unknown error"), sudoers_path);
752 errorfile = sudoers_path;
754 if (!parse_error && check_aliases(strict, quiet) != 0) {
756 errorfile = sudoers_path;
761 if (errorlineno != -1)
762 (void) printf(_("parse error in %s near line %d\n"),
763 errorfile, errorlineno);
765 (void) printf(_("parse error in %s\n"), errorfile);
767 (void) printf(_("%s: parsed OK\n"), sudoers_path);
770 /* Check mode and owner in strict mode. */
771 if (strict && yyin != stdin && fstat(fileno(yyin), &sb) == 0) {
772 if (sb.st_uid != SUDOERS_UID || sb.st_gid != SUDOERS_GID) {
776 _("%s: wrong owner (uid, gid) should be (%u, %u)\n"),
777 sudoers_path, SUDOERS_UID, SUDOERS_GID);
780 if ((sb.st_mode & 07777) != SUDOERS_MODE) {
783 fprintf(stderr, _("%s: bad permissions, should be mode 0%o\n"),
784 sudoers_path, SUDOERS_MODE);
793 * Used to open (and lock) the initial sudoers file and to also open
794 * any subsequent files #included via a callback from the parser.
797 open_sudoers(const char *path, int doedit, int *keepopen)
799 struct sudoersfile *entry;
802 /* Check for existing entry */
803 tq_foreach_fwd(&sudoerslist, entry) {
804 if (strcmp(path, entry->path) == 0)
808 entry = emalloc(sizeof(*entry));
809 entry->path = estrdup(path);
813 entry->fd = open(entry->path, O_RDWR | O_CREAT, SUDOERS_MODE);
815 entry->doedit = doedit;
816 if (entry->fd == -1) {
817 warning("%s", entry->path);
821 if (!lock_file(entry->fd, SUDO_TLOCK))
822 errorx(1, _("%s busy, try again later"), entry->path);
823 if ((fp = fdopen(entry->fd, "r")) == NULL)
824 error(1, "%s", entry->path);
825 tq_append(&sudoerslist, entry);
827 /* Already exists, open .tmp version if there is one. */
828 if (entry->tpath != NULL) {
829 if ((fp = fopen(entry->tpath, "r")) == NULL)
830 error(1, "%s", entry->tpath);
832 if ((fp = fdopen(entry->fd, "r")) == NULL)
833 error(1, "%s", entry->path);
837 if (keepopen != NULL)
843 get_editor(char **args)
845 char *Editor, *EditorArgs, *EditorPath, *UserEditor, *UserEditorArgs;
848 * Check VISUAL and EDITOR environment variables to see which editor
849 * the user wants to use (we may not end up using it though).
850 * If the path is not fully-qualified, make it so and check that
851 * the specified executable actually exists.
853 UserEditorArgs = NULL;
854 if ((UserEditor = getenv("VISUAL")) == NULL || *UserEditor == '\0')
855 UserEditor = getenv("EDITOR");
856 if (UserEditor && *UserEditor == '\0')
858 else if (UserEditor) {
859 UserEditorArgs = get_args(UserEditor);
860 if (find_path(UserEditor, &Editor, NULL, getenv("PATH"), 0) == FOUND) {
863 if (def_env_editor) {
864 /* If we are honoring $EDITOR this is a fatal error. */
865 errorx(1, _("specified editor (%s) doesn't exist"), UserEditor);
867 /* Otherwise, just ignore $EDITOR. */
874 * See if we can use the user's choice of editors either because
875 * we allow any $EDITOR or because $EDITOR is in the allowable list.
877 Editor = EditorArgs = EditorPath = NULL;
878 if (def_env_editor && UserEditor) {
880 EditorArgs = UserEditorArgs;
881 } else if (UserEditor) {
882 struct stat editor_sb;
883 struct stat user_editor_sb;
884 char *base, *userbase;
886 if (stat(UserEditor, &user_editor_sb) != 0) {
887 /* Should never happen since we already checked above. */
888 error(1, _("unable to stat editor (%s)"), UserEditor);
890 EditorPath = estrdup(def_editor);
891 Editor = strtok(EditorPath, ":");
893 EditorArgs = get_args(Editor);
895 * Both Editor and UserEditor should be fully qualified but
898 if ((base = strrchr(Editor, '/')) == NULL)
900 if ((userbase = strrchr(UserEditor, '/')) == NULL) {
907 * We compare the basenames first and then use stat to match
910 if (strcmp(base, userbase) == 0) {
911 if (stat(Editor, &editor_sb) == 0 && S_ISREG(editor_sb.st_mode)
912 && (editor_sb.st_mode & 0000111) &&
913 editor_sb.st_dev == user_editor_sb.st_dev &&
914 editor_sb.st_ino == user_editor_sb.st_ino)
917 } while ((Editor = strtok(NULL, ":")));
921 * Can't use $EDITOR, try each element of def_editor until we
922 * find one that exists, is regular, and is executable.
924 if (Editor == NULL || *Editor == '\0') {
926 EditorPath = estrdup(def_editor);
927 Editor = strtok(EditorPath, ":");
929 EditorArgs = get_args(Editor);
930 if (sudo_goodpath(Editor, NULL))
932 } while ((Editor = strtok(NULL, ":")));
934 /* Bleah, none of the editors existed! */
935 if (Editor == NULL || *Editor == '\0')
936 errorx(1, _("no editor found (editor path = %s)"), def_editor);
943 * Split out any command line arguments and return them.
951 while (*args && !isblank((unsigned char) *args))
955 while (*args && isblank((unsigned char) *args))
958 return *args ? args : NULL;
962 * Look up the hostname and set user_host and user_shost.
967 char *p, thost[MAXHOSTNAMELEN + 1];
969 if (gethostname(thost, sizeof(thost)) != 0) {
970 user_host = user_shost = "localhost";
973 thost[sizeof(thost) - 1] = '\0';
974 user_host = estrdup(thost);
976 if ((p = strchr(user_host, '.'))) {
978 user_shost = estrdup(user_host);
981 user_shost = user_host;
986 alias_remove_recursive(char *name, int type, int strict, int quiet)
992 if ((a = alias_find(name, type)) != NULL) {
993 tq_foreach_fwd(&a->members, m) {
994 if (m->type == ALIAS) {
995 if (!alias_remove_recursive(m->name, type, strict, quiet))
1001 a = alias_remove(name, type);
1003 rbinsert(alias_freelist, a);
1008 check_alias(char *name, int type, int strict, int quiet)
1014 if ((a = alias_find(name, type)) != NULL) {
1015 /* check alias contents */
1016 tq_foreach_fwd(&a->members, m) {
1017 if (m->type == ALIAS)
1018 error += check_alias(m->name, type, strict, quiet);
1023 if (errno == ELOOP) {
1025 _("Error: cycle in %s_Alias `%s'") :
1026 _("Warning: cycle in %s_Alias `%s'");
1029 _("Error: %s_Alias `%s' referenced but not defined") :
1030 _("Warning: %s_Alias `%s' referenced but not defined");
1033 type == HOSTALIAS ? "Host" : type == CMNDALIAS ? "Cmnd" :
1034 type == USERALIAS ? "User" : type == RUNASALIAS ? "Runas" :
1044 * Iterate through the sudoers datastructures looking for undefined
1045 * aliases or unused aliases.
1048 check_aliases(int strict, int quiet)
1050 struct cmndspec *cs;
1051 struct member *m, *binding;
1052 struct privilege *priv;
1053 struct userspec *us;
1055 int atype, error = 0;
1057 alias_freelist = rbcreate(alias_compare);
1059 /* Forward check. */
1060 tq_foreach_fwd(&userspecs, us) {
1061 tq_foreach_fwd(&us->users, m) {
1062 if (m->type == ALIAS) {
1064 error += check_alias(m->name, USERALIAS, strict, quiet);
1067 tq_foreach_fwd(&us->privileges, priv) {
1068 tq_foreach_fwd(&priv->hostlist, m) {
1069 if (m->type == ALIAS) {
1071 error += check_alias(m->name, HOSTALIAS, strict, quiet);
1074 tq_foreach_fwd(&priv->cmndlist, cs) {
1075 tq_foreach_fwd(&cs->runasuserlist, m) {
1076 if (m->type == ALIAS) {
1078 error += check_alias(m->name, RUNASALIAS, strict, quiet);
1081 if ((m = cs->cmnd)->type == ALIAS) {
1083 error += check_alias(m->name, CMNDALIAS, strict, quiet);
1089 /* Reverse check (destructive) */
1090 tq_foreach_fwd(&userspecs, us) {
1091 tq_foreach_fwd(&us->users, m) {
1092 if (m->type == ALIAS) {
1094 if (!alias_remove_recursive(m->name, USERALIAS, strict, quiet))
1098 tq_foreach_fwd(&us->privileges, priv) {
1099 tq_foreach_fwd(&priv->hostlist, m) {
1100 if (m->type == ALIAS) {
1102 if (!alias_remove_recursive(m->name, HOSTALIAS, strict,
1107 tq_foreach_fwd(&priv->cmndlist, cs) {
1108 tq_foreach_fwd(&cs->runasuserlist, m) {
1109 if (m->type == ALIAS) {
1111 if (!alias_remove_recursive(m->name, RUNASALIAS,
1116 if ((m = cs->cmnd)->type == ALIAS) {
1118 if (!alias_remove_recursive(m->name, CMNDALIAS, strict,
1125 tq_foreach_fwd(&defaults, d) {
1133 case DEFAULTS_RUNAS:
1140 continue; /* not an alias */
1142 tq_foreach_fwd(&d->binding, binding) {
1143 for (m = binding; m != NULL; m = m->next) {
1144 if (m->type == ALIAS) {
1146 if (!alias_remove_recursive(m->name, atype, strict, quiet))
1152 rbdestroy(alias_freelist, alias_free);
1154 /* If all aliases were referenced we will have an empty tree. */
1155 if (!no_aliases() && !quiet)
1156 alias_apply(print_unused, strict ? "Error" : "Warning");
1158 return strict ? error : 0;
1162 print_unused(void *v1, void *v2)
1164 struct alias *a = (struct alias *)v1;
1165 char *prefix = (char *)v2;
1167 warningx(_("%s: unused %s_Alias %s"), prefix,
1168 a->type == HOSTALIAS ? "Host" : a->type == CMNDALIAS ? "Cmnd" :
1169 a->type == USERALIAS ? "User" : a->type == RUNASALIAS ? "Runas" :
1170 "Unknown", a->name);
1175 * Unlink any sudoers temp files that remain.
1178 cleanup(int gotsignal)
1180 struct sudoersfile *sp;
1182 tq_foreach_fwd(&sudoerslist, sp) {
1183 if (sp->tpath != NULL)
1184 (void) unlink(sp->tpath);
1193 * Unlink sudoers temp files (if any) and exit.
1198 const char *signame, *myname;
1201 #define emsg " exiting due to signal: "
1202 myname = getprogname();
1203 signame = strsignal(signo);
1204 if (write(STDERR_FILENO, myname, strlen(myname)) == -1 ||
1205 write(STDERR_FILENO, emsg, sizeof(emsg) - 1) == -1 ||
1206 write(STDERR_FILENO, signame, strlen(signame)) == -1 ||
1207 write(STDERR_FILENO, "\n", 1) == -1)
1208 /* shut up glibc */;
1215 (void) fprintf(fatal ? stderr : stdout,
1216 "usage: %s [-chqsV] [-f sudoers]\n", getprogname());
1224 (void) printf(_("%s - safely edit the sudoers file\n\n"), getprogname());
1226 (void) puts(_("\nOptions:\n"
1227 " -c check-only mode\n"
1228 " -f sudoers specify sudoers file location\n"
1229 " -h display help message and exit\n"
1230 " -q less verbose (quiet) syntax error messages\n"
1231 " -s strict syntax checking\n"
1232 " -V display version information and exit"));
1237 visudo_printf(int msg_type, const char *fmt, ...)
1243 case SUDO_CONV_INFO_MSG:
1246 case SUDO_CONV_ERROR_MSG:
1255 vfprintf(fp, fmt, ap);