xref: /freebsd/lib/libc/posix1e/acl_init.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1515d7c92SRobert Watson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
42de14c39SRobert Watson  * Copyright (c) 1999, 2000, 2001 Robert N. M. Watson
5515d7c92SRobert Watson  * All rights reserved.
6515d7c92SRobert Watson  *
7515d7c92SRobert Watson  * Redistribution and use in source and binary forms, with or without
8515d7c92SRobert Watson  * modification, are permitted provided that the following conditions
9515d7c92SRobert Watson  * are met:
10515d7c92SRobert Watson  * 1. Redistributions of source code must retain the above copyright
11515d7c92SRobert Watson  *    notice, this list of conditions and the following disclaimer.
12515d7c92SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
13515d7c92SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
14515d7c92SRobert Watson  *    documentation and/or other materials provided with the distribution.
15515d7c92SRobert Watson  *
16515d7c92SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17515d7c92SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18515d7c92SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19515d7c92SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20515d7c92SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21515d7c92SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22515d7c92SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23515d7c92SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24515d7c92SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25515d7c92SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26515d7c92SRobert Watson  * SUCH DAMAGE.
27515d7c92SRobert Watson  */
28515d7c92SRobert Watson /*
29515d7c92SRobert Watson  * acl_init -- return a fresh acl structure
308f45e8c0SRobert Watson  * acl_dup -- duplicate an acl and return the new copy
31515d7c92SRobert Watson  */
32515d7c92SRobert Watson 
33515d7c92SRobert Watson #include <sys/types.h>
347bd44e92SThomas Moestl #include "namespace.h"
35515d7c92SRobert Watson #include <sys/acl.h>
367bd44e92SThomas Moestl #include "un-namespace.h"
37515d7c92SRobert Watson #include <errno.h>
38515d7c92SRobert Watson #include <stdlib.h>
39515d7c92SRobert Watson #include <string.h>
40aa015c8eSEdward Tomasz Napierala #include <assert.h>
41aa015c8eSEdward Tomasz Napierala 
42aa015c8eSEdward Tomasz Napierala #include "acl_support.h"
43aa015c8eSEdward Tomasz Napierala 
44aa015c8eSEdward Tomasz Napierala #ifndef CTASSERT
45aa015c8eSEdward Tomasz Napierala #define CTASSERT(x)		_CTASSERT(x, __LINE__)
46aa015c8eSEdward Tomasz Napierala #define _CTASSERT(x, y)		__CTASSERT(x, y)
47aa015c8eSEdward Tomasz Napierala #define __CTASSERT(x, y)	typedef char __assert_ ## y [(x) ? 1 : -1]
48aa015c8eSEdward Tomasz Napierala #endif
49aa015c8eSEdward Tomasz Napierala 
50aa015c8eSEdward Tomasz Napierala CTASSERT(1 << _ACL_T_ALIGNMENT_BITS > sizeof(struct acl_t_struct));
51515d7c92SRobert Watson 
52515d7c92SRobert Watson acl_t
acl_init(int count)53515d7c92SRobert Watson acl_init(int count)
54515d7c92SRobert Watson {
55aa015c8eSEdward Tomasz Napierala 	int error;
560f626307SChris D. Faulhaber 	acl_t acl;
57515d7c92SRobert Watson 
58d3352316SRobert Watson 	if (count > ACL_MAX_ENTRIES) {
59515d7c92SRobert Watson 		errno = ENOMEM;
60f0078215SRobert Watson 		return (NULL);
61515d7c92SRobert Watson 	}
620f626307SChris D. Faulhaber 	if (count < 0) {
630f626307SChris D. Faulhaber 		errno = EINVAL;
640f626307SChris D. Faulhaber 		return (NULL);
650f626307SChris D. Faulhaber 	}
66515d7c92SRobert Watson 
67aa015c8eSEdward Tomasz Napierala 	error = posix_memalign((void *)&acl, 1 << _ACL_T_ALIGNMENT_BITS,
68aa015c8eSEdward Tomasz Napierala 	    sizeof(struct acl_t_struct));
69b0f957f9SEdward Tomasz Napierala 	if (error) {
70b0f957f9SEdward Tomasz Napierala 		errno = error;
71aa015c8eSEdward Tomasz Napierala 		return (NULL);
72b0f957f9SEdward Tomasz Napierala 	}
73aa015c8eSEdward Tomasz Napierala 
740f626307SChris D. Faulhaber 	bzero(acl, sizeof(struct acl_t_struct));
75aa015c8eSEdward Tomasz Napierala 	acl->ats_brand = ACL_BRAND_UNKNOWN;
76ae1add4eSEdward Tomasz Napierala 	acl->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
77515d7c92SRobert Watson 
78515d7c92SRobert Watson 	return (acl);
79515d7c92SRobert Watson }
80515d7c92SRobert Watson 
818f45e8c0SRobert Watson acl_t
acl_dup(acl_t acl)828f45e8c0SRobert Watson acl_dup(acl_t acl)
838f45e8c0SRobert Watson {
840f626307SChris D. Faulhaber 	acl_t	acl_new;
858f45e8c0SRobert Watson 
868f45e8c0SRobert Watson 	acl_new = acl_init(ACL_MAX_ENTRIES);
879fd46b02SChris D. Faulhaber 	if (acl_new != NULL) {
888f45e8c0SRobert Watson 		*acl_new = *acl;
890f626307SChris D. Faulhaber 		acl->ats_cur_entry = 0;
900f626307SChris D. Faulhaber 		acl_new->ats_cur_entry = 0;
919fd46b02SChris D. Faulhaber 	}
928f45e8c0SRobert Watson 
938f45e8c0SRobert Watson 	return (acl_new);
948f45e8c0SRobert Watson }
95