2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Created: Sat Sep 9 01:56:14 1995 ylo
6 * Code for uid-swapping.
10 RCSID("$OpenBSD: uidswap.c,v 1.7 2000/06/20 01:39:45 markus Exp $");
14 #ifdef WITH_IRIX_AUDIT
16 #endif /* WITH_IRIX_AUDIT */
19 * Note: all these functions must work in all of the following cases:
23 * Additionally, they must work regardless of whether the system has
24 * POSIX saved uids or not.
27 #ifdef _POSIX_SAVED_IDS
28 /* Lets assume that posix saved ids also work with seteuid, even though that
29 is not part of the posix specification. */
30 #define SAVED_IDS_WORK_WITH_SETEUID
32 /* Saved effective uid. */
33 static uid_t saved_euid = 0;
35 #endif /* _POSIX_SAVED_IDS */
38 * Temporarily changes to the given uid. If the effective user
39 * id is not root, this does nothing. This call cannot be nested.
42 temporarily_use_uid(uid_t uid)
44 #ifdef SAVED_IDS_WORK_WITH_SETEUID
45 /* Save the current euid. */
46 saved_euid = geteuid();
48 /* Set the effective uid to the given (unprivileged) uid. */
49 if (seteuid(uid) == -1)
50 debug("seteuid %d: %.100s", (int) uid, strerror(errno));
51 #else /* SAVED_IDS_WORK_WITH_SETUID */
52 /* Propagate the privileged uid to all of our uids. */
53 if (setuid(geteuid()) < 0)
54 debug("setuid %d: %.100s", (int) geteuid(), strerror(errno));
56 /* Set the effective uid to the given (unprivileged) uid. */
57 if (seteuid(uid) == -1)
58 debug("seteuid %d: %.100s", (int) uid, strerror(errno));
59 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
63 * Restores to the original uid.
68 #ifdef SAVED_IDS_WORK_WITH_SETEUID
69 /* Set the effective uid back to the saved uid. */
70 if (seteuid(saved_euid) < 0)
71 debug("seteuid %d: %.100s", (int) saved_euid, strerror(errno));
72 #else /* SAVED_IDS_WORK_WITH_SETEUID */
74 * We are unable to restore the real uid to its unprivileged value.
75 * Propagate the real uid (usually more privileged) to effective uid
79 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
83 * Permanently sets all uids to the given uid. This cannot be
84 * called while temporarily_use_uid is effective.
87 permanently_set_uid(uid_t uid)
89 #ifdef WITH_IRIX_AUDIT
90 if (sysconf(_SC_AUDIT)) {
91 debug("Setting sat id to %d", (int) uid);
93 debug("error setting satid: %.100s", strerror(errno));
95 #endif /* WITH_IRIX_AUDIT */
98 debug("setuid %d: %.100s", (int) uid, strerror(errno));