xref: /freebsd/lib/libc/posix1e/acl_init.c (revision aa015c8e4ad07fbe76ec137fa491c89856b871e0)
1515d7c92SRobert Watson /*-
22de14c39SRobert Watson  * Copyright (c) 1999, 2000, 2001 Robert N. M. Watson
3515d7c92SRobert Watson  * All rights reserved.
4515d7c92SRobert Watson  *
5515d7c92SRobert Watson  * Redistribution and use in source and binary forms, with or without
6515d7c92SRobert Watson  * modification, are permitted provided that the following conditions
7515d7c92SRobert Watson  * are met:
8515d7c92SRobert Watson  * 1. Redistributions of source code must retain the above copyright
9515d7c92SRobert Watson  *    notice, this list of conditions and the following disclaimer.
10515d7c92SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
11515d7c92SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
12515d7c92SRobert Watson  *    documentation and/or other materials provided with the distribution.
13515d7c92SRobert Watson  *
14515d7c92SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15515d7c92SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16515d7c92SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17515d7c92SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18515d7c92SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19515d7c92SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20515d7c92SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21515d7c92SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22515d7c92SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23515d7c92SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24515d7c92SRobert Watson  * SUCH DAMAGE.
25515d7c92SRobert Watson  */
26515d7c92SRobert Watson /*
27515d7c92SRobert Watson  * acl_init -- return a fresh acl structure
288f45e8c0SRobert Watson  * acl_dup -- duplicate an acl and return the new copy
29515d7c92SRobert Watson  */
30515d7c92SRobert Watson 
31333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
32333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
33333fc21eSDavid E. O'Brien 
34515d7c92SRobert Watson #include <sys/types.h>
357bd44e92SThomas Moestl #include "namespace.h"
36515d7c92SRobert Watson #include <sys/acl.h>
377bd44e92SThomas Moestl #include "un-namespace.h"
38515d7c92SRobert Watson #include <errno.h>
39515d7c92SRobert Watson #include <stdlib.h>
40515d7c92SRobert Watson #include <string.h>
41aa015c8eSEdward Tomasz Napierala #include <assert.h>
42aa015c8eSEdward Tomasz Napierala 
43aa015c8eSEdward Tomasz Napierala #include "acl_support.h"
44aa015c8eSEdward Tomasz Napierala 
45aa015c8eSEdward Tomasz Napierala #ifndef CTASSERT
46aa015c8eSEdward Tomasz Napierala #define CTASSERT(x)		_CTASSERT(x, __LINE__)
47aa015c8eSEdward Tomasz Napierala #define _CTASSERT(x, y)		__CTASSERT(x, y)
48aa015c8eSEdward Tomasz Napierala #define __CTASSERT(x, y)	typedef char __assert_ ## y [(x) ? 1 : -1]
49aa015c8eSEdward Tomasz Napierala #endif
50aa015c8eSEdward Tomasz Napierala 
51aa015c8eSEdward Tomasz Napierala CTASSERT(1 << _ACL_T_ALIGNMENT_BITS > sizeof(struct acl_t_struct));
52515d7c92SRobert Watson 
53515d7c92SRobert Watson acl_t
54515d7c92SRobert Watson acl_init(int count)
55515d7c92SRobert Watson {
56aa015c8eSEdward Tomasz Napierala 	int error;
570f626307SChris D. Faulhaber 	acl_t acl;
58515d7c92SRobert Watson 
59d3352316SRobert Watson 	if (count > ACL_MAX_ENTRIES) {
60515d7c92SRobert Watson 		errno = ENOMEM;
61f0078215SRobert Watson 		return (NULL);
62515d7c92SRobert Watson 	}
630f626307SChris D. Faulhaber 	if (count < 0) {
640f626307SChris D. Faulhaber 		errno = EINVAL;
650f626307SChris D. Faulhaber 		return (NULL);
660f626307SChris D. Faulhaber 	}
67515d7c92SRobert Watson 
68aa015c8eSEdward Tomasz Napierala 	error = posix_memalign((void *)&acl, 1 << _ACL_T_ALIGNMENT_BITS,
69aa015c8eSEdward Tomasz Napierala 	    sizeof(struct acl_t_struct));
70aa015c8eSEdward Tomasz Napierala 	if (error)
71aa015c8eSEdward Tomasz Napierala 		return (NULL);
72aa015c8eSEdward Tomasz Napierala 
730f626307SChris D. Faulhaber 	bzero(acl, sizeof(struct acl_t_struct));
74aa015c8eSEdward Tomasz Napierala 	acl->ats_brand = ACL_BRAND_UNKNOWN;
75ae1add4eSEdward Tomasz Napierala 	acl->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
76515d7c92SRobert Watson 
77515d7c92SRobert Watson 	return (acl);
78515d7c92SRobert Watson }
79515d7c92SRobert Watson 
808f45e8c0SRobert Watson acl_t
818f45e8c0SRobert Watson acl_dup(acl_t acl)
828f45e8c0SRobert Watson {
830f626307SChris D. Faulhaber 	acl_t	acl_new;
848f45e8c0SRobert Watson 
858f45e8c0SRobert Watson 	acl_new = acl_init(ACL_MAX_ENTRIES);
869fd46b02SChris D. Faulhaber 	if (acl_new != NULL) {
878f45e8c0SRobert Watson 		*acl_new = *acl;
880f626307SChris D. Faulhaber 		acl->ats_cur_entry = 0;
890f626307SChris D. Faulhaber 		acl_new->ats_cur_entry = 0;
909fd46b02SChris D. Faulhaber 	}
918f45e8c0SRobert Watson 
928f45e8c0SRobert Watson 	return (acl_new);
938f45e8c0SRobert Watson }
94