]> git.armaanb.net Git - opendoas.git/blob - libopenbsd/setusercontext.c
Add configure script
[opendoas.git] / libopenbsd / setusercontext.c
1 /*
2  * Copyright (c) 2015 Nathan Holstein <nathan.holstein@gmail.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <sys/resource.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <errno.h>
21 #include <pwd.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <grp.h>
25
26 #include "includes.h"
27
28 int
29 setusercontext(login_cap_t *lc, struct passwd *pw, uid_t uid, unsigned int flags)
30 {
31         int ret;
32
33         if (lc != NULL || pw == NULL ||
34                         (flags & ~(LOGIN_SETGROUP | LOGIN_SETPRIORITY |
35                                    LOGIN_SETRESOURCES | LOGIN_SETUMASK |
36                                    LOGIN_SETUSER)) != 0) {
37                 errno = EINVAL;
38                 return -1;
39         }
40
41         if (flags & LOGIN_SETGROUP) {
42                 if ((ret = setgid(pw->pw_gid)) != 0)
43                         return ret;
44                 if ((ret = initgroups(pw->pw_name, pw->pw_gid)) != 0)
45                         return ret;
46         }
47
48         if (flags & LOGIN_SETPRIORITY) {
49                 if ((ret = setpriority(PRIO_PROCESS, getpid(), 0)) != 0)
50                         return ret;
51                 if ((ret = setpriority(PRIO_USER, uid, 0)) != 0)
52                         return ret;
53         }
54
55         if (flags & LOGIN_SETRESOURCES) {
56         }
57
58         if (flags & LOGIN_SETUMASK)
59                 umask(S_IWGRP | S_IWOTH);
60
61         if (flags & LOGIN_SETUSER)
62                 return setuid(uid);
63
64         return 0;
65 }
66