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
38 slap_init_user( char *user, char *group )
40 uid_t uid = (uid_t) 0;
41 gid_t gid = (gid_t) 0;
45 if ( isdigit( (unsigned char) *user )) {
48 pwd = getpwuid( uid );
52 pwd = getpwnam( user );
55 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
61 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
74 if ( isdigit( (unsigned char) *group )) {
77 grp = getgrgid( gid );
81 grp = getgrnam( group );
86 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
95 if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
96 Debug( LDAP_DEBUG_ANY,
97 "Could not set the group access (gid) list\n", 0, 0, 0 );
108 if ( setgid( gid ) != 0 ) {
109 Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
114 if ( setegid( gid ) != 0 ) {
115 Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
123 if ( setuid( uid ) != 0 ) {
124 Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
129 if ( seteuid( uid ) != 0 ) {
130 Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
138 #endif /* HAVE_PWD_H && HAVE_GRP_H */