3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* user.c - set user id, group id and group access list
8 * Copyright 1999 by PM Lashley.
11 * Redistribution and use in source and binary forms are permitted only
12 * as authorized by the OpenLDAP Public License. A copy of this
13 * license is available at http://www.OpenLDAP.org/license.html or
14 * in file LICENSE in the top-level directory of the distribution.
19 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
23 #include <ac/stdlib.h>
33 #include <ac/unistd.h>
38 * Set real and effective user id and group id, and group access list
39 * The user and group arguments are freed.
43 slap_init_user( char *user, char *group )
47 int got_uid = 0, got_gid = 0;
51 if ( isdigit( (unsigned char) *user )) {
55 pwd = getpwuid( uid );
62 pwd = getpwnam( user );
66 LDAP_LOG( OPERATION, INFO,
67 "slap_init_user: No passwd entry for user %s\n", user, 0, 0 );
69 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
77 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
92 if ( isdigit( (unsigned char) *group )) {
95 grp = getgrgid( gid );
99 grp = getgrnam( group );
105 LDAP_LOG( OPERATION, INFO,
106 "slap_init_user: No group entry for group %s\n", group, 0, 0 );
108 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
112 exit( EXIT_FAILURE );
120 if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
122 LDAP_LOG( OPERATION, INFO,
123 "slap_init_user: Could not set the group access (gid) list.\n",
126 Debug( LDAP_DEBUG_ANY,
127 "Could not set the group access (gid) list\n", 0, 0, 0 );
130 exit( EXIT_FAILURE );
140 if ( setgid( gid ) != 0 ) {
142 LDAP_LOG( OPERATION, INFO,
143 "slap_init_user: could not set real group id to %d\n",
146 Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
150 exit( EXIT_FAILURE );
153 if ( setegid( gid ) != 0 ) {
155 LDAP_LOG( OPERATION, INFO,
156 "slap_init_user: Could not set effective group id to %d\n",
159 Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
163 exit( EXIT_FAILURE );
169 if ( setuid( uid ) != 0 ) {
171 LDAP_LOG( OPERATION, INFO,
172 "slap_init_user: Could not set real user id to %d\n",
175 Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
179 exit( EXIT_FAILURE );
182 if ( seteuid( uid ) != 0 ) {
184 LDAP_LOG( OPERATION, INFO,
185 "slap_init_user: Could not set effective user id to %d\n",
188 Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
192 exit( EXIT_FAILURE );
198 #endif /* HAVE_PWD_H && HAVE_GRP_H */