acl_entry.c revision 194955
174667Sjedgar/*
290781Sjedgar * Copyright (c) 2001-2002 Chris D. Faulhaber
374667Sjedgar * All rights reserved.
474667Sjedgar *
574667Sjedgar * Redistribution and use in source and binary forms, with or without
674667Sjedgar * modification, are permitted provided that the following conditions
774667Sjedgar * are met:
874667Sjedgar * 1. Redistributions of source code must retain the above copyright
974667Sjedgar *    notice, this list of conditions and the following disclaimer.
1074667Sjedgar * 2. Redistributions in binary form must reproduce the above copyright
1174667Sjedgar *    notice, this list of conditions and the following disclaimer in the
1274667Sjedgar *    documentation and/or other materials provided with the distribution.
1374667Sjedgar *
1474667Sjedgar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1574667Sjedgar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1674667Sjedgar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1774667Sjedgar * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1874667Sjedgar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1974667Sjedgar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2074667Sjedgar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2174667Sjedgar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2274667Sjedgar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2374667Sjedgar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2474667Sjedgar * SUCH DAMAGE.
2574667Sjedgar */
2674667Sjedgar
2792986Sobrien#include <sys/cdefs.h>
2892986Sobrien__FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_entry.c 194955 2009-06-25 12:46:59Z trasz $");
2992986Sobrien
3074667Sjedgar#include <sys/types.h>
3175185Stmm#include "namespace.h"
3274667Sjedgar#include <sys/acl.h>
3375185Stmm#include "un-namespace.h"
3474667Sjedgar
3574667Sjedgar#include <errno.h>
3674667Sjedgar#include <stdlib.h>
3774667Sjedgar
3875928Sjedgar/*
3975928Sjedgar * acl_create_entry() (23.4.7): create a new ACL entry in the ACL pointed
4075928Sjedgar * to by acl_p.
4175928Sjedgar */
4274667Sjedgarint
4374667Sjedgaracl_create_entry(acl_t *acl_p, acl_entry_t *entry_p)
4474667Sjedgar{
4575928Sjedgar	struct acl *acl_int;
4674667Sjedgar
4790781Sjedgar	if (acl_p == NULL) {
4874667Sjedgar		errno = EINVAL;
4990781Sjedgar		return (-1);
5074667Sjedgar	}
5174667Sjedgar
5275928Sjedgar	acl_int = &(*acl_p)->ats_acl;
5374667Sjedgar
54192966Strasz	/*
55192966Strasz	 * +1, because we are checking if there is space left for one more
56192966Strasz	 * entry.
57192966Strasz	 */
58192966Strasz	if ((acl_int->acl_cnt + 1 >= ACL_MAX_ENTRIES) ||
59192966Strasz	    (acl_int->acl_cnt < 0)) {
6075928Sjedgar		errno = EINVAL;
6190781Sjedgar		return (-1);
6275928Sjedgar	}
6374667Sjedgar
6475928Sjedgar	*entry_p = &acl_int->acl_entry[acl_int->acl_cnt++];
6575928Sjedgar
6674667Sjedgar	(**entry_p).ae_tag  = ACL_UNDEFINED_TAG;
6774667Sjedgar	(**entry_p).ae_id   = ACL_UNDEFINED_ID;
6874667Sjedgar	(**entry_p).ae_perm = ACL_PERM_NONE;
69192586Strasz	(**entry_p).ae_entry_type = 0;
70192586Strasz	(**entry_p).ae_flags = 0;
7174667Sjedgar
7275928Sjedgar	(*acl_p)->ats_cur_entry = 0;
7375928Sjedgar
7490781Sjedgar	return (0);
7574667Sjedgar}
7674667Sjedgar
77194955Straszint
78194955Straszacl_create_entry_np(acl_t *acl_p, acl_entry_t *entry_p, int offset)
79194955Strasz{
80194955Strasz	int i;
81194955Strasz	struct acl *acl_int;
82194955Strasz
83194955Strasz	if (acl_p == NULL) {
84194955Strasz		errno = EINVAL;
85194955Strasz		return (-1);
86194955Strasz	}
87194955Strasz
88194955Strasz	acl_int = &(*acl_p)->ats_acl;
89194955Strasz
90194955Strasz	if ((acl_int->acl_cnt + 1 >= ACL_MAX_ENTRIES) || (acl_int->acl_cnt < 0)) {
91194955Strasz		errno = EINVAL;
92194955Strasz		return (-1);
93194955Strasz	}
94194955Strasz
95194955Strasz	if (offset < 0 || offset >= acl_int->acl_cnt) {
96194955Strasz		errno = EINVAL;
97194955Strasz		return (-1);
98194955Strasz	}
99194955Strasz
100194955Strasz	/* Make room for the new entry. */
101194955Strasz	for (i = acl_int->acl_cnt; i > offset; i--)
102194955Strasz		acl_int->acl_entry[i] = acl_int->acl_entry[i - 1];
103194955Strasz
104194955Strasz	acl_int->acl_cnt++;
105194955Strasz
106194955Strasz	*entry_p = &acl_int->acl_entry[offset];
107194955Strasz
108194955Strasz	(**entry_p).ae_tag  = ACL_UNDEFINED_TAG;
109194955Strasz	(**entry_p).ae_id   = ACL_UNDEFINED_ID;
110194955Strasz	(**entry_p).ae_perm = ACL_PERM_NONE;
111194955Strasz	(**entry_p).ae_entry_type = 0;
112194955Strasz	(**entry_p).ae_flags= 0;
113194955Strasz
114194955Strasz	(*acl_p)->ats_cur_entry = 0;
115194955Strasz
116194955Strasz	return (0);
117194955Strasz}
118194955Strasz
11975928Sjedgar/*
12075928Sjedgar * acl_get_entry() (23.4.14): returns an ACL entry from an ACL
12175928Sjedgar * indicated by entry_id.
12275928Sjedgar */
12374667Sjedgarint
12474667Sjedgaracl_get_entry(acl_t acl, int entry_id, acl_entry_t *entry_p)
12574667Sjedgar{
12675928Sjedgar	struct acl *acl_int;
12774667Sjedgar
12890781Sjedgar	if (acl == NULL) {
12975928Sjedgar		errno = EINVAL;
13090781Sjedgar		return (-1);
13175928Sjedgar	}
13275928Sjedgar	acl_int = &acl->ats_acl;
13375928Sjedgar
13475928Sjedgar	switch(entry_id) {
13575928Sjedgar	case ACL_FIRST_ENTRY:
13675928Sjedgar		acl->ats_cur_entry = 0;
13775928Sjedgar		/* PASSTHROUGH */
13875928Sjedgar	case ACL_NEXT_ENTRY:
13975928Sjedgar		if (acl->ats_cur_entry >= acl->ats_acl.acl_cnt)
14075928Sjedgar			return 0;
14175928Sjedgar		*entry_p = &acl_int->acl_entry[acl->ats_cur_entry++];
14290781Sjedgar		return (1);
14375928Sjedgar	}
14475928Sjedgar
14575928Sjedgar	errno = EINVAL;
14690781Sjedgar	return (-1);
14774667Sjedgar}
148