3 * Copyright 1998-1999 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>
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",
72 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
87 if ( isdigit( (unsigned char) *group )) {
90 grp = getgrgid( gid );
94 grp = getgrnam( group );
99 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
101 exit( EXIT_FAILURE );
109 if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
110 Debug( LDAP_DEBUG_ANY,
111 "Could not set the group access (gid) list\n", 0, 0, 0 );
112 exit( EXIT_FAILURE );
122 if ( setgid( gid ) != 0 ) {
123 Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
125 exit( EXIT_FAILURE );
128 if ( setegid( gid ) != 0 ) {
129 Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
131 exit( EXIT_FAILURE );
137 if ( setuid( uid ) != 0 ) {
138 Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
140 exit( EXIT_FAILURE );
143 if ( seteuid( uid ) != 0 ) {
144 Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
146 exit( EXIT_FAILURE );
152 #endif /* HAVE_PWD_H && HAVE_GRP_H */