acl_calc_mask.c revision 75185
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 75185 2001-04-04 18:00:52Z tmm $
27 */
28
29#include <sys/types.h>
30#include "namespace.h"
31#include <sys/acl.h>
32#include "un-namespace.h"
33
34#include <errno.h>
35
36/*
37 * acl_calc_mask() calculates and set the permissions associated
38 * with the ACL_MASK ACL entry.  If the ACL already contains an
39 * ACL_MASK entry, its permissions shall be overwritten; if not,
40 * one shall be added.
41 */
42int
43acl_calc_mask(acl_t *acl_p)
44{
45	acl_t acl_new;
46	int group_obj, i, mask_mode, mask_num, other_obj, user_obj;
47
48	/* check args */
49	if (!acl_p || !*acl_p || ((*acl_p)->acl_cnt < 3) ||
50	    ((*acl_p)->acl_cnt > ACL_MAX_ENTRIES)) {
51		errno = EINVAL;
52		return -1;
53	}
54
55	acl_new = acl_dup(*acl_p);
56	if (!acl_new)
57		return -1;
58
59	user_obj = group_obj = other_obj = mask_mode = 0;
60	mask_num = -1;
61
62	/* gather permissions and find a mask entry */
63	for (i = 0; i < acl_new->acl_cnt; i++) {
64		switch(acl_new->acl_entry[i].ae_tag) {
65		case ACL_USER_OBJ:
66			user_obj++;
67			break;
68		case ACL_OTHER:
69			other_obj++;
70			break;
71		case ACL_GROUP_OBJ:
72			group_obj++;
73			/* FALLTHROUGH */
74		case ACL_GROUP:
75		case ACL_USER:
76			mask_mode |=
77			    acl_new->acl_entry[i].ae_perm & ACL_PERM_BITS;
78			break;
79		case ACL_MASK:
80			mask_num = i;
81			break;
82		default:
83			errno = EINVAL;
84			acl_free(acl_new);
85			return -1;
86			/* NOTREACHED */
87		}
88	}
89	if ((user_obj != 1) || (group_obj != 1) || (other_obj != 1)) {
90		errno = EINVAL;
91		acl_free(acl_new);
92		return -1;
93	}
94	/* if a mask entry already exists, overwrite the perms */
95	if (mask_num != -1) {
96		acl_new->acl_entry[mask_num].ae_perm = mask_mode;
97	} else {
98		/* if no mask exists, check acl_cnt... */
99		if (acl_new->acl_cnt == ACL_MAX_ENTRIES) {
100			errno = EINVAL;
101			acl_free(acl_new);
102			return -1;
103		}
104		/* ...and add the mask entry */
105		acl_new->acl_entry[acl_new->acl_cnt].ae_tag  = ACL_MASK;
106		acl_new->acl_entry[acl_new->acl_cnt].ae_id   = 0;
107		acl_new->acl_entry[acl_new->acl_cnt].ae_perm = mask_mode;
108		acl_new->acl_cnt++;
109	}
110
111	if (acl_valid(acl_new) == -1) {
112		errno = EINVAL;
113		acl_free(acl_new);
114		return -1;
115	}
116
117	**acl_p = *acl_new;
118	acl_free(acl_new);
119
120	return 0;
121}
122