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 */
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
70 #if TIME_WITH_SYS_TIME
80 #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 print_selfref(char *name, int, int, int);
113 static void print_undefined(char *name, int, int, int);
114 static void setup_signals(void);
115 static void help(void) __attribute__((__noreturn__));
116 static void usage(int);
120 extern void yyerror(const char *);
121 extern void yyrestart(FILE *);
124 * External globals exported by the parser
126 extern struct rbtree *aliases;
128 extern char *sudoers, *errorfile;
129 extern int errorlineno, parse_error;
137 struct interface *interfaces;
138 struct sudo_user sudo_user;
139 struct passwd *list_pw;
140 sudo_printf_t sudo_printf = visudo_printf;
141 static struct sudoersfile_list sudoerslist;
142 static struct rbtree *alias_freelist;
145 main(int argc, char *argv[])
147 struct sudoersfile *sp;
148 char *args, *editor, *sudoers_path;
149 int ch, checkonly, quiet, strict, oldperms;
150 #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
151 extern char *malloc_options;
152 malloc_options = "AFGJPR";
155 #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
156 setprogname(argc > 0 ? argv[0] : "visudo");
164 checkonly = oldperms = quiet = strict = FALSE;
165 sudoers_path = _PATH_SUDOERS;
166 while ((ch = getopt(argc, argv, "Vcf:sq")) != -1) {
169 (void) printf("%s version %s\n", getprogname(), PACKAGE_VERSION);
170 (void) printf("%s grammar version %d\n", getprogname(), SUDOERS_GRAMMAR_VERSION);
173 checkonly++; /* check mode */
176 sudoers_path = optarg; /* sudoers file path */
183 strict++; /* strict mode */
186 quiet++; /* quiet mode */
200 /* Mock up a fake sudo_user struct. */
202 if ((sudo_user.pw = sudo_getpwuid(getuid())) == NULL)
203 errorx(1, "you don't exist in the passwd database");
206 /* Setup defaults data structures. */
210 exit(check_syntax(sudoers_path, quiet, strict));
213 * Parse the existing sudoers file(s) in quiet mode to highlight any
214 * existing errors and to pull in editor and env_editor conf values.
216 if ((yyin = open_sudoers(sudoers_path, TRUE, NULL)) == NULL) {
217 error(1, "%s", sudoers_path);
219 init_parser(sudoers_path, 0);
221 (void) update_defaults(SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER);
223 editor = get_editor(&args);
225 /* Install signal handlers to clean up temp files if we are killed. */
228 /* Edit the sudoers file(s) */
229 tq_foreach_fwd(&sudoerslist, sp) {
232 if (sp != tq_first(&sudoerslist)) {
233 printf("press return to edit %s: ", sp->path);
234 while ((ch = getchar()) != EOF && ch != '\n')
237 edit_sudoers(sp, editor, args, -1);
240 /* Check edited files for a parse error and re-edit any that fail. */
241 reparse_sudoers(editor, args, strict, quiet);
243 /* Install the sudoers temp files. */
244 tq_foreach_fwd(&sudoerslist, sp) {
246 (void) unlink(sp->tpath);
248 (void) install_sudoers(sp, oldperms);
255 * Edit each sudoers file.
256 * Returns TRUE on success, else FALSE.
259 edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno)
261 int tfd; /* sudoers temp file descriptor */
262 int modified; /* was the file modified? */
263 int ac; /* argument count */
264 char **av; /* argument vector for run_command */
265 char *cp; /* scratch char pointer */
266 char buf[PATH_MAX*2]; /* buffer used for copying files */
267 char linestr[64]; /* string version of lineno */
268 struct timeval tv, tv1, tv2; /* time before and after edit */
269 struct timeval orig_mtim; /* starting mtime of sudoers file */
270 off_t orig_size; /* starting size of sudoers file */
271 ssize_t nread; /* number of bytes read */
272 struct stat sb; /* stat buffer */
275 if (fstat(sp->fd, &sb) == -1)
277 if (stat(sp->path, &sb) == -1)
279 error(1, "can't stat %s", sp->path);
280 orig_size = sb.st_size;
281 mtim_get(&sb, &orig_mtim);
283 /* Create the temp file if needed and set timestamp. */
284 if (sp->tpath == NULL) {
285 easprintf(&sp->tpath, "%s.tmp", sp->path);
286 tfd = open(sp->tpath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
288 error(1, "%s", sp->tpath);
290 /* Copy sp->path -> sp->tpath and reset the mtime. */
291 if (orig_size != 0) {
292 (void) lseek(sp->fd, (off_t)0, SEEK_SET);
293 while ((nread = read(sp->fd, buf, sizeof(buf))) > 0)
294 if (write(tfd, buf, nread) != nread)
295 error(1, "write error");
297 /* Add missing newline at EOF if needed. */
298 if (nread > 0 && buf[nread - 1] != '\n') {
300 if (write(tfd, buf, 1) != 1)
301 error(1, "write error");
306 (void) touch(-1, sp->tpath, &orig_mtim);
308 /* Find the length of the argument vector */
309 ac = 3 + (lineno > 0);
314 for (wasblank = FALSE, cp = args; *cp; cp++) {
315 if (isblank((unsigned char) *cp))
324 /* Build up argument vector for the command */
325 av = emalloc2(ac, sizeof(char *));
326 if ((av[0] = strrchr(editor, '/')) != NULL)
332 (void) snprintf(linestr, sizeof(linestr), "+%d", lineno);
336 for ((cp = strtok(args, " \t")); cp; (cp = strtok(NULL, " \t")))
339 av[ac++] = sp->tpath;
344 * We cannot check the editor's exit value against 0 since
345 * XPG4 specifies that vi's exit value is a function of the
346 * number of errors during editing (?!?!).
348 gettimeofday(&tv1, NULL);
349 if (run_command(editor, av) != -1) {
350 gettimeofday(&tv2, NULL);
354 if (stat(sp->tpath, &sb) < 0) {
355 warningx("cannot stat temporary file (%s), %s unchanged",
356 sp->tpath, sp->path);
359 if (sb.st_size == 0 && orig_size != 0) {
360 warningx("zero length temporary file (%s), %s unchanged",
361 sp->tpath, sp->path);
366 warningx("editor (%s) failed, %s unchanged", editor, sp->path);
370 /* Set modified bit if use changed the file. */
373 if (orig_size == sb.st_size && timevalcmp(&orig_mtim, &tv, ==)) {
375 * If mtime and size match but the user spent no measurable
376 * time in the editor we can't tell if the file was changed.
378 timevalsub(&tv1, &tv2);
379 if (timevalisset(&tv2))
384 * If modified in this edit session, mark as modified.
387 sp->modified = modified;
389 warningx("%s unchanged", sp->tpath);
395 * Parse sudoers after editing and re-edit any ones that caused a parse error.
396 * Returns TRUE on success, else FALSE.
399 reparse_sudoers(char *editor, char *args, int strict, int quiet)
401 struct sudoersfile *sp, *last;
406 * Parse the edited sudoers files and do sanity checking
409 sp = tq_first(&sudoerslist);
410 last = tq_last(&sudoerslist);
411 fp = fopen(sp->tpath, "r+");
413 errorx(1, "can't re-open temporary file (%s), %s unchanged.",
414 sp->tpath, sp->path);
416 /* Clean slate for each parse */
418 init_parser(sp->path, quiet);
420 /* Parse the sudoers temp file */
422 if (yyparse() && !parse_error) {
423 warningx("unabled to parse temporary file (%s), unknown error",
426 errorfile = sp->path;
430 if (!update_defaults(SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER) ||
431 check_aliases(strict, quiet) != 0) {
433 errorfile = sp->path;
438 * Got an error, prompt the user for what to do now
442 case 'Q' : parse_error = FALSE; /* ignore parse error */
444 case 'x' : cleanup(0);
450 /* Edit file with the parse error */
451 tq_foreach_fwd(&sudoerslist, sp) {
452 if (errorfile == NULL || strcmp(sp->path, errorfile) == 0) {
453 edit_sudoers(sp, editor, args, errorlineno);
458 errorx(1, "internal error, can't find %s in list!", sudoers);
461 /* If any new #include directives were added, edit them too. */
462 for (sp = last->next; sp != NULL; sp = sp->next) {
463 printf("press return to edit %s: ", sp->path);
464 while ((ch = getchar()) != EOF && ch != '\n')
466 edit_sudoers(sp, editor, args, errorlineno);
468 } while (parse_error);
474 * Set the owner and mode on a sudoers temp file and
475 * move it into place. Returns TRUE on success, else FALSE.
478 install_sudoers(struct sudoersfile *sp, int oldperms)
483 * Change mode and ownership of temp file so when
484 * we move it to sp->path things are kosher.
487 /* Use perms of the existing file. */
489 if (fstat(sp->fd, &sb) == -1)
491 if (stat(sp->path, &sb) == -1)
493 error(1, "can't stat %s", sp->path);
494 if (chown(sp->tpath, sb.st_uid, sb.st_gid) != 0) {
495 warning("unable to set (uid, gid) of %s to (%d, %d)",
496 sp->tpath, sb.st_uid, sb.st_gid);
498 if (chmod(sp->tpath, sb.st_mode & 0777) != 0) {
499 warning("unable to change mode of %s to 0%o", sp->tpath,
500 (sb.st_mode & 0777));
503 if (chown(sp->tpath, SUDOERS_UID, SUDOERS_GID) != 0) {
504 warning("unable to set (uid, gid) of %s to (%d, %d)",
505 sp->tpath, SUDOERS_UID, SUDOERS_GID);
508 if (chmod(sp->tpath, SUDOERS_MODE) != 0) {
509 warning("unable to change mode of %s to 0%o", sp->tpath,
516 * Now that sp->tpath is sane (parses ok) it needs to be
517 * rename(2)'d to sp->path. If the rename(2) fails we try using
518 * mv(1) in case sp->tpath and sp->path are on different file systems.
520 if (rename(sp->tpath, sp->path) == 0) {
524 if (errno == EXDEV) {
526 warningx("%s and %s not on the same file system, using mv to rename",
527 sp->tpath, sp->path);
529 /* Build up argument vector for the command */
530 if ((av[0] = strrchr(_PATH_MV, '/')) != NULL)
539 if (run_command(_PATH_MV, av)) {
540 warningx("command failed: '%s %s %s', %s unchanged",
541 _PATH_MV, sp->tpath, sp->path, sp->path);
542 (void) unlink(sp->tpath);
550 warning("error renaming %s, %s unchanged", sp->tpath, sp->path);
551 (void) unlink(sp->tpath);
595 group_plugin_query(const char *user, const char *group, const struct passwd *pw)
601 * Assuming a parse error occurred, prompt the user for what they want
602 * to do now. Returns the first letter of their choice.
610 (void) fputs("What now? ", stdout);
612 for (c = choice; c != '\n' && c != EOF;)
624 (void) puts("Options are:");
625 (void) puts(" (e)dit sudoers file again");
626 (void) puts(" e(x)it without saving changes to sudoers file");
627 (void) puts(" (Q)uit and save changes to sudoers file (DANGER!)\n");
633 * Install signal handlers for visudo.
641 * Setup signal handlers to cleanup nicely.
643 zero_bytes(&sa, sizeof(sa));
644 sigemptyset(&sa.sa_mask);
645 sa.sa_flags = SA_RESTART;
646 sa.sa_handler = quit;
647 (void) sigaction(SIGTERM, &sa, NULL);
648 (void) sigaction(SIGHUP, &sa, NULL);
649 (void) sigaction(SIGINT, &sa, NULL);
650 (void) sigaction(SIGQUIT, &sa, NULL);
654 run_command(char *path, char **argv)
659 switch (pid = fork()) {
661 error(1, "unable to run %s", path);
662 break; /* NOTREACHED */
666 closefrom(STDERR_FILENO + 1);
668 warning("unable to run %s", path);
670 break; /* NOTREACHED */
674 rv = waitpid(pid, &status, 0);
675 } while (rv == -1 && errno == EINTR);
677 if (rv == -1 || !WIFEXITED(status))
679 return WEXITSTATUS(status);
683 check_syntax(char *sudoers_path, int quiet, int strict)
688 if (strcmp(sudoers_path, "-") == 0) {
690 sudoers_path = "stdin";
691 } else if ((yyin = fopen(sudoers_path, "r")) == NULL) {
693 warning("unable to open %s", sudoers_path);
696 init_parser(sudoers_path, quiet);
697 if (yyparse() && !parse_error) {
699 warningx("failed to parse %s file, unknown error", sudoers_path);
701 errorfile = sudoers_path;
703 if (!parse_error && check_aliases(strict, quiet) != 0) {
705 errorfile = sudoers_path;
710 if (errorlineno != -1)
711 (void) printf("parse error in %s near line %d\n", errorfile,
714 (void) printf("parse error in %s\n", errorfile);
716 (void) printf("%s: parsed OK\n", sudoers_path);
719 /* Check mode and owner in strict mode. */
721 if (strict && yyin != stdin && fstat(fileno(yyin), &sb) == 0)
723 if (strict && yyin != stdin && stat(sudoers_path, &sb) == 0)
726 if (sb.st_uid != SUDOERS_UID || sb.st_gid != SUDOERS_GID) {
729 fprintf(stderr, "%s: wrong owner (uid, gid) should be (%d, %d)\n",
730 sudoers_path, SUDOERS_UID, SUDOERS_GID);
733 if ((sb.st_mode & 07777) != SUDOERS_MODE) {
736 fprintf(stderr, "%s: bad permissions, should be mode 0%o\n",
737 sudoers_path, SUDOERS_MODE);
746 * Used to open (and lock) the initial sudoers file and to also open
747 * any subsequent files #included via a callback from the parser.
750 open_sudoers(const char *path, int doedit, int *keepopen)
752 struct sudoersfile *entry;
755 /* Check for existing entry */
756 tq_foreach_fwd(&sudoerslist, entry) {
757 if (strcmp(path, entry->path) == 0)
761 entry = emalloc(sizeof(*entry));
762 entry->path = estrdup(path);
766 entry->fd = open(entry->path, O_RDWR | O_CREAT, SUDOERS_MODE);
768 entry->doedit = doedit;
769 if (entry->fd == -1) {
770 warning("%s", entry->path);
774 if (!lock_file(entry->fd, SUDO_TLOCK))
775 errorx(1, "%s busy, try again later", entry->path);
776 if ((fp = fdopen(entry->fd, "r")) == NULL)
777 error(1, "%s", entry->path);
778 tq_append(&sudoerslist, entry);
780 /* Already exists, open .tmp version if there is one. */
781 if (entry->tpath != NULL) {
782 if ((fp = fopen(entry->tpath, "r")) == NULL)
783 error(1, "%s", entry->tpath);
785 if ((fp = fdopen(entry->fd, "r")) == NULL)
786 error(1, "%s", entry->path);
790 if (keepopen != NULL)
796 get_editor(char **args)
798 char *Editor, *EditorArgs, *EditorPath, *UserEditor, *UserEditorArgs;
801 * Check VISUAL and EDITOR environment variables to see which editor
802 * the user wants to use (we may not end up using it though).
803 * If the path is not fully-qualified, make it so and check that
804 * the specified executable actually exists.
806 UserEditorArgs = NULL;
807 if ((UserEditor = getenv("VISUAL")) == NULL || *UserEditor == '\0')
808 UserEditor = getenv("EDITOR");
809 if (UserEditor && *UserEditor == '\0')
811 else if (UserEditor) {
812 UserEditorArgs = get_args(UserEditor);
813 if (find_path(UserEditor, &Editor, NULL, getenv("PATH"), 0) == FOUND) {
816 if (def_env_editor) {
817 /* If we are honoring $EDITOR this is a fatal error. */
818 errorx(1, "specified editor (%s) doesn't exist!", UserEditor);
820 /* Otherwise, just ignore $EDITOR. */
827 * See if we can use the user's choice of editors either because
828 * we allow any $EDITOR or because $EDITOR is in the allowable list.
830 Editor = EditorArgs = EditorPath = NULL;
831 if (def_env_editor && UserEditor) {
833 EditorArgs = UserEditorArgs;
834 } else if (UserEditor) {
835 struct stat editor_sb;
836 struct stat user_editor_sb;
837 char *base, *userbase;
839 if (stat(UserEditor, &user_editor_sb) != 0) {
840 /* Should never happen since we already checked above. */
841 error(1, "unable to stat editor (%s)", UserEditor);
843 EditorPath = estrdup(def_editor);
844 Editor = strtok(EditorPath, ":");
846 EditorArgs = get_args(Editor);
848 * Both Editor and UserEditor should be fully qualified but
851 if ((base = strrchr(Editor, '/')) == NULL)
853 if ((userbase = strrchr(UserEditor, '/')) == NULL) {
860 * We compare the basenames first and then use stat to match
863 if (strcmp(base, userbase) == 0) {
864 if (stat(Editor, &editor_sb) == 0 && S_ISREG(editor_sb.st_mode)
865 && (editor_sb.st_mode & 0000111) &&
866 editor_sb.st_dev == user_editor_sb.st_dev &&
867 editor_sb.st_ino == user_editor_sb.st_ino)
870 } while ((Editor = strtok(NULL, ":")));
874 * Can't use $EDITOR, try each element of def_editor until we
875 * find one that exists, is regular, and is executable.
877 if (Editor == NULL || *Editor == '\0') {
879 EditorPath = estrdup(def_editor);
880 Editor = strtok(EditorPath, ":");
882 EditorArgs = get_args(Editor);
883 if (sudo_goodpath(Editor, NULL))
885 } while ((Editor = strtok(NULL, ":")));
887 /* Bleah, none of the editors existed! */
888 if (Editor == NULL || *Editor == '\0')
889 errorx(1, "no editor found (editor path = %s)", def_editor);
896 * Split out any command line arguments and return them.
904 while (*args && !isblank((unsigned char) *args))
908 while (*args && isblank((unsigned char) *args))
911 return *args ? args : NULL;
915 * Look up the hostname and set user_host and user_shost.
920 char *p, thost[MAXHOSTNAMELEN + 1];
922 if (gethostname(thost, sizeof(thost)) != 0) {
923 user_host = user_shost = "localhost";
926 thost[sizeof(thost) - 1] = '\0';
927 user_host = estrdup(thost);
929 if ((p = strchr(user_host, '.'))) {
931 user_shost = estrdup(user_host);
934 user_shost = user_host;
939 alias_remove_recursive(char *name, int type, int strict, int quiet)
945 if ((a = alias_find(name, type)) != NULL) {
946 tq_foreach_fwd(&a->members, m) {
947 if (m->type == ALIAS) {
948 if (strcmp(name, m->name) == 0) {
949 print_selfref(m->name, type, strict, quiet);
952 if (!alias_remove_recursive(m->name, type, strict, quiet))
959 a = alias_remove(name, type);
961 rbinsert(alias_freelist, a);
966 * Iterate through the sudoers datastructures looking for undefined
967 * aliases or unused aliases.
970 check_aliases(int strict, int quiet)
973 struct member *m, *binding;
974 struct privilege *priv;
977 int atype, error = 0;
979 alias_freelist = rbcreate(alias_compare);
982 tq_foreach_fwd(&userspecs, us) {
983 tq_foreach_fwd(&us->users, m) {
984 if (m->type == ALIAS) {
986 if (alias_find(m->name, USERALIAS) == NULL) {
987 print_undefined(m->name, USERALIAS, strict, quiet);
992 tq_foreach_fwd(&us->privileges, priv) {
993 tq_foreach_fwd(&priv->hostlist, m) {
994 if (m->type == ALIAS) {
996 if (alias_find(m->name, HOSTALIAS) == NULL) {
997 print_undefined(m->name, HOSTALIAS, strict, quiet);
1002 tq_foreach_fwd(&priv->cmndlist, cs) {
1003 tq_foreach_fwd(&cs->runasuserlist, m) {
1004 if (m->type == ALIAS) {
1006 if (alias_find(m->name, RUNASALIAS) == NULL) {
1007 print_undefined(m->name, RUNASALIAS, strict, quiet);
1012 if ((m = cs->cmnd)->type == ALIAS) {
1014 if (alias_find(m->name, CMNDALIAS) == NULL) {
1015 print_undefined(m->name, CMNDALIAS, strict, quiet);
1023 /* Reverse check (destructive) */
1024 tq_foreach_fwd(&userspecs, us) {
1025 tq_foreach_fwd(&us->users, m) {
1026 if (m->type == ALIAS) {
1027 if (!alias_remove_recursive(m->name, USERALIAS, strict, quiet))
1031 tq_foreach_fwd(&us->privileges, priv) {
1032 tq_foreach_fwd(&priv->hostlist, m) {
1033 if (m->type == ALIAS)
1034 if (!alias_remove_recursive(m->name, HOSTALIAS, strict,
1038 tq_foreach_fwd(&priv->cmndlist, cs) {
1039 tq_foreach_fwd(&cs->runasuserlist, m) {
1040 if (m->type == ALIAS)
1041 if (!alias_remove_recursive(m->name, RUNASALIAS,
1045 if ((m = cs->cmnd)->type == ALIAS)
1046 if (!alias_remove_recursive(m->name, CMNDALIAS, strict,
1052 tq_foreach_fwd(&defaults, d) {
1060 case DEFAULTS_RUNAS:
1067 continue; /* not an alias */
1069 tq_foreach_fwd(&d->binding, binding) {
1070 for (m = binding; m != NULL; m = m->next) {
1071 if (m->type == ALIAS)
1072 if (!alias_remove_recursive(m->name, atype, strict, quiet))
1077 rbdestroy(alias_freelist, alias_free);
1079 /* If all aliases were referenced we will have an empty tree. */
1080 if (!no_aliases() && !quiet)
1081 alias_apply(print_unused, strict ? "Error" : "Warning");
1083 return strict ? error : 0;
1087 print_undefined(char *name, int type, int strict, int quiet)
1090 warningx("%s: %s_Alias `%s' referenced but not defined",
1091 strict ? "Error" : "Warning",
1092 type == HOSTALIAS ? "Host" : type == CMNDALIAS ? "Cmnd" :
1093 type == USERALIAS ? "User" : type == RUNASALIAS ? "Runas" :
1099 print_selfref(name, type, strict, quiet)
1106 warningx("%s: %s_Alias `%s' references self",
1107 strict ? "Error" : "Warning",
1108 type == HOSTALIAS ? "Host" : type == CMNDALIAS ? "Cmnd" :
1109 type == USERALIAS ? "User" : type == RUNASALIAS ? "Runas" :
1115 print_unused(void *v1, void *v2)
1117 struct alias *a = (struct alias *)v1;
1118 char *prefix = (char *)v2;
1120 warningx("%s: unused %s_Alias %s", prefix,
1121 a->type == HOSTALIAS ? "Host" : a->type == CMNDALIAS ? "Cmnd" :
1122 a->type == USERALIAS ? "User" : a->type == RUNASALIAS ? "Runas" :
1123 "Unknown", a->name);
1128 * Unlink any sudoers temp files that remain.
1131 cleanup(int gotsignal)
1133 struct sudoersfile *sp;
1135 tq_foreach_fwd(&sudoerslist, sp) {
1136 if (sp->tpath != NULL)
1137 (void) unlink(sp->tpath);
1146 * Unlink sudoers temp files (if any) and exit.
1151 const char *signame, *myname;
1154 #define emsg " exiting due to signal: "
1155 myname = getprogname();
1156 signame = strsignal(signo);
1157 if (write(STDERR_FILENO, myname, strlen(myname)) == -1 ||
1158 write(STDERR_FILENO, emsg, sizeof(emsg) - 1) == -1 ||
1159 write(STDERR_FILENO, signame, strlen(signame)) == -1 ||
1160 write(STDERR_FILENO, "\n", 1) == -1)
1161 /* shut up glibc */;
1168 (void) fprintf(fatal ? stderr : stdout,
1169 "usage: %s [-chqsV] [-f sudoers]\n", getprogname());
1177 (void) printf("%s - safely edit the sudoers file\n\n", getprogname());
1179 (void) puts("\nOptions:");
1180 (void) puts(" -c check-only mode");
1181 (void) puts(" -f sudoers specify sudoers file location");
1182 (void) puts(" -h display help message and exit");
1183 (void) puts(" -q less verbose (quiet) syntax error messages");
1184 (void) puts(" -s strict syntax checking");
1185 (void) puts(" -V display version information and exit");
1190 visudo_printf(int msg_type, const char *fmt, ...)
1196 case SUDO_CONV_INFO_MSG:
1199 case SUDO_CONV_ERROR_MSG:
1208 vfprintf(fp, fmt, ap);