restrict-access.c revision 9eadac5bdb6d809f0413a07c51c53552ce161756
/* Copyright (c) 2002-2003 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>
#define HARD_MAX_GROUPS 10240
#ifndef NGROUPS_MAX
# define NGROUPS_MAX 128
#endif
const char *chroot_dir,
const char *extra_groups)
{
extra_groups, NULL));
}
if (first_valid_gid != 0) {
}
if (last_valid_gid != 0) {
}
}
{
/* @UNSAFE */
i_fatal("getgroups() failed: %m");
gid_count *= 2;
}
*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_panic("won't happen");
}
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=");
}