restrict-access.c revision ef9c0566c2c9473e085e3dd59a64750cd59df894
/* Copyright (c) 2002-2008 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "restrict-access.h"
#include "env-util.h"
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <grp.h>
const char *chroot_dir,
const char *extra_groups)
{
extra_groups, NULL));
}
if (first_valid_gid != 0) {
}
if (last_valid_gid != 0) {
}
}
{
i_fatal("getgroups() failed: %m");
/* @UNSAFE */
i_fatal("getgroups() failed: %m");
*gid_count_r = ret;
return gid_list;
}
bool *have_root_group)
{
/* @UNSAFE */
const char *env;
unsigned int i, used;
if (gid_list[i] >= first_valid &&
if (gid_list[i] == 0)
*have_root_group = TRUE;
}
}
return FALSE;
return TRUE;
}
{
}
bool preserve_existing, bool *have_root_group)
{
unsigned int gid_count;
if (preserve_existing) {
have_root_group) &&
/* nothing dropped, no extra groups to grant. */
return;
}
} else {
/* nothing to do */
return;
}
/* Some OSes don't like an empty groups list,
so use the effective GID as the only one. */
gid_count = 1;
}
/* add extra groups to gids list */
i_unreached();
}
i_fatal("setgroups(%s) failed: Too many extra groups",
} else {
i_fatal("setgroups() failed: %m");
}
}
}
void restrict_access_by_env(bool disallow_root)
{
const char *env;
/* set the primary group */
have_root_group = gid == 0;
i_fatal("setgid(%s) failed with euid=%s, egid=%s: %m",
}
}
/* set system user's groups */
i_fatal("initgroups(%s, %s) failed: %m",
}
}
/* add extra groups. if we set system user's groups, drop the
restricted groups at the same time. */
if (is_root) {
);
}
/* chrooting */
/* kludge: localtime() must be called before chroot(),
or the timezone isn't known */
time_t t = 0;
(void)localtime(&t);
}
}
if (chdir("/") != 0)
i_fatal("chdir(/) failed: %m");
}
}
/* uid last */
if (uid != 0) {
i_fatal("setuid(%s) failed with euid=%s: %m",
}
}
/* verify that we actually dropped the privileges */
if (uid != 0 || disallow_root) {
if (setuid(0) == 0) {
if (uid == 0)
i_fatal("Running as root isn't permitted");
i_fatal("We couldn't drop root privileges");
}
}
if (gid == 0)
i_fatal("GID 0 isn't permitted");
i_fatal("We couldn't drop root group privileges "
}
}
/* clear the environment, so we don't fail if we get back here */
env_put("RESTRICT_USER=");
env_put("RESTRICT_CHROOT=");
env_put("RESTRICT_SETUID=");
env_put("RESTRICT_SETGID=");
env_put("RESTRICT_SETEXTRAGROUPS=");
env_put("RESTRICT_GID_FIRST=");
env_put("RESTRICT_GID_LAST=");
}