]> git.sur5r.net Git - openldap/blob - servers/slapd/user.c
ITS#3579 fix init order
[openldap] / servers / slapd / user.c
1 /* user.c - set user id, group id and group access list */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * Portions Copyright 1999 PM Lashley.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17
18 #include "portable.h"
19
20 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #ifdef HAVE_PWD_H
27 #include <pwd.h>
28 #endif
29 #ifdef HAVE_GRP_H
30 #include <grp.h>
31 #endif
32
33 #include <ac/ctype.h>
34 #include <ac/unistd.h>
35
36 #include "slap.h"
37
38 /*
39  * Set real and effective user id and group id, and group access list
40  * The user and group arguments are freed.
41  */
42
43 void
44 slap_init_user( char *user, char *group )
45 {
46     uid_t       uid = 0;
47     gid_t       gid = 0;
48     int         got_uid = 0, got_gid = 0;
49
50     if ( user ) {
51         struct passwd *pwd;
52         if ( isdigit( (unsigned char) *user )) {
53             got_uid = 1;
54             uid = atoi( user );
55 #ifdef HAVE_GETPWUID
56             pwd = getpwuid( uid );
57             goto did_getpw;
58 #else
59             free( user );
60             user = NULL;
61 #endif
62         } else {
63             pwd = getpwnam( user );
64         did_getpw:
65             if ( pwd == NULL ) {
66 #ifdef NEW_LOGGING
67                     LDAP_LOG( OPERATION, INFO, 
68                                 "slap_init_user: No passwd entry for user %s\n", user, 0, 0 );
69 #else
70                 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
71                        user, 0, 0 );
72 #endif
73
74                 exit( EXIT_FAILURE );
75             }
76             if ( got_uid ) {
77                 free( user );
78                 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
79             } else {
80                 got_uid = 1;
81                 uid = pwd->pw_uid;
82             }
83             got_gid = 1;
84             gid = pwd->pw_gid;
85 #ifdef HAVE_ENDPWENT
86             endpwent();
87 #endif
88         }
89     }
90
91     if ( group ) {
92         struct group *grp;
93         if ( isdigit( (unsigned char) *group )) {
94             gid = atoi( group );
95 #ifdef HAVE_GETGRGID
96             grp = getgrgid( gid );
97             goto did_group;
98 #endif
99         } else {
100             grp = getgrnam( group );
101             if ( grp != NULL )
102                 gid = grp->gr_gid;
103         did_group:
104             if ( grp == NULL ) {
105 #ifdef NEW_LOGGING
106                 LDAP_LOG( OPERATION, INFO, 
107                         "slap_init_user: No group entry for group %s\n", group, 0, 0 );
108 #else
109                 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
110                        group, 0, 0 );
111 #endif
112
113                 exit( EXIT_FAILURE );
114             }
115         }
116         free( group );
117         got_gid = 1;
118     }
119
120     if ( user ) {
121         if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
122 #ifdef NEW_LOGGING
123             LDAP_LOG( OPERATION, INFO,
124                         "slap_init_user: Could not set the group access (gid) list.\n", 
125                         0, 0, 0 );
126 #else
127             Debug( LDAP_DEBUG_ANY,
128                    "Could not set the group access (gid) list\n", 0, 0, 0 );
129 #endif
130
131             exit( EXIT_FAILURE );
132         }
133         free( user );
134     }
135
136 #ifdef HAVE_ENDGRENT
137     endgrent();
138 #endif
139
140     if ( got_gid ) {
141         if ( setgid( gid ) != 0 ) {
142 #ifdef NEW_LOGGING
143             LDAP_LOG( OPERATION, INFO, 
144                         "slap_init_user: could not set real group id to %d\n", 
145                         (int)gid, 0, 0);
146 #else
147             Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
148                        (int) gid, 0, 0 );
149 #endif
150
151             exit( EXIT_FAILURE );
152         }
153 #ifdef HAVE_SETEGID
154         if ( setegid( gid ) != 0 ) {
155 #ifdef NEW_LOGGING
156             LDAP_LOG( OPERATION, INFO, 
157                    "slap_init_user: Could not set effective group id to %d\n",
158                    (int)gid, 0, 0);
159 #else
160             Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
161                        (int) gid, 0, 0 );
162 #endif
163
164             exit( EXIT_FAILURE );
165         }
166 #endif
167     }
168
169     if ( got_uid ) {
170         if ( setuid( uid ) != 0 ) {
171 #ifdef NEW_LOGGING
172             LDAP_LOG( OPERATION, INFO, 
173                         "slap_init_user: Could not set real user id to %d\n", 
174                         (int)uid, 0, 0 );
175 #else
176             Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
177                        (int) uid, 0, 0 );
178 #endif
179
180             exit( EXIT_FAILURE );
181         }
182 #ifdef HAVE_SETEUID
183         if ( seteuid( uid ) != 0 ) {
184 #ifdef NEW_LOGGING
185             LDAP_LOG( OPERATION, INFO, 
186                         "slap_init_user: Could not set effective user id to %d\n", 
187                         (int)uid, 0, 0 );
188 #else
189             Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
190                        (int) uid, 0, 0 );
191 #endif
192
193             exit( EXIT_FAILURE );
194         }
195 #endif
196     }
197 }
198
199 #endif /* HAVE_PWD_H && HAVE_GRP_H */