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)
27 #include <ac/unistd.h>
33 * Set real and effective user id and group id, and group access list
37 slap_init_user( char *user, char *group )
39 uid_t uid = (uid_t) -1;
40 gid_t gid = (gid_t) -1;
44 if ( isdigit( (unsigned char) *user )) {
47 pwd = getpwuid( uid );
51 pwd = getpwnam( user );
54 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
60 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
73 if ( isdigit( (unsigned char) *group )) {
76 grp = getgrgid( gid );
80 grp = getgrnam( group );
85 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
94 if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
95 Debug( LDAP_DEBUG_ANY,
96 "Could not set the group access (gid) list\n", 0, 0, 0 );
107 if ( setgid( gid ) != 0 ) {
108 Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
113 if ( setegid( gid ) != 0 ) {
114 Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
122 if ( setuid( uid ) != 0 ) {
123 Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
128 if ( seteuid( uid ) != 0 ) {
129 Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
137 #endif /* HAVE_PWD_H && HAVE_GRP_H */