]> granicus.if.org Git - shadow/blob - libmisc/setugid.c
(failcheck): The failed argument is a bool.
[shadow] / libmisc / setugid.c
1 /*
2  * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 1998, Marek Michałkiewicz
4  * Copyright (c) 2003 - 2005, Tomasz Kłoczko
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
30  */
31
32 /*
33  * Separated from setup.c.  --marekm
34  */
35
36 #include <config.h>
37
38 #ident "$Id$"
39
40 #include <stdio.h>
41 #include <grp.h>
42 #include "prototypes.h"
43 #include "defines.h"
44 #include <pwd.h>
45 #include "getdef.h"
46 /*
47  * setup_uid_gid() split in two functions for PAM support -
48  * pam_setcred() needs to be called after initgroups(), but
49  * before setuid().
50  */
51 int setup_groups (const struct passwd *info)
52 {
53         /*
54          * Set the real group ID to the primary group ID in the password
55          * file.
56          */
57         if (setgid (info->pw_gid) == -1) {
58                 perror ("setgid");
59                 SYSLOG ((LOG_ERR, "bad group ID `%d' for user `%s': %m\n",
60                          info->pw_gid, info->pw_name));
61                 closelog ();
62                 return -1;
63         }
64 #ifdef HAVE_INITGROUPS
65         /*
66          * For systems which support multiple concurrent groups, go get
67          * the group set from the /etc/group file.
68          */
69         if (initgroups (info->pw_name, info->pw_gid) == -1) {
70                 perror ("initgroups");
71                 SYSLOG ((LOG_ERR, "initgroups failed for user `%s': %m\n",
72                          info->pw_name));
73                 closelog ();
74                 return -1;
75         }
76 #endif
77         return 0;
78 }
79
80 int change_uid (const struct passwd *info)
81 {
82         /*
83          * Set the real UID to the UID value in the password file.
84          */
85         if (setuid (info->pw_uid)) {
86                 perror ("setuid");
87                 SYSLOG ((LOG_ERR, "bad user ID `%d' for user `%s': %m\n",
88                          (int) info->pw_uid, info->pw_name));
89                 closelog ();
90                 return -1;
91         }
92         return 0;
93 }
94
95 /*
96  *      setup_uid_gid() performs the following steps -
97  *
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
102  *
103  *      Returns 0 on success, or -1 on failure.
104  */
105
106 int setup_uid_gid (const struct passwd *info, int is_console)
107 {
108         if (setup_groups (info) < 0)
109                 return -1;
110
111 #ifdef HAVE_INITGROUPS
112         if (is_console) {
113                 char *cp = getdef_str ("CONSOLE_GROUPS");
114
115                 if (cp && add_groups (cp))
116                         perror ("Warning: add_groups");
117         }
118 #endif                          /* HAVE_INITGROUPS */
119
120         if (change_uid (info) < 0)
121                 return -1;
122
123         return 0;
124 }