]> git.armaanb.net Git - opendoas.git/blobdiff - doas.c
check for login_cap.h and use setusercontext if available
[opendoas.git] / doas.c
diff --git a/doas.c b/doas.c
index 008132b3a6e0d8c485f040049a973f54cec18040..c95dee3b7af0ff87a9b0d22d1263037aa4a5967d 100644 (file)
--- a/doas.c
+++ b/doas.c
@@ -20,6 +20,9 @@
 #include <sys/ioctl.h>
 
 #include <limits.h>
+#ifdef HAVE_LOGIN_CAP_H
+#include <login_cap.h>
+#endif
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -340,7 +343,7 @@ main(int argc, char **argv)
        if (!permit(uid, groups, ngroups, &rule, target, cmd,
            (const char **)argv + 1)) {
                syslog(LOG_AUTHPRIV | LOG_NOTICE,
-                   "failed command for %s: %s", mypw->pw_name, cmdline);
+                   "command not permitted for %s: %s", mypw->pw_name, cmdline);
                errc(1, EPERM, NULL);
        }
 
@@ -379,20 +382,30 @@ main(int argc, char **argv)
            rule->options & PERSIST);
 #endif
 
+#ifdef HAVE_LOGIN_CAP_H
+       if (setusercontext(NULL, targpw, target, LOGIN_SETGROUP |
+           LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
+           LOGIN_SETUSER) != 0)
+               errx(1, "failed to set user context for target");
+#else
        if (setresgid(targpw->pw_gid, targpw->pw_gid, targpw->pw_gid) != 0)
                err(1, "setresgid");
        if (initgroups(targpw->pw_name, targpw->pw_gid) != 0)
                err(1, "initgroups");
        if (setresuid(target, target, target) != 0)
                err(1, "setresuid");
+#endif
 
        if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
                cwd = "(failed)";
        else
                cwd = cwdpath;
 
-       syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
-           mypw->pw_name, cmdline, targpw->pw_name, cwd);
+       if (!(rule->options & NOLOG)) {
+               syslog(LOG_AUTHPRIV | LOG_INFO,
+                   "%s ran command %s as %s from %s",
+                   mypw->pw_name, cmdline, targpw->pw_name, cwd);
+       }
 
        envp = prepenv(rule, mypw, targpw);