restrict-access.c revision 8afe3f0e832b8b3483b692205bbd59c0110a20fd
/* Copyright (c) 2002-2004 Timo Sirainen */
#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(void)
{
/* @UNSAFE */
const char *env;
if (first_valid_gid == 0 && last_valid_gid == 0)
return;
t_push();
if (gid_list[i] >= first_valid_gid &&
}
/* it did contain restricted groups, remove it */
i_fatal("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("setgroups() failed: %m");
t_pop();
}
void restrict_access_by_env(int disallow_root)
{
const char *env;
/* 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. */
/* 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) {
}
/* 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=");
}