]> git.armaanb.net Git - opendoas.git/blob - libopenbsd/setusercontext.c
Implement the semantics of setusercontext().
[opendoas.git] / libopenbsd / setusercontext.c
1 /* Copyright 2015 Nathan Holstein */
2
3 #include <sys/resource.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <errno.h>
7 #include <pwd.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10
11 #include "openbsd.h"
12
13 int
14 setusercontext(login_cap_t *lc, struct passwd *pw, uid_t uid, unsigned int flags)
15 {
16         int ret;
17
18         if (lc != NULL || pw == NULL ||
19                         (flags & ~(LOGIN_SETGROUP | LOGIN_SETPRIORITY |
20                                    LOGIN_SETRESOURCES | LOGIN_SETUMASK |
21                                    LOGIN_SETUSER)) != 0) {
22                 errno = EINVAL;
23                 return -1;
24         }
25
26         if (flags & LOGIN_SETGROUP) {
27                 if ((ret = setgid(pw->pw_gid)) != 0)
28                         return ret;
29                 if ((ret = initgroups(pw->pw_name, pw->pw_gid)) != 0)
30                         return ret;
31         }
32
33         if (flags & LOGIN_SETPRIORITY) {
34                 if ((ret = setpriority(PRIO_PROCESS, getpid(), 0)) != 0)
35                         return ret;
36                 if ((ret = setpriority(PRIO_USER, uid, 0)) != 0)
37                         return ret;
38         }
39
40         if (flags & LOGIN_SETRESOURCES) {
41         }
42
43         if (flags & LOGIN_SETUMASK)
44                 umask(S_IWGRP | S_IWOTH);
45
46         if (flags & LOGIN_SETUSER)
47                 return setuid(uid);
48
49         return 0;
50 }
51