nfs_commonacl.c 13 KB
Newer Older
1
/*-
2
3
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
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
 * Copyright (c) 2009 Rick Macklem, University of Guelph
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <fs/nfs/nfsport.h>

extern int nfsrv_useacl;

static int nfsrv_acemasktoperm(u_int32_t acetype, u_int32_t mask, int owner,
    enum vtype type, acl_perm_t *permp);

/*
 * Handle xdr for an ace.
 */
Ryan Moeller's avatar
Ryan Moeller committed
43
int
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
nfsrv_dissectace(struct nfsrv_descript *nd, struct acl_entry *acep,
    int *aceerrp, int *acesizep, NFSPROC_T *p)
{
	u_int32_t *tl;
	int len, gotid = 0, owner = 0, error = 0, aceerr = 0;
	u_char *name, namestr[NFSV4_SMALLSTR + 1];
	u_int32_t flag, mask, acetype;
	gid_t gid;
	uid_t uid;

	*aceerrp = 0;
	acep->ae_flags = 0;
	NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
	acetype = fxdr_unsigned(u_int32_t, *tl++);
	flag = fxdr_unsigned(u_int32_t, *tl++);
	mask = fxdr_unsigned(u_int32_t, *tl++);
	len = fxdr_unsigned(int, *tl);
61
62
63
64
65
	/*
	 * The RFCs do not specify a limit to the length of the "who", but
	 * NFSV4_OPAQUELIMIT (1024) should be sufficient.
	 */
	if (len < 0 || len > NFSV4_OPAQUELIMIT) {
Zack Kirsch's avatar
Zack Kirsch committed
66
67
		error = NFSERR_BADXDR;
		goto nfsmout;
68
69
70
71
72
	} else if (len == 0) {
		/* Netapp filers return a 0 length who for nil users */
		acep->ae_tag = ACL_UNDEFINED_TAG;
		acep->ae_id = ACL_UNDEFINED_ID;
		acep->ae_perm = (acl_perm_t)0;
73
		acep->ae_entry_type = ACL_ENTRY_TYPE_DENY;
74
75
		if (acesizep)
			*acesizep = 4 * NFSX_UNSIGNED;
Zack Kirsch's avatar
Zack Kirsch committed
76
77
		error = 0;
		goto nfsmout;
78
79
80
81
82
83
84
85
86
	}
	if (len > NFSV4_SMALLSTR)
		name = malloc(len + 1, M_NFSSTRING, M_WAITOK);
	else
		name = namestr;
	error = nfsrv_mtostr(nd, name, len);
	if (error) {
		if (len > NFSV4_SMALLSTR)
			free(name, M_NFSSTRING);
Zack Kirsch's avatar
Zack Kirsch committed
87
		goto nfsmout;
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	}
	if (len == 6) {
		if (!NFSBCMP(name, "OWNER@", 6)) {
			acep->ae_tag = ACL_USER_OBJ;
			acep->ae_id = ACL_UNDEFINED_ID;
			owner = 1;
			gotid = 1;
		} else if (!NFSBCMP(name, "GROUP@", 6)) {
			acep->ae_tag = ACL_GROUP_OBJ;
			acep->ae_id = ACL_UNDEFINED_ID;
			gotid = 1;
		}
	} else if (len == 9 && !NFSBCMP(name, "EVERYONE@", 9)) {
		acep->ae_tag = ACL_EVERYONE;
		acep->ae_id = ACL_UNDEFINED_ID;
		gotid = 1;
	}
	if (gotid == 0) {
		if (flag & NFSV4ACE_IDENTIFIERGROUP) {
			acep->ae_tag = ACL_GROUP;
108
			aceerr = nfsv4_strtogid(nd, name, len, &gid);
109
110
111
112
			if (aceerr == 0)
				acep->ae_id = (uid_t)gid;
		} else {
			acep->ae_tag = ACL_USER;
113
			aceerr = nfsv4_strtouid(nd, name, len, &uid);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
			if (aceerr == 0)
				acep->ae_id = uid;
		}
	}
	if (len > NFSV4_SMALLSTR)
		free(name, M_NFSSTRING);

	if (aceerr == 0) {
		/*
		 * Handle the flags.
		 */
		flag &= ~NFSV4ACE_IDENTIFIERGROUP;
		if (flag & NFSV4ACE_FILEINHERIT) {
			flag &= ~NFSV4ACE_FILEINHERIT;
			acep->ae_flags |= ACL_ENTRY_FILE_INHERIT;
		}
		if (flag & NFSV4ACE_DIRECTORYINHERIT) {
			flag &= ~NFSV4ACE_DIRECTORYINHERIT;
			acep->ae_flags |= ACL_ENTRY_DIRECTORY_INHERIT;
		}
		if (flag & NFSV4ACE_NOPROPAGATEINHERIT) {
			flag &= ~NFSV4ACE_NOPROPAGATEINHERIT;
136
			acep->ae_flags |= ACL_ENTRY_NO_PROPAGATE_INHERIT;
137
138
139
		}
		if (flag & NFSV4ACE_INHERITONLY) {
			flag &= ~NFSV4ACE_INHERITONLY;
140
			acep->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
141
142
143
144
145
146
147
148
149
150
		}
		if (flag & NFSV4ACE_SUCCESSFULACCESS) {
			flag &= ~NFSV4ACE_SUCCESSFULACCESS;
			acep->ae_flags |= ACL_ENTRY_SUCCESSFUL_ACCESS;
		}
		if (flag & NFSV4ACE_FAILEDACCESS) {
			flag &= ~NFSV4ACE_FAILEDACCESS;
			acep->ae_flags |= ACL_ENTRY_FAILED_ACCESS;
		}
		/*
151
		 * Set ae_entry_type.
152
153
		 */
		if (acetype == NFSV4ACE_ALLOWEDTYPE)
154
			acep->ae_entry_type = ACL_ENTRY_TYPE_ALLOW;
155
		else if (acetype == NFSV4ACE_DENIEDTYPE)
156
			acep->ae_entry_type = ACL_ENTRY_TYPE_DENY;
157
		else if (acetype == NFSV4ACE_AUDITTYPE)
158
			acep->ae_entry_type = ACL_ENTRY_TYPE_AUDIT;
159
		else if (acetype == NFSV4ACE_ALARMTYPE)
160
			acep->ae_entry_type = ACL_ENTRY_TYPE_ALARM;
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
		else
			aceerr = NFSERR_ATTRNOTSUPP;
	}

	/*
	 * Now, check for unsupported flag bits.
	 */
	if (aceerr == 0 && flag != 0)
		aceerr = NFSERR_ATTRNOTSUPP;

	/*
	 * And turn the mask into perm bits.
	 */
	if (aceerr == 0)
		aceerr = nfsrv_acemasktoperm(acetype, mask, owner, VREG,
		    &acep->ae_perm);
	*aceerrp = aceerr;
	if (acesizep)
		*acesizep = NFSM_RNDUP(len) + (4 * NFSX_UNSIGNED);
Zack Kirsch's avatar
Zack Kirsch committed
180
	error = 0;
181
nfsmout:
Zack Kirsch's avatar
Zack Kirsch committed
182
	NFSEXITCODE(error);
183
184
185
186
187
188
189
190
191
192
193
	return (error);
}

