acl_calc_mask.c revision 90781
1/*
2 * Copyright (c) 2001-2002 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 90781 2002-02-17 20:05:20Z jedgar $
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#include <stdio.h>
36
37/*
38 * acl_calc_mask() (23.4.2): calculate and set the permissions
39 * associated with the ACL_MASK ACL entry.  If the ACL already
40 * contains an ACL_MASK entry, its permissions shall be
41 * overwritten; if not, one shall be added.
42 */
43int
44acl_calc_mask(acl_t *acl_p)
45{
46	struct acl	*acl_int, *acl_int_new;
47	acl_t		acl_new;
48	int		i, mask_mode, mask_num;
49
50	/*
51	 * (23.4.2.4) requires acl_p to point to a pointer to a valid ACL.
52	 * Since one of the primary reasons to use this function would be
53	 * to calculate the appropriate mask to obtain a valid ACL, we only
54	 * perform sanity checks here and validate the ACL prior to
55	 * returning.
56	 */
57	if (acl_p == NULL || *acl_p == NULL) {
58		errno = EINVAL;
59		return (-1);
60	}
61	acl_int = &(*acl_p)->ats_acl;
62	if ((acl_int->acl_cnt < 3) || (acl_int->acl_cnt > ACL_MAX_ENTRIES)) {
63		errno = EINVAL;
64		return (-1);
65	}
66
67	acl_new = acl_dup(*acl_p);
68	if (acl_new == NULL)
69		return (-1);
70	acl_int_new = &acl_new->ats_acl;
71
72	mask_mode = 0;
73	mask_num = -1;
74
75	/* gather permissions and find a mask entry */
76	for (i = 0; i < acl_int_new->acl_cnt; i++) {
77		switch(acl_int_new->acl_entry[i].ae_tag) {
78		case ACL_USER:
79		case ACL_GROUP:
80		case ACL_GROUP_OBJ:
81			mask_mode |=
82			    acl_int_new->acl_entry[i].ae_perm & ACL_PERM_BITS;
83			break;
84		case ACL_MASK:
85			mask_num = i;
86			break;
87		}
88	}
89
90	/* if a mask entry already exists, overwrite the perms */
91	if (mask_num != -1)
92		acl_int_new->acl_entry[mask_num].ae_perm = mask_mode;
93	else {
94		/* if no mask exists, check acl_cnt... */
95		if (acl_int_new->acl_cnt == ACL_MAX_ENTRIES) {
96			errno = ENOMEM;
97			return (-1);
98		}
99		/* ...and add the mask entry */
100		acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_tag = ACL_MASK;
101		acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_id =
102		    ACL_UNDEFINED_ID;
103		acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_perm =
104		    mask_mode;
105		acl_int_new->acl_cnt++;
106	}
107
108	if (acl_valid(acl_new) == -1) {
109		errno = EINVAL;
110		acl_free(acl_new);
111		return (-1);
112	}
113
114	**acl_p = *acl_new;
115	acl_free(acl_new);
116
117	return (0);
118}
119