]> git.sur5r.net Git - openldap/blob - servers/slapd/user.c
d79325bfdd75882ef03fe3ce84399f2a6e3d4e00
[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  * The user and group arguments are freed.
36  */
37
38 void
39 slap_init_user( char *user, char *group )
40 {
41     uid_t       uid;
42     gid_t       gid;
43     int         got_uid = 0, got_gid = 0;
44
45     if ( user ) {
46         struct passwd *pwd;
47         if ( isdigit( (unsigned char) *user )) {
48             got_uid = 1;
49             uid = atoi( user );
50 #ifdef HAVE_GETPWUID
51             pwd = getpwuid( uid );
52             goto did_getpw;
53 #else
54             free( user );
55             user = NULL;
56 #endif
57         } else {
58             pwd = getpwnam( user );
59         did_getpw:
60             if ( pwd == NULL ) {
61                 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
62                        user, 0, 0 );
63                 exit( 1 );
64             }
65             if ( got_uid ) {
66                 free( user );
67                 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
68             } else {
69                 got_uid = 1;
70                 uid = pwd->pw_uid;
71             }
72             got_gid = 1;
73             gid = pwd->pw_gid;
74 #ifdef HAVE_ENDPWENT
75             endpwent();
76 #endif
77         }
78     }
79
80     if ( group ) {
81         struct group *grp;
82         if ( isdigit( (unsigned char) *group )) {
83             gid = atoi( group );
84 #ifdef HAVE_GETGRGID
85             grp = getgrgid( gid );
86             goto did_group;
87 #endif
88         } else {
89             grp = getgrnam( group );
90             if ( grp != NULL )
91                 gid = grp->gr_gid;
92         did_group:
93             if ( grp == NULL ) {
94                 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
95                        group, 0, 0 );
96                 exit( 1 );
97             }
98         }
99         free( group );
100         got_gid = 1;
101     }
102
103     if ( user ) {
104         if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
105             Debug( LDAP_DEBUG_ANY,
106                    "Could not set the group access (gid) list\n", 0, 0, 0 );
107             exit( 1 );
108         }
109         free( user );
110     }
111
112 #ifdef HAVE_ENDGRENT
113     endgrent();
114 #endif
115
116     if ( got_gid ) {
117         if ( setgid( gid ) != 0 ) {
118             Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
119                    gid, 0, 0 );
120             exit( 1 );
121         }
122 #ifdef HAVE_SETEGID
123         if ( setegid( gid ) != 0 ) {
124             Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
125                    gid, 0, 0 );
126             exit( 1 );
127         }
128 #endif
129     }
130
131     if ( got_uid ) {
132         if ( setuid( uid ) != 0 ) {
133             Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
134                    uid, 0, 0 );
135             exit( 1 );
136         }
137 #ifdef HAVE_SETEUID
138         if ( seteuid( uid ) != 0 ) {
139             Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
140                    uid, 0, 0 );
141             exit( 1 );
142         }
143 #endif
144     }
145 }
146
147 #endif /* HAVE_PWD_H && HAVE_GRP_H */