]> git.armaanb.net Git - opendoas.git/blob - libopenbsd/bsd-setres_id.c
use config.h and link objects instead of libopenbsd.a
[opendoas.git] / libopenbsd / bsd-setres_id.c
1 /*
2  * Copyright (c) 2012 Darren Tucker (dtucker at zip com au).
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 "config.h"
18
19 #include <sys/types.h>
20
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <string.h>
25
26 #if !defined(HAVE_SETRESGID) || defined(BROKEN_SETRESGID)
27 int
28 setresgid(gid_t rgid, gid_t egid, gid_t sgid)
29 {
30         int ret = 0;
31
32         if (rgid != sgid) {
33                 errno = ENOSYS;
34                 return -1;
35         }
36 #if defined(HAVE_SETREGID) && !defined(BROKEN_SETREGID)
37         if (setregid(rgid, egid) < 0) {
38                 ret = -1;
39         }
40 #else
41         if (setegid(egid) < 0) {
42                 ret = -1;
43         }
44         if (setgid(rgid) < 0) {
45                 ret = -1;
46         }
47 #endif
48         return ret;
49 }
50 #endif
51
52 #if !defined(HAVE_SETRESUID) || defined(BROKEN_SETRESUID)
53 int
54 setresuid(uid_t ruid, uid_t euid, uid_t suid)
55 {
56         int ret = 0;
57
58         if (ruid != suid) {
59                 errno = ENOSYS;
60                 return -1;
61         }
62 #if defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
63         if (setreuid(ruid, euid) < 0) {
64                 ret = -1;
65         }
66 #else
67
68 # ifndef SETEUID_BREAKS_SETUID
69         if (seteuid(euid) < 0) {
70                 ret = -1;
71         }
72 # endif
73         if (setuid(ruid) < 0) {
74                 ret = -1;
75         }
76 #endif
77         return ret;
78 }
79 #endif