xref: /src/bin/setfacl/mask.c (revision 575e89cb1786a00387e9c1391055263068c4e0ae)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001-2002 Chris D. Faulhaber
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/acl.h>
31 #include <sys/stat.h>
32 
33 #include <err.h>
34 
35 #include "setfacl.h"
36 
37 /* set the appropriate mask the given ACL's */
38 int
set_acl_mask(acl_t * prev_acl,const char * filename)39 set_acl_mask(acl_t *prev_acl, const char *filename)
40 {
41 	acl_entry_t entry;
42 	acl_t acl;
43 	acl_tag_t tag;
44 	int entry_id;
45 
46 	entry = NULL;
47 
48 	/*
49 	 * ... if a mask entry is specified, then the permissions of the mask
50 	 * entry in the resulting ACL shall be set to the permissions in the
51 	 * specified ACL mask entry.
52 	 */
53 	if (have_mask)
54 		return (0);
55 
56 	acl = acl_dup(*prev_acl);
57 	if (acl == NULL)
58 		err(1, "%s: acl_dup() failed", filename);
59 
60 	if (!n_flag) {
61 		/*
62 		 * If no mask entry is specified and the -n option is not
63 		 * specified, then the permissions of the resulting ACL mask
64 		 * entry shall be set to the union of the permissions
65 		 * associated with all entries which belong to the file group
66 		 * class in the resulting ACL
67 		 */
68 		if (acl_calc_mask(&acl)) {
69 			warn("%s: acl_calc_mask() failed", filename);
70 			acl_free(acl);
71 			return (-1);
72 		}
73 	} else {
74 		/*
75 		 * If no mask entry is specified and the -n option is
76 		 * specified, then the permissions of the resulting ACL
77 		 * mask entry shall remain unchanged ...
78 		 */
79 
80 		entry_id = ACL_FIRST_ENTRY;
81 
82 		while (acl_get_entry(acl, entry_id, &entry) == 1) {
83 			entry_id = ACL_NEXT_ENTRY;
84 			if (acl_get_tag_type(entry, &tag) == -1)
85 				err(1, "%s: acl_get_tag_type() failed",
86 				    filename);
87 
88 			if (tag == ACL_MASK) {
89 				acl_free(acl);
90 				return (0);
91 			}
92 		}
93 
94 		/*
95 		 * If no mask entry is specified, the -n option is specified,
96 		 * and no ACL mask entry exists in the ACL associated with the
97 		 * file, then write an error message to standard error and
98 		 * continue with the next file.
99 		 */
100 		warnx("%s: warning: no mask entry", filename);
101 		acl_free(acl);
102 		return (0);
103 	}
104 
105 	acl_free(*prev_acl);
106 	*prev_acl = acl_dup(acl);
107 	acl_free(acl);
108 
109 	return (0);
110 }
111