diff options
author | Ben Lindstrom <mouring@eviladmin.org> | 2001-04-26 23:03:37 +0000 |
---|---|---|
committer | Ben Lindstrom <mouring@eviladmin.org> | 2001-04-26 23:03:37 +0000 |
commit | 4468b260cf894cf659b7e92294ee51745cab80a3 (patch) | |
tree | efa510b4dd8c53397f164bd2394643fbfb41c6c6 | |
parent | 07183b8e17da2fcb077f5205047ac0cf6b5805af (diff) |
- (bal) Fixed uidswap.c so it should work on non-posix complient systems.
patch based on 2.5.2 version by djm.
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | uidswap.c | 47 |
2 files changed, 45 insertions, 8 deletions
@@ -1,3 +1,7 @@ | |||
1 | 20010427 | ||
2 | - (bal) Fixed uidswap.c so it should work on non-posix complient systems. | ||
3 | patch based on 2.5.2 version by djm. | ||
4 | |||
1 | 20010425 | 5 | 20010425 |
2 | - OpenBSD CVS Sync | 6 | - OpenBSD CVS Sync |
3 | - markus@cvs.openbsd.org 2001/04/23 21:57:07 | 7 | - markus@cvs.openbsd.org 2001/04/23 21:57:07 |
@@ -5252,4 +5256,4 @@ | |||
5252 | - Wrote replacements for strlcpy and mkdtemp | 5256 | - Wrote replacements for strlcpy and mkdtemp |
5253 | - Released 1.0pre1 | 5257 | - Released 1.0pre1 |
5254 | 5258 | ||
5255 | $Id: ChangeLog,v 1.1171 2001/04/26 04:40:28 tim Exp $ | 5259 | $Id: ChangeLog,v 1.1172 2001/04/26 23:03:37 mouring Exp $ |
@@ -26,14 +26,18 @@ RCSID("$OpenBSD: uidswap.c,v 1.16 2001/04/20 16:32:22 markus Exp $"); | |||
26 | * POSIX saved uids or not. | 26 | * POSIX saved uids or not. |
27 | */ | 27 | */ |
28 | 28 | ||
29 | #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS) | ||
29 | /* Lets assume that posix saved ids also work with seteuid, even though that | 30 | /* Lets assume that posix saved ids also work with seteuid, even though that |
30 | is not part of the posix specification. */ | 31 | is not part of the posix specification. */ |
32 | #define SAVED_IDS_WORK_WITH_SETEUID | ||
33 | /* Saved effective uid. */ | ||
34 | static uid_t saved_euid = 0; | ||
35 | static gid_t saved_egid = 0; | ||
36 | #endif | ||
31 | 37 | ||
32 | /* Saved effective uid. */ | 38 | /* Saved effective uid. */ |
33 | static int privileged = 0; | 39 | static int privileged = 0; |
34 | static int temporarily_use_uid_effective = 0; | 40 | static int temporarily_use_uid_effective = 0; |
35 | static uid_t saved_euid = 0; | ||
36 | static gid_t saved_egid; | ||
37 | static gid_t saved_egroups[NGROUPS_MAX], user_groups[NGROUPS_MAX]; | 41 | static gid_t saved_egroups[NGROUPS_MAX], user_groups[NGROUPS_MAX]; |
38 | static int saved_egroupslen = -1, user_groupslen = -1; | 42 | static int saved_egroupslen = -1, user_groupslen = -1; |
39 | 43 | ||
@@ -45,16 +49,24 @@ void | |||
45 | temporarily_use_uid(struct passwd *pw) | 49 | temporarily_use_uid(struct passwd *pw) |
46 | { | 50 | { |
47 | /* Save the current euid, and egroups. */ | 51 | /* Save the current euid, and egroups. */ |
52 | #ifdef SAVED_IDS_WORK_WITH_SETEUID | ||
48 | saved_euid = geteuid(); | 53 | saved_euid = geteuid(); |
54 | saved_egid = getegid(); | ||
49 | debug("temporarily_use_uid: %d/%d (e=%d)", | 55 | debug("temporarily_use_uid: %d/%d (e=%d)", |
50 | pw->pw_uid, pw->pw_gid, saved_euid); | 56 | pw->pw_uid, pw->pw_gid, saved_euid); |
51 | if (saved_euid != 0) { | 57 | if (saved_euid != 0) { |
52 | privileged = 0; | 58 | privileged = 0; |
53 | return; | 59 | return; |
54 | } | 60 | } |
61 | #else | ||
62 | if (geteuid() != 0) { | ||
63 | privileged = 0; | ||
64 | return; | ||
65 | } | ||
66 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ | ||
67 | |||
55 | privileged = 1; | 68 | privileged = 1; |
56 | temporarily_use_uid_effective = 1; | 69 | temporarily_use_uid_effective = 1; |
57 | saved_egid = getegid(); | ||
58 | saved_egroupslen = getgroups(NGROUPS_MAX, saved_egroups); | 70 | saved_egroupslen = getgroups(NGROUPS_MAX, saved_egroups); |
59 | if (saved_egroupslen < 0) | 71 | if (saved_egroupslen < 0) |
60 | fatal("getgroups: %.100s", strerror(errno)); | 72 | fatal("getgroups: %.100s", strerror(errno)); |
@@ -71,7 +83,14 @@ temporarily_use_uid(struct passwd *pw) | |||
71 | /* Set the effective uid to the given (unprivileged) uid. */ | 83 | /* Set the effective uid to the given (unprivileged) uid. */ |
72 | if (setgroups(user_groupslen, user_groups) < 0) | 84 | if (setgroups(user_groupslen, user_groups) < 0) |
73 | fatal("setgroups: %.100s", strerror(errno)); | 85 | fatal("setgroups: %.100s", strerror(errno)); |
74 | pw->pw_gid = pw->pw_gid; | 86 | #ifndef SAVED_IDS_WORK_WITH_SETEUID |
87 | /* Propagate the privileged gid to all of our gids. */ | ||
88 | if (setgid(getegid()) < 0) | ||
89 | debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno)); | ||
90 | /* Propagate the privileged uid to all of our uids. */ | ||
91 | if (setuid(geteuid()) < 0) | ||
92 | debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno)); | ||
93 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ | ||
75 | if (setegid(pw->pw_gid) < 0) | 94 | if (setegid(pw->pw_gid) < 0) |
76 | fatal("setegid %u: %.100s", (u_int) pw->pw_gid, | 95 | fatal("setegid %u: %.100s", (u_int) pw->pw_gid, |
77 | strerror(errno)); | 96 | strerror(errno)); |
@@ -92,13 +111,27 @@ restore_uid(void) | |||
92 | return; | 111 | return; |
93 | if (!temporarily_use_uid_effective) | 112 | if (!temporarily_use_uid_effective) |
94 | fatal("restore_uid: temporarily_use_uid not effective"); | 113 | fatal("restore_uid: temporarily_use_uid not effective"); |
114 | |||
115 | #ifdef SAVED_IDS_WORK_WITH_SETEUID | ||
95 | /* Set the effective uid back to the saved privileged uid. */ | 116 | /* Set the effective uid back to the saved privileged uid. */ |
96 | if (seteuid(saved_euid) < 0) | 117 | if (seteuid(saved_euid) < 0) |
97 | fatal("seteuid %u: %.100s", (u_int) saved_euid, strerror(errno)); | 118 | fatal("seteuid %u: %.100s", (u_int) saved_euid, |
119 | strerror(errno)); | ||
120 | if (setegid(saved_egid) < 0) | ||
121 | fatal("setegid %u: %.100s", (u_int) saved_egid, | ||
122 | strerror(errno)); | ||
123 | #else /* SAVED_IDS_WORK_WITH_SETEUID */ | ||
124 | /* | ||
125 | * We are unable to restore the real uid to its unprivileged value. | ||
126 | * Propagate the real uid (usually more privileged) to effective uid | ||
127 | * as well. | ||
128 | */ | ||
129 | setuid(getuid()); | ||
130 | setgid(getgid()); | ||
131 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ | ||
132 | |||
98 | if (setgroups(saved_egroupslen, saved_egroups) < 0) | 133 | if (setgroups(saved_egroupslen, saved_egroups) < 0) |
99 | fatal("setgroups: %.100s", strerror(errno)); | 134 | fatal("setgroups: %.100s", strerror(errno)); |
100 | if (setegid(saved_egid) < 0) | ||
101 | fatal("setegid %u: %.100s", (u_int) saved_egid, strerror(errno)); | ||
102 | temporarily_use_uid_effective = 0; | 135 | temporarily_use_uid_effective = 0; |
103 | } | 136 | } |
104 | 137 | ||