1 /* user.c - set user id, group id and group access list */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/user.c,v 1.25.2.3 2008/02/11 23:26:45 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 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>
40 * Set real and effective user id and group id, and group access list
41 * The user and group arguments are freed.
45 slap_init_user( char *user
, char *group
)
49 int got_uid
= 0, got_gid
= 0;
53 if ( isdigit( (unsigned char) *user
) ) {
57 if ( lutil_atou( &u
, user
) != 0 ) {
58 Debug( LDAP_DEBUG_ANY
, "Unble to parse user %s\n",
65 pwd
= getpwuid( uid
);
72 pwd
= getpwnam( user
);
75 Debug( LDAP_DEBUG_ANY
, "No passwd entry for user %s\n",
82 user
= (pwd
!= NULL
? ch_strdup( pwd
->pw_name
) : NULL
);
97 if ( isdigit( (unsigned char) *group
)) {
100 if ( lutil_atou( &g
, group
) != 0 ) {
101 Debug( LDAP_DEBUG_ANY
, "Unble to parse group %s\n",
104 exit( EXIT_FAILURE
);
108 grp
= getgrgid( gid
);
112 grp
= getgrnam( group
);
117 Debug( LDAP_DEBUG_ANY
, "No group entry for group %s\n",
120 exit( EXIT_FAILURE
);
128 if ( getuid() == 0 && initgroups( user
, gid
) != 0 ) {
129 Debug( LDAP_DEBUG_ANY
,
130 "Could not set the group access (gid) list\n", 0, 0, 0 );
132 exit( EXIT_FAILURE
);
142 if ( setgid( gid
) != 0 ) {
143 Debug( LDAP_DEBUG_ANY
, "Could not set real group id to %d\n",
146 exit( EXIT_FAILURE
);
149 if ( setegid( gid
) != 0 ) {
150 Debug( LDAP_DEBUG_ANY
, "Could not set effective group id to %d\n",
153 exit( EXIT_FAILURE
);
159 if ( setuid( uid
) != 0 ) {
160 Debug( LDAP_DEBUG_ANY
, "Could not set real user id to %d\n",
163 exit( EXIT_FAILURE
);
166 if ( seteuid( uid
) != 0 ) {
167 Debug( LDAP_DEBUG_ANY
, "Could not set effective user id to %d\n",
170 exit( EXIT_FAILURE
);
176 #endif /* HAVE_PWD_H && HAVE_GRP_H */