]> git.armaanb.net Git - opendoas.git/blobdiff - doas.c
Implement command matching without execution. This just extends
[opendoas.git] / doas.c
diff --git a/doas.c b/doas.c
index b77e17f6953eb88faa8bd69f95f5ac8fc7a3f35d..79639261f61ded5a007ad820c3c6be7590ea41ef 100644 (file)
--- a/doas.c
+++ b/doas.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: doas.c,v 1.14 2015/07/20 01:04:37 tedu Exp $ */
+/* $OpenBSD: doas.c,v 1.21 2015/07/24 06:36:42 zhuk Exp $ */
 /*
  * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
  *
@@ -36,7 +36,7 @@
 static void __dead
 usage(void)
 {
-       fprintf(stderr, "usage: doas [-s] [-u user] command [args]\n");
+       fprintf(stderr, "usage: doas [-s] [-C config] [-u user] command [args]\n");
        exit(1);
 }
 
@@ -152,7 +152,7 @@ permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
 }
 
 static void
-parseconfig(const char *filename)
+parseconfig(const char *filename, int checkperms)
 {
        extern FILE *yyfp;
        extern int yyparse(void);
@@ -160,19 +160,26 @@ parseconfig(const char *filename)
 
        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
@@ -214,7 +221,7 @@ copyenv(const char **oldenvp, struct rule *rule)
        int ei;
        int nsafe, nbad;
        int nextras = 0;
-       
+
        nbad = arraylen(badset);
        if ((rule->options & KEEPENV) && !rule->envlist) {
                size_t i, ii;
@@ -227,7 +234,7 @@ copyenv(const char **oldenvp, struct rule *rule)
                        for (j = 0; j < nbad; j++) {
                                size_t len = strlen(badset[j]);
                                if (strncmp(oldenvp[i], badset[j], len) == 0 &&
-                                   oldenvp[i][len] == '=') {
+                                   oldenvp[i][len] == '=') {
                                        break;
                                }
                        }
@@ -275,11 +282,32 @@ fail(void)
        exit(1);
 }
 
+static int
+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" : "");
+               return 1;
+       } else {
+               printf("deny\n");
+               return 0;
+       }
+}
+
 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;
@@ -294,10 +322,11 @@ main(int argc, char **argv, char **envp)
        int i, ch;
        int sflag = 0;
 
-       parseconfig("/etc/doas.conf");
-
-       while ((ch = getopt(argc, argv, "su:")) != -1) {
+       while ((ch = getopt(argc, argv, "C:su:")) != -1) {
                switch (ch) {
+               case 'C':
+                       confpath = optarg;
+                       break;
                case 'u':
                        if (parseuid(optarg, &target) != 0)
                                errx(1, "unknown user");
@@ -313,7 +342,10 @@ main(int argc, char **argv, char **envp)
        argv += optind;
        argc -= optind;
 
-       if ((!sflag && !argc) || (sflag && argc))
+       if (confpath) {
+               if (sflag)
+                       usage();
+       } else if ((!sflag && !argc) || (sflag && argc))
                usage();
 
        uid = getuid();
@@ -337,6 +369,11 @@ main(int argc, char **argv, char **envp)
                argc = 1;
        }
 
+       if (confpath)
+               exit(!checkconfig(confpath, argc, argv, uid, groups, ngroups,
+                   target));
+       parseconfig("/etc/doas.conf", 1);
+
        cmd = argv[0];
        if (strlcpy(cmdline, argv[0], sizeof(cmdline)) >= sizeof(cmdline))
                errx(1, "command line too long");