]> git.armaanb.net Git - opendoas.git/blobdiff - doas.c
improve wording, from Thanos Tsouanas
[opendoas.git] / doas.c
diff --git a/doas.c b/doas.c
index 1e1fb530f737c28845080ac31554498734c6efb9..47ae35ada88b03cd7aec9ba31d4599f256327a8b 100644 (file)
--- a/doas.c
+++ b/doas.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: doas.c,v 1.2 2015/07/16 21:00:59 tedu Exp $ */
+/* $OpenBSD: doas.c,v 1.5 2015/07/16 22:11:01 nicm Exp $ */
 /*
  * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
  *
@@ -14,7 +14,9 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
+
 #include <sys/types.h>
+#include <sys/stat.h>
 
 #include <limits.h>
 #include <login_cap.h>
@@ -139,18 +141,28 @@ parseconfig(const char *filename)
 {
        extern FILE *yyfp;
        extern int yyparse(void);
+       struct stat sb;
 
        yyfp = fopen(filename, "r");
        if (!yyfp) {
                fprintf(stderr, "doas is not enabled.\n");
                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);
+
        yyparse();
        fclose(yyfp);
 }
 
 static int
-copyenvhelper(const char **oldenvp, const char **safeset, int nsafe, char **envp, int ei)
+copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
+    char **envp, int ei)
 {
        int i;
        for (i = 0; i < nsafe; i++) {
@@ -187,6 +199,8 @@ copyenv(const char **oldenvp, struct rule *rule)
        if ((rule->options & KEEPENV) && !rule->envlist) {
                j = arraylen(oldenvp);
                envp = reallocarray(NULL, j + 1, sizeof(char *));
+               if (!envp)
+                       err(1, "reallocarray");
                for (i = 0; i < j; i++) {
                        if (!(envp[i] = strdup(oldenvp[i])))
                                err(1, "strdup");
@@ -239,7 +253,8 @@ main(int argc, char **argv, char **envp)
        struct rule *rule;
        const char *cmd;
        int i, ch;
-       const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin";
+       const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
+           "/usr/local/bin:/usr/local/sbin";
 
        parseconfig("/etc/doas.conf");
 
@@ -278,13 +293,15 @@ main(int argc, char **argv, char **envp)
        groups[ngroups++] = getgid();
 
        if (!permit(uid, groups, ngroups, &rule, target, cmd)) {
-               syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed command for %s: %s", myname, cmdline);
+               syslog(LOG_AUTHPRIV | LOG_NOTICE,
+                   "failed command for %s: %s", myname, cmdline);
                fail();
        }
 
        if (!(rule->options & NOPASS)) {
                if (!auth_userokay(myname, NULL, NULL, NULL)) {
-                       syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed password for %s", myname);
+                       syslog(LOG_AUTHPRIV | LOG_NOTICE,
+                           "failed password for %s", myname);
                        fail();
                }
        }
@@ -298,7 +315,8 @@ main(int argc, char **argv, char **envp)
            LOGIN_SETUSER) != 0)
                errx(1, "failed to set user context for target");
 
-       syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s", myname, pw->pw_name, cmdline);
+       syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
+           myname, pw->pw_name, cmdline);
        setenv("PATH", safepath, 1);
        execvpe(cmd, argv, envp);
        err(1, "%s", cmd);