/*
 * Turn an NFSv4 ace mask into R/W/X flag bits.
 */
static int
nfsrv_acemasktoperm(u_int32_t acetype, u_int32_t mask, int owner,
    enum vtype type, acl_perm_t *permp)
{
	acl_perm_t perm = 0x0;
Zack Kirsch's avatar
Zack Kirsch committed
194
	int error = 0;
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

	if (mask & NFSV4ACE_READDATA) {
		mask &= ~NFSV4ACE_READDATA;
		perm |= ACL_READ_DATA;
	}
	if (mask & NFSV4ACE_LISTDIRECTORY) {
		mask &= ~NFSV4ACE_LISTDIRECTORY;
		perm |= ACL_LIST_DIRECTORY;
	}
	if (mask & NFSV4ACE_WRITEDATA) {
		mask &= ~NFSV4ACE_WRITEDATA;
		perm |= ACL_WRITE_DATA;
	}
	if (mask & NFSV4ACE_ADDFILE) {
		mask &= ~NFSV4ACE_ADDFILE;
		perm |= ACL_ADD_FILE;
	}
	if (mask & NFSV4ACE_APPENDDATA) {
		mask &= ~NFSV4ACE_APPENDDATA;
		perm |= ACL_APPEND_DATA;
	}
	if (mask & NFSV4ACE_ADDSUBDIRECTORY) {
		mask &= ~NFSV4ACE_ADDSUBDIRECTORY;
		perm |= ACL_ADD_SUBDIRECTORY;
	}
	if (mask & NFSV4ACE_READNAMEDATTR) {
		mask &= ~NFSV4ACE_READNAMEDATTR;
		perm |= ACL_READ_NAMED_ATTRS;
	}
	if (mask & NFSV4ACE_WRITENAMEDATTR) {
		mask &= ~NFSV4ACE_WRITENAMEDATTR;
		perm |= ACL_WRITE_NAMED_ATTRS;
	}
	if (mask & NFSV4ACE_EXECUTE) {
		mask &= ~NFSV4ACE_EXECUTE;
		perm |= ACL_EXECUTE;
	}
	if (mask & NFSV4ACE_SEARCH) {
		mask &= ~NFSV4ACE_SEARCH;
234
		perm |= ACL_EXECUTE;
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
	}
	if (mask & NFSV4ACE_DELETECHILD) {
		mask &= ~NFSV4ACE_DELETECHILD;
		perm |= ACL_DELETE_CHILD;
	}
	if (mask & NFSV4ACE_READATTRIBUTES) {
		mask &= ~NFSV4ACE_READATTRIBUTES;
		perm |= ACL_READ_ATTRIBUTES;
	}
	if (mask & NFSV4ACE_WRITEATTRIBUTES) {
		mask &= ~NFSV4ACE_WRITEATTRIBUTES;
		perm |= ACL_WRITE_ATTRIBUTES;
	}
	if (mask & NFSV4ACE_DELETE) {
		mask &= ~NFSV4ACE_DELETE;
		perm |= ACL_DELETE;
	}
	if (mask & NFSV4ACE_READACL) {
		mask &= ~NFSV4ACE_READACL;
		perm |= ACL_READ_ACL;
	}
	if (mask & NFSV4ACE_WRITEACL) {
		mask &= ~NFSV4ACE_WRITEACL;
		perm |= ACL_WRITE_ACL;
	}
	if (mask & NFSV4ACE_WRITEOWNER) {
		mask &= ~NFSV4ACE_WRITEOWNER;
		perm |= ACL_WRITE_OWNER;
	}
	if (mask & NFSV4ACE_SYNCHRONIZE) {
		mask &= ~NFSV4ACE_SYNCHRONIZE;
		perm |= ACL_SYNCHRONIZE;
	}
Zack Kirsch's avatar
Zack Kirsch committed
268
269
270
271
	if (mask != 0) {
		error = NFSERR_ATTRNOTSUPP;
		goto out;
	}
272
	*permp = perm;
Zack Kirsch's avatar
Zack Kirsch committed
273
274
275
276

out:
	NFSEXITCODE(error);
	return (error);
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
}

