mask.c revision 90127
174465Srwatson/*
290127Sjedgar * Copyright (c) 2001-2002 Chris D. Faulhaber
374465Srwatson * All rights reserved.
474465Srwatson *
574465Srwatson * Redistribution and use in source and binary forms, with or without
674465Srwatson * modification, are permitted provided that the following conditions
774465Srwatson * are met:
874465Srwatson * 1. Redistributions of source code must retain the above copyright
974465Srwatson *    notice, this list of conditions and the following disclaimer.
1074465Srwatson * 2. Redistributions in binary form must reproduce the above copyright
1174465Srwatson *    notice, this list of conditions and the following disclaimer in the
1274465Srwatson *    documentation and/or other materials provided with the distribution.
1374465Srwatson *
1474465Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1574465Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1674465Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1774465Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE VOICES IN HIS HEAD BE
1874465Srwatson * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1974465Srwatson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2074465Srwatson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2174465Srwatson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2274465Srwatson * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2374465Srwatson * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2474465Srwatson * POSSIBILITY OF SUCH DAMAGE.
2574465Srwatson *
2674465Srwatson * $FreeBSD: head/bin/setfacl/mask.c 90127 2002-02-03 02:37:43Z jedgar $
2774465Srwatson */
2874465Srwatson
2974465Srwatson#include <sys/types.h>
3074465Srwatson#include <sys/acl.h>
3174465Srwatson#include <sys/stat.h>
3274465Srwatson
3374465Srwatson#include <err.h>
3474465Srwatson#include <errno.h>
3574465Srwatson#include <stdio.h>
3674465Srwatson#include <stdlib.h>
3774465Srwatson
3874465Srwatson#include "setfacl.h"
3974465Srwatson
4074465Srwatson/* set the appropriate mask the given ACL's */
4174465Srwatsonint
4275928Sjedgarset_acl_mask(acl_t *prev_acl)
4374465Srwatson{
4490127Sjedgar	acl_entry_t entry, entry_new;
4574465Srwatson	acl_t acl;
4675928Sjedgar	acl_tag_t tag;
4775928Sjedgar	int entry_id;
4874465Srwatson
4975928Sjedgar	entry = NULL;
5075928Sjedgar
5174465Srwatson	/*
5274465Srwatson	 * ... if a mask entry is specified, then the permissions of the mask
5374465Srwatson	 * entry in the resulting ACL shall be set to the permissions in the
5474465Srwatson	 * specified ACL mask entry.
5574465Srwatson	 */
5674465Srwatson	if (have_mask)
5787254Sjedgar		return (0);
5874465Srwatson
5975928Sjedgar	acl = acl_dup(*prev_acl);
6087254Sjedgar	if (acl == NULL)
6187254Sjedgar		err(1, "acl_dup() failed");
6274465Srwatson
6387254Sjedgar	if (n_flag == 0) {
6474465Srwatson		/*
6574465Srwatson		 * If no mask entry is specified and the -n option is not
6674465Srwatson		 * specified, then the permissions of the resulting ACL mask
6774465Srwatson		 * entry shall be set to the union of the permissions
6874465Srwatson		 * associated with all entries which belong to the file group
6974465Srwatson		 * class in the resulting ACL
7074465Srwatson		 */
7174465Srwatson		if (acl_calc_mask(&acl)) {
7274465Srwatson			warn("acl_calc_mask() failed");
7374465Srwatson			acl_free(acl);
7487254Sjedgar			return (-1);
7574465Srwatson		}
7674465Srwatson	} else {
7774465Srwatson		/*
7874465Srwatson		 * If no mask entry is specified and the -n option is
7974465Srwatson		 * specified, then the permissions of the resulting ACL
8074465Srwatson		 * mask entry shall remain unchanged ...
8174465Srwatson		 */
8275928Sjedgar
8375928Sjedgar		entry_id = ACL_FIRST_ENTRY;
8475928Sjedgar
8575928Sjedgar		while (acl_get_entry(acl, entry_id, &entry) == 1) {
8675928Sjedgar			entry_id = ACL_NEXT_ENTRY;
8775928Sjedgar			if (acl_get_tag_type(entry, &tag) == -1)
8875928Sjedgar				err(1, "acl_get_tag_type() failed");
8975928Sjedgar
9075928Sjedgar			if (tag == ACL_MASK) {
9174465Srwatson				acl_free(acl);
9287254Sjedgar				return (0);
9374465Srwatson			}
9475928Sjedgar		}
9574465Srwatson
9674465Srwatson		/*
9774465Srwatson		 * If no mask entry is specified, the -n option is specified,
9874465Srwatson		 * and no ACL mask entry exists in the ACL associated with the
9974465Srwatson		 * file, then write an error message to standard error and
10074465Srwatson		 * continue with the next file.
10174465Srwatson		 */
10274465Srwatson		warnx("warning: no mask entry");
10374465Srwatson		acl_free(acl);
10487254Sjedgar		return (0);
10574465Srwatson	}
10674465Srwatson
10790127Sjedgar	acl_free(*prev_acl);
10890127Sjedgar	*prev_acl = acl_init(ACL_MAX_ENTRIES);
10990127Sjedgar	if (*prev_acl == NULL)
11090127Sjedgar		err(1, "acl_init() failed");
11190127Sjedgar
11290127Sjedgar	entry_id = ACL_FIRST_ENTRY;
11390127Sjedgar	while (acl_get_entry(acl, entry_id, &entry) == 1) {
11490127Sjedgar		entry_id = ACL_NEXT_ENTRY;
11590127Sjedgar		if (acl_create_entry(prev_acl, &entry_new) == -1)
11690127Sjedgar			err(1, "acl_create_entry() failed");
11790127Sjedgar		if (acl_copy_entry(entry_new, entry) == -1)
11890127Sjedgar			err(1, "acl_copy_entry() failed");
11990127Sjedgar	}
12090127Sjedgar
12174465Srwatson	acl_free(acl);
12274465Srwatson
12387254Sjedgar	return (0);
12474465Srwatson}
125