1 /* user.c - set user id, group id and group access list
3 * Copyright 1999 by PM Lashley and The OpenLDAP Foundation.
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.
14 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
18 #include <ac/stdlib.h>
28 #include <ac/unistd.h>
34 * Set real and effective user id and group id, and group access list
35 * The user and group arguments are freed.
39 slap_init_user( char *user, char *group )
43 int got_uid = 0, got_gid = 0;
47 if ( isdigit( (unsigned char) *user )) {
51 pwd = getpwuid( uid );
58 pwd = getpwnam( user );
61 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
67 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
82 if ( isdigit( (unsigned char) *group )) {
85 grp = getgrgid( gid );
89 grp = getgrnam( group );
94 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
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 );
117 if ( setgid( gid ) != 0 ) {
118 Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
123 if ( setegid( gid ) != 0 ) {
124 Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
132 if ( setuid( uid ) != 0 ) {
133 Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
138 if ( seteuid( uid ) != 0 ) {
139 Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
147 #endif /* HAVE_PWD_H && HAVE_GRP_H */