]> git.sur5r.net Git - openldap/blob - servers/slapd/user.c
unifdef -UNEW_LOGGING
[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-2004 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                 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
67                        user, 0, 0 );
68
69                 exit( EXIT_FAILURE );
70             }
71             if ( got_uid ) {
72                 free( user );
73                 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
74             } else {
75                 got_uid = 1;
76                 uid = pwd->pw_uid;
77             }
78             got_gid = 1;
79             gid = pwd->pw_gid;
80 #ifdef HAVE_ENDPWENT
81             endpwent();
82 #endif
83         }
84     }
85
86     if ( group ) {
87         struct group *grp;
88         if ( isdigit( (unsigned char) *group )) {
89             gid = atoi( group );
90 #ifdef HAVE_GETGRGID
91             grp = getgrgid( gid );
92             goto did_group;
93 #endif
94         } else {
95             grp = getgrnam( group );
96             if ( grp != NULL )
97                 gid = grp->gr_gid;
98         did_group:
99             if ( grp == NULL ) {
100                 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
101                        group, 0, 0 );
102
103                 exit( EXIT_FAILURE );
104             }
105         }
106         free( group );
107         got_gid = 1;
108     }
109
110     if ( user ) {
111         if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
112             Debug( LDAP_DEBUG_ANY,
113                    "Could not set the group access (gid) list\n", 0, 0, 0 );
114
115             exit( EXIT_FAILURE );
116         }
117         free( user );
118     }
119
120 #ifdef HAVE_ENDGRENT
121     endgrent();
122 #endif
123
124     if ( got_gid ) {
125         if ( setgid( gid ) != 0 ) {
126             Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
127                        (int) gid, 0, 0 );
128
129             exit( EXIT_FAILURE );
130         }
131 #ifdef HAVE_SETEGID
132         if ( setegid( gid ) != 0 ) {
133             Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
134                        (int) gid, 0, 0 );
135
136             exit( EXIT_FAILURE );
137         }
138 #endif
139     }
140
141     if ( got_uid ) {
142         if ( setuid( uid ) != 0 ) {
143             Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
144                        (int) uid, 0, 0 );
145
146             exit( EXIT_FAILURE );
147         }
148 #ifdef HAVE_SETEUID
149         if ( seteuid( uid ) != 0 ) {
150             Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
151                        (int) uid, 0, 0 );
152
153             exit( EXIT_FAILURE );
154         }
155 #endif
156     }
157 }
158
159 #endif /* HAVE_PWD_H && HAVE_GRP_H */