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