]> git.sur5r.net Git - openldap/blob - servers/slapd/user.c
(re)introduce o_connid such that STATS doesn't need c_mutex (which it
[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
18 #include <ac/stdlib.h>
19
20 #ifdef HAVE_PWD_H
21 #include <pwd.h>
22 #endif
23 #ifdef HAVE_GRP_H
24 #include <grp.h>
25 #endif
26
27 #include <ac/ctype.h>
28 #include <ac/unistd.h>
29
30 #include "slap.h"
31
32
33 /*
34  * Set real and effective user id and group id, and group access list
35  */
36
37 void
38 slap_init_user( char *user, char *group )
39 {
40     uid_t       uid = (uid_t) 0;
41     gid_t       gid = (gid_t) 0;
42
43     if ( user ) {
44         struct passwd *pwd;
45         if ( isdigit( (unsigned char) *user )) {
46             uid = atoi( user );
47 #ifdef HAVE_GETPWUID
48             pwd = getpwuid( uid );
49             goto did_getpw;
50 #endif
51         } else {
52             pwd = getpwnam( user );
53         did_getpw:
54             if ( pwd == NULL ) {
55                 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
56                        user, 0, 0 );
57                 exit( 1 );
58             }
59             if ( uid > 0 ) {
60                 free( user );
61                 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
62             } else {
63                 uid = pwd->pw_uid;
64             }
65             gid = pwd->pw_gid;
66 #ifdef HAVE_ENDPWENT
67             endpwent();
68 #endif
69         }
70     }
71
72     if ( group ) {
73         struct group *grp;
74         if ( isdigit( (unsigned char) *group )) {
75             gid = atoi( group );
76 #ifdef HAVE_GETGRGID
77             grp = getgrgid( gid );
78             goto did_group;
79 #endif
80         } else {
81             grp = getgrnam( group );
82             if ( grp != NULL )
83                 gid = grp->gr_gid;
84         did_group:
85             if ( grp == NULL ) {
86                 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
87                        group, 0, 0 );
88                 exit( 1 );
89             }
90         }
91         free( group );
92     }
93
94     if ( user ) {
95         if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
96             Debug( LDAP_DEBUG_ANY,
97                    "Could not set the group access (gid) list\n", 0, 0, 0 );
98             exit( 1 );
99         }
100         free( user );
101     }
102
103 #ifdef HAVE_ENDGRENT
104     endgrent();
105 #endif
106
107     if ( gid > 0 ) {
108         if ( setgid( gid ) != 0 ) {
109             Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
110                    gid, 0, 0 );
111             exit( 1 );
112         }
113 #ifdef HAVE_SETEGID
114         if ( setegid( gid ) != 0 ) {
115             Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
116                    gid, 0, 0 );
117             exit( 1 );
118         }
119 #endif
120     }
121
122     if ( uid > 0 ) {
123         if ( setuid( uid ) != 0 ) {
124             Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
125                    uid, 0, 0 );
126             exit( 1 );
127         }
128 #ifdef HAVE_SETEUID
129         if ( seteuid( uid ) != 0 ) {
130             Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
131                    uid, 0, 0 );
132             exit( 1 );
133         }
134 #endif
135     }
136 }
137
138 #endif /* HAVE_PWD_H && HAVE_GRP_H */