]> git.sur5r.net Git - openldap/blob - servers/slapd/user.c
Fix Debug() with too few arguments.
[openldap] / servers / slapd / user.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /* user.c - set user id, group id and group access list
6  *
7  * Copyright 1999 by PM Lashley.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted only
11  * as authorized by the OpenLDAP Public License.  A copy of this
12  * license is available at http://www.OpenLDAP.org/license.html or
13  * in file LICENSE in the top-level directory of the distribution.
14 */
15
16 #include "portable.h"
17
18 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
19
20 #include <stdio.h>
21
22 #include <ac/stdlib.h>
23
24 #ifdef HAVE_PWD_H
25 #include <pwd.h>
26 #endif
27 #ifdef HAVE_GRP_H
28 #include <grp.h>
29 #endif
30
31 #include <ac/ctype.h>
32 #include <ac/unistd.h>
33
34 #include "slap.h"
35
36
37 /*
38  * Set real and effective user id and group id, and group access list
39  * The user and group arguments are freed.
40  */
41
42 void
43 slap_init_user( char *user, char *group )
44 {
45     uid_t       uid;
46     gid_t       gid;
47     int         got_uid = 0, got_gid = 0;
48
49     if ( user ) {
50         struct passwd *pwd;
51         if ( isdigit( (unsigned char) *user )) {
52             got_uid = 1;
53             uid = atoi( user );
54 #ifdef HAVE_GETPWUID
55             pwd = getpwuid( uid );
56             goto did_getpw;
57 #else
58             free( user );
59             user = NULL;
60 #endif
61         } else {
62             pwd = getpwnam( user );
63         did_getpw:
64             if ( pwd == NULL ) {
65                 Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
66                        user, 0, 0 );
67                 exit( EXIT_FAILURE );
68             }
69             if ( got_uid ) {
70                 free( user );
71                 user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
72             } else {
73                 got_uid = 1;
74                 uid = pwd->pw_uid;
75             }
76             got_gid = 1;
77             gid = pwd->pw_gid;
78 #ifdef HAVE_ENDPWENT
79             endpwent();
80 #endif
81         }
82     }
83
84     if ( group ) {
85         struct group *grp;
86         if ( isdigit( (unsigned char) *group )) {
87             gid = atoi( group );
88 #ifdef HAVE_GETGRGID
89             grp = getgrgid( gid );
90             goto did_group;
91 #endif
92         } else {
93             grp = getgrnam( group );
94             if ( grp != NULL )
95                 gid = grp->gr_gid;
96         did_group:
97             if ( grp == NULL ) {
98                 Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
99                        group, 0, 0 );
100                 exit( EXIT_FAILURE );
101             }
102         }
103         free( group );
104         got_gid = 1;
105     }
106
107     if ( user ) {
108         if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
109             Debug( LDAP_DEBUG_ANY,
110                    "Could not set the group access (gid) list\n", 0, 0, 0 );
111             exit( EXIT_FAILURE );
112         }
113         free( user );
114     }
115
116 #ifdef HAVE_ENDGRENT
117     endgrent();
118 #endif
119
120     if ( got_gid ) {
121         if ( setgid( gid ) != 0 ) {
122             Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
123                    gid, 0, 0 );
124             exit( EXIT_FAILURE );
125         }
126 #ifdef HAVE_SETEGID
127         if ( setegid( gid ) != 0 ) {
128             Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
129                    gid, 0, 0 );
130             exit( EXIT_FAILURE );
131         }
132 #endif
133     }
134
135     if ( got_uid ) {
136         if ( setuid( uid ) != 0 ) {
137             Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
138                    uid, 0, 0 );
139             exit( EXIT_FAILURE );
140         }
141 #ifdef HAVE_SETEUID
142         if ( seteuid( uid ) != 0 ) {
143             Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
144                    uid, 0, 0 );
145             exit( EXIT_FAILURE );
146         }
147 #endif
148     }
149 }
150
151 #endif /* HAVE_PWD_H && HAVE_GRP_H */