forked from unfs3/unfs3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.c
293 lines (244 loc) · 5.91 KB
/
user.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* UNFS3 user and group id handling
* (C) 2003, Pascal Schmidt
* see file LICENSE for license details
*/
#include "config.h"
#ifndef WIN32
#include <pwd.h>
#include <grp.h>
#include <syslog.h>
#include <unistd.h>
#endif /* WIN32 */
#include <sys/types.h>
#include <sys/stat.h>
#include <rpc/rpc.h>
#include <stdlib.h>
#include "nfs.h"
#include "mount.h"
#include "daemon.h"
#include "user.h"
#include "backend.h"
#include "Config/exports.h"
/* user and group id we squash to */
static uid_t squash_uid = 65534;
static gid_t squash_gid = 65534;
/* whether we can use seteuid/setegid */
static int can_switch = TRUE;
/*
* initialize group and user id used for squashing
*/
void get_squash_ids(void)
{
backend_passwdstruct *passwd;
if (can_switch) {
passwd = backend_getpwnam("nobody");
if (passwd) {
squash_uid = passwd->pw_uid;
squash_gid = passwd->pw_gid;
} else {
squash_uid = 65534;
squash_gid = 65534;
}
}
}
/*
* mangle an id
*/
static int mangle(int id, int squash)
{
if (!can_switch || (exports_opts & OPT_ALL_SQUASH))
return squash;
else if (exports_opts & OPT_NO_ROOT_SQUASH)
return id;
else if (id == 0)
return squash;
else
return id;
}
/*
* Mangle a given user id according to current settings
*/
int mangle_uid(int id)
{
int squash = squash_uid;
if (exports_anonuid() != ANON_NOTSPECIAL)
squash = exports_anonuid();
return mangle(id, squash);
}
/*
* Mangle a given group id according to current settings
*/
int mangle_gid(int id)
{
int squash = squash_gid;
if (exports_anongid() != ANON_NOTSPECIAL)
squash = exports_anongid();
return mangle(id, squash);
}
/*
* return user id of a request
*/
int get_uid(struct svc_req *req)
{
struct authunix_parms *auth = (struct authunix_parms *) req->rq_clntcred;
int squash = squash_uid;
if (exports_anonuid() != ANON_NOTSPECIAL)
squash = exports_anonuid();
if (req->rq_cred.oa_flavor == AUTH_UNIX)
return mangle(auth->aup_uid, squash);
else
return squash; /* fallback if no uid given */
}
/*
* return group id of a request
*/
static int get_gid(struct svc_req *req)
{
struct authunix_parms *auth = (struct authunix_parms *) req->rq_clntcred;
int squash = squash_gid;
if (exports_anongid() != ANON_NOTSPECIAL)
squash = exports_anongid();
if (req->rq_cred.oa_flavor == AUTH_UNIX)
return mangle(auth->aup_gid, squash);
else
return squash; /* fallback if no gid given */
}
/*
* check whether a request comes from a given user id
*/
int is_owner(int owner, struct svc_req *req)
{
return (int) (owner == get_uid(req));
}
/*
* check if a request comes from somebody who has a given group id
*/
int has_group(int group, struct svc_req *req)
{
struct authunix_parms *auth = (struct authunix_parms *) req->rq_clntcred;
unsigned int i;
if (req->rq_cred.oa_flavor == AUTH_UNIX) {
if (mangle(auth->aup_gid, squash_gid) == group)
return TRUE;
/* search groups */
for (i = 0; i < auth->aup_len; i++)
if (mangle(auth->aup_gids[i], squash_gid) == group)
return TRUE;
}
return FALSE;
}
/*
* switch to root
*/
void switch_to_root(void)
{
if (!can_switch)
return;
backend_setegid(0);
backend_seteuid(0);
}
/*
* switch auxiliary group ids
*/
static int switch_groups(struct svc_req *req)
{
struct authunix_parms *auth = (struct authunix_parms *) req->rq_clntcred;
unsigned int i, max;
max = (auth->aup_len <= 32) ? auth->aup_len : 32;
for (i = 0; i < max; ++i) {
auth->aup_gids[i] = mangle(auth->aup_gids[i], squash_gid);
}
return backend_setgroups(max, auth->aup_gids);
}
/*
* switch user and group id to values listed in request
*/
void switch_user(struct svc_req *req)
{
int uid, gid, aid;
if (!can_switch)
return;
if (opt_singleuser || (backend_getuid() != 0)) {
/*
* have uid/gid functions behave correctly by squashing
* all user and group ids to the current values
*
* otherwise ACCESS would malfunction
*/
squash_uid = backend_getuid();
squash_gid = backend_getgid();
can_switch = FALSE;
return;
}
backend_setegid(0);
backend_seteuid(0);
gid = backend_setegid(get_gid(req));
aid = switch_groups(req);
uid = backend_seteuid(get_uid(req));
if (uid == -1 || gid == -1 || aid == -1) {
logmsg(LOG_EMERG, "euid/egid switching failed, aborting");
daemon_exit(CRISIS);
}
}
/*
* re-switch to root for reading executable files
*/
void read_executable(struct svc_req *req, backend_statstruct buf)
{
int have_exec = 0;
if (is_owner(buf.st_uid, req)) {
if (!(buf.st_mode & S_IRUSR) && (buf.st_mode & S_IXUSR))
have_exec = 1;
} else if (has_group(buf.st_gid, req)) {
if (!(buf.st_mode & S_IRGRP) && (buf.st_mode & S_IXGRP))
have_exec = 1;
} else {
if (!(buf.st_mode & S_IROTH) && (buf.st_mode & S_IXOTH))
have_exec = 1;
}
if (have_exec) {
backend_setegid(0);
backend_seteuid(0);
}
}
/*
* re-switch to root for reading owned file
*/
void read_by_owner(struct svc_req *req, backend_statstruct buf)
{
int have_owner = 0;
int have_read = 0;
have_owner = is_owner(buf.st_uid, req);
if (have_owner && (buf.st_mode & S_IRUSR)) {
have_read = 1;
} else if (has_group(buf.st_gid, req) && (buf.st_mode & S_IRGRP)) {
have_read = 1;
} else if (buf.st_mode & S_IROTH) {
have_read = 1;
}
if (have_owner && !have_read) {
backend_setegid(0);
backend_seteuid(0);
}
}
/*
* re-switch to root for writing owned file
*/
void write_by_owner(struct svc_req *req, backend_statstruct buf)
{
int have_owner = 0;
int have_write = 0;
have_owner = is_owner(buf.st_uid, req);
if (have_owner && (buf.st_mode & S_IWUSR)) {
have_write = 1;
} else if (has_group(buf.st_gid, req) && (buf.st_mode & S_IWGRP)) {
have_write = 1;
} else if (buf.st_mode & S_IWOTH) {
have_write = 1;
}
if (have_owner && !have_write) {
backend_setegid(0);
backend_seteuid(0);
}
}