Home | History | Annotate | Line # | Download | only in setfacl
      1 /*	$NetBSD: mask.c,v 1.1 2020/05/16 18:31:45 christos Exp $	*/
      2 
      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/cdefs.h>
     30 #if 0
     31 __FBSDID("$FreeBSD: head/bin/setfacl/mask.c 333065 2018-04-27 15:25:24Z emaste $");
     32 #else
     33 __RCSID("$NetBSD: mask.c,v 1.1 2020/05/16 18:31:45 christos Exp $");
     34 #endif
     35 
     36 #include <sys/types.h>
     37 #include <sys/acl.h>
     38 #include <sys/stat.h>
     39 
     40 #include <err.h>
     41 #include <errno.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 
     45 #include "setfacl.h"
     46 
     47 /* set the appropriate mask the given ACL's */
     48 int
     49 set_acl_mask(acl_t *prev_acl, const char *filename)
     50 {
     51 	acl_entry_t entry;
     52 	acl_t acl;
     53 	acl_tag_t tag;
     54 	int entry_id;
     55 
     56 	entry = NULL;
     57 
     58 	/*
     59 	 * ... if a mask entry is specified, then the permissions of the mask
     60 	 * entry in the resulting ACL shall be set to the permissions in the
     61 	 * specified ACL mask entry.
     62 	 */
     63 	if (have_mask)
     64 		return (0);
     65 
     66 	acl = acl_dup(*prev_acl);
     67 	if (acl == NULL)
     68 		err(1, "%s: acl_dup() failed", filename);
     69 
     70 	if (!n_flag) {
     71 		/*
     72 		 * If no mask entry is specified and the -n option is not
     73 		 * specified, then the permissions of the resulting ACL mask
     74 		 * entry shall be set to the union of the permissions
     75 		 * associated with all entries which belong to the file group
     76 		 * class in the resulting ACL
     77 		 */
     78 		if (acl_calc_mask(&acl)) {
     79 			warn("%s: acl_calc_mask() failed", filename);
     80 			acl_free(acl);
     81 			return (-1);
     82 		}
     83 	} else {
     84 		/*
     85 		 * If no mask entry is specified and the -n option is
     86 		 * specified, then the permissions of the resulting ACL
     87 		 * mask entry shall remain unchanged ...
     88 		 */
     89 
     90 		entry_id = ACL_FIRST_ENTRY;
     91 
     92 		while (acl_get_entry(acl, entry_id, &entry) == 1) {
     93 			entry_id = ACL_NEXT_ENTRY;
     94 			if (acl_get_tag_type(entry, &tag) == -1)
     95 				err(1, "%s: acl_get_tag_type() failed",
     96 				    filename);
     97 
     98 			if (tag == ACL_MASK) {
     99 				acl_free(acl);
    100 				return (0);
    101 			}
    102 		}
    103 
    104 		/*
    105 		 * If no mask entry is specified, the -n option is specified,
    106 		 * and no ACL mask entry exists in the ACL associated with the
    107 		 * file, then write an error message to standard error and
    108 		 * continue with the next file.
    109 		 */
    110 		warnx("%s: warning: no mask entry", filename);
    111 		acl_free(acl);
    112 		return (0);
    113 	}
    114 
    115 	acl_free(*prev_acl);
    116 	*prev_acl = acl_dup(acl);
    117 	acl_free(acl);
    118 
    119 	return (0);
    120 }
    121