user-util.c revision b1d5277372a26e5a5b9980174652e1e287ba6b14
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering This file is part of systemd.
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering Copyright 2010 Lennart Poettering
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering systemd is free software; you can redistribute it and/or modify it
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering under the terms of the GNU Lesser General Public License as published by
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering (at your option) any later version.
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering systemd is distributed in the hope that it will be useful, but
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering Lesser General Public License for more details.
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering You should have received a copy of the GNU Lesser General Public License
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* Some libc APIs use UID_INVALID as special placeholder */
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
b1d5277372a26e5a5b9980174652e1e287ba6b14Lennart Poetteringint parse_uid(const char *s, uid_t *ret) {
b1d5277372a26e5a5b9980174652e1e287ba6b14Lennart Poettering assert_cc(sizeof(uid_t) == sizeof(uint32_t));
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering return -ENXIO; /* we return ENXIO instead of EINVAL
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering * here, to make it easy to distuingish
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering * invalid numeric uids invalid
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* Shortcut things to avoid NSS lookups */
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw)
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering const char *e;
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering const char **shell) {
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* We enforce some special rules for uid=0: in order to avoid
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering * NSS lookups for root we hardcode its data. */
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering if (streq(*username, "root") || streq(*username, "0")) {
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* If there are multiple users with the same id, make
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering * sure to leave $USER to the configured value instead
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering * of the first occurrence in the database. However if
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering * the uid was configured by a numeric uid, then let's
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering * pick the real username from /etc/passwd. */
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poetteringint get_group_creds(const char **groupname, gid_t *gid) {
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* We enforce some special rules for gid=0: in order to avoid
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering * NSS lookups for root we hardcode its data. */
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering if (streq(*groupname, "root") || streq(*groupname, "0")) {
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering gids = alloca(sizeof(gid_t) * ngroups_max);
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering for (i = 0; i < r; i++)
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering const char *e;
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* Take the user specified one */
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* Hardcode home directory for root to avoid NSS */
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* Check the database... */
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering const char *e;
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* Take the user specified one */
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* Hardcode home directory for root to avoid NSS */
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering /* Check the database... */
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering if (setresgid(0, 0, 0) < 0)
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering if (setresuid(0, 0, 0) < 0)