/* local functions */
static int nfsrv_buildace(struct nfsrv_descript *, u_char *, int,
    enum vtype, int, int, struct acl_entry *);

/*
 * This function builds an NFS ace.
 */
static int
nfsrv_buildace(struct nfsrv_descript *nd, u_char *name, int namelen,
    enum vtype type, int group, int owner, struct acl_entry *ace)
{
	u_int32_t *tl, aceflag = 0x0, acemask = 0x0, acetype;
	int full_len;

	full_len = NFSM_RNDUP(namelen);
	NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED + full_len);

	/*
	 * Fill in the ace type.
	 */
299
	if (ace->ae_entry_type & ACL_ENTRY_TYPE_ALLOW)
300
		acetype = NFSV4ACE_ALLOWEDTYPE;
301
	else if (ace->ae_entry_type & ACL_ENTRY_TYPE_DENY)
302
		acetype = NFSV4ACE_DENIEDTYPE;
303
	else if (ace->ae_entry_type & ACL_ENTRY_TYPE_AUDIT)
304
305
306
307
308
309
310
311
312
313
314
315
		acetype = NFSV4ACE_AUDITTYPE;
	else
		acetype = NFSV4ACE_ALARMTYPE;
	*tl++ = txdr_unsigned(acetype);

	/*
	 * Set the flag bits from the ACL.
	 */
	if (ace->ae_flags & ACL_ENTRY_FILE_INHERIT)
		aceflag |= NFSV4ACE_FILEINHERIT;
	if (ace->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT)
		aceflag |= NFSV4ACE_DIRECTORYINHERIT;
