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.
16 * Note: all these functions must work in all of the following cases:
20 * Additionally, they must work regardless of whether the system has
21 * POSIX saved uids or not.
24 #ifdef _POSIX_SAVED_IDS
25 /* Lets assume that posix saved ids also work with seteuid, even though that
26 is not part of the posix specification. */
27 #define SAVED_IDS_WORK_WITH_SETEUID
29 /* Saved effective uid. */
30 static uid_t saved_euid = 0;
32 #endif /* _POSIX_SAVED_IDS */
35 * Temporarily changes to the given uid. If the effective user
36 * id is not root, this does nothing. This call cannot be nested.
39 temporarily_use_uid(uid_t uid)
41 #ifdef SAVED_IDS_WORK_WITH_SETEUID
42 /* Save the current euid. */
43 saved_euid = geteuid();
45 /* Set the effective uid to the given (unprivileged) uid. */
46 if (seteuid(uid) == -1)
47 debug("seteuid %d: %.100s", (int) uid, strerror(errno));
48 #else /* SAVED_IDS_WORK_WITH_SETUID */
49 /* Propagate the privileged uid to all of our uids. */
50 if (setuid(geteuid()) < 0)
51 debug("setuid %d: %.100s", (int) geteuid(), strerror(errno));
53 /* Set the effective uid to the given (unprivileged) uid. */
54 if (seteuid(uid) == -1)
55 debug("seteuid %d: %.100s", (int) uid, strerror(errno));
56 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
60 * Restores to the original uid.
65 #ifdef SAVED_IDS_WORK_WITH_SETEUID
66 /* Set the effective uid back to the saved uid. */
67 if (seteuid(saved_euid) < 0)
68 debug("seteuid %d: %.100s", (int) saved_euid, strerror(errno));
69 #else /* SAVED_IDS_WORK_WITH_SETEUID */
71 * We are unable to restore the real uid to its unprivileged value.
72 * Propagate the real uid (usually more privileged) to effective uid
76 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
80 * Permanently sets all uids to the given uid. This cannot be
81 * called while temporarily_use_uid is effective.
84 permanently_set_uid(uid_t uid)
87 debug("setuid %d: %.100s", (int) uid, strerror(errno));