/*-1* Copyright (c) 2001-2002 Chris D. Faulhaber2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/types.h>27#include <sys/acl.h>28#include <sys/stat.h>2930#include <err.h>3132#include "setfacl.h"3334/* set the appropriate mask the given ACL's */35int36set_acl_mask(acl_t *prev_acl, const char *filename)37{38acl_entry_t entry;39acl_t acl;40acl_tag_t tag;41int entry_id;4243entry = NULL;4445/*46* ... if a mask entry is specified, then the permissions of the mask47* entry in the resulting ACL shall be set to the permissions in the48* specified ACL mask entry.49*/50if (have_mask)51return (0);5253acl = acl_dup(*prev_acl);54if (acl == NULL)55err(1, "%s: acl_dup() failed", filename);5657if (!n_flag) {58/*59* If no mask entry is specified and the -n option is not60* specified, then the permissions of the resulting ACL mask61* entry shall be set to the union of the permissions62* associated with all entries which belong to the file group63* class in the resulting ACL64*/65if (acl_calc_mask(&acl)) {66warn("%s: acl_calc_mask() failed", filename);67acl_free(acl);68return (-1);69}70} else {71/*72* If no mask entry is specified and the -n option is73* specified, then the permissions of the resulting ACL74* mask entry shall remain unchanged ...75*/7677entry_id = ACL_FIRST_ENTRY;7879while (acl_get_entry(acl, entry_id, &entry) == 1) {80entry_id = ACL_NEXT_ENTRY;81if (acl_get_tag_type(entry, &tag) == -1)82err(1, "%s: acl_get_tag_type() failed",83filename);8485if (tag == ACL_MASK) {86acl_free(acl);87return (0);88}89}9091/*92* If no mask entry is specified, the -n option is specified,93* and no ACL mask entry exists in the ACL associated with the94* file, then write an error message to standard error and95* continue with the next file.96*/97warnx("%s: warning: no mask entry", filename);98acl_free(acl);99return (0);100}101102acl_free(*prev_acl);103*prev_acl = acl_dup(acl);104acl_free(acl);105106return (0);107}108109110