mask.c revision 75928
174465Srwatson/*
274465Srwatson * Copyright (c) 2001 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 75928 2001-04-24 22:45:41Z 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#include <sysexits.h>
3874465Srwatson
3974465Srwatson#include "setfacl.h"
4074465Srwatson
4174465Srwatson/* set the appropriate mask the given ACL's */
4274465Srwatsonint
4375928Sjedgarset_acl_mask(acl_t *prev_acl)
4474465Srwatson{
4575928Sjedgar	acl_entry_t entry;
4674465Srwatson	acl_t acl;
4775928Sjedgar	acl_tag_t tag;
4875928Sjedgar	int entry_id;
4974465Srwatson
5075928Sjedgar	entry = NULL;
5175928Sjedgar
5274465Srwatson	/*
5374465Srwatson	 * ... if a mask entry is specified, then the permissions of the mask
5474465Srwatson	 * entry in the resulting ACL shall be set to the permissions in the
5574465Srwatson	 * specified ACL mask entry.
5674465Srwatson	 */
5774465Srwatson	if (have_mask)
5874465Srwatson		return 0;
5974465Srwatson
6075928Sjedgar	acl = acl_dup(*prev_acl);
6174465Srwatson	if (!acl)
6274465Srwatson		err(EX_OSERR, "acl_dup() failed");
6374465Srwatson
6474465Srwatson	if (!n_flag) {
6574465Srwatson		/*
6674465Srwatson		 * If no mask entry is specified and the -n option is not
6774465Srwatson		 * specified, then the permissions of the resulting ACL mask
6874465Srwatson		 * entry shall be set to the union of the permissions
6974465Srwatson		 * associated with all entries which belong to the file group
7074465Srwatson		 * class in the resulting ACL
7174465Srwatson		 */
7274465Srwatson		if (acl_calc_mask(&acl)) {
7374465Srwatson			warn("acl_calc_mask() failed");
7474465Srwatson			acl_free(acl);
7574465Srwatson			return -1;
7674465Srwatson		}
7774465Srwatson	} else {
7874465Srwatson		/*
7974465Srwatson		 * If no mask entry is specified and the -n option is
8074465Srwatson		 * specified, then the permissions of the resulting ACL
8174465Srwatson		 * mask entry shall remain unchanged ...
8274465Srwatson		 */
8375928Sjedgar
8475928Sjedgar		entry_id = ACL_FIRST_ENTRY;
8575928Sjedgar
8675928Sjedgar		while (acl_get_entry(acl, entry_id, &entry) == 1) {
8775928Sjedgar			entry_id = ACL_NEXT_ENTRY;
8875928Sjedgar			if (acl_get_tag_type(entry, &tag) == -1)
8975928Sjedgar				err(1, "acl_get_tag_type() failed");
9075928Sjedgar
9175928Sjedgar			if (tag == ACL_MASK) {
9274465Srwatson				acl_free(acl);
9374465Srwatson				return 0;
9474465Srwatson			}
9575928Sjedgar		}
9674465Srwatson
9774465Srwatson		/*
9874465Srwatson		 * If no mask entry is specified, the -n option is specified,
9974465Srwatson		 * and no ACL mask entry exists in the ACL associated with the
10074465Srwatson		 * file, then write an error message to standard error and
10174465Srwatson		 * continue with the next file.
10274465Srwatson		 */
10374465Srwatson		warnx("warning: no mask entry");
10474465Srwatson		acl_free(acl);
10574465Srwatson		return 0;
10674465Srwatson	}
10774465Srwatson
10875928Sjedgar	prev_acl = &acl;
10974465Srwatson	acl_free(acl);
11074465Srwatson
11174465Srwatson	return 0;
11274465Srwatson}
113