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 "includes.h"
18
19 #include <sys/types.h>
20
21 #include <stdarg.h>
22 #include <unistd.h>
23 #include <string.h>
24
25 #include "log.h"
26
27 #if !defined(HAVE_SETRESGID) || defined(BROKEN_SETRESGID)
28 int
setresgid(gid_t rgid,gid_t egid,gid_t sgid)29 setresgid(gid_t rgid, gid_t egid, gid_t sgid)
30 {
31 int ret = 0, saved_errno;
32
33 if (rgid != sgid) {
34 errno = ENOSYS;
35 return -1;
36 }
37 #if defined(HAVE_SETREGID) && !defined(BROKEN_SETREGID)
38 if (setregid(rgid, egid) < 0) {
39 saved_errno = errno;
40 error("setregid %lu: %.100s", (u_long)rgid, strerror(errno));
41 errno = saved_errno;
42 ret = -1;
43 }
44 #else
45 if (setegid(egid) < 0) {
46 saved_errno = errno;
47 error("setegid %lu: %.100s", (u_long)egid, strerror(errno));
48 errno = saved_errno;
49 ret = -1;
50 }
51 if (setgid(rgid) < 0) {
52 saved_errno = errno;
53 error("setgid %lu: %.100s", (u_long)rgid, strerror(errno));
54 errno = saved_errno;
55 ret = -1;
56 }
57 #endif
58 return ret;
59 }
60 #endif
61
62 #if !defined(HAVE_SETRESUID) || defined(BROKEN_SETRESUID)
63 int
setresuid(uid_t ruid,uid_t euid,uid_t suid)64 setresuid(uid_t ruid, uid_t euid, uid_t suid)
65 {
66 int ret = 0, saved_errno;
67
68 if (ruid != suid) {
69 errno = ENOSYS;
70 return -1;
71 }
72 #if defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
73 if (setreuid(ruid, euid) < 0) {
74 saved_errno = errno;
75 error("setreuid %lu: %.100s", (u_long)ruid, strerror(errno));
76 errno = saved_errno;
77 ret = -1;
78 }
79 #else
80
81 # ifndef SETEUID_BREAKS_SETUID
82 if (seteuid(euid) < 0) {
83 saved_errno = errno;
84 error("seteuid %lu: %.100s", (u_long)euid, strerror(errno));
85 errno = saved_errno;
86 ret = -1;
87 }
88 # endif
89 if (setuid(ruid) < 0) {
90 saved_errno = errno;
91 error("setuid %lu: %.100s", (u_long)ruid, strerror(errno));
92 errno = saved_errno;
93 ret = -1;
94 }
95 #endif
96 return ret;
97 }
98 #endif
99