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