2 /* $OpenBSD: uidswap.c,v 1.35 2006/08/03 03:34:42 deraadt Exp $ */
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * Code for uid-swapping.
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
17 __RCSID("$NetBSD: uidswap.c,v 1.4 2006/09/28 21:22:15 christos Exp $");
18 #include <sys/param.h>
29 * Note: all these functions must work in all of the following cases:
33 * Additionally, they must work regardless of whether the system has
34 * POSIX saved uids or not.
37 /* Lets assume that posix saved ids also work with seteuid, even though that
38 is not part of the posix specification. */
40 /* Saved effective uid. */
41 static int privileged
= 0;
42 static int temporarily_use_uid_effective
= 0;
43 static uid_t saved_euid
= 0;
44 static gid_t saved_egid
;
45 static gid_t saved_egroups
[NGROUPS_MAX
], user_groups
[NGROUPS_MAX
];
46 static int saved_egroupslen
= -1, user_groupslen
= -1;
49 * Temporarily changes to the given uid. If the effective user
50 * id is not root, this does nothing. This call cannot be nested.
53 temporarily_use_uid(struct passwd
*pw
)
55 /* Save the current euid, and egroups. */
56 saved_euid
= geteuid();
57 saved_egid
= getegid();
58 debug("temporarily_use_uid: %u/%u (e=%u/%u)",
59 (u_int
)pw
->pw_uid
, (u_int
)pw
->pw_gid
,
60 (u_int
)saved_euid
, (u_int
)saved_egid
);
61 if (saved_euid
!= 0) {
66 temporarily_use_uid_effective
= 1;
67 saved_egroupslen
= getgroups(NGROUPS_MAX
, saved_egroups
);
68 if (saved_egroupslen
< 0)
69 fatal("getgroups: %.100s", strerror(errno
));
71 /* set and save the user's groups */
72 if (user_groupslen
== -1) {
73 if (initgroups(pw
->pw_name
, pw
->pw_gid
) < 0)
74 fatal("initgroups: %s: %.100s", pw
->pw_name
,
76 user_groupslen
= getgroups(NGROUPS_MAX
, user_groups
);
77 if (user_groupslen
< 0)
78 fatal("getgroups: %.100s", strerror(errno
));
80 /* Set the effective uid to the given (unprivileged) uid. */
81 if (setgroups(user_groupslen
, user_groups
) < 0)
82 fatal("setgroups: %.100s", strerror(errno
));
83 if (setegid(pw
->pw_gid
) < 0)
84 fatal("setegid %u: %.100s", (u_int
)pw
->pw_gid
,
86 if (seteuid(pw
->pw_uid
) == -1)
87 fatal("seteuid %u: %.100s", (u_int
)pw
->pw_uid
,
92 * Restores to the original (privileged) uid.
97 /* it's a no-op unless privileged */
99 debug("restore_uid: (unprivileged)");
102 if (!temporarily_use_uid_effective
)
103 fatal("restore_uid: temporarily_use_uid not effective");
104 debug("restore_uid: %u/%u", (u_int
)saved_euid
, (u_int
)saved_egid
);
105 /* Set the effective uid back to the saved privileged uid. */
106 if (seteuid(saved_euid
) < 0)
107 fatal("seteuid %u: %.100s", (u_int
)saved_euid
, strerror(errno
));
108 if (setgroups(saved_egroupslen
, saved_egroups
) < 0)
109 fatal("setgroups: %.100s", strerror(errno
));
110 if (setegid(saved_egid
) < 0)
111 fatal("setegid %u: %.100s", (u_int
)saved_egid
, strerror(errno
));
112 temporarily_use_uid_effective
= 0;
116 * Permanently sets all uids to the given uid. This cannot be
117 * called while temporarily_use_uid is effective.
120 permanently_set_uid(struct passwd
*pw
)
123 fatal("permanently_set_uid: no user given");
124 if (temporarily_use_uid_effective
)
125 fatal("permanently_set_uid: temporarily_use_uid effective");
126 debug("permanently_set_uid: %u/%u", (u_int
)pw
->pw_uid
,
129 if (setgid(pw
->pw_gid
) < 0)
130 fatal("setgid %u: %.100s", (u_int
)pw
->pw_gid
, strerror(errno
));
131 if (setegid(pw
->pw_gid
) < 0)
132 fatal("setegid %u: %.100s", (u_int
)pw
->pw_gid
, strerror(errno
));
134 if (setuid(pw
->pw_uid
) < 0)
135 fatal("setuid %u: %.100s", (u_int
)pw
->pw_uid
, strerror(errno
));
136 if (seteuid(pw
->pw_uid
) < 0)
137 fatal("seteuid %u: %.100s", (u_int
)pw
->pw_uid
, strerror(errno
));
139 /* Verify GID drop was successful */
140 if (getgid() != pw
->pw_gid
|| getegid() != pw
->pw_gid
) {
141 fatal("%s: egid incorrect gid:%u egid:%u (should be %u)",
142 __func__
, (u_int
)getgid(), (u_int
)getegid(),
146 /* Verify UID drop was successful */
147 if (getuid() != pw
->pw_uid
|| geteuid() != pw
->pw_uid
) {
148 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
149 __func__
, (u_int
)getuid(), (u_int
)geteuid(),
155 permanently_drop_suid(uid_t uid
)
157 debug("permanently_drop_suid: %u", (u_int
)uid
);
158 if (seteuid(uid
) < 0)
159 fatal("seteuid %u: %.100s", (u_int
)uid
, strerror(errno
));
161 fatal("setuid %u: %.100s", (u_int
)uid
, strerror(errno
));
163 /* Verify UID drop was successful */
164 if (getuid() != uid
|| geteuid() != uid
) {
165 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
166 __func__
, (u_int
)getuid(), (u_int
)geteuid(), (u_int
)uid
);