-/* $OpenBSD: doas.c,v 1.9 2015/07/18 18:44:26 tedu Exp $ */
+/* $OpenBSD: doas.c,v 1.28 2015/07/27 15:38:11 espie Exp $ */
/*
* Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
*
static void __dead
usage(void)
{
- fprintf(stderr, "usage: doas [-s] [-u user] command [args]\n");
+ fprintf(stderr, "usage: doas [-ns] [-C config] [-u user] command [args]\n");
exit(1);
}
static int
match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
- struct rule *r)
+ const char **cmdargs, struct rule *r)
{
int i;
}
if (r->target && uidcheck(r->target, target) != 0)
return 0;
- if (r->cmd && strcmp(r->cmd, cmd) != 0)
- return 0;
+ if (r->cmd) {
+ if (strcmp(r->cmd, cmd))
+ return 0;
+ if (r->cmdargs) {
+ /* if arguments were given, they should match explicitly */
+ for (i = 0; r->cmdargs[i]; i++) {
+ if (!cmdargs[i])
+ return 0;
+ if (strcmp(r->cmdargs[i], cmdargs[i]))
+ return 0;
+ }
+ if (cmdargs[i])
+ return 0;
+ }
+ }
return 1;
}
static int
permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
- uid_t target, const char *cmd)
+ uid_t target, const char *cmd, const char **cmdargs)
{
int i;
*lastr = NULL;
for (i = 0; i < nrules; i++) {
- if (match(uid, groups, ngroups, target, cmd, rules[i]))
+ if (match(uid, groups, ngroups, target, cmd, cmdargs, rules[i]))
*lastr = rules[i];
}
if (!*lastr)
}
static void
-parseconfig(const char *filename)
+parseconfig(const char *filename, int checkperms)
{
extern FILE *yyfp;
extern int yyparse(void);
yyfp = fopen(filename, "r");
if (!yyfp) {
- fprintf(stderr, "doas is not enabled.\n");
+ if (checkperms)
+ fprintf(stderr, "doas is not enabled.\n");
+ else
+ warn("could not open config file");
exit(1);
}
- if (fstat(fileno(yyfp), &sb) != 0)
- err(1, "fstat(\"%s\")", filename);
- if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
- errx(1, "%s is writable by group or other", filename);
- if (sb.st_uid != 0)
- errx(1, "%s is not owned by root", filename);
+ if (checkperms) {
+ if (fstat(fileno(yyfp), &sb) != 0)
+ err(1, "fstat(\"%s\")", filename);
+ if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
+ errx(1, "%s is writable by group or other", filename);
+ if (sb.st_uid != 0)
+ errx(1, "%s is not owned by root", filename);
+ }
yyparse();
fclose(yyfp);
+ if (parse_errors)
+ exit(1);
}
static int
copyenv(const char **oldenvp, struct rule *rule)
{
const char *safeset[] = {
- "DISPLAY", "HOME", "LOGNAME", "MAIL", "SHELL",
+ "DISPLAY", "HOME", "LOGNAME", "MAIL",
"PATH", "TERM", "USER", "USERNAME",
- NULL,
+ NULL
+ };
+ const char *badset[] = {
+ "ENV",
+ NULL
};
char **envp;
const char **extra;
int ei;
- int i, j;
- int nsafe;
+ int nsafe, nbad;
int nextras = 0;
-
+
+ nbad = arraylen(badset);
if ((rule->options & KEEPENV) && !rule->envlist) {
- j = arraylen(oldenvp);
- envp = reallocarray(NULL, j + 1, sizeof(char *));
+ size_t i, ii;
+ size_t oldlen = arraylen(oldenvp);
+ envp = reallocarray(NULL, oldlen + 1, sizeof(char *));
if (!envp)
err(1, "reallocarray");
- for (i = 0; i < j; i++) {
- if (!(envp[i] = strdup(oldenvp[i])))
- err(1, "strdup");
+ for (ii = i = 0; i < oldlen; i++) {
+ size_t j;
+ for (j = 0; j < nbad; j++) {
+ size_t len = strlen(badset[j]);
+ if (strncmp(oldenvp[i], badset[j], len) == 0 &&
+ oldenvp[i][len] == '=') {
+ break;
+ }
+ }
+ if (j == nbad) {
+ if (!(envp[ii] = strdup(oldenvp[i])))
+ err(1, "strdup");
+ ii++;
+ }
}
- envp[i] = NULL;
+ envp[ii] = NULL;
return envp;
}
nsafe = arraylen(safeset);
if ((extra = rule->envlist)) {
+ size_t i;
nextras = arraylen(extra);
for (i = 0; i < nsafe; i++) {
+ size_t j;
for (j = 0; j < nextras; j++) {
if (strcmp(extra[j], safeset[i]) == 0) {
- extra[j--] = extra[nextras--];
+ nextras--;
+ extra[j] = extra[nextras];
extra[nextras] = NULL;
+ j--;
}
}
}
exit(1);
}
+static void __dead
+checkconfig(const char *confpath, int argc, char **argv,
+ uid_t uid, gid_t *groups, int ngroups, uid_t target)
+{
+ struct rule *rule;
+
+ setresuid(uid, uid, uid);
+ parseconfig(confpath, 0);
+ if (!argc)
+ exit(0);
+
+ if (permit(uid, groups, ngroups, &rule, target, argv[0],
+ (const char **)argv + 1)) {
+ printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
+ exit(0);
+ } else {
+ printf("deny\n");
+ exit(1);
+ }
+}
+
int
main(int argc, char **argv, char **envp)
{
const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
"/usr/local/bin:/usr/local/sbin";
+ const char *confpath = NULL;
char *shargv[] = { NULL, NULL };
char *sh;
const char *cmd;
int ngroups;
int i, ch;
int sflag = 0;
+ int nflag = 0;
- parseconfig("/etc/doas.conf");
-
- while ((ch = getopt(argc, argv, "su:")) != -1) {
+ uid = getuid();
+ while ((ch = getopt(argc, argv, "C:nsu:")) != -1) {
switch (ch) {
+ case 'C':
+ confpath = optarg;
+ break;
case 'u':
if (parseuid(optarg, &target) != 0)
errx(1, "unknown user");
break;
+ case 'n':
+ nflag = 1;
+ break;
case 's':
sflag = 1;
break;
argv += optind;
argc -= optind;
- if ((!sflag && !argc) || (sflag && argc))
+ if (confpath) {
+ if (sflag)
+ usage();
+ } else if ((!sflag && !argc) || (sflag && argc))
usage();
uid = getuid();
argc = 1;
}
- cmd = argv[0];
- if (strlcpy(cmdline, argv[0], sizeof(cmdline)) >= sizeof(cmdline))
- errx(1, "command line too long");
+ if (confpath) {
+ checkconfig(confpath, argc, argv, uid, groups, ngroups,
+ target);
+ exit(1); /* fail safe */
+ }
+
+ parseconfig("/etc/doas.conf", 1);
+
+ /* cmdline is used only for logging, no need to abort on truncate */
+ (void) strlcpy(cmdline, argv[0], sizeof(cmdline));
for (i = 1; i < argc; i++) {
if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
- errx(1, "command line too long");
+ break;
if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
- errx(1, "command line too long");
+ break;
}
- if (!permit(uid, groups, ngroups, &rule, target, cmd)) {
+ cmd = argv[0];
+ if (!permit(uid, groups, ngroups, &rule, target, cmd,
+ (const char**)argv + 1)) {
syslog(LOG_AUTHPRIV | LOG_NOTICE,
"failed command for %s: %s", myname, cmdline);
fail();
}
if (!(rule->options & NOPASS)) {
+ if (nflag)
+ errx(1, "Authorization required");
if (!auth_userokay(myname, NULL, NULL, NULL)) {
syslog(LOG_AUTHPRIV | LOG_NOTICE,
"failed password for %s", myname);