]> git.sur5r.net Git - openldap/blob - servers/slapd/user.c
Modify ad_cmp() macro to support use as an ordering function.
[openldap] / servers / slapd / user.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 /*
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", LDAP_LEVEL_INFO,
68                                "slap_init_user: No passwd entry for user %s\n",
69                                user ));
70 #else
71                 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
72                        user, 0, 0 );
73 #endif
74
75                 exit( EXIT_FAILURE );
76             }
77             if ( got_uid ) {
78                 free( user );
79                 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
80             } else {
81                 got_uid = 1;
82                 uid = pwd->pw_uid;
83             }
84             got_gid = 1;
85             gid = pwd->pw_gid;
86 #ifdef HAVE_ENDPWENT
87             endpwent();
88 #endif
89         }
90     }
91
92     if ( group ) {
93         struct group *grp;
94         if ( isdigit( (unsigned char) *group )) {
95             gid = atoi( group );
96 #ifdef HAVE_GETGRGID
97             grp = getgrgid( gid );
98             goto did_group;
99 #endif
100         } else {
101             grp = getgrnam( group );
102             if ( grp != NULL )
103                 gid = grp->gr_gid;
104         did_group:
105             if ( grp == NULL ) {
106 #ifdef NEW_LOGGING
107                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
108                            "slap_init_user: No group entry for group %s\n", group));
109 #else
110                 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
111                        group, 0, 0 );
112 #endif
113
114                 exit( EXIT_FAILURE );
115             }
116         }
117         free( group );
118         got_gid = 1;
119     }
120
121     if ( user ) {
122         if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
123 #ifdef NEW_LOGGING
124             LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
125                        "slap_init_user: Could not set the group access (gid) list.\n" ));
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", LDAP_LEVEL_INFO,
144                        "slap_init_user: could not set real group id to %d\n", (int)gid));
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", LDAP_LEVEL_INFO,
156                        "slap_init_user: Could not set effective group id to %d\n",(int)gid));
157 #else
158             Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
159                        (int) gid, 0, 0 );
160 #endif
161
162             exit( EXIT_FAILURE );
163         }
164 #endif
165     }
166
167     if ( got_uid ) {
168         if ( setuid( uid ) != 0 ) {
169 #ifdef NEW_LOGGING
170             LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
171                        "slap_init_user: Could not set real user id to %d\n", (int)uid ));
172 #else
173             Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
174                        (int) uid, 0, 0 );
175 #endif
176
177             exit( EXIT_FAILURE );
178         }
179 #ifdef HAVE_SETEUID
180         if ( seteuid( uid ) != 0 ) {
181 #ifdef NEW_LOGGING
182             LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
183                        "slap_init_user: Could not set effective user id to %d\n", (int)uid ));
184 #else
185             Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
186                        (int) uid, 0, 0 );
187 #endif
188
189             exit( EXIT_FAILURE );
190         }
191 #endif
192     }
193 }
194
195 #endif /* HAVE_PWD_H && HAVE_GRP_H */