316
	if (ace->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT)
317
		aceflag |= NFSV4ACE_NOPROPAGATEINHERIT;
318
	if (ace->ae_flags & ACL_ENTRY_INHERIT_ONLY)
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
		aceflag |= NFSV4ACE_INHERITONLY;
	if (ace->ae_flags & ACL_ENTRY_SUCCESSFUL_ACCESS)
		aceflag |= NFSV4ACE_SUCCESSFULACCESS;
	if (ace->ae_flags & ACL_ENTRY_FAILED_ACCESS)
		aceflag |= NFSV4ACE_FAILEDACCESS;
	if (group)
		aceflag |= NFSV4ACE_IDENTIFIERGROUP;
	*tl++ = txdr_unsigned(aceflag);
	if (type == VDIR) {
		if (ace->ae_perm & ACL_LIST_DIRECTORY)
			acemask |= NFSV4ACE_LISTDIRECTORY;
		if (ace->ae_perm & ACL_ADD_FILE)
			acemask |= NFSV4ACE_ADDFILE;
		if (ace->ae_perm & ACL_ADD_SUBDIRECTORY)
			acemask |= NFSV4ACE_ADDSUBDIRECTORY;
		if (ace->ae_perm & ACL_READ_NAMED_ATTRS)
			acemask |= NFSV4ACE_READNAMEDATTR;
		if (ace->ae_perm & ACL_WRITE_NAMED_ATTRS)
			acemask |= NFSV4ACE_WRITENAMEDATTR;
338
		if (ace->ae_perm & ACL_EXECUTE)
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
			acemask |= NFSV4ACE_SEARCH;
		if (ace->ae_perm & ACL_DELETE_CHILD)
			acemask |= NFSV4ACE_DELETECHILD;
		if (ace->ae_perm & ACL_READ_ATTRIBUTES)
			acemask |= NFSV4ACE_READATTRIBUTES;
		if (ace->ae_perm & ACL_WRITE_ATTRIBUTES)
			acemask |= NFSV4ACE_WRITEATTRIBUTES;
		if (ace->ae_perm & ACL_DELETE)
			acemask |= NFSV4ACE_DELETE;
		if (ace->ae_perm & ACL_READ_ACL)
			acemask |= NFSV4ACE_READACL;
		if (ace->ae_perm & ACL_WRITE_ACL)
			acemask |= NFSV4ACE_WRITEACL;
		if (ace->ae_perm & ACL_WRITE_OWNER)
			acemask |= NFSV4ACE_WRITEOWNER;
354
355
		if (ace->ae_perm & ACL_SYNCHRONIZE)
			acemask |= NFSV4ACE_SYNCHRONIZE;
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
	} else {
		if (ace->ae_perm & ACL_READ_DATA)
			acemask |= NFSV4ACE_READDATA;
		if (ace->ae_perm & ACL_WRITE_DATA)
			acemask |= NFSV4ACE_WRITEDATA;
		if (ace->ae_perm & ACL_APPEND_DATA)
			acemask |= NFSV4ACE_APPENDDATA;
		if (ace->ae_perm & ACL_READ_NAMED_ATTRS)
			acemask |= NFSV4ACE_READNAMEDATTR;
		if (ace->ae_perm & ACL_WRITE_NAMED_ATTRS)
			acemask |= NFSV4ACE_WRITENAMEDATTR;
		if (ace->ae_perm & ACL_EXECUTE)
			acemask |= NFSV4ACE_EXECUTE;
		if (ace->ae_perm & ACL_READ_ATTRIBUTES)
			acemask |= NFSV4ACE_READATTRIBUTES;
		if (ace->ae_perm & ACL_WRITE_ATTRIBUTES)
			acemask |= NFSV4ACE_WRITEATTRIBUTES;
		if (ace->ae_perm & ACL_DELETE)
			acemask |= NFSV4ACE_DELETE;
		if (ace->ae_perm & ACL_READ_ACL)
			acemask |= NFSV4ACE_READACL;
		if (ace->ae_perm & ACL_WRITE_ACL)
			acemask |= NFSV4ACE_WRITEACL;
		if (ace->ae_perm & ACL_WRITE_OWNER)
			acemask |= NFSV4ACE_WRITEOWNER;
		if (ace->ae_perm & ACL_SYNCHRONIZE)
			acemask |= NFSV4ACE_SYNCHRONIZE;
	}
	*tl++ = txdr_unsigned(acemask);
	*tl++ = txdr_unsigned(namelen);
	if (full_len - namelen)
		*(tl + (namelen / NFSX_UNSIGNED)) = 0x0;
	NFSBCOPY(name, (caddr_t)tl, namelen);
	return (full_len + 4 * NFSX_UNSIGNED);
}

