acl_entry.c revision 194955
15116SN/A/*
25116SN/A * Copyright (c) 2001-2002 Chris D. Faulhaber
35116SN/A * All rights reserved.
45116SN/A *
55116SN/A * Redistribution and use in source and binary forms, with or without
65116SN/A * modification, are permitted provided that the following conditions
75116SN/A * are met:
85116SN/A * 1. Redistributions of source code must retain the above copyright
95116SN/A *    notice, this list of conditions and the following disclaimer.
105116SN/A * 2. Redistributions in binary form must reproduce the above copyright
115116SN/A *    notice, this list of conditions and the following disclaimer in the
125116SN/A *    documentation and/or other materials provided with the distribution.
135116SN/A *
145116SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
155116SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
165116SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
175116SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
185116SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
195116SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
205116SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
215116SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
225116SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
235116SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
245116SN/A * SUCH DAMAGE.
255116SN/A */
265116SN/A
275116SN/A#include <sys/cdefs.h>
285116SN/A__FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_entry.c 194955 2009-06-25 12:46:59Z trasz $");
295116SN/A
305116SN/A#include <sys/types.h>
315116SN/A#include "namespace.h"
325116SN/A#include <sys/acl.h>
335116SN/A#include "un-namespace.h"
345116SN/A
355116SN/A#include <errno.h>
365116SN/A#include <stdlib.h>
375116SN/A
385116SN/A/*
395116SN/A * acl_create_entry() (23.4.7): create a new ACL entry in the ACL pointed
405116SN/A * to by acl_p.
415116SN/A */
425116SN/Aint
435116SN/Aacl_create_entry(acl_t *acl_p, acl_entry_t *entry_p)
445116SN/A{
455116SN/A	struct acl *acl_int;
465116SN/A
475116SN/A	if (acl_p == NULL) {
485116SN/A		errno = EINVAL;
495116SN/A		return (-1);
505116SN/A	}
515116SN/A
525116SN/A	acl_int = &(*acl_p)->ats_acl;
535116SN/A
545116SN/A	/*
555116SN/A	 * +1, because we are checking if there is space left for one more
565116SN/A	 * entry.
575116SN/A	 */
585116SN/A	if ((acl_int->acl_cnt + 1 >= ACL_MAX_ENTRIES) ||
595116SN/A	    (acl_int->acl_cnt < 0)) {
605116SN/A		errno = EINVAL;
615116SN/A		return (-1);
625116SN/A	}
635116SN/A
645116SN/A	*entry_p = &acl_int->acl_entry[acl_int->acl_cnt++];
655116SN/A
665116SN/A	(**entry_p).ae_tag  = ACL_UNDEFINED_TAG;
675116SN/A	(**entry_p).ae_id   = ACL_UNDEFINED_ID;
685116SN/A	(**entry_p).ae_perm = ACL_PERM_NONE;
695116SN/A	(**entry_p).ae_entry_type = 0;
705116SN/A	(**entry_p).ae_flags = 0;
715116SN/A
725116SN/A	(*acl_p)->ats_cur_entry = 0;
735116SN/A
745116SN/A	return (0);
755116SN/A}
765116SN/A
775116SN/Aint
785116SN/Aacl_create_entry_np(acl_t *acl_p, acl_entry_t *entry_p, int offset)
795116SN/A{
805116SN/A	int i;
815116SN/A	struct acl *acl_int;
825116SN/A
835116SN/A	if (acl_p == NULL) {
845116SN/A		errno = EINVAL;
855116SN/A		return (-1);
865116SN/A	}
875116SN/A
885116SN/A	acl_int = &(*acl_p)->ats_acl;
895116SN/A
905116SN/A	if ((acl_int->acl_cnt + 1 >= ACL_MAX_ENTRIES) || (acl_int->acl_cnt < 0)) {
915116SN/A		errno = EINVAL;
925116SN/A		return (-1);
935116SN/A	}
945116SN/A
955116SN/A	if (offset < 0 || offset >= acl_int->acl_cnt) {
965116SN/A		errno = EINVAL;
975116SN/A		return (-1);
985116SN/A	}
995116SN/A
1005116SN/A	/* Make room for the new entry. */
1015116SN/A	for (i = acl_int->acl_cnt; i > offset; i--)
1025116SN/A		acl_int->acl_entry[i] = acl_int->acl_entry[i - 1];
1035116SN/A
1045116SN/A	acl_int->acl_cnt++;
1055116SN/A
1065116SN/A	*entry_p = &acl_int->acl_entry[offset];
1075116SN/A
1085116SN/A	(**entry_p).ae_tag  = ACL_UNDEFINED_TAG;
1095116SN/A	(**entry_p).ae_id   = ACL_UNDEFINED_ID;
1105116SN/A	(**entry_p).ae_perm = ACL_PERM_NONE;
1115116SN/A	(**entry_p).ae_entry_type = 0;
1125116SN/A	(**entry_p).ae_flags= 0;
1135116SN/A
1145116SN/A	(*acl_p)->ats_cur_entry = 0;
1155116SN/A
1165116SN/A	return (0);
1175116SN/A}
1185116SN/A
1195116SN/A/*
1205116SN/A * acl_get_entry() (23.4.14): returns an ACL entry from an ACL
1215116SN/A * indicated by entry_id.
1225116SN/A */
1235116SN/Aint
1245116SN/Aacl_get_entry(acl_t acl, int entry_id, acl_entry_t *entry_p)
1255116SN/A{
1265116SN/A	struct acl *acl_int;
1275116SN/A
1285116SN/A	if (acl == NULL) {
1295116SN/A		errno = EINVAL;
1305116SN/A		return (-1);
1315116SN/A	}
1325116SN/A	acl_int = &acl->ats_acl;
1335116SN/A
1345116SN/A	switch(entry_id) {
1355116SN/A	case ACL_FIRST_ENTRY:
1365116SN/A		acl->ats_cur_entry = 0;
1375116SN/A		/* PASSTHROUGH */
1385116SN/A	case ACL_NEXT_ENTRY:
1395116SN/A		if (acl->ats_cur_entry >= acl->ats_acl.acl_cnt)
1405116SN/A			return 0;
1415116SN/A		*entry_p = &acl_int->acl_entry[acl->ats_cur_entry++];
1425116SN/A		return (1);
1435116SN/A	}
1445116SN/A
1455116SN/A	errno = EINVAL;
1465116SN/A	return (-1);
1475116SN/A}
1485116SN/A