restrict-access.c revision ee2854ca90c45c4b083fa6f2d162d127286bb94d
/* Copyright (c) 2002-2007 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;
}
static void drop_restricted_groups(bool *have_root_group)
{
/* @UNSAFE */
const char *env;
if (first_valid_gid == 0 && last_valid_gid == 0)
return;
t_push();
if (gid_list[i] >= first_valid_gid &&
if (gid_list[i] == 0)
*have_root_group = TRUE;
}
}
/* it did contain restricted groups, remove it */
i_fatal("Couldn't drop restricted groups: "
"setgroups() failed: %m");
}
}
t_pop();
}
{
}
static void grant_extra_groups(const char *groups)
{
const char *const *tmp;
int gid_count;
t_push();
if (**tmp == '\0')
continue;
i_unreached();
}
i_fatal("Couldn't set mail_extra_groups: "
"setgroups(%s) failed: %m", groups);
}
t_pop();
}
void restrict_access_by_env(bool disallow_root)
{
const char *env;
bool have_root_group;
/* groups - the getgid() checks are just so we don't fail if we're
not running as root and try to just use our own GID. Do this
before chrooting so initgroups() actually works. */
have_root_group = gid == 0;
/* user not known, use only this one group */
i_fatal("setgroups(%s) failed: %m",
}
} else {
i_fatal("initgroups(%s, %s) failed: %m",
}
}
}
/* grant additional groups to process */
/* 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=");
}