acl_calc_mask.c revision 74432
1/*
2 * Copyright (c) 2001 Chris D. Faulhaber
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE VOICES IN HIS HEAD BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: head/lib/libc/posix1e/acl_calc_mask.c 74432 2001-03-19 03:19:51Z jedgar $
27 */
28
29#include <sys/types.h>
30#include <sys/acl.h>
31
32#include <errno.h>
33
34/*
35 * acl_calc_mask() calculates and set the permissions associated
36 * with the ACL_MASK ACL entry.  If the ACL already contains an
37 * ACL_MASK entry, its permissions shall be overwritten; if not,
38 * one shall be added.
39 */
40int
41acl_calc_mask(acl_t *acl_p)
42{
43	acl_t acl_new;
44	int group_obj, i, mask_mode, mask_num, other_obj, user_obj;
45
46	/* check args */
47	if (!acl_p || !*acl_p || ((*acl_p)->acl_cnt < 3) ||
48	    ((*acl_p)->acl_cnt > ACL_MAX_ENTRIES)) {
49		errno = EINVAL;
50		return -1;
51	}
52
53	acl_new = acl_dup(*acl_p);
54	if (!acl_new)
55		return -1;
56
57	user_obj = group_obj = other_obj = mask_mode = 0;
58	mask_num = -1;
59
60	/* gather permissions and find a mask entry */
61	for (i = 0; i < acl_new->acl_cnt; i++) {
62		switch(acl_new->acl_entry[i].ae_tag) {
63		case ACL_USER_OBJ:
64			user_obj++;
65			break;
66		case ACL_OTHER:
67			other_obj++;
68			break;
69		case ACL_GROUP_OBJ:
70			group_obj++;
71			/* FALLTHROUGH */
72		case ACL_GROUP:
73		case ACL_USER:
74			mask_mode |=
75			    acl_new->acl_entry[i].ae_perm & ACL_PERM_BITS;
76			break;
77		case ACL_MASK:
78			mask_num = i;
79			break;
80		default:
81			errno = EINVAL;
82			acl_free(acl_new);
83			return -1;
84			/* NOTREACHED */
85		}
86	}
87	if ((user_obj != 1) || (group_obj != 1) || (other_obj != 1)) {
88		errno = EINVAL;
89		acl_free(acl_new);
90		return -1;
91	}
92	/* if a mask entry already exists, overwrite the perms */
93	if (mask_num != -1) {
94		acl_new->acl_entry[mask_num].ae_perm = mask_mode;
95	} else {
96		/* if no mask exists, check acl_cnt... */
97		if (acl_new->acl_cnt == ACL_MAX_ENTRIES) {
98			errno = EINVAL;
99			acl_free(acl_new);
100			return -1;
101		}
102		/* ...and add the mask entry */
103		acl_new->acl_entry[acl_new->acl_cnt].ae_tag  = ACL_MASK;
104		acl_new->acl_entry[acl_new->acl_cnt].ae_id   = 0;
105		acl_new->acl_entry[acl_new->acl_cnt].ae_perm = mask_mode;
106		acl_new->acl_cnt++;
107	}
108
109	if (acl_valid(acl_new) == -1) {
110		errno = EINVAL;
111		acl_free(acl_new);
112		return -1;
113	}
114
115	**acl_p = *acl_new;
116	acl_free(acl_new);
117
118	return 0;
119}
120