int modified;
int doedit;
};
-TQ_DECLARE(sudoersfile);
+TQ_DECLARE(sudoersfile)
/*
* Function prototypes
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__));
+static void help __P((void)) __attribute__((__noreturn__));
+static void usage __P((int));
extern void yyerror __P((const char *));
extern void yyrestart __P((FILE *));
Argv = argv;
if ((Argc = argc) < 1)
- usage();
+ usage(1);
/*
* Arg handling.
*/
checkonly = oldperms = quiet = strict = FALSE;
sudoers_path = _PATH_SUDOERS;
- while ((ch = getopt(argc, argv, "Vcf:sq")) != -1) {
+ while ((ch = getopt(argc, argv, "Vcf:hsq")) != -1) {
switch (ch) {
case 'V':
(void) printf("%s version %s\n", getprogname(), PACKAGE_VERSION);
sudoers_path = optarg; /* sudoers file path */
oldperms = TRUE;
break;
+ case 'h':
+ help();
+ break;
case 's':
strict++; /* strict mode */
break;
quiet++; /* quiet mode */
break;
default:
- usage();
+ usage(1);
}
}
argc -= optind;
argv += optind;
if (argc)
- usage();
+ usage(1);
sudo_setpwent();
sudo_setgrent();
/* Add missing newline at EOF if needed. */
if (nread > 0 && buf[nread - 1] != '\n') {
buf[0] = '\n';
- write(tfd, buf, 1);
+ if (write(tfd, buf, 1) != 1)
+ error(1, "write error");
}
}
(void) close(tfd);
if (stat(sp->tpath, &sb) < 0) {
warningx("cannot stat temporary file (%s), %s unchanged",
sp->tpath, sp->path);
- return(FALSE);
+ return FALSE;
}
if (sb.st_size == 0 && orig_size != 0) {
warningx("zero length temporary file (%s), %s unchanged",
sp->tpath, sp->path);
sp->modified = TRUE;
- return(FALSE);
+ return FALSE;
}
} else {
warningx("editor (%s) failed, %s unchanged", editor, sp->path);
- return(FALSE);
+ return FALSE;
}
/* Set modified bit if use changed the file. */
else
warningx("%s unchanged", sp->tpath);
- return(TRUE);
+ return TRUE;
}
/*
}
} while (parse_error);
- return(TRUE);
+ return TRUE;
}
/*
if (stat(sp->path, &sb) == -1)
#endif
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);
+ if (chown(sp->tpath, sb.st_uid, sb.st_gid) != 0) {
+ warning("unable to set (uid, gid) of %s to (%d, %d)",
+ sp->tpath, sb.st_uid, sb.st_gid);
+ }
+ if (chmod(sp->tpath, sb.st_mode & 0777) != 0) {
+ warning("unable to change mode of %s to 0%o", sp->tpath,
+ (sb.st_mode & 0777));
+ }
} else {
if (chown(sp->tpath, SUDOERS_UID, SUDOERS_GID) != 0) {
warning("unable to set (uid, gid) of %s to (%d, %d)",
sp->tpath, SUDOERS_UID, SUDOERS_GID);
- return(FALSE);
+ return FALSE;
}
if (chmod(sp->tpath, SUDOERS_MODE) != 0) {
warning("unable to change mode of %s to 0%o", sp->tpath,
SUDOERS_MODE);
- return(FALSE);
+ return FALSE;
}
}
(void) unlink(sp->tpath);
efree(sp->tpath);
sp->tpath = NULL;
- return(FALSE);
+ return FALSE;
}
efree(sp->tpath);
sp->tpath = NULL;
} else {
warning("error renaming %s, %s unchanged", sp->tpath, sp->path);
(void) unlink(sp->tpath);
- return(FALSE);
+ return FALSE;
}
}
- return(TRUE);
+ return TRUE;
}
/* STUB */
int
user_is_exempt()
{
- return(FALSE);
+ return FALSE;
}
/* STUB */
sudo_getepw(pw)
const struct passwd *pw;
{
- return (pw->pw_passwd);
+ return pw->pw_passwd;
}
/*
case 'e':
case 'x':
case 'Q':
- return(choice);
+ return choice;
default:
(void) puts("Options are:");
(void) puts(" (e)dit sudoers file again");
} while (rv == -1 && errno == EINTR);
if (rv == -1 || !WIFEXITED(status))
- return(-1);
- return(WEXITSTATUS(status));
+ return -1;
+ return WEXITSTATUS(status);
}
static int
struct stat sb;
int error;
- if ((yyin = fopen(sudoers_path, "r")) == NULL) {
+ if (strcmp(sudoers_path, "-") == 0) {
+ yyin = stdin;
+ sudoers_path = "stdin";
+ } else if ((yyin = fopen(sudoers_path, "r")) == NULL) {
if (!quiet)
warning("unable to open %s", sudoers_path);
exit(1);
}
/* Check mode and owner in strict mode. */
#ifdef HAVE_FSTAT
- if (strict && fstat(fileno(yyin), &sb) == 0)
+ if (strict && yyin != stdin && fstat(fileno(yyin), &sb) == 0)
#else
- if (strict && stat(sudoers_path, &sb) == 0)
+ if (strict && yyin != stdin && stat(sudoers_path, &sb) == 0)
#endif
{
if (sb.st_uid != SUDOERS_UID || sb.st_gid != SUDOERS_GID) {
}
}
- return(error);
+ return error;
}
/*
if (entry->fd == -1) {
warning("%s", entry->path);
efree(entry);
- return(NULL);
+ return NULL;
}
if (!lock_file(entry->fd, SUDO_TLOCK))
errorx(1, "%s busy, try again later", entry->path);
}
if (keepopen != NULL)
*keepopen = TRUE;
- return(fp);
+ return fp;
}
static char *
errorx(1, "no editor found (editor path = %s)", def_editor);
}
*args = EditorArgs;
- return(Editor);
+ return Editor;
}
/*
while (*args && isblank((unsigned char) *args))
args++;
}
- return(*args ? args : NULL);
+ return *args ? args : NULL;
}
/*
a = alias_remove(name, type);
if (a)
rbinsert(alias_freelist, a);
- return(error);
+ return 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,
+ if (!alias_remove_recursive(m->name, HOSTALIAS, strict,
quiet))
error++;
}
if (!no_aliases() && !quiet)
alias_apply(print_unused, strict ? "Error" : "Warning");
- return (strict ? error : 0);
+ return strict ? error : 0;
}
static void
a->type == HOSTALIAS ? "Host" : a->type == CMNDALIAS ? "Cmnd" :
a->type == USERALIAS ? "User" : a->type == RUNASALIAS ? "Runas" :
"Unknown", a->name);
- return(0);
+ return 0;
}
/*
{
cleanup(signo);
#define emsg " exiting due to signal.\n"
- write(STDERR_FILENO, getprogname(), strlen(getprogname()));
- write(STDERR_FILENO, emsg, sizeof(emsg) - 1);
+ if (write(STDERR_FILENO, getprogname(), strlen(getprogname())) == -1 ||
+ write(STDERR_FILENO, emsg, sizeof(emsg) - 1) == -1)
+ /* shut up glibc */;
_exit(signo);
}
static void
-usage()
+usage(fatal)
+ int fatal;
{
- (void) fprintf(stderr, "usage: %s [-c] [-q] [-s] [-V] [-f sudoers]\n",
- getprogname());
- exit(1);
+ (void) fprintf(fatal ? stderr : stdout,
+ "usage: %s [-chqsV] [-f sudoers]\n", getprogname());
+ if (fatal)
+ exit(1);
+}
+
+static void
+help()
+{
+ (void) printf("%s - safely edit the sudoers file\n\n", getprogname());
+ usage(0);
+ (void) puts("\nOptions:");
+ (void) puts(" -c check-only mode");
+ (void) puts(" -f sudoers specify sudoers file location");
+ (void) puts(" -h display help message and exit");
+ (void) puts(" -q less verbose (quiet) syntax error messages");
+ (void) puts(" -s strict syntax checking");
+ (void) puts(" -V display version information and exit");
+ exit(0);
}