2005-01-10 08:39:26 +00:00
|
|
|
/*-
|
2001-03-19 18:09:25 +00:00
|
|
|
* Copyright (c) 2001 Chris D. Faulhaber
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR THE VOICES IN HIS HEAD BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-03-19 18:09:25 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/acl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "setfacl.h"
|
|
|
|
|
2001-12-03 00:51:36 +00:00
|
|
|
/*
|
|
|
|
* remove ACL entries from an ACL
|
|
|
|
*/
|
2001-03-19 18:09:25 +00:00
|
|
|
int
|
|
|
|
remove_acl(acl_t acl, acl_t *prev_acl)
|
|
|
|
{
|
2001-04-24 22:45:41 +00:00
|
|
|
acl_entry_t entry;
|
|
|
|
acl_t acl_new;
|
|
|
|
acl_tag_t tag;
|
|
|
|
int carried_error, entry_id;
|
2001-03-19 18:09:25 +00:00
|
|
|
|
|
|
|
carried_error = 0;
|
|
|
|
|
|
|
|
if (acl_type == ACL_TYPE_ACCESS)
|
2001-12-03 01:20:52 +00:00
|
|
|
acl_new = acl_dup(prev_acl[ACCESS_ACL]);
|
2001-03-19 18:09:25 +00:00
|
|
|
else
|
2001-12-03 01:20:52 +00:00
|
|
|
acl_new = acl_dup(prev_acl[DEFAULT_ACL]);
|
2001-12-03 00:51:36 +00:00
|
|
|
if (acl_new == NULL)
|
|
|
|
err(1, "acl_dup() failed");
|
2001-03-19 18:09:25 +00:00
|
|
|
|
2001-04-24 22:45:41 +00:00
|
|
|
tag = ACL_UNDEFINED_TAG;
|
|
|
|
|
2001-03-19 18:09:25 +00:00
|
|
|
/* find and delete the entry */
|
2001-04-24 22:45:41 +00:00
|
|
|
entry_id = ACL_FIRST_ENTRY;
|
|
|
|
while (acl_get_entry(acl, entry_id, &entry) == 1) {
|
|
|
|
entry_id = ACL_NEXT_ENTRY;
|
|
|
|
if (acl_get_tag_type(entry, &tag) == -1)
|
|
|
|
err(1, "acl_get_tag_type() failed");
|
|
|
|
if (tag == ACL_MASK)
|
2001-03-19 18:09:25 +00:00
|
|
|
have_mask++;
|
2001-04-24 22:45:41 +00:00
|
|
|
if (acl_delete_entry(acl_new, entry) == -1) {
|
2001-03-19 18:09:25 +00:00
|
|
|
carried_error++;
|
|
|
|
warnx("cannot remove non-existent acl entry");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acl_type == ACL_TYPE_ACCESS) {
|
2001-12-03 01:20:52 +00:00
|
|
|
acl_free(prev_acl[ACCESS_ACL]);
|
|
|
|
prev_acl[ACCESS_ACL] = acl_new;
|
2001-03-19 18:09:25 +00:00
|
|
|
} else {
|
2001-12-03 01:20:52 +00:00
|
|
|
acl_free(prev_acl[DEFAULT_ACL]);
|
|
|
|
prev_acl[DEFAULT_ACL] = acl_new;
|
2001-03-19 18:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (carried_error)
|
2001-12-03 00:51:36 +00:00
|
|
|
return (-1);
|
2001-03-19 18:09:25 +00:00
|
|
|
|
2001-12-03 00:51:36 +00:00
|
|
|
return (0);
|
2001-03-19 18:09:25 +00:00
|
|
|
}
|
|
|
|
|
2001-12-03 00:51:36 +00:00
|
|
|
/*
|
|
|
|
* remove default entries
|
|
|
|
*/
|
2001-03-19 18:09:25 +00:00
|
|
|
int
|
|
|
|
remove_default(acl_t *prev_acl)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (prev_acl[1]) {
|
2001-04-24 22:45:41 +00:00
|
|
|
acl_free(prev_acl[1]);
|
|
|
|
prev_acl[1] = acl_init(ACL_MAX_ENTRIES);
|
2001-12-03 00:51:36 +00:00
|
|
|
if (prev_acl[1] == NULL)
|
2001-04-24 22:45:41 +00:00
|
|
|
err(1, "acl_init() failed");
|
2001-03-19 18:09:25 +00:00
|
|
|
} else {
|
|
|
|
warn("cannot remove default ACL");
|
2001-12-03 00:51:36 +00:00
|
|
|
return (-1);
|
2001-03-19 18:09:25 +00:00
|
|
|
}
|
2001-12-03 00:51:36 +00:00
|
|
|
return (0);
|
2001-03-19 18:09:25 +00:00
|
|
|
}
|
|
|
|
|
2001-12-03 00:51:36 +00:00
|
|
|
/*
|
|
|
|
* remove extended entries
|
|
|
|
*/
|
2001-03-19 18:09:25 +00:00
|
|
|
void
|
|
|
|
remove_ext(acl_t *prev_acl)
|
|
|
|
{
|
|
|
|
acl_t acl_new, acl_old;
|
2001-04-24 22:45:41 +00:00
|
|
|
acl_entry_t entry, entry_new;
|
|
|
|
acl_permset_t perm;
|
|
|
|
acl_tag_t tag;
|
|
|
|
int entry_id, have_mask_entry;
|
2001-03-19 18:09:25 +00:00
|
|
|
|
|
|
|
if (acl_type == ACL_TYPE_ACCESS)
|
2001-12-03 01:20:52 +00:00
|
|
|
acl_old = acl_dup(prev_acl[ACCESS_ACL]);
|
2001-03-19 18:09:25 +00:00
|
|
|
else
|
2001-12-03 01:20:52 +00:00
|
|
|
acl_old = acl_dup(prev_acl[DEFAULT_ACL]);
|
2001-12-03 00:51:36 +00:00
|
|
|
if (acl_old == NULL)
|
|
|
|
err(1, "acl_dup() failed");
|
2001-03-19 18:09:25 +00:00
|
|
|
|
|
|
|
have_mask_entry = 0;
|
|
|
|
acl_new = acl_init(ACL_MAX_ENTRIES);
|
2001-12-03 00:51:36 +00:00
|
|
|
if (acl_new == NULL)
|
|
|
|
err(1, "acl_init() failed");
|
2001-04-24 22:45:41 +00:00
|
|
|
tag = ACL_UNDEFINED_TAG;
|
2001-03-19 18:09:25 +00:00
|
|
|
|
|
|
|
/* only save the default user/group/other entries */
|
2001-04-24 22:45:41 +00:00
|
|
|
entry_id = ACL_FIRST_ENTRY;
|
|
|
|
while (acl_get_entry(acl_old, entry_id, &entry) == 1) {
|
|
|
|
entry_id = ACL_NEXT_ENTRY;
|
|
|
|
|
|
|
|
if (acl_get_tag_type(entry, &tag) == -1)
|
|
|
|
err(1, "acl_get_tag_type() failed");
|
|
|
|
|
|
|
|
switch(tag) {
|
2001-03-19 18:09:25 +00:00
|
|
|
case ACL_USER_OBJ:
|
|
|
|
case ACL_GROUP_OBJ:
|
2001-04-24 22:45:41 +00:00
|
|
|
case ACL_OTHER:
|
|
|
|
if (acl_get_tag_type(entry, &tag) == -1)
|
|
|
|
err(1, "acl_get_tag_type() failed");
|
|
|
|
if (acl_get_permset(entry, &perm) == -1)
|
|
|
|
err(1, "acl_get_permset() failed");
|
|
|
|
if (acl_create_entry(&acl_new, &entry_new) == -1)
|
|
|
|
err(1, "acl_create_entry() failed");
|
|
|
|
if (acl_set_tag_type(entry_new, tag) == -1)
|
|
|
|
err(1, "acl_set_tag_type() failed");
|
|
|
|
if (acl_set_permset(entry_new, perm) == -1)
|
|
|
|
err(1, "acl_get_permset() failed");
|
|
|
|
if (acl_copy_entry(entry_new, entry) == -1)
|
|
|
|
err(1, "acl_copy_entry() failed");
|
2001-03-19 18:09:25 +00:00
|
|
|
break;
|
|
|
|
case ACL_MASK:
|
|
|
|
have_mask_entry = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2001-04-24 22:45:41 +00:00
|
|
|
}
|
2001-12-03 00:51:36 +00:00
|
|
|
if (have_mask_entry && n_flag == 0) {
|
2001-04-24 22:45:41 +00:00
|
|
|
if (acl_calc_mask(&acl_new) == -1)
|
|
|
|
err(1, "acl_calc_mask() failed");
|
|
|
|
} else {
|
|
|
|
have_mask = 1;
|
|
|
|
}
|
2001-03-19 18:09:25 +00:00
|
|
|
|
|
|
|
if (acl_type == ACL_TYPE_ACCESS) {
|
2001-12-03 01:20:52 +00:00
|
|
|
acl_free(prev_acl[ACCESS_ACL]);
|
|
|
|
prev_acl[ACCESS_ACL] = acl_new;
|
2001-03-19 18:09:25 +00:00
|
|
|
} else {
|
2001-12-03 01:20:52 +00:00
|
|
|
acl_free(prev_acl[DEFAULT_ACL]);
|
|
|
|
prev_acl[DEFAULT_ACL] = acl_new;
|
2001-03-19 18:09:25 +00:00
|
|
|
}
|
|
|
|
}
|