user.c 3.15 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
4
5
/*
 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
6
7
/* user.c - set user id, group id and group access list
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
8
 * Copyright 1999 by PM Lashley.
9
10
11
12
13
14
15
16
17
18
 * 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"

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

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

#include <ac/stdlib.h>
24
25

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

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

#include "slap.h"


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

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

    if ( user ) {
	struct passwd *pwd;
	if ( isdigit( (unsigned char) *user )) {
53
	    got_uid = 1;
54
55
56
57
	    uid = atoi( user );
#ifdef HAVE_GETPWUID
	    pwd = getpwuid( uid );
	    goto did_getpw;
58
59
60
#else
	    free( user );
	    user = NULL;
61
62
63
64
65
66
67
#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
68
		exit( EXIT_FAILURE );
69
	    }
70
	    if ( got_uid ) {
71
72
73
		free( user );
		user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
	    } else {
74
		got_uid = 1;
75
76
		uid = pwd->pw_uid;
	    }
77
	    got_gid = 1;
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
	    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
101
		exit( EXIT_FAILURE );
102
103
104
	    }
	}
	free( group );
105
	got_gid = 1;
106
107
108
109
110
111
    }

    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
112
	    exit( EXIT_FAILURE );
113
114
115
116
117
118
119
120
	}
	free( user );
    }

#ifdef HAVE_ENDGRENT
    endgrent();
#endif

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

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

#endif /* HAVE_PWD_H && HAVE_GRP_H */