user.c 3.09 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
3
4
/*
 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
5
6
/* user.c - set user id, group id and group access list
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
 * Copyright 1999 by PM Lashley.
8
9
10
11
12
13
14
15
16
17
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted only
 * as authorized by the OpenLDAP Public License.  A copy of this
 * license is available at http://www.OpenLDAP.org/license.html or
 * in file LICENSE in the top-level directory of the distribution.
*/

#include "portable.h"

18
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
19
20

#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
21
22

#include <ac/stdlib.h>
23
24

#ifdef HAVE_PWD_H
25
#include <pwd.h>
26
27
#endif
#ifdef HAVE_GRP_H
28
#include <grp.h>
29
#endif
30
31
32
33
34
35
36
37
38

#include <ac/ctype.h>
#include <ac/unistd.h>

#include "slap.h"


/*
 * Set real and effective user id and group id, and group access list
39
 * The user and group arguments are freed.
40
41
42
43
44
 */

void
slap_init_user( char *user, char *group )
{
45
46
47
    uid_t	uid;
    gid_t	gid;
    int  	got_uid = 0, got_gid = 0;
48
49
50
51

    if ( user ) {
	struct passwd *pwd;
	if ( isdigit( (unsigned char) *user )) {
52
	    got_uid = 1;
53
54
55
56
	    uid = atoi( user );
#ifdef HAVE_GETPWUID
	    pwd = getpwuid( uid );
	    goto did_getpw;
57
58
59
#else
	    free( user );
	    user = NULL;
60
61
62
63
64
65
66
#endif
	} else {
	    pwd = getpwnam( user );
	did_getpw:
	    if ( pwd == NULL ) {
		Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
		       user, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
67
		exit( EXIT_FAILURE );
68
	    }
69
	    if ( got_uid ) {
70
71
72
		free( user );
		user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
	    } else {
73
		got_uid = 1;
74
75
		uid = pwd->pw_uid;
	    }
76
	    got_gid = 1;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	    gid = pwd->pw_gid;
#ifdef HAVE_ENDPWENT
	    endpwent();
#endif
	}
    }

    if ( group ) {
	struct group *grp;
	if ( isdigit( (unsigned char) *group )) {
	    gid = atoi( group );
#ifdef HAVE_GETGRGID
	    grp = getgrgid( gid );
	    goto did_group;
#endif
	} else {
	    grp = getgrnam( group );
	    if ( grp != NULL )
		gid = grp->gr_gid;
	did_group:
	    if ( grp == NULL ) {
		Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
		       group, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
100
		exit( EXIT_FAILURE );
101
102
103
	    }
	}
	free( group );
104
	got_gid = 1;
105
106
107
108
109
110
    }

    if ( user ) {
	if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
	    Debug( LDAP_DEBUG_ANY,
		   "Could not set the group access (gid) list\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
111
	    exit( EXIT_FAILURE );
112
113
114
115
116
117
118
119
	}
	free( user );
    }

#ifdef HAVE_ENDGRENT
    endgrent();
#endif

120
    if ( got_gid ) {
121
122
123
	if ( setgid( gid ) != 0 ) {
	    Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
		   gid, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
124
	    exit( EXIT_FAILURE );
125
	}
126
#ifdef HAVE_SETEGID
127
128
129
	if ( setegid( gid ) != 0 ) {
	    Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
		   gid, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
130
	    exit( EXIT_FAILURE );
131
	}
132
#endif
133
134
    }

135
    if ( got_uid ) {
136
	if ( setuid( uid ) != 0 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
137
	    Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
138
		   uid, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
139
	    exit( EXIT_FAILURE );
140
	}
141
#ifdef HAVE_SETEUID
142
	if ( seteuid( uid ) != 0 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
143
	    Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
144
		   uid, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
145
	    exit( EXIT_FAILURE );
146
	}
147
#endif
148
149
150
151
    }
}

#endif /* HAVE_PWD_H && HAVE_GRP_H */