Home | History | Annotate | Line # | Download | only in getfacl
getfacl.c revision 1.1
      1  1.1  christos /*	$NetBSD: getfacl.c,v 1.1 2020/05/16 18:31:45 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 1999, 2001, 2002 Robert N M Watson
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This software was developed by Robert Watson for the TrustedBSD Project.
      8  1.1  christos  *
      9  1.1  christos  * Redistribution and use in source and binary forms, with or without
     10  1.1  christos  * modification, are permitted provided that the following conditions
     11  1.1  christos  * are met:
     12  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     14  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  christos  *    documentation and/or other materials provided with the distribution.
     17  1.1  christos  *
     18  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  christos  * SUCH DAMAGE.
     29  1.1  christos  */
     30  1.1  christos /*
     31  1.1  christos  * getfacl -- POSIX.1e utility to extract ACLs from files and directories
     32  1.1  christos  * and send the results to stdout
     33  1.1  christos  */
     34  1.1  christos 
     35  1.1  christos 
     36  1.1  christos #include <sys/cdefs.h>
     37  1.1  christos #if 0
     38  1.1  christos __FBSDID("$FreeBSD: head/bin/getfacl/getfacl.c 340014 2018-11-01 17:45:29Z markj $");
     39  1.1  christos #else
     40  1.1  christos __RCSID("$NetBSD: getfacl.c,v 1.1 2020/05/16 18:31:45 christos Exp $");
     41  1.1  christos #endif
     42  1.1  christos 
     43  1.1  christos #include <sys/types.h>
     44  1.1  christos #include <sys/param.h>
     45  1.1  christos #include <sys/acl.h>
     46  1.1  christos #include <sys/stat.h>
     47  1.1  christos 
     48  1.1  christos #include <err.h>
     49  1.1  christos #include <errno.h>
     50  1.1  christos #include <grp.h>
     51  1.1  christos #include <pwd.h>
     52  1.1  christos #include <stdio.h>
     53  1.1  christos #include <stdlib.h>
     54  1.1  christos #include <string.h>
     55  1.1  christos #include <unistd.h>
     56  1.1  christos 
     57  1.1  christos static int more_than_one = 0;
     58  1.1  christos 
     59  1.1  christos static __dead void
     60  1.1  christos usage(void)
     61  1.1  christos {
     62  1.1  christos 
     63  1.1  christos 	fprintf(stderr, "Usage: %s [-dhnqv] [file ...]\n", getprogname());
     64  1.1  christos }
     65  1.1  christos 
     66  1.1  christos static char *
     67  1.1  christos getuname(uid_t uid)
     68  1.1  christos {
     69  1.1  christos 	struct passwd *pw;
     70  1.1  christos 	static char uids[10];
     71  1.1  christos 
     72  1.1  christos 	if ((pw = getpwuid(uid)) == NULL) {
     73  1.1  christos 		(void)snprintf(uids, sizeof(uids), "%u", uid);
     74  1.1  christos 		return (uids);
     75  1.1  christos 	} else
     76  1.1  christos 		return (pw->pw_name);
     77  1.1  christos }
     78  1.1  christos 
     79  1.1  christos static char *
     80  1.1  christos getgname(gid_t gid)
     81  1.1  christos {
     82  1.1  christos 	struct group *gr;
     83  1.1  christos 	static char gids[10];
     84  1.1  christos 
     85  1.1  christos 	if ((gr = getgrgid(gid)) == NULL) {
     86  1.1  christos 		(void)snprintf(gids, sizeof(gids), "%u", gid);
     87  1.1  christos 		return (gids);
     88  1.1  christos 	} else
     89  1.1  christos 		return (gr->gr_name);
     90  1.1  christos }
     91  1.1  christos 
     92  1.1  christos /*
     93  1.1  christos  * return an ACL corresponding to the permissions
     94  1.1  christos  * contained in struct stat
     95  1.1  christos  */
     96  1.1  christos static acl_t
     97  1.1  christos acl_from_stat(const struct stat *sb)
     98  1.1  christos {
     99  1.1  christos 	acl_t acl;
    100  1.1  christos 	acl_entry_t entry;
    101  1.1  christos 	acl_permset_t perms;
    102  1.1  christos 
    103  1.1  christos 	/* create the ACL */
    104  1.1  christos 	acl = acl_init(3);
    105  1.1  christos 	if (!acl)
    106  1.1  christos 		return NULL;
    107  1.1  christos 
    108  1.1  christos 	/* First entry: ACL_USER_OBJ */
    109  1.1  christos 	if (acl_create_entry(&acl, &entry) == -1)
    110  1.1  christos 		return NULL;
    111  1.1  christos 	if (acl_set_tag_type(entry, ACL_USER_OBJ) == -1)
    112  1.1  christos 		return NULL;
    113  1.1  christos 
    114  1.1  christos 	if (acl_get_permset(entry, &perms) == -1)
    115  1.1  christos 		return NULL;
    116  1.1  christos 	if (acl_clear_perms(perms) == -1)
    117  1.1  christos 		return NULL;
    118  1.1  christos 
    119  1.1  christos 	/* calculate user mode */
    120  1.1  christos 	if (sb->st_mode & S_IRUSR)
    121  1.1  christos 		if (acl_add_perm(perms, ACL_READ) == -1)
    122  1.1  christos 			return NULL;
    123  1.1  christos 	if (sb->st_mode & S_IWUSR)
    124  1.1  christos 		if (acl_add_perm(perms, ACL_WRITE) == -1)
    125  1.1  christos 			return NULL;
    126  1.1  christos 	if (sb->st_mode & S_IXUSR)
    127  1.1  christos 		if (acl_add_perm(perms, ACL_EXECUTE) == -1)
    128  1.1  christos 			return NULL;
    129  1.1  christos 	if (acl_set_permset(entry, perms) == -1)
    130  1.1  christos 		return NULL;
    131  1.1  christos 
    132  1.1  christos 	/* Second entry: ACL_GROUP_OBJ */
    133  1.1  christos 	if (acl_create_entry(&acl, &entry) == -1)
    134  1.1  christos 		return NULL;
    135  1.1  christos 	if (acl_set_tag_type(entry, ACL_GROUP_OBJ) == -1)
    136  1.1  christos 		return NULL;
    137  1.1  christos 
    138  1.1  christos 	if (acl_get_permset(entry, &perms) == -1)
    139  1.1  christos 		return NULL;
    140  1.1  christos 	if (acl_clear_perms(perms) == -1)
    141  1.1  christos 		return NULL;
    142  1.1  christos 
    143  1.1  christos 	/* calculate group mode */
    144  1.1  christos 	if (sb->st_mode & S_IRGRP)
    145  1.1  christos 		if (acl_add_perm(perms, ACL_READ) == -1)
    146  1.1  christos 			return NULL;
    147  1.1  christos 	if (sb->st_mode & S_IWGRP)
    148  1.1  christos 		if (acl_add_perm(perms, ACL_WRITE) == -1)
    149  1.1  christos 			return NULL;
    150  1.1  christos 	if (sb->st_mode & S_IXGRP)
    151  1.1  christos 		if (acl_add_perm(perms, ACL_EXECUTE) == -1)
    152  1.1  christos 			return NULL;
    153  1.1  christos 	if (acl_set_permset(entry, perms) == -1)
    154  1.1  christos 		return NULL;
    155  1.1  christos 
    156  1.1  christos 	/* Third entry: ACL_OTHER */
    157  1.1  christos 	if (acl_create_entry(&acl, &entry) == -1)
    158  1.1  christos 		return NULL;
    159  1.1  christos 	if (acl_set_tag_type(entry, ACL_OTHER) == -1)
    160  1.1  christos 		return NULL;
    161  1.1  christos 
    162  1.1  christos 	if (acl_get_permset(entry, &perms) == -1)
    163  1.1  christos 		return NULL;
    164  1.1  christos 	if (acl_clear_perms(perms) == -1)
    165  1.1  christos 		return NULL;
    166  1.1  christos 
    167  1.1  christos 	/* calculate other mode */
    168  1.1  christos 	if (sb->st_mode & S_IROTH)
    169  1.1  christos 		if (acl_add_perm(perms, ACL_READ) == -1)
    170  1.1  christos 			return NULL;
    171  1.1  christos 	if (sb->st_mode & S_IWOTH)
    172  1.1  christos 		if (acl_add_perm(perms, ACL_WRITE) == -1)
    173  1.1  christos 			return NULL;
    174  1.1  christos 	if (sb->st_mode & S_IXOTH)
    175  1.1  christos 		if (acl_add_perm(perms, ACL_EXECUTE) == -1)
    176  1.1  christos 			return NULL;
    177  1.1  christos 	if (acl_set_permset(entry, perms) == -1)
    178  1.1  christos 		return NULL;
    179  1.1  christos 
    180  1.1  christos 	return(acl);
    181  1.1  christos }
    182  1.1  christos 
    183  1.1  christos static int
    184  1.1  christos print_acl(char *path, acl_type_t type, int hflag, int iflag, int nflag,
    185  1.1  christos     int qflag, int vflag)
    186  1.1  christos {
    187  1.1  christos 	struct stat	sb;
    188  1.1  christos 	acl_t	acl;
    189  1.1  christos 	char	*acl_text;
    190  1.1  christos 	int	error, flags = 0, ret;
    191  1.1  christos 
    192  1.1  christos 	if (hflag)
    193  1.1  christos 		error = lstat(path, &sb);
    194  1.1  christos 	else
    195  1.1  christos 		error = stat(path, &sb);
    196  1.1  christos 	if (error == -1) {
    197  1.1  christos 		warn("%s: stat() failed", path);
    198  1.1  christos 		return(-1);
    199  1.1  christos 	}
    200  1.1  christos 
    201  1.1  christos 	if (hflag)
    202  1.1  christos 		ret = lpathconf(path, _PC_ACL_NFS4);
    203  1.1  christos 	else
    204  1.1  christos 		ret = pathconf(path, _PC_ACL_NFS4);
    205  1.1  christos 	if (ret > 0) {
    206  1.1  christos 		if (type == ACL_TYPE_DEFAULT) {
    207  1.1  christos 			warnx("%s: there are no default entries in NFSv4 ACLs",
    208  1.1  christos 			    path);
    209  1.1  christos 			return (-1);
    210  1.1  christos 		}
    211  1.1  christos 		type = ACL_TYPE_NFS4;
    212  1.1  christos 	} else if (ret < 0 && errno != EINVAL) {
    213  1.1  christos 		warn("%s: pathconf(..., _PC_ACL_NFS4) failed", path);
    214  1.1  christos 		return (-1);
    215  1.1  christos 	}
    216  1.1  christos 
    217  1.1  christos 	if (more_than_one)
    218  1.1  christos 		printf("\n");
    219  1.1  christos 	else
    220  1.1  christos 		more_than_one++;
    221  1.1  christos 
    222  1.1  christos 	if (!qflag)
    223  1.1  christos 		printf("# file: %s\n# owner: %s\n# group: %s\n", path,
    224  1.1  christos 		    getuname(sb.st_uid), getgname(sb.st_gid));
    225  1.1  christos 
    226  1.1  christos 	if (hflag)
    227  1.1  christos 		acl = acl_get_link_np(path, type);
    228  1.1  christos 	else
    229  1.1  christos 		acl = acl_get_file(path, type);
    230  1.1  christos 	if (!acl) {
    231  1.1  christos 		if (errno != EOPNOTSUPP) {
    232  1.1  christos 			warn("%s", path);
    233  1.1  christos 			return(-1);
    234  1.1  christos 		}
    235  1.1  christos 		errno = 0;
    236  1.1  christos 		if (type == ACL_TYPE_DEFAULT)
    237  1.1  christos 			return(0);
    238  1.1  christos 		acl = acl_from_stat(&sb);
    239  1.1  christos 		if (!acl) {
    240  1.1  christos 			warn("%s: acl_from_stat() failed", path);
    241  1.1  christos 			return(-1);
    242  1.1  christos 		}
    243  1.1  christos 	}
    244  1.1  christos 
    245  1.1  christos 	if (iflag)
    246  1.1  christos 		flags |= ACL_TEXT_APPEND_ID;
    247  1.1  christos 
    248  1.1  christos 	if (nflag)
    249  1.1  christos 		flags |= ACL_TEXT_NUMERIC_IDS;
    250  1.1  christos 
    251  1.1  christos 	if (vflag)
    252  1.1  christos 		flags |= ACL_TEXT_VERBOSE;
    253  1.1  christos 
    254  1.1  christos 	acl_text = acl_to_text_np(acl, 0, flags);
    255  1.1  christos 	if (!acl_text) {
    256  1.1  christos 		warn("%s: acl_to_text_np() failed", path);
    257  1.1  christos 		return(-1);
    258  1.1  christos 	}
    259  1.1  christos 
    260  1.1  christos 	printf("%s", acl_text);
    261  1.1  christos 
    262  1.1  christos 	(void)acl_free(acl);
    263  1.1  christos 	(void)acl_free(acl_text);
    264  1.1  christos 
    265  1.1  christos 	return(0);
    266  1.1  christos }
    267  1.1  christos 
    268  1.1  christos static int
    269  1.1  christos print_acl_from_stdin(acl_type_t type, int hflag, int iflag, int nflag,
    270  1.1  christos     int qflag, int vflag)
    271  1.1  christos {
    272  1.1  christos 	char	*p, pathname[PATH_MAX];
    273  1.1  christos 	int	carried_error = 0;
    274  1.1  christos 
    275  1.1  christos 	while (fgets(pathname, (int)sizeof(pathname), stdin)) {
    276  1.1  christos 		if ((p = strchr(pathname, '\n')) != NULL)
    277  1.1  christos 			*p = '\0';
    278  1.1  christos 		if (print_acl(pathname, type, hflag, iflag, nflag,
    279  1.1  christos 		    qflag, vflag) == -1) {
    280  1.1  christos 			carried_error = -1;
    281  1.1  christos 		}
    282  1.1  christos 	}
    283  1.1  christos 
    284  1.1  christos 	return(carried_error);
    285  1.1  christos }
    286  1.1  christos 
    287  1.1  christos int
    288  1.1  christos main(int argc, char *argv[])
    289  1.1  christos {
    290  1.1  christos 	acl_type_t	type = ACL_TYPE_ACCESS;
    291  1.1  christos 	int	carried_error = 0;
    292  1.1  christos 	int	ch, error, i;
    293  1.1  christos 	int	hflag, iflag, qflag, nflag, vflag;
    294  1.1  christos 
    295  1.1  christos 	hflag = 0;
    296  1.1  christos 	iflag = 0;
    297  1.1  christos 	qflag = 0;
    298  1.1  christos 	nflag = 0;
    299  1.1  christos 	vflag = 0;
    300  1.1  christos 	while ((ch = getopt(argc, argv, "dhinqv")) != -1)
    301  1.1  christos 		switch(ch) {
    302  1.1  christos 		case 'd':
    303  1.1  christos 			type = ACL_TYPE_DEFAULT;
    304  1.1  christos 			break;
    305  1.1  christos 		case 'h':
    306  1.1  christos 			hflag = 1;
    307  1.1  christos 			break;
    308  1.1  christos 		case 'i':
    309  1.1  christos 			iflag = 1;
    310  1.1  christos 			break;
    311  1.1  christos 		case 'n':
    312  1.1  christos 			nflag = 1;
    313  1.1  christos 			break;
    314  1.1  christos 		case 'q':
    315  1.1  christos 			qflag = 1;
    316  1.1  christos 			break;
    317  1.1  christos 		case 'v':
    318  1.1  christos 			vflag = 1;
    319  1.1  christos 			break;
    320  1.1  christos 		default:
    321  1.1  christos 			usage();
    322  1.1  christos 			return(-1);
    323  1.1  christos 		}
    324  1.1  christos 	argc -= optind;
    325  1.1  christos 	argv += optind;
    326  1.1  christos 
    327  1.1  christos 	if (argc == 0) {
    328  1.1  christos 		error = print_acl_from_stdin(type, hflag, iflag, nflag,
    329  1.1  christos 		    qflag, vflag);
    330  1.1  christos 		return(error ? 1 : 0);
    331  1.1  christos 	}
    332  1.1  christos 
    333  1.1  christos 	for (i = 0; i < argc; i++) {
    334  1.1  christos 		if (!strcmp(argv[i], "-")) {
    335  1.1  christos 			error = print_acl_from_stdin(type, hflag, iflag, nflag,
    336  1.1  christos 			    qflag, vflag);
    337  1.1  christos 			if (error == -1)
    338  1.1  christos 				carried_error = -1;
    339  1.1  christos 		} else {
    340  1.1  christos 			error = print_acl(argv[i], type, hflag, iflag, nflag,
    341  1.1  christos 			    qflag, vflag);
    342  1.1  christos 			if (error == -1)
    343  1.1  christos 				carried_error = -1;
    344  1.1  christos 		}
    345  1.1  christos 	}
    346  1.1  christos 
    347  1.1  christos 	return(carried_error ? 1 : 0);
    348  1.1  christos }
    349