1 /*
2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. All rights reserved.
3 * Copyright 2002-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
4 * Distributed under the terms of the MIT License.
5 */
6
7
8 #include <syscalls.h>
9
10 #include <ctype.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <limits.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <pwd.h>
19
20 #include <errno_private.h>
21
22
23 template<typename T>
24 static inline T
set_errno_if_necessary(const T & result)25 set_errno_if_necessary(const T& result)
26 {
27 if (result < 0) {
28 __set_errno(result);
29 return -1;
30 }
31
32 return result;
33 }
34
35
36 // #pragma mark -
37
38
39 gid_t
getegid(void)40 getegid(void)
41 {
42 return _kern_getgid(true);
43 }
44
45
46 uid_t
geteuid(void)47 geteuid(void)
48 {
49 return _kern_getuid(true);
50 }
51
52
53 gid_t
getgid(void)54 getgid(void)
55 {
56 return _kern_getgid(false);
57 }
58
59
60 uid_t
getuid(void)61 getuid(void)
62 {
63 return _kern_getuid(false);
64 }
65
66
67 int
setgid(gid_t gid)68 setgid(gid_t gid)
69 {
70 return set_errno_if_necessary(_kern_setregid(gid, (gid_t)-1, true));
71 }
72
73
74 int
setuid(uid_t uid)75 setuid(uid_t uid)
76 {
77 return set_errno_if_necessary(_kern_setreuid(uid, (uid_t)-1, true));
78 }
79
80
81 int
setegid(gid_t gid)82 setegid(gid_t gid)
83 {
84 return set_errno_if_necessary(_kern_setregid((gid_t)-1, gid, false));
85 }
86
87
88 int
seteuid(uid_t uid)89 seteuid(uid_t uid)
90 {
91 return set_errno_if_necessary(_kern_setreuid((uid_t)-1, uid, false));
92 }
93
94
95 int
setregid(gid_t rgid,gid_t egid)96 setregid(gid_t rgid, gid_t egid)
97 {
98 return set_errno_if_necessary(_kern_setregid(rgid, egid, false));
99 }
100
101
102 int
setreuid(uid_t ruid,uid_t euid)103 setreuid(uid_t ruid, uid_t euid)
104 {
105 return set_errno_if_necessary(_kern_setreuid(ruid, euid, false));
106 }
107
108
109 int
getgroups(int groupCount,gid_t groupList[])110 getgroups(int groupCount, gid_t groupList[])
111 {
112 return set_errno_if_necessary(_kern_getgroups(groupCount, groupList));
113 }
114
115
116 int
setgroups(int groupCount,const gid_t * groupList)117 setgroups(int groupCount, const gid_t* groupList)
118 {
119 return set_errno_if_necessary(_kern_setgroups(groupCount, groupList));
120 }
121
122 // NOTE: getgrouplist() and initgroups() are implemented in grp.cpp.
123