xref: /freebsd/lib/libc/posix1e/acl_entry.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1d915a14eSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
4e76872c1SChris D. Faulhaber  * Copyright (c) 2001-2002 Chris D. Faulhaber
54bf60dfaSChris D. Faulhaber  * All rights reserved.
64bf60dfaSChris D. Faulhaber  *
74bf60dfaSChris D. Faulhaber  * Redistribution and use in source and binary forms, with or without
84bf60dfaSChris D. Faulhaber  * modification, are permitted provided that the following conditions
94bf60dfaSChris D. Faulhaber  * are met:
104bf60dfaSChris D. Faulhaber  * 1. Redistributions of source code must retain the above copyright
114bf60dfaSChris D. Faulhaber  *    notice, this list of conditions and the following disclaimer.
124bf60dfaSChris D. Faulhaber  * 2. Redistributions in binary form must reproduce the above copyright
134bf60dfaSChris D. Faulhaber  *    notice, this list of conditions and the following disclaimer in the
144bf60dfaSChris D. Faulhaber  *    documentation and/or other materials provided with the distribution.
154bf60dfaSChris D. Faulhaber  *
164bf60dfaSChris D. Faulhaber  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174bf60dfaSChris D. Faulhaber  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184bf60dfaSChris D. Faulhaber  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194bf60dfaSChris D. Faulhaber  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204bf60dfaSChris D. Faulhaber  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214bf60dfaSChris D. Faulhaber  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224bf60dfaSChris D. Faulhaber  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234bf60dfaSChris D. Faulhaber  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244bf60dfaSChris D. Faulhaber  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254bf60dfaSChris D. Faulhaber  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264bf60dfaSChris D. Faulhaber  * SUCH DAMAGE.
274bf60dfaSChris D. Faulhaber  */
284bf60dfaSChris D. Faulhaber 
294bf60dfaSChris D. Faulhaber #include <sys/types.h>
307bd44e92SThomas Moestl #include "namespace.h"
314bf60dfaSChris D. Faulhaber #include <sys/acl.h>
327bd44e92SThomas Moestl #include "un-namespace.h"
334bf60dfaSChris D. Faulhaber 
344bf60dfaSChris D. Faulhaber #include <errno.h>
354bf60dfaSChris D. Faulhaber #include <stdlib.h>
364bf60dfaSChris D. Faulhaber 
370f626307SChris D. Faulhaber /*
380f626307SChris D. Faulhaber  * acl_create_entry() (23.4.7): create a new ACL entry in the ACL pointed
390f626307SChris D. Faulhaber  * to by acl_p.
400f626307SChris D. Faulhaber  */
414bf60dfaSChris D. Faulhaber int
acl_create_entry(acl_t * acl_p,acl_entry_t * entry_p)424bf60dfaSChris D. Faulhaber acl_create_entry(acl_t *acl_p, acl_entry_t *entry_p)
434bf60dfaSChris D. Faulhaber {
440f626307SChris D. Faulhaber 	struct acl *acl_int;
454bf60dfaSChris D. Faulhaber 
46e76872c1SChris D. Faulhaber 	if (acl_p == NULL) {
474bf60dfaSChris D. Faulhaber 		errno = EINVAL;
48e76872c1SChris D. Faulhaber 		return (-1);
494bf60dfaSChris D. Faulhaber 	}
504bf60dfaSChris D. Faulhaber 
510f626307SChris D. Faulhaber 	acl_int = &(*acl_p)->ats_acl;
524bf60dfaSChris D. Faulhaber 
5360a8b602SEdward Tomasz Napierala 	/*
5460a8b602SEdward Tomasz Napierala 	 * +1, because we are checking if there is space left for one more
5560a8b602SEdward Tomasz Napierala 	 * entry.
5660a8b602SEdward Tomasz Napierala 	 */
5718c12a53SEdward Tomasz Napierala 	if (acl_int->acl_cnt + 1 >= ACL_MAX_ENTRIES) {
580f626307SChris D. Faulhaber 		errno = EINVAL;
59e76872c1SChris D. Faulhaber 		return (-1);
600f626307SChris D. Faulhaber 	}
610f626307SChris D. Faulhaber 
620f626307SChris D. Faulhaber 	*entry_p = &acl_int->acl_entry[acl_int->acl_cnt++];
634bf60dfaSChris D. Faulhaber 
644bf60dfaSChris D. Faulhaber 	(**entry_p).ae_tag  = ACL_UNDEFINED_TAG;
654bf60dfaSChris D. Faulhaber 	(**entry_p).ae_id   = ACL_UNDEFINED_ID;
664bf60dfaSChris D. Faulhaber 	(**entry_p).ae_perm = ACL_PERM_NONE;
67ae1add4eSEdward Tomasz Napierala 	(**entry_p).ae_entry_type = 0;
68ae1add4eSEdward Tomasz Napierala 	(**entry_p).ae_flags = 0;
694bf60dfaSChris D. Faulhaber 
700f626307SChris D. Faulhaber 	(*acl_p)->ats_cur_entry = 0;
710f626307SChris D. Faulhaber 
72e76872c1SChris D. Faulhaber 	return (0);
734bf60dfaSChris D. Faulhaber }
744bf60dfaSChris D. Faulhaber 
75aa015c8eSEdward Tomasz Napierala int
acl_create_entry_np(acl_t * acl_p,acl_entry_t * entry_p,int offset)76aa015c8eSEdward Tomasz Napierala acl_create_entry_np(acl_t *acl_p, acl_entry_t *entry_p, int offset)
77aa015c8eSEdward Tomasz Napierala {
78aa015c8eSEdward Tomasz Napierala 	int i;
79aa015c8eSEdward Tomasz Napierala 	struct acl *acl_int;
80aa015c8eSEdward Tomasz Napierala 
81aa015c8eSEdward Tomasz Napierala 	if (acl_p == NULL) {
82aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
83aa015c8eSEdward Tomasz Napierala 		return (-1);
84aa015c8eSEdward Tomasz Napierala 	}
85aa015c8eSEdward Tomasz Napierala 
86aa015c8eSEdward Tomasz Napierala 	acl_int = &(*acl_p)->ats_acl;
87aa015c8eSEdward Tomasz Napierala 
88d243a85eSEdward Tomasz Napierala 	if (acl_int->acl_cnt + 1 >= ACL_MAX_ENTRIES) {
89aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
90aa015c8eSEdward Tomasz Napierala 		return (-1);
91aa015c8eSEdward Tomasz Napierala 	}
92aa015c8eSEdward Tomasz Napierala 
9345d55945SEdward Tomasz Napierala 	if (offset < 0 || offset > acl_int->acl_cnt) {
94aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
95aa015c8eSEdward Tomasz Napierala 		return (-1);
96aa015c8eSEdward Tomasz Napierala 	}
97aa015c8eSEdward Tomasz Napierala 
98aa015c8eSEdward Tomasz Napierala 	/* Make room for the new entry. */
99aa015c8eSEdward Tomasz Napierala 	for (i = acl_int->acl_cnt; i > offset; i--)
100aa015c8eSEdward Tomasz Napierala 		acl_int->acl_entry[i] = acl_int->acl_entry[i - 1];
101aa015c8eSEdward Tomasz Napierala 
102aa015c8eSEdward Tomasz Napierala 	acl_int->acl_cnt++;
103aa015c8eSEdward Tomasz Napierala 
104aa015c8eSEdward Tomasz Napierala 	*entry_p = &acl_int->acl_entry[offset];
105aa015c8eSEdward Tomasz Napierala 
106aa015c8eSEdward Tomasz Napierala 	(**entry_p).ae_tag  = ACL_UNDEFINED_TAG;
107aa015c8eSEdward Tomasz Napierala 	(**entry_p).ae_id   = ACL_UNDEFINED_ID;
108aa015c8eSEdward Tomasz Napierala 	(**entry_p).ae_perm = ACL_PERM_NONE;
109aa015c8eSEdward Tomasz Napierala 	(**entry_p).ae_entry_type = 0;
110aa015c8eSEdward Tomasz Napierala 	(**entry_p).ae_flags= 0;
111aa015c8eSEdward Tomasz Napierala 
112aa015c8eSEdward Tomasz Napierala 	(*acl_p)->ats_cur_entry = 0;
113aa015c8eSEdward Tomasz Napierala 
114aa015c8eSEdward Tomasz Napierala 	return (0);
115aa015c8eSEdward Tomasz Napierala }
116aa015c8eSEdward Tomasz Napierala 
1170f626307SChris D. Faulhaber /*
1180f626307SChris D. Faulhaber  * acl_get_entry() (23.4.14): returns an ACL entry from an ACL
1190f626307SChris D. Faulhaber  * indicated by entry_id.
1200f626307SChris D. Faulhaber  */
1214bf60dfaSChris D. Faulhaber int
acl_get_entry(acl_t acl,int entry_id,acl_entry_t * entry_p)1224bf60dfaSChris D. Faulhaber acl_get_entry(acl_t acl, int entry_id, acl_entry_t *entry_p)
1234bf60dfaSChris D. Faulhaber {
1240f626307SChris D. Faulhaber 	struct acl *acl_int;
1254bf60dfaSChris D. Faulhaber 
126e76872c1SChris D. Faulhaber 	if (acl == NULL) {
1270f626307SChris D. Faulhaber 		errno = EINVAL;
128e76872c1SChris D. Faulhaber 		return (-1);
1290f626307SChris D. Faulhaber 	}
1300f626307SChris D. Faulhaber 	acl_int = &acl->ats_acl;
1310f626307SChris D. Faulhaber 
1320f626307SChris D. Faulhaber 	switch(entry_id) {
1330f626307SChris D. Faulhaber 	case ACL_FIRST_ENTRY:
1340f626307SChris D. Faulhaber 		acl->ats_cur_entry = 0;
1350f626307SChris D. Faulhaber 		/* PASSTHROUGH */
1360f626307SChris D. Faulhaber 	case ACL_NEXT_ENTRY:
1370f626307SChris D. Faulhaber 		if (acl->ats_cur_entry >= acl->ats_acl.acl_cnt)
1380f626307SChris D. Faulhaber 			return 0;
1390f626307SChris D. Faulhaber 		*entry_p = &acl_int->acl_entry[acl->ats_cur_entry++];
140e76872c1SChris D. Faulhaber 		return (1);
1410f626307SChris D. Faulhaber 	}
1420f626307SChris D. Faulhaber 
1430f626307SChris D. Faulhaber 	errno = EINVAL;
144e76872c1SChris D. Faulhaber 	return (-1);
1454bf60dfaSChris D. Faulhaber }
146