/*
 * Build an NFSv4 ACL.
 */
Ryan Moeller's avatar
Ryan Moeller committed
395
int
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
nfsrv_buildacl(struct nfsrv_descript *nd, NFSACL_T *aclp, enum vtype type,
    NFSPROC_T *p)
{
	int i, entrycnt = 0, retlen;
	u_int32_t *entrycntp;
	int isowner, isgroup, namelen, malloced;
	u_char *name, namestr[NFSV4_SMALLSTR];

	NFSM_BUILD(entrycntp, u_int32_t *, NFSX_UNSIGNED);
	retlen = NFSX_UNSIGNED;
	/*
	 * Loop through the acl entries, building each one.
	 */
	for (i = 0; i < aclp->acl_cnt; i++) {
		isowner = isgroup = malloced = 0;
		switch (aclp->acl_entry[i].ae_tag) {
		case ACL_USER_OBJ:
			isowner = 1;
			name = "OWNER@";
			namelen = 6;
			break;
		case ACL_GROUP_OBJ:
			isgroup = 1;
			name = "GROUP@";
			namelen = 6;
			break;
		case ACL_EVERYONE:
			name = "EVERYONE@";
			namelen = 9;
			break;
		case ACL_USER:
			name = namestr;
			nfsv4_uidtostr(aclp->acl_entry[i].ae_id, &name,
429
			    &namelen);
430
431
432
433
434
435
436
			if (name != namestr)
				malloced = 1;
			break;
		case ACL_GROUP:
			isgroup = 1;
			name = namestr;
			nfsv4_gidtostr((gid_t)aclp->acl_entry[i].ae_id, &name,
437
			    &namelen);
438
439
440
441
442
			if (name != namestr)
				malloced = 1;
			break;
		default:
			continue;
443
		}
444
445
446
447
448
449
450
451
452
453
454
455
456
457
		retlen += nfsrv_buildace(nd, name, namelen, type, isgroup,
		    isowner, &aclp->acl_entry[i]);
		entrycnt++;
		if (malloced)
			free(name, M_NFSSTRING);
	}
	*entrycntp = txdr_unsigned(entrycnt);
	return (retlen);
}

/*
 * Compare two NFSv4 acls.
 * Return 0 if they are the same, 1 if not the same.
 */
Ryan Moeller's avatar
Ryan Moeller committed
458
int
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
nfsrv_compareacl(NFSACL_T *aclp1, NFSACL_T *aclp2)
{
	int i;
	struct acl_entry *acep1, *acep2;

	if (aclp1->acl_cnt != aclp2->acl_cnt)
		return (1);
	acep1 = aclp1->acl_entry;
	acep2 = aclp2->acl_entry;
	for (i = 0; i < aclp1->acl_cnt; i++) {
		if (acep1->ae_tag != acep2->ae_tag)
			return (1);
		switch (acep1->ae_tag) {
		case ACL_GROUP:
		case ACL_USER:
			if (acep1->ae_id != acep2->ae_id)
				return (1);
			/* fall through */
		case ACL_USER_OBJ:
		case ACL_GROUP_OBJ:
		case ACL_OTHER:
			if (acep1->ae_perm != acep2->ae_perm)
				return (1);
482
		}
483
484
485
486
487
		acep1++;
		acep2++;
	}
	return (0);
}