1 /* user.c - set user id, group id and group access list */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
6 * Portions Copyright 1999 PM Lashley.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
20 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
24 #include <ac/stdlib.h>
34 #include <ac/unistd.h>
39 * Set real and effective user id and group id, and group access list
40 * The user and group arguments are freed.
44 slap_init_user( char *user, char *group )
48 int got_uid = 0, got_gid = 0;
52 if ( isdigit( (unsigned char) *user )) {
56 pwd = getpwuid( uid );
63 pwd = getpwnam( user );
67 LDAP_LOG( OPERATION, INFO,
68 "slap_init_user: No passwd entry for user %s\n", user, 0, 0 );
70 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
78 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
93 if ( isdigit( (unsigned char) *group )) {
96 grp = getgrgid( gid );
100 grp = getgrnam( group );
106 LDAP_LOG( OPERATION, INFO,
107 "slap_init_user: No group entry for group %s\n", group, 0, 0 );
109 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
113 exit( EXIT_FAILURE );
121 if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
123 LDAP_LOG( OPERATION, INFO,
124 "slap_init_user: Could not set the group access (gid) list.\n",
127 Debug( LDAP_DEBUG_ANY,
128 "Could not set the group access (gid) list\n", 0, 0, 0 );
131 exit( EXIT_FAILURE );
141 if ( setgid( gid ) != 0 ) {
143 LDAP_LOG( OPERATION, INFO,
144 "slap_init_user: could not set real group id to %d\n",
147 Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
151 exit( EXIT_FAILURE );
154 if ( setegid( gid ) != 0 ) {
156 LDAP_LOG( OPERATION, INFO,
157 "slap_init_user: Could not set effective group id to %d\n",
160 Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
164 exit( EXIT_FAILURE );
170 if ( setuid( uid ) != 0 ) {
172 LDAP_LOG( OPERATION, INFO,
173 "slap_init_user: Could not set real user id to %d\n",
176 Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
180 exit( EXIT_FAILURE );
183 if ( seteuid( uid ) != 0 ) {
185 LDAP_LOG( OPERATION, INFO,
186 "slap_init_user: Could not set effective user id to %d\n",
189 Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
193 exit( EXIT_FAILURE );
199 #endif /* HAVE_PWD_H && HAVE_GRP_H */