1 /* Copyright (c) The Exim Maintainers 2022 */
2 /* SPDX-License-Identifier: GPL-2.0-or-later */
10 PRIV_DROPPING, PRIV_DROPPED,
11 PRIV_RESTORING, PRIV_RESTORED
12 } priv_state = PRIV_RESTORED;
15 static uid_t priv_euid;
16 static gid_t priv_egid;
17 static gid_t priv_groups[EXIM_GROUPLIST_SIZE + 1];
18 static int priv_ngroups;
20 /* Inspired by OpenSSH's temporarily_use_uid(). Thanks! */
23 priv_drop_temp(const uid_t temp_uid, const gid_t temp_gid)
25 if (priv_state != PRIV_RESTORED)
26 log_write(0, LOG_PANIC_DIE, "priv_drop_temp: unexpected priv_state %d != %d", priv_state, PRIV_RESTORED);
28 priv_state = PRIV_DROPPING;
30 priv_euid = geteuid();
31 if (priv_euid == root_uid)
33 priv_egid = getegid();
34 priv_ngroups = getgroups(nelem(priv_groups), priv_groups);
36 log_write(0, LOG_PANIC_DIE, "getgroups: %s", strerror(errno));
38 if (priv_ngroups > 0 && setgroups(1, &temp_gid) != 0)
39 log_write(0, LOG_PANIC_DIE, "setgroups: %s", strerror(errno));
40 if (setegid(temp_gid) != 0)
41 log_write(0, LOG_PANIC_DIE, "setegid(%d): %s", temp_gid, strerror(errno));
42 if (seteuid(temp_uid) != 0)
43 log_write(0, LOG_PANIC_DIE, "seteuid(%d): %s", temp_uid, strerror(errno));
45 if (geteuid() != temp_uid)
46 log_write(0, LOG_PANIC_DIE, "getdeuid() != %d", temp_uid);
47 if (getegid() != temp_gid)
48 log_write(0, LOG_PANIC_DIE, "getegid() != %d", temp_gid);
51 priv_state = PRIV_DROPPED;
54 /* Inspired by OpenSSH's restore_uid(). Thanks! */
59 if (priv_state != PRIV_DROPPED)
60 log_write(0, LOG_PANIC_DIE, "priv_restore: unexpected priv_state %d != %d", priv_state, PRIV_DROPPED);
61 priv_state = PRIV_RESTORING;
63 if (priv_euid == root_uid)
65 if (seteuid(priv_euid) != 0)
66 log_write(0, LOG_PANIC_DIE, "seteuid(%d): %s", priv_euid, strerror(errno));
67 if (setegid(priv_egid) != 0)
68 log_write(0, LOG_PANIC_DIE, "setegid(%d): %s", priv_egid, strerror(errno));
69 if (priv_ngroups > 0 && setgroups(priv_ngroups, priv_groups) != 0)
70 log_write(0, LOG_PANIC_DIE, "setgroups: %s", strerror(errno));
72 if (geteuid() != priv_euid)
73 log_write(0, LOG_PANIC_DIE, "getdeuid() != %d", priv_euid);
74 if (getegid() != priv_egid)
75 log_write(0, LOG_PANIC_DIE, "getdegid() != %d", priv_egid);
78 priv_state = PRIV_RESTORED;