7 PRIV_DROPPING, PRIV_DROPPED,
8 PRIV_RESTORING, PRIV_RESTORED
9 } priv_state = PRIV_RESTORED;
12 static uid_t priv_euid;
13 static gid_t priv_egid;
14 static gid_t priv_groups[EXIM_GROUPLIST_SIZE + 1];
15 static int priv_ngroups;
17 /* Inspired by OpenSSH's temporarily_use_uid(). Thanks! */
20 priv_drop_temp(const uid_t temp_uid, const gid_t temp_gid)
22 if (priv_state != PRIV_RESTORED)
23 log_write(0, LOG_PANIC_DIE, "priv_drop_temp: unexpected priv_state %d != %d", priv_state, PRIV_RESTORED);
25 priv_state = PRIV_DROPPING;
27 priv_euid = geteuid();
28 if (priv_euid == root_uid)
30 priv_egid = getegid();
31 priv_ngroups = getgroups(nelem(priv_groups), priv_groups);
33 log_write(0, LOG_PANIC_DIE, "getgroups: %s", strerror(errno));
35 if (priv_ngroups > 0 && setgroups(1, &temp_gid) != 0)
36 log_write(0, LOG_PANIC_DIE, "setgroups: %s", strerror(errno));
37 if (setegid(temp_gid) != 0)
38 log_write(0, LOG_PANIC_DIE, "setegid(%d): %s", temp_gid, strerror(errno));
39 if (seteuid(temp_uid) != 0)
40 log_write(0, LOG_PANIC_DIE, "seteuid(%d): %s", temp_uid, strerror(errno));
42 if (geteuid() != temp_uid)
43 log_write(0, LOG_PANIC_DIE, "getdeuid() != %d", temp_uid);
44 if (getegid() != temp_gid)
45 log_write(0, LOG_PANIC_DIE, "getegid() != %d", temp_gid);
48 priv_state = PRIV_DROPPED;
51 /* Inspired by OpenSSH's restore_uid(). Thanks! */
56 if (priv_state != PRIV_DROPPED)
57 log_write(0, LOG_PANIC_DIE, "priv_restore: unexpected priv_state %d != %d", priv_state, PRIV_DROPPED);
58 priv_state = PRIV_RESTORING;
60 if (priv_euid == root_uid)
62 if (seteuid(priv_euid) != 0)
63 log_write(0, LOG_PANIC_DIE, "seteuid(%d): %s", priv_euid, strerror(errno));
64 if (setegid(priv_egid) != 0)
65 log_write(0, LOG_PANIC_DIE, "setegid(%d): %s", priv_egid, strerror(errno));
66 if (priv_ngroups > 0 && setgroups(priv_ngroups, priv_groups) != 0)
67 log_write(0, LOG_PANIC_DIE, "setgroups: %s", strerror(errno));
69 if (geteuid() != priv_euid)
70 log_write(0, LOG_PANIC_DIE, "getdeuid() != %d", priv_euid);
71 if (getegid() != priv_egid)
72 log_write(0, LOG_PANIC_DIE, "getdegid() != %d", priv_egid);
75 priv_state = PRIV_RESTORED;