xref: /freebsd/bin/setfacl/mask.c (revision e043f372)
19ddb49cbSWarner Losh /*-
242dcd869SChris D. Faulhaber  * Copyright (c) 2001-2002 Chris D. Faulhaber
343960f15SRobert Watson  * All rights reserved.
443960f15SRobert Watson  *
543960f15SRobert Watson  * Redistribution and use in source and binary forms, with or without
643960f15SRobert Watson  * modification, are permitted provided that the following conditions
743960f15SRobert Watson  * are met:
843960f15SRobert Watson  * 1. Redistributions of source code must retain the above copyright
943960f15SRobert Watson  *    notice, this list of conditions and the following disclaimer.
1043960f15SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
1143960f15SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
1243960f15SRobert Watson  *    documentation and/or other materials provided with the distribution.
1343960f15SRobert Watson  *
1443960f15SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1543960f15SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1643960f15SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a303eae7SJoel Dahl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a303eae7SJoel Dahl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a303eae7SJoel Dahl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a303eae7SJoel Dahl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a303eae7SJoel Dahl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a303eae7SJoel Dahl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a303eae7SJoel Dahl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a303eae7SJoel Dahl  * SUCH DAMAGE.
2543960f15SRobert Watson  */
2643960f15SRobert Watson 
2743960f15SRobert Watson #include <sys/types.h>
2843960f15SRobert Watson #include <sys/acl.h>
2943960f15SRobert Watson #include <sys/stat.h>
3043960f15SRobert Watson 
3143960f15SRobert Watson #include <err.h>
3243960f15SRobert Watson 
3343960f15SRobert Watson #include "setfacl.h"
3443960f15SRobert Watson 
3543960f15SRobert Watson /* set the appropriate mask the given ACL's */
3643960f15SRobert Watson int
set_acl_mask(acl_t * prev_acl,const char * filename)373f221878SEdward Tomasz Napierala set_acl_mask(acl_t *prev_acl, const char *filename)
3843960f15SRobert Watson {
3932e7fd59SChris D. Faulhaber 	acl_entry_t entry;
4043960f15SRobert Watson 	acl_t acl;
410f626307SChris D. Faulhaber 	acl_tag_t tag;
420f626307SChris D. Faulhaber 	int entry_id;
430f626307SChris D. Faulhaber 
440f626307SChris D. Faulhaber 	entry = NULL;
4543960f15SRobert Watson 
4643960f15SRobert Watson 	/*
4743960f15SRobert Watson 	 * ... if a mask entry is specified, then the permissions of the mask
4843960f15SRobert Watson 	 * entry in the resulting ACL shall be set to the permissions in the
4943960f15SRobert Watson 	 * specified ACL mask entry.
5043960f15SRobert Watson 	 */
5143960f15SRobert Watson 	if (have_mask)
52a043a09dSChris D. Faulhaber 		return (0);
5343960f15SRobert Watson 
540f626307SChris D. Faulhaber 	acl = acl_dup(*prev_acl);
55a043a09dSChris D. Faulhaber 	if (acl == NULL)
563f221878SEdward Tomasz Napierala 		err(1, "%s: acl_dup() failed", filename);
5743960f15SRobert Watson 
58b79f74ccSEd Maste 	if (!n_flag) {
5943960f15SRobert Watson 		/*
6043960f15SRobert Watson 		 * If no mask entry is specified and the -n option is not
6143960f15SRobert Watson 		 * specified, then the permissions of the resulting ACL mask
6243960f15SRobert Watson 		 * entry shall be set to the union of the permissions
6343960f15SRobert Watson 		 * associated with all entries which belong to the file group
6443960f15SRobert Watson 		 * class in the resulting ACL
6543960f15SRobert Watson 		 */
6643960f15SRobert Watson 		if (acl_calc_mask(&acl)) {
673f221878SEdward Tomasz Napierala 			warn("%s: acl_calc_mask() failed", filename);
6843960f15SRobert Watson 			acl_free(acl);
69a043a09dSChris D. Faulhaber 			return (-1);
7043960f15SRobert Watson 		}
7143960f15SRobert Watson 	} else {
7243960f15SRobert Watson 		/*
7343960f15SRobert Watson 		 * If no mask entry is specified and the -n option is
7443960f15SRobert Watson 		 * specified, then the permissions of the resulting ACL
7543960f15SRobert Watson 		 * mask entry shall remain unchanged ...
7643960f15SRobert Watson 		 */
770f626307SChris D. Faulhaber 
780f626307SChris D. Faulhaber 		entry_id = ACL_FIRST_ENTRY;
790f626307SChris D. Faulhaber 
800f626307SChris D. Faulhaber 		while (acl_get_entry(acl, entry_id, &entry) == 1) {
810f626307SChris D. Faulhaber 			entry_id = ACL_NEXT_ENTRY;
820f626307SChris D. Faulhaber 			if (acl_get_tag_type(entry, &tag) == -1)
833f221878SEdward Tomasz Napierala 				err(1, "%s: acl_get_tag_type() failed",
843f221878SEdward Tomasz Napierala 				    filename);
850f626307SChris D. Faulhaber 
860f626307SChris D. Faulhaber 			if (tag == ACL_MASK) {
8743960f15SRobert Watson 				acl_free(acl);
88a043a09dSChris D. Faulhaber 				return (0);
8943960f15SRobert Watson 			}
900f626307SChris D. Faulhaber 		}
9143960f15SRobert Watson 
9243960f15SRobert Watson 		/*
9343960f15SRobert Watson 		 * If no mask entry is specified, the -n option is specified,
9443960f15SRobert Watson 		 * and no ACL mask entry exists in the ACL associated with the
9543960f15SRobert Watson 		 * file, then write an error message to standard error and
9643960f15SRobert Watson 		 * continue with the next file.
9743960f15SRobert Watson 		 */
983f221878SEdward Tomasz Napierala 		warnx("%s: warning: no mask entry", filename);
9943960f15SRobert Watson 		acl_free(acl);
100a043a09dSChris D. Faulhaber 		return (0);
10143960f15SRobert Watson 	}
10243960f15SRobert Watson 
10342dcd869SChris D. Faulhaber 	acl_free(*prev_acl);
10432e7fd59SChris D. Faulhaber 	*prev_acl = acl_dup(acl);
10543960f15SRobert Watson 	acl_free(acl);
10643960f15SRobert Watson 
107a043a09dSChris D. Faulhaber 	return (0);
10843960f15SRobert Watson }
109