xref: /freebsd/lib/libc/posix1e/acl_entry.c (revision 60a8b6022dc27a2d41ff9bcb742a90742825c833)
14bf60dfaSChris D. Faulhaber /*
2e76872c1SChris D. Faulhaber  * Copyright (c) 2001-2002 Chris D. Faulhaber
34bf60dfaSChris D. Faulhaber  * All rights reserved.
44bf60dfaSChris D. Faulhaber  *
54bf60dfaSChris D. Faulhaber  * Redistribution and use in source and binary forms, with or without
64bf60dfaSChris D. Faulhaber  * modification, are permitted provided that the following conditions
74bf60dfaSChris D. Faulhaber  * are met:
84bf60dfaSChris D. Faulhaber  * 1. Redistributions of source code must retain the above copyright
94bf60dfaSChris D. Faulhaber  *    notice, this list of conditions and the following disclaimer.
104bf60dfaSChris D. Faulhaber  * 2. Redistributions in binary form must reproduce the above copyright
114bf60dfaSChris D. Faulhaber  *    notice, this list of conditions and the following disclaimer in the
124bf60dfaSChris D. Faulhaber  *    documentation and/or other materials provided with the distribution.
134bf60dfaSChris D. Faulhaber  *
144bf60dfaSChris D. Faulhaber  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
154bf60dfaSChris D. Faulhaber  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164bf60dfaSChris D. Faulhaber  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
174bf60dfaSChris D. Faulhaber  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
184bf60dfaSChris D. Faulhaber  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
194bf60dfaSChris D. Faulhaber  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
204bf60dfaSChris D. Faulhaber  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
214bf60dfaSChris D. Faulhaber  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
224bf60dfaSChris D. Faulhaber  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
234bf60dfaSChris D. Faulhaber  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
244bf60dfaSChris D. Faulhaber  * SUCH DAMAGE.
254bf60dfaSChris D. Faulhaber  */
264bf60dfaSChris D. Faulhaber 
27333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
28333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
29333fc21eSDavid E. O'Brien 
304bf60dfaSChris D. Faulhaber #include <sys/types.h>
317bd44e92SThomas Moestl #include "namespace.h"
324bf60dfaSChris D. Faulhaber #include <sys/acl.h>
337bd44e92SThomas Moestl #include "un-namespace.h"
344bf60dfaSChris D. Faulhaber 
354bf60dfaSChris D. Faulhaber #include <errno.h>
364bf60dfaSChris D. Faulhaber #include <stdlib.h>
374bf60dfaSChris D. Faulhaber 
380f626307SChris D. Faulhaber /*
390f626307SChris D. Faulhaber  * acl_create_entry() (23.4.7): create a new ACL entry in the ACL pointed
400f626307SChris D. Faulhaber  * to by acl_p.
410f626307SChris D. Faulhaber  */
424bf60dfaSChris D. Faulhaber int
434bf60dfaSChris D. Faulhaber acl_create_entry(acl_t *acl_p, acl_entry_t *entry_p)
444bf60dfaSChris D. Faulhaber {
450f626307SChris D. Faulhaber 	struct acl *acl_int;
464bf60dfaSChris D. Faulhaber 
47e76872c1SChris D. Faulhaber 	if (acl_p == NULL) {
484bf60dfaSChris D. Faulhaber 		errno = EINVAL;
49e76872c1SChris D. Faulhaber 		return (-1);
504bf60dfaSChris D. Faulhaber 	}
514bf60dfaSChris D. Faulhaber 
520f626307SChris D. Faulhaber 	acl_int = &(*acl_p)->ats_acl;
534bf60dfaSChris D. Faulhaber 
5460a8b602SEdward Tomasz Napierala 	/*
5560a8b602SEdward Tomasz Napierala 	 * +1, because we are checking if there is space left for one more
5660a8b602SEdward Tomasz Napierala 	 * entry.
5760a8b602SEdward Tomasz Napierala 	 */
5860a8b602SEdward Tomasz Napierala 	if ((acl_int->acl_cnt + 1 >= ACL_MAX_ENTRIES) ||
5960a8b602SEdward Tomasz Napierala 	    (acl_int->acl_cnt < 0)) {
600f626307SChris D. Faulhaber 		errno = EINVAL;
61e76872c1SChris D. Faulhaber 		return (-1);
620f626307SChris D. Faulhaber 	}
630f626307SChris D. Faulhaber 
640f626307SChris D. Faulhaber 	*entry_p = &acl_int->acl_entry[acl_int->acl_cnt++];
654bf60dfaSChris D. Faulhaber 
664bf60dfaSChris D. Faulhaber 	(**entry_p).ae_tag  = ACL_UNDEFINED_TAG;
674bf60dfaSChris D. Faulhaber 	(**entry_p).ae_id   = ACL_UNDEFINED_ID;
684bf60dfaSChris D. Faulhaber 	(**entry_p).ae_perm = ACL_PERM_NONE;
69ae1add4eSEdward Tomasz Napierala 	(**entry_p).ae_entry_type = 0;
70ae1add4eSEdward Tomasz Napierala 	(**entry_p).ae_flags = 0;
714bf60dfaSChris D. Faulhaber 
720f626307SChris D. Faulhaber 	(*acl_p)->ats_cur_entry = 0;
730f626307SChris D. Faulhaber 
74e76872c1SChris D. Faulhaber 	return (0);
754bf60dfaSChris D. Faulhaber }
764bf60dfaSChris D. Faulhaber 
770f626307SChris D. Faulhaber /*
780f626307SChris D. Faulhaber  * acl_get_entry() (23.4.14): returns an ACL entry from an ACL
790f626307SChris D. Faulhaber  * indicated by entry_id.
800f626307SChris D. Faulhaber  */
814bf60dfaSChris D. Faulhaber int
824bf60dfaSChris D. Faulhaber acl_get_entry(acl_t acl, int entry_id, acl_entry_t *entry_p)
834bf60dfaSChris D. Faulhaber {
840f626307SChris D. Faulhaber 	struct acl *acl_int;
854bf60dfaSChris D. Faulhaber 
86e76872c1SChris D. Faulhaber 	if (acl == NULL) {
870f626307SChris D. Faulhaber 		errno = EINVAL;
88e76872c1SChris D. Faulhaber 		return (-1);
890f626307SChris D. Faulhaber 	}
900f626307SChris D. Faulhaber 	acl_int = &acl->ats_acl;
910f626307SChris D. Faulhaber 
920f626307SChris D. Faulhaber 	switch(entry_id) {
930f626307SChris D. Faulhaber 	case ACL_FIRST_ENTRY:
940f626307SChris D. Faulhaber 		acl->ats_cur_entry = 0;
950f626307SChris D. Faulhaber 		/* PASSTHROUGH */
960f626307SChris D. Faulhaber 	case ACL_NEXT_ENTRY:
970f626307SChris D. Faulhaber 		if (acl->ats_cur_entry >= acl->ats_acl.acl_cnt)
980f626307SChris D. Faulhaber 			return 0;
990f626307SChris D. Faulhaber 		*entry_p = &acl_int->acl_entry[acl->ats_cur_entry++];
100e76872c1SChris D. Faulhaber 		return (1);
1010f626307SChris D. Faulhaber 	}
1020f626307SChris D. Faulhaber 
1030f626307SChris D. Faulhaber 	errno = EINVAL;
104e76872c1SChris D. Faulhaber 	return (-1);
1054bf60dfaSChris D. Faulhaber }
106