1 /* $Id: bsd-setres_id.c,v 1.2 2013/12/07 21:23:09 djm Exp $ */
2
3 /*
4 * Copyright (c) 2012 Darren Tucker (dtucker at zip com au).
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "includes.h"
20
21 #include <sys/types.h>
22
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #include "log.h"
28
29 #if !defined(HAVE_SETRESGID) || defined(BROKEN_SETRESGID)
30 int
setresgid(gid_t rgid,gid_t egid,gid_t sgid)31 setresgid(gid_t rgid, gid_t egid, gid_t sgid)
32 {
33 int ret = 0, saved_errno;
34
35 if (rgid != sgid) {
36 errno = ENOSYS;
37 return -1;
38 }
39 #if defined(HAVE_SETREGID) && !defined(BROKEN_SETREGID)
40 if (setregid(rgid, egid) < 0) {
41 saved_errno = errno;
42 error("setregid %u: %.100s", rgid, strerror(errno));
43 errno = saved_errno;
44 ret = -1;
45 }
46 #else
47 if (setegid(egid) < 0) {
48 saved_errno = errno;
49 error("setegid %u: %.100s", (u_int)egid, strerror(errno));
50 errno = saved_errno;
51 ret = -1;
52 }
53 if (setgid(rgid) < 0) {
54 saved_errno = errno;
55 error("setgid %u: %.100s", rgid, strerror(errno));
56 errno = saved_errno;
57 ret = -1;
58 }
59 #endif
60 return ret;
61 }
62 #endif
63
64 #if !defined(HAVE_SETRESUID) || defined(BROKEN_SETRESUID)
65 int
setresuid(uid_t ruid,uid_t euid,uid_t suid)66 setresuid(uid_t ruid, uid_t euid, uid_t suid)
67 {
68 int ret = 0, saved_errno;
69
70 if (ruid != suid) {
71 errno = ENOSYS;
72 return -1;
73 }
74 #if defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
75 if (setreuid(ruid, euid) < 0) {
76 saved_errno = errno;
77 error("setreuid %u: %.100s", ruid, strerror(errno));
78 errno = saved_errno;
79 ret = -1;
80 }
81 #else
82
83 # ifndef SETEUID_BREAKS_SETUID
84 if (seteuid(euid) < 0) {
85 saved_errno = errno;
86 error("seteuid %u: %.100s", euid, strerror(errno));
87 errno = saved_errno;
88 ret = -1;
89 }
90 # endif
91 if (setuid(ruid) < 0) {
92 saved_errno = errno;
93 error("setuid %u: %.100s", ruid, strerror(errno));
94 errno = saved_errno;
95 ret = -1;
96 }
97 #endif
98 return ret;
99 }
100 #endif
101