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 );
66 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
73 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
88 if ( isdigit( (unsigned char) *group )) {
91 grp = getgrgid( gid );
95 grp = getgrnam( group );
100 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
103 exit( EXIT_FAILURE );
111 if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
112 Debug( LDAP_DEBUG_ANY,
113 "Could not set the group access (gid) list\n", 0, 0, 0 );
115 exit( EXIT_FAILURE );
125 if ( setgid( gid ) != 0 ) {
126 Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
129 exit( EXIT_FAILURE );
132 if ( setegid( gid ) != 0 ) {
133 Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
136 exit( EXIT_FAILURE );
142 if ( setuid( uid ) != 0 ) {
143 Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
146 exit( EXIT_FAILURE );
149 if ( seteuid( uid ) != 0 ) {
150 Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
153 exit( EXIT_FAILURE );
159 #endif /* HAVE_PWD_H && HAVE_GRP_H */