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