2 * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 1998, Marek Michałkiewicz
4 * Copyright (c) 2003 - 2005, Tomasz Kłoczko
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the copyright holders or contributors may not be used to
16 * endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Separated from setup.c. --marekm
42 #include "prototypes.h"
47 * setup_uid_gid() split in two functions for PAM support -
48 * pam_setcred() needs to be called after initgroups(), but
51 int setup_groups (const struct passwd *info)
54 * Set the real group ID to the primary group ID in the password
57 if (setgid (info->pw_gid) == -1) {
59 SYSLOG ((LOG_ERR, "bad group ID `%d' for user `%s': %m\n",
60 info->pw_gid, info->pw_name));
64 #ifdef HAVE_INITGROUPS
66 * For systems which support multiple concurrent groups, go get
67 * the group set from the /etc/group file.
69 if (initgroups (info->pw_name, info->pw_gid) == -1) {
70 perror ("initgroups");
71 SYSLOG ((LOG_ERR, "initgroups failed for user `%s': %m\n",
80 int change_uid (const struct passwd *info)
83 * Set the real UID to the UID value in the password file.
85 if (setuid (info->pw_uid)) {
87 SYSLOG ((LOG_ERR, "bad user ID `%d' for user `%s': %m\n",
88 (int) info->pw_uid, info->pw_name));
96 * setup_uid_gid() performs the following steps -
98 * set the group ID to the value from the password file entry
99 * set the supplementary group IDs
100 * optionally call specified function which may add more groups
101 * set the user ID to the value from the password file entry
103 * Returns 0 on success, or -1 on failure.
106 int setup_uid_gid (const struct passwd *info, int is_console)
108 if (setup_groups (info) < 0)
111 #ifdef HAVE_INITGROUPS
113 char *cp = getdef_str ("CONSOLE_GROUPS");
115 if (cp && add_groups (cp))
116 perror ("Warning: add_groups");
118 #endif /* HAVE_INITGROUPS */
120 if (change_uid (info) < 0)