user.c revision 1.1.1.5 1 1.1.1.2 lukem /* $NetBSD: user.c,v 1.1.1.5 2017/02/09 01:46:59 christos Exp $ */
2 1.1.1.2 lukem
3 1.1 lukem /* user.c - set user id, group id and group access list */
4 1.1.1.4 tron /* $OpenLDAP$ */
5 1.1 lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 1.1 lukem *
7 1.1.1.5 christos * Copyright 1998-2016 The OpenLDAP Foundation.
8 1.1 lukem * Portions Copyright 1999 PM Lashley.
9 1.1 lukem * All rights reserved.
10 1.1 lukem *
11 1.1 lukem * Redistribution and use in source and binary forms, with or without
12 1.1 lukem * modification, are permitted only as authorized by the OpenLDAP
13 1.1 lukem * Public License.
14 1.1 lukem *
15 1.1 lukem * A copy of this license is available in the file LICENSE in the
16 1.1 lukem * top-level directory of the distribution or, alternatively, at
17 1.1 lukem * <http://www.OpenLDAP.org/license.html>.
18 1.1 lukem */
19 1.1 lukem
20 1.1.1.5 christos #include <sys/cdefs.h>
21 1.1.1.5 christos __RCSID("$NetBSD: user.c,v 1.1.1.5 2017/02/09 01:46:59 christos Exp $");
22 1.1.1.5 christos
23 1.1 lukem #include "portable.h"
24 1.1 lukem
25 1.1 lukem #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
26 1.1 lukem
27 1.1 lukem #include <stdio.h>
28 1.1 lukem
29 1.1 lukem #include <ac/stdlib.h>
30 1.1 lukem
31 1.1 lukem #ifdef HAVE_PWD_H
32 1.1 lukem #include <pwd.h>
33 1.1 lukem #endif
34 1.1 lukem #ifdef HAVE_GRP_H
35 1.1 lukem #include <grp.h>
36 1.1 lukem #endif
37 1.1 lukem
38 1.1 lukem #include <ac/ctype.h>
39 1.1 lukem #include <ac/unistd.h>
40 1.1 lukem
41 1.1 lukem #include "slap.h"
42 1.1 lukem #include "lutil.h"
43 1.1 lukem
44 1.1 lukem /*
45 1.1 lukem * Set real and effective user id and group id, and group access list
46 1.1 lukem * The user and group arguments are freed.
47 1.1 lukem */
48 1.1 lukem
49 1.1 lukem void
50 1.1 lukem slap_init_user( char *user, char *group )
51 1.1 lukem {
52 1.1 lukem uid_t uid = 0;
53 1.1 lukem gid_t gid = 0;
54 1.1 lukem int got_uid = 0, got_gid = 0;
55 1.1 lukem
56 1.1 lukem if ( user ) {
57 1.1 lukem struct passwd *pwd;
58 1.1 lukem if ( isdigit( (unsigned char) *user ) ) {
59 1.1 lukem unsigned u;
60 1.1 lukem
61 1.1 lukem got_uid = 1;
62 1.1 lukem if ( lutil_atou( &u, user ) != 0 ) {
63 1.1 lukem Debug( LDAP_DEBUG_ANY, "Unble to parse user %s\n",
64 1.1 lukem user, 0, 0 );
65 1.1 lukem
66 1.1 lukem exit( EXIT_FAILURE );
67 1.1 lukem }
68 1.1 lukem uid = (uid_t)u;
69 1.1 lukem #ifdef HAVE_GETPWUID
70 1.1 lukem pwd = getpwuid( uid );
71 1.1 lukem goto did_getpw;
72 1.1 lukem #else
73 1.1 lukem free( user );
74 1.1 lukem user = NULL;
75 1.1 lukem #endif
76 1.1 lukem } else {
77 1.1 lukem pwd = getpwnam( user );
78 1.1 lukem did_getpw:
79 1.1 lukem if ( pwd == NULL ) {
80 1.1 lukem Debug( LDAP_DEBUG_ANY, "No passwd entry for user %s\n",
81 1.1 lukem user, 0, 0 );
82 1.1 lukem
83 1.1 lukem exit( EXIT_FAILURE );
84 1.1 lukem }
85 1.1 lukem if ( got_uid ) {
86 1.1 lukem free( user );
87 1.1 lukem user = (pwd != NULL ? ch_strdup( pwd->pw_name ) : NULL);
88 1.1 lukem } else {
89 1.1 lukem got_uid = 1;
90 1.1 lukem uid = pwd->pw_uid;
91 1.1 lukem }
92 1.1 lukem got_gid = 1;
93 1.1 lukem gid = pwd->pw_gid;
94 1.1 lukem #ifdef HAVE_ENDPWENT
95 1.1 lukem endpwent();
96 1.1 lukem #endif
97 1.1 lukem }
98 1.1 lukem }
99 1.1 lukem
100 1.1 lukem if ( group ) {
101 1.1 lukem struct group *grp;
102 1.1 lukem if ( isdigit( (unsigned char) *group )) {
103 1.1 lukem unsigned g;
104 1.1 lukem
105 1.1 lukem if ( lutil_atou( &g, group ) != 0 ) {
106 1.1 lukem Debug( LDAP_DEBUG_ANY, "Unble to parse group %s\n",
107 1.1 lukem group, 0, 0 );
108 1.1 lukem
109 1.1 lukem exit( EXIT_FAILURE );
110 1.1 lukem }
111 1.1 lukem gid = (uid_t)g;
112 1.1 lukem #ifdef HAVE_GETGRGID
113 1.1 lukem grp = getgrgid( gid );
114 1.1 lukem goto did_group;
115 1.1 lukem #endif
116 1.1 lukem } else {
117 1.1 lukem grp = getgrnam( group );
118 1.1 lukem if ( grp != NULL )
119 1.1 lukem gid = grp->gr_gid;
120 1.1 lukem did_group:
121 1.1 lukem if ( grp == NULL ) {
122 1.1 lukem Debug( LDAP_DEBUG_ANY, "No group entry for group %s\n",
123 1.1 lukem group, 0, 0 );
124 1.1 lukem
125 1.1 lukem exit( EXIT_FAILURE );
126 1.1 lukem }
127 1.1 lukem }
128 1.1 lukem free( group );
129 1.1 lukem got_gid = 1;
130 1.1 lukem }
131 1.1 lukem
132 1.1 lukem if ( user ) {
133 1.1 lukem if ( getuid() == 0 && initgroups( user, gid ) != 0 ) {
134 1.1 lukem Debug( LDAP_DEBUG_ANY,
135 1.1 lukem "Could not set the group access (gid) list\n", 0, 0, 0 );
136 1.1 lukem
137 1.1 lukem exit( EXIT_FAILURE );
138 1.1 lukem }
139 1.1 lukem free( user );
140 1.1 lukem }
141 1.1 lukem
142 1.1 lukem #ifdef HAVE_ENDGRENT
143 1.1 lukem endgrent();
144 1.1 lukem #endif
145 1.1 lukem
146 1.1 lukem if ( got_gid ) {
147 1.1 lukem if ( setgid( gid ) != 0 ) {
148 1.1 lukem Debug( LDAP_DEBUG_ANY, "Could not set real group id to %d\n",
149 1.1 lukem (int) gid, 0, 0 );
150 1.1 lukem
151 1.1 lukem exit( EXIT_FAILURE );
152 1.1 lukem }
153 1.1 lukem #ifdef HAVE_SETEGID
154 1.1 lukem if ( setegid( gid ) != 0 ) {
155 1.1 lukem Debug( LDAP_DEBUG_ANY, "Could not set effective group id to %d\n",
156 1.1 lukem (int) gid, 0, 0 );
157 1.1 lukem
158 1.1 lukem exit( EXIT_FAILURE );
159 1.1 lukem }
160 1.1 lukem #endif
161 1.1 lukem }
162 1.1 lukem
163 1.1 lukem if ( got_uid ) {
164 1.1 lukem if ( setuid( uid ) != 0 ) {
165 1.1 lukem Debug( LDAP_DEBUG_ANY, "Could not set real user id to %d\n",
166 1.1 lukem (int) uid, 0, 0 );
167 1.1 lukem
168 1.1 lukem exit( EXIT_FAILURE );
169 1.1 lukem }
170 1.1 lukem #ifdef HAVE_SETEUID
171 1.1 lukem if ( seteuid( uid ) != 0 ) {
172 1.1 lukem Debug( LDAP_DEBUG_ANY, "Could not set effective user id to %d\n",
173 1.1 lukem (int) uid, 0, 0 );
174 1.1 lukem
175 1.1 lukem exit( EXIT_FAILURE );
176 1.1 lukem }
177 1.1 lukem #endif
178 1.1 lukem }
179 1.1 lukem }
180 1.1 lukem
181 1.1 lukem #endif /* HAVE_PWD_H && HAVE_GRP_H */
182