]> git.sur5r.net Git - openldap/blob - servers/slapd/user.c
Mirroring changes related to indexing to db2 backend...
[openldap] / servers / slapd / user.c
1 /* user.c - set user id, group id and group access list
2  *
3  * Copyright 1999 by PM Lashley and The OpenLDAP Foundation.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted only
7  * as authorized by the OpenLDAP Public License.  A copy of this
8  * license is available at http://www.OpenLDAP.org/license.html or
9  * in file LICENSE in the top-level directory of the distribution.
10 */
11
12 #include "portable.h"
13
14 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #ifdef HAVE_PWD_H
20 #include <pwd.h>
21 #endif
22 #ifdef HAVE_GRP_H
23 #include <grp.h>
24 #endif
25
26 #include <ac/ctype.h>
27 #include <ac/unistd.h>
28
29 #include "slap.h"
30
31
32 /*
33  * Set real and effective user id and group id, and group access list
34  */
35
36 void
37 slap_init_user( char *user, char *group )
38 {
39     uid_t       uid = (uid_t) -1;
40     gid_t       gid = (gid_t) -1;
41
42     if ( user ) {
43         struct passwd *pwd;
44         if ( isdigit( (unsigned char) *user )) {
45             uid = atoi( user );
46 #ifdef HAVE_GETPWUID
47             pwd = getpwuid( uid );
48             goto did_getpw;
49 #endif
50         } else {
51             pwd = getpwnam( user );
52         did_getpw:
53             if ( pwd == NULL ) {
54                 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
55                        user, 0, 0 );
56                 exit( 1 );
57             }
58             if ( uid >= 0 ) {
59                 free( user );
60                 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
61             } else {
62                 uid = pwd->pw_uid;
63             }
64             gid = pwd->pw_gid;
65 #ifdef HAVE_ENDPWENT
66             endpwent();
67 #endif
68         }
69     }
70
71     if ( group ) {
72         struct group *grp;
73         if ( isdigit( (unsigned char) *group )) {
74             gid = atoi( group );
75 #ifdef HAVE_GETGRGID
76             grp = getgrgid( gid );
77             goto did_group;
78 #endif
79         } else {
80             grp = getgrnam( group );
81             if ( grp != NULL )
82                 gid = grp->gr_gid;
83         did_group:
84             if ( grp == NULL ) {
85                 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
86                        group, 0, 0 );
87                 exit( 1 );
88             }
89         }
90         free( group );
91     }
92
93     if ( user ) {
94         if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
95             Debug( LDAP_DEBUG_ANY,
96                    "Could not set the group access (gid) list\n", 0, 0, 0 );
97             exit( 1 );
98         }
99         free( user );
100     }
101
102 #ifdef HAVE_ENDGRENT
103     endgrent();
104 #endif
105
106     if ( gid >= 0 ) {
107         if ( setgid( gid ) != 0 ) {
108             Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
109                    gid, 0, 0 );
110             exit( 1 );
111         }
112 #ifdef HAVE_SETEGID
113         if ( setegid( gid ) != 0 ) {
114             Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
115                    gid, 0, 0 );
116             exit( 1 );
117         }
118 #endif
119     }
120
121     if ( uid >= 0 ) {
122         if ( setuid( uid ) != 0 ) {
123             Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
124                    uid, 0, 0 );
125             exit( 1 );
126         }
127 #ifdef HAVE_SETEUID
128         if ( seteuid( uid ) != 0 ) {
129             Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
130                    uid, 0, 0 );
131             exit( 1 );
132         }
133 #endif
134     }
135 }
136
137 #endif /* HAVE_PWD_H && HAVE_GRP_H */