usr/src/lib/libsec/common/aclutils.c
author Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
Wed, 14 Apr 2010 13:25:42 -0600
changeset 12164 0eb8d6741e37
parent 10228 ca7b8ea13ef5
child 12926 e95332bf1454
permissions -rw-r--r--
PSARC/2010/029 Improved ACL interoperability 6923083 ZFS/NFS/SMB ACL interoperability changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     1
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     2
 * CDDL HEADER START
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     3
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
1462
117f7ea5a828 6355341 tar cannot restore ACLs to UFS
marks
parents: 1420
diff changeset
     5
 * Common Development and Distribution License (the "License").
117f7ea5a828 6355341 tar cannot restore ACLs to UFS
marks
parents: 1420
diff changeset
     6
 * You may not use this file except in compliance with the License.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    21
/*
12164
0eb8d6741e37 PSARC/2010/029 Improved ACL interoperability
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 10228
diff changeset
    22
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    23
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    24
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    25
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    26
#include <stdlib.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
#include <string.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
#include <unistd.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <limits.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <grp.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <pwd.h>
1462
117f7ea5a828 6355341 tar cannot restore ACLs to UFS
marks
parents: 1420
diff changeset
    32
#include <strings.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <sys/types.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
#include <errno.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#include <sys/stat.h>
1420
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
    36
#include <sys/varargs.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <locale.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <aclutils.h>
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4321
diff changeset
    39
#include <sys/avl.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
#include <acl_common.h>
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
    41
#include <idmap.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
#define	ACL_PATH	0
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    44
#define	ACL_FD		1
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    46
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
typedef union {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
	const char *file;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
	int  fd;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
} acl_inp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    52
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
 * Determine whether a file has a trivial ACL
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
 * returns: 	0 = trivial
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
 *		1 = nontrivial
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
 *		<0 some other system failure, such as ENOENT or EPERM
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
acl_trivial(const char *filename)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
	int acl_flavor;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
	int aclcnt;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
	int cntcmd;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
	int val = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
	ace_t *acep;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
	acl_flavor = pathconf(filename, _PC_ACL_ENABLED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
	if (acl_flavor == _ACL_ACE_ENABLED)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
		cntcmd = ACE_GETACLCNT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    72
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
		cntcmd = GETACLCNT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
	aclcnt = acl(filename, cntcmd, 0, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
	if (aclcnt > 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    77
		if (acl_flavor == _ACL_ACE_ENABLED) {
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    78
			acep = malloc(sizeof (ace_t) * aclcnt);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    79
			if (acep == NULL)
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    80
				return (-1);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    81
			if (acl(filename, ACE_GETACL,
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    82
			    aclcnt, acep) < 0) {
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    83
				free(acep);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    84
				return (-1);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    85
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    87
			val = ace_trivial(acep, aclcnt);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    88
			free(acep);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 789
diff changeset
    89
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
		} else if (aclcnt > MIN_ACL_ENTRIES)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
			val = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
	return (val);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
1462
117f7ea5a828 6355341 tar cannot restore ACLs to UFS
marks
parents: 1420
diff changeset
    96
1477
18d318bc863e 6389529 libsec fails to compile with gcc
marks
parents: 1462
diff changeset
    97
static int
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
cacl_get(acl_inp inp, int get_flag, int type, acl_t **aclp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    99
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   100
	const char *fname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   101
	int fd;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   102
	int ace_acl = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   104
	int getcmd, cntcmd;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   105
	acl_t *acl_info;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   106
	int	save_errno;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   107
	int	stat_error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   108
	struct stat64 statbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   109
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   110
	*aclp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
	if (type == ACL_PATH) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
		fname = inp.file;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   113
		ace_acl = pathconf(fname, _PC_ACL_ENABLED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   114
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   115
		fd = inp.fd;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   116
		ace_acl = fpathconf(fd, _PC_ACL_ENABLED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   117
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   118
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   120
	 * if acl's aren't supported then
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   121
	 * send it through the old GETACL interface
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   122
	 */
1666
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   123
	if (ace_acl == 0 || ace_acl == -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
		ace_acl = _ACL_ACLENT_ENABLED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   125
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
	if (ace_acl & _ACL_ACE_ENABLED) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
		cntcmd = ACE_GETACLCNT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
		getcmd = ACE_GETACL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
		acl_info = acl_alloc(ACE_T);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   131
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   132
		cntcmd = GETACLCNT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   133
		getcmd = GETACL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   134
		acl_info = acl_alloc(ACLENT_T);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   135
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   136
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   137
	if (acl_info == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   139
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   140
	if (type == ACL_PATH) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   141
		acl_info->acl_cnt = acl(fname, cntcmd, 0, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   142
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   143
		acl_info->acl_cnt = facl(fd, cntcmd, 0, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   144
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   145
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   146
	save_errno = errno;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   147
	if (acl_info->acl_cnt < 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   148
		acl_free(acl_info);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   149
		errno = save_errno;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   150
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   151
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   152
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
	if (acl_info->acl_cnt == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   154
		acl_free(acl_info);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   155
		errno = save_errno;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   156
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   157
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   158
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   159
	acl_info->acl_aclp =
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   160
	    malloc(acl_info->acl_cnt * acl_info->acl_entry_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   161
	save_errno = errno;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   162
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   163
	if (acl_info->acl_aclp == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   164
		acl_free(acl_info);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   165
		errno = save_errno;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   166
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   167
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   168
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   169
	if (type == ACL_PATH) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   170
		stat_error = stat64(fname, &statbuf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   171
		error = acl(fname, getcmd, acl_info->acl_cnt,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   172
		    acl_info->acl_aclp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   173
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   174
		stat_error = fstat64(fd, &statbuf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   175
		error = facl(fd, getcmd, acl_info->acl_cnt,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   176
		    acl_info->acl_aclp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   177
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   178
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   179
	save_errno = errno;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   180
	if (error == -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   181
		acl_free(acl_info);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   182
		errno = save_errno;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   183
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   184
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   185
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   186
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   187
	if (stat_error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   188
		acl_info->acl_flags =
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   189
		    (S_ISDIR(statbuf.st_mode) ? ACL_IS_DIR : 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   190
	} else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   191
		acl_info->acl_flags = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   192
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   193
	switch (acl_info->acl_type) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   194
	case ACLENT_T:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   195
		if (acl_info->acl_cnt <= MIN_ACL_ENTRIES)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   196
			acl_info->acl_flags |= ACL_IS_TRIVIAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   197
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   198
	case ACE_T:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   199
		if (ace_trivial(acl_info->acl_aclp, acl_info->acl_cnt) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   200
			acl_info->acl_flags |= ACL_IS_TRIVIAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   201
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   202
	default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   203
		errno = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   204
		acl_free(acl_info);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   205
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   206
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   207
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   208
	if ((acl_info->acl_flags & ACL_IS_TRIVIAL) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   209
	    (get_flag & ACL_NO_TRIVIAL)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   210
		acl_free(acl_info);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   211
		errno = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   212
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   213
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   214
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   215
	*aclp = acl_info;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   216
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   217
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   218
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   219
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   220
 * return -1 on failure, otherwise the number of acl
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   221
 * entries is returned
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   222
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   223
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   224
acl_get(const char *path, int get_flag, acl_t **aclp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   225
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   226
	acl_inp acl_inp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   227
	acl_inp.file = path;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   228
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   229
	return (cacl_get(acl_inp, get_flag, ACL_PATH, aclp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   230
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   231
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   232
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   233
facl_get(int fd, int get_flag, acl_t **aclp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   234
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   235
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   236
	acl_inp acl_inp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   237
	acl_inp.fd = fd;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   238
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   239
	return (cacl_get(acl_inp, get_flag, ACL_FD, aclp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   240
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   241
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   242
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   243
 * Set an ACL, translates acl to ace_t when appropriate.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   244
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   245
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   246
cacl_set(acl_inp *acl_inp, acl_t *aclp, int type)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   247
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   248
	int error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   249
	int acl_flavor_target;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   250
	struct stat64 statbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   251
	int stat_error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   252
	int isdir;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   253
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   254
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   255
	if (type == ACL_PATH) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   256
		stat_error = stat64(acl_inp->file, &statbuf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   257
		if (stat_error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   258
			return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   259
		acl_flavor_target = pathconf(acl_inp->file, _PC_ACL_ENABLED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   260
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   261
		stat_error = fstat64(acl_inp->fd, &statbuf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   262
		if (stat_error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   263
			return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   264
		acl_flavor_target = fpathconf(acl_inp->fd, _PC_ACL_ENABLED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   265
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   266
1666
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   267
	/*
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   268
	 * If target returns an error or 0 from pathconf call then
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   269
	 * fall back to UFS/POSIX Draft interface.
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   270
	 * In the case of 0 we will then fail in either acl(2) or
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   271
	 * acl_translate().  We could erroneously get 0 back from
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   272
	 * a file system that is using fs_pathconf() and not answering
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   273
	 * the _PC_ACL_ENABLED question itself.
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   274
	 */
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   275
	if (acl_flavor_target == 0 || acl_flavor_target == -1)
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   276
		acl_flavor_target = _ACL_ACLENT_ENABLED;
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   277
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   278
	isdir = S_ISDIR(statbuf.st_mode);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   279
1462
117f7ea5a828 6355341 tar cannot restore ACLs to UFS
marks
parents: 1420
diff changeset
   280
	if ((error = acl_translate(aclp, acl_flavor_target, isdir,
117f7ea5a828 6355341 tar cannot restore ACLs to UFS
marks
parents: 1420
diff changeset
   281
	    statbuf.st_uid, statbuf.st_gid)) != 0) {
117f7ea5a828 6355341 tar cannot restore ACLs to UFS
marks
parents: 1420
diff changeset
   282
		return (error);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   283
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   284
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   285
	if (type == ACL_PATH) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   286
		error = acl(acl_inp->file,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   287
		    (aclp->acl_type == ACE_T) ? ACE_SETACL : SETACL,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   288
		    aclp->acl_cnt, aclp->acl_aclp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   289
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   290
		error = facl(acl_inp->fd,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   291
		    (aclp->acl_type == ACE_T) ? ACE_SETACL : SETACL,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   292
		    aclp->acl_cnt, aclp->acl_aclp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   293
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   294
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   295
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   296
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   297
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   298
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   299
acl_set(const char *path, acl_t *aclp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   300
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   301
	acl_inp acl_inp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   302
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   303
	acl_inp.file = path;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   304
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   305
	return (cacl_set(&acl_inp, aclp, ACL_PATH));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   306
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   307
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   308
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   309
facl_set(int fd, acl_t *aclp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   310
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   311
	acl_inp acl_inp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   312
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
	acl_inp.fd = fd;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   314
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   315
	return (cacl_set(&acl_inp, aclp, ACL_FD));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   316
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   317
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   318
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   319
acl_cnt(acl_t *aclp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   320
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   321
	return (aclp->acl_cnt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   322
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   323
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   324
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   325
acl_type(acl_t *aclp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   326
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   327
	return (aclp->acl_type);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   328
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   329
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   330
acl_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   331
acl_dup(acl_t *aclp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   332
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   333
	acl_t *newaclp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   334
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   335
	newaclp = acl_alloc(aclp->acl_type);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   336
	if (newaclp == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   337
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   338
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   339
	newaclp->acl_aclp = malloc(aclp->acl_entry_size * aclp->acl_cnt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   340
	if (newaclp->acl_aclp == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   341
		acl_free(newaclp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   342
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   343
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   344
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   345
	(void) memcpy(newaclp->acl_aclp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
	    aclp->acl_aclp, aclp->acl_entry_size * aclp->acl_cnt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   347
	newaclp->acl_cnt = aclp->acl_cnt;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   348
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   349
	return (newaclp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   350
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   351
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   352
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   353
acl_flags(acl_t *aclp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   354
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   355
	return (aclp->acl_flags);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   356
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   357
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   358
void *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   359
acl_data(acl_t *aclp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   360
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   361
	return (aclp->acl_aclp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   362
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   363
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   364
/*
1953
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   365
 * Take an acl array and build an acl_t.
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   366
 */
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   367
acl_t *
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   368
acl_to_aclp(enum acl_type type, void *acl, int count)
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   369
{
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   370
	acl_t *aclp;
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   371
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   372
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   373
	aclp = acl_alloc(type);
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   374
	if (aclp == NULL)
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   375
		return (aclp);
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   376
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   377
	aclp->acl_aclp = acl;
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   378
	aclp->acl_cnt = count;
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   379
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   380
	return (aclp);
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   381
}
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   382
7d218c488035 6421216 ufsrestore should use acl_set() for setting ACLs
marks
parents: 1666
diff changeset
   383
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   384
 * Remove an ACL from a file and create a trivial ACL based
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   385
 * off of the mode argument.  After acl has been set owner/group
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   386
 * are updated to match owner,group arguments
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   387
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   388
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   389
acl_strip(const char *file, uid_t owner, gid_t group, mode_t mode)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   390
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   391
	int	error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   392
	aclent_t min_acl[MIN_ACL_ENTRIES];
12164
0eb8d6741e37 PSARC/2010/029 Improved ACL interoperability
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 10228
diff changeset
   393
	ace_t	*min_ace_acl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   394
	int	acl_flavor;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   395
	int	aclcnt;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   396
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
	acl_flavor = pathconf(file, _PC_ACL_ENABLED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   398
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   400
	 * force it through aclent flavor when file system doesn't
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   401
	 * understand question
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   402
	 */
1666
07697c578888 6401243 ZFS ACLs should not break third party filesystems
marks
parents: 1477
diff changeset
   403
	if (acl_flavor == 0 || acl_flavor == -1)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   404
		acl_flavor = _ACL_ACLENT_ENABLED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   405
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
	if (acl_flavor & _ACL_ACLENT_ENABLED) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   407
		min_acl[0].a_type = USER_OBJ;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   408
		min_acl[0].a_id   = owner;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   409
		min_acl[0].a_perm = ((mode & 0700) >> 6);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   410
		min_acl[1].a_type = GROUP_OBJ;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
		min_acl[1].a_id   = group;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   412
		min_acl[1].a_perm = ((mode & 0070) >> 3);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
		min_acl[2].a_type = CLASS_OBJ;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   414
		min_acl[2].a_id   = (uid_t)-1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   415
		min_acl[2].a_perm = ((mode & 0070) >> 3);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   416
		min_acl[3].a_type = OTHER_OBJ;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   417
		min_acl[3].a_id   = (uid_t)-1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   418
		min_acl[3].a_perm = (mode & 0007);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   419
		aclcnt = 4;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   420
		error = acl(file, SETACL, aclcnt, min_acl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   421
	} else if (acl_flavor & _ACL_ACE_ENABLED) {
12164
0eb8d6741e37 PSARC/2010/029 Improved ACL interoperability
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 10228
diff changeset
   422
		if ((error = acl_trivial_create(mode, &min_ace_acl,
0eb8d6741e37 PSARC/2010/029 Improved ACL interoperability
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 10228
diff changeset
   423
		    &aclcnt)) != 0)
0eb8d6741e37 PSARC/2010/029 Improved ACL interoperability
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 10228
diff changeset
   424
			return (error);
0eb8d6741e37 PSARC/2010/029 Improved ACL interoperability
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 10228
diff changeset
   425
		error = acl(file, ACE_SETACL, aclcnt, min_ace_acl);
0eb8d6741e37 PSARC/2010/029 Improved ACL interoperability
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 10228
diff changeset
   426
		free(min_ace_acl);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   427
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   428
		errno = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   429
		error = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   430
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   431
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   432
	if (error == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   433
		error = chown(file, owner, group);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   434
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   435
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   436
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   437
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   438
ace_match(void *entry1, void *entry2)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   439
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   440
	ace_t *p1 = (ace_t *)entry1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
	ace_t *p2 = (ace_t *)entry2;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   442
	ace_t ace1, ace2;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   443
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   444
	ace1 = *p1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   445
	ace2 = *p2;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   446
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   447
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   448
	 * Need to fixup who field for abstrations for
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   449
	 * accurate comparison, since field is undefined.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   450
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   451
	if (ace1.a_flags & (ACE_OWNER|ACE_GROUP|ACE_EVERYONE))
4321
a8930ec16e52 PSARC 2007/064 Unified POSIX and Windows Credentials for Solaris
casper
parents: 1953
diff changeset
   452
		ace1.a_who = (uid_t)-1;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
	if (ace2.a_flags & (ACE_OWNER|ACE_GROUP|ACE_EVERYONE))
4321
a8930ec16e52 PSARC 2007/064 Unified POSIX and Windows Credentials for Solaris
casper
parents: 1953
diff changeset
   454
		ace2.a_who = (uid_t)-1;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   455
	return (memcmp(&ace1, &ace2, sizeof (ace_t)));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   456
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   457
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   458
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   459
aclent_match(void *entry1, void *entry2)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   460
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   461
	aclent_t *aclent1 = (aclent_t *)entry1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   462
	aclent_t *aclent2 = (aclent_t *)entry2;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   463
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   464
	return (memcmp(aclent1, aclent2, sizeof (aclent_t)));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   465
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   466
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   467
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   468
 * Find acl entries in acl that correspond to removeacl.  Search
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   469
 * is started from slot.  The flag argument indicates whether to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   470
 * remove all matches or just the first match.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   471
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   472
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   473
acl_removeentries(acl_t *acl, acl_t *removeacl, int start_slot, int flag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   474
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   475
	int i, j;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   476
	int match;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   477
	int (*acl_match)(void *acl1, void *acl2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   478
	void *acl_entry, *remove_entry;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   479
	void *start;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   480
	int found = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   481
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   482
	if (flag != ACL_REMOVE_ALL && flag != ACL_REMOVE_FIRST)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   483
		flag = ACL_REMOVE_FIRST;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   484
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   485
	if (acl == NULL || removeacl == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   486
		return (EACL_NO_ACL_ENTRY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   487
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   488
	if (acl->acl_type != removeacl->acl_type)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   489
		return (EACL_DIFF_TYPE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   490
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   491
	if (acl->acl_type == ACLENT_T)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   492
		acl_match = aclent_match;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   493
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   494
		acl_match = ace_match;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   495
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   496
	for (i = 0, remove_entry = removeacl->acl_aclp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   497
	    i != removeacl->acl_cnt; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   498
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   499
		j = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   500
		acl_entry = (char *)acl->acl_aclp +
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   501
		    (acl->acl_entry_size * start_slot);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   502
		for (;;) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   503
			match = acl_match(acl_entry, remove_entry);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   504
			if (match == 0)  {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   505
				found++;
8672
1a4716fc81f2 6787376 chmod core dump by SIGBUS while removing ACE recursively
Renaud Manus <Renaud.Manus@Sun.COM>
parents: 7680
diff changeset
   506
1a4716fc81f2 6787376 chmod core dump by SIGBUS while removing ACE recursively
Renaud Manus <Renaud.Manus@Sun.COM>
parents: 7680
diff changeset
   507
				/* avoid memmove if last entry */
1a4716fc81f2 6787376 chmod core dump by SIGBUS while removing ACE recursively
Renaud Manus <Renaud.Manus@Sun.COM>
parents: 7680
diff changeset
   508
				if (acl->acl_cnt == (j + 1)) {
1a4716fc81f2 6787376 chmod core dump by SIGBUS while removing ACE recursively
Renaud Manus <Renaud.Manus@Sun.COM>
parents: 7680
diff changeset
   509
					acl->acl_cnt--;
1a4716fc81f2 6787376 chmod core dump by SIGBUS while removing ACE recursively
Renaud Manus <Renaud.Manus@Sun.COM>
parents: 7680
diff changeset
   510
					break;
1a4716fc81f2 6787376 chmod core dump by SIGBUS while removing ACE recursively
Renaud Manus <Renaud.Manus@Sun.COM>
parents: 7680
diff changeset
   511
				}
1a4716fc81f2 6787376 chmod core dump by SIGBUS while removing ACE recursively
Renaud Manus <Renaud.Manus@Sun.COM>
parents: 7680
diff changeset
   512
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   513
				start = (char *)acl_entry +
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   514
				    acl->acl_entry_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   515
				(void) memmove(acl_entry, start,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   516
				    acl->acl_entry_size *
8672
1a4716fc81f2 6787376 chmod core dump by SIGBUS while removing ACE recursively
Renaud Manus <Renaud.Manus@Sun.COM>
parents: 7680
diff changeset
   517
				    (acl->acl_cnt-- - (j + 1)));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   518
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   519
				if (flag == ACL_REMOVE_FIRST)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   520
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   521
				/*
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   522
				 * List has changed, just continue so this
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   523
				 * slot gets checked with it's new contents.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   524
				 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   525
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   526
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   527
			acl_entry = ((char *)acl_entry + acl->acl_entry_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   528
			if (++j >= acl->acl_cnt) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   529
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   530
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   531
		}
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   532
		remove_entry = (char *)remove_entry + removeacl->acl_entry_size;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   533
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   534
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   535
	return ((found == 0) ? EACL_NO_ACL_ENTRY : 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   536
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   537
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   538
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   539
 * Replace entires entries in acl1 with the corresponding entries
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   540
 * in newentries.  The where argument specifies where to begin
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   541
 * the replacement.  If the where argument is 1 greater than the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   542
 * number of acl entries in acl1 then they are appended.  If the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   543
 * where argument is 2+ greater than the number of acl entries then
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   544
 * EACL_INVALID_SLOT is returned.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   545
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   546
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   547
acl_modifyentries(acl_t *acl1, acl_t *newentries, int where)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   548
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   549
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   550
	int slot;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   551
	int slots_needed;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   552
	int slots_left;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   553
	int newsize;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   554
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   555
	if (acl1 == NULL || newentries == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
		return (EACL_NO_ACL_ENTRY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   557
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   558
	if (where < 0 || where >= acl1->acl_cnt)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   559
		return (EACL_INVALID_SLOT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   560
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   561
	if (acl1->acl_type != newentries->acl_type)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   562
		return (EACL_DIFF_TYPE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   563
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   564
	slot = where;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   565
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   566
	slots_left = acl1->acl_cnt - slot + 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   567
	if (slots_left < newentries->acl_cnt) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
		slots_needed = newentries->acl_cnt - slots_left;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   569
		newsize = (acl1->acl_entry_size * acl1->acl_cnt) +
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
		    (acl1->acl_entry_size * slots_needed);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
		acl1->acl_aclp = realloc(acl1->acl_aclp, newsize);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   572
		if (acl1->acl_aclp == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
			return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   574
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   575
	(void) memcpy((char *)acl1->acl_aclp + (acl1->acl_entry_size * slot),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   576
	    newentries->acl_aclp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   577
	    newentries->acl_entry_size * newentries->acl_cnt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   578
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   579
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   580
	 * Did ACL grow?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   581
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   582
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   583
	if ((slot + newentries->acl_cnt) > acl1->acl_cnt) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   584
		acl1->acl_cnt = slot + newentries->acl_cnt;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   585
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   589
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   590
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   591
 * Add acl2 entries into acl1.  The where argument specifies where
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   592
 * to add the entries.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   593
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   594
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   595
acl_addentries(acl_t *acl1, acl_t *acl2, int where)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   596
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   597
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   598
	int newsize;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   599
	int len;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   600
	void *start;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   601
	void *to;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   602
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   603
	if (acl1 == NULL || acl2 == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   604
		return (EACL_NO_ACL_ENTRY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   605
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   606
	if (acl1->acl_type != acl2->acl_type)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   607
		return (EACL_DIFF_TYPE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   608
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   609
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   610
	 * allow where to specify 1 past last slot for an append operation
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   611
	 * but anything greater is an error.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   612
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   613
	if (where < 0 || where > acl1->acl_cnt)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   614
		return (EACL_INVALID_SLOT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   615
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   616
	newsize = (acl2->acl_entry_size * acl2->acl_cnt) +
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   617
	    (acl1->acl_entry_size * acl1->acl_cnt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   618
	acl1->acl_aclp = realloc(acl1->acl_aclp, newsize);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   619
	if (acl1->acl_aclp == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   620
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   621
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   622
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   623
	 * first push down entries where new ones will be inserted
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   625
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   626
	to = (void *)((char *)acl1->acl_aclp +
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   627
	    ((where + acl2->acl_cnt) * acl1->acl_entry_size));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   628
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   629
	start = (void *)((char *)acl1->acl_aclp +
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   630
	    where * acl1->acl_entry_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   631
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   632
	if (where < acl1->acl_cnt) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   633
		len = (acl1->acl_cnt - where) * acl1->acl_entry_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   634
		(void) memmove(to, start, len);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   635
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   636
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   637
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   638
	 * now stick in new entries.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   639
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   640
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   641
	(void) memmove(start, acl2->acl_aclp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   642
	    acl2->acl_cnt * acl2->acl_entry_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   643
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   644
	acl1->acl_cnt += acl2->acl_cnt;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   645
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   646
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   647
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   648
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   649
 * return text for an ACL error.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   650
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   651
char *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   652
acl_strerror(int errnum)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   653
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   654
	switch (errnum) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   655
	case EACL_GRP_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   656
		return (dgettext(TEXT_DOMAIN,
1420
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   657
		    "There is more than one group or default group entry"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   658
	case EACL_USER_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   659
		return (dgettext(TEXT_DOMAIN,
1420
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   660
		    "There is more than one user or default user entry"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   661
	case EACL_OTHER_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   662
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   663
		    "There is more than one other entry"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   664
	case EACL_CLASS_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   665
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   666
		    "There is more than one mask entry"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   667
	case EACL_DUPLICATE_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   668
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   669
		    "Duplicate user or group entries"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   670
	case EACL_MISS_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   671
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   672
		    "Missing user/group owner, other, mask entry"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
	case EACL_MEM_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   674
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   675
		    "Memory error"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   676
	case EACL_ENTRY_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   677
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   678
		    "Unrecognized entry type"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
	case EACL_INHERIT_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   680
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   681
		    "Invalid inheritance flags"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   682
	case EACL_FLAGS_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   683
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   684
		    "Unrecognized entry flags"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   685
	case EACL_PERM_MASK_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   686
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
		    "Invalid ACL permissions"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   688
	case EACL_COUNT_ERROR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   689
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   690
		    "Invalid ACL count"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   691
	case EACL_INVALID_SLOT:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   693
		    "Invalid ACL entry number specified"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   694
	case EACL_NO_ACL_ENTRY:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   695
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   696
		    "ACL entry doesn't exist"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   697
	case EACL_DIFF_TYPE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   698
		return (dgettext(TEXT_DOMAIN,
10228
ca7b8ea13ef5 6835751 typo in zfs upgrade -v
stephanie scheffler <Stephanie.Scheffler@Sun.COM>
parents: 8672
diff changeset
   699
		    "Different file system ACL types cannot be merged"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   700
	case EACL_INVALID_USER_GROUP:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   701
		return (dgettext(TEXT_DOMAIN, "Invalid user or group"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   702
	case EACL_INVALID_STR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   703
		return (dgettext(TEXT_DOMAIN, "ACL string is invalid"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   704
	case EACL_FIELD_NOT_BLANK:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   705
		return (dgettext(TEXT_DOMAIN, "Field expected to be blank"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   706
	case EACL_INVALID_ACCESS_TYPE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   707
		return (dgettext(TEXT_DOMAIN, "Invalid access type"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   708
	case EACL_UNKNOWN_DATA:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   709
		return (dgettext(TEXT_DOMAIN, "Unrecognized entry"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   710
	case EACL_MISSING_FIELDS:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   711
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   712
		    "ACL specification missing required fields"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   713
	case EACL_INHERIT_NOTDIR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   714
		return (dgettext(TEXT_DOMAIN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   715
		    "Inheritance flags are only allowed on directories"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   716
	case -1:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   717
		return (strerror(errno));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   718
	default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   719
		errno = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   720
		return (dgettext(TEXT_DOMAIN, "Unknown error"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   721
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   722
}
1420
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   723
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   724
extern int yyinteractive;
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   725
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   726
/* PRINTFLIKE1 */
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   727
void
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   728
acl_error(const char *fmt, ...)
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   729
{
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   730
	va_list va;
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   731
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   732
	if (yyinteractive == 0)
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   733
		return;
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   734
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   735
	va_start(va, fmt);
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   736
	(void) vfprintf(stderr, fmt, va);
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   737
	va_end(va);
4cb3c26a37e0 PSARC/2006/043 ls -V
marks
parents: 1231
diff changeset
   738
}
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   739
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   740
int
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   741
sid_to_id(char *sid, boolean_t user, uid_t *id)
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   742
{
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   743
	idmap_handle_t *idmap_hdl = NULL;
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   744
	idmap_get_handle_t *get_hdl = NULL;
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   745
	char *rid_start = NULL;
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   746
	idmap_stat status;
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   747
	char *end;
7680
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   748
	int error = 1;
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   749
	char *domain_start;
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   750
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   751
	if ((domain_start = strchr(sid, '@')) == NULL) {
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   752
		idmap_rid_t rid;
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   753
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   754
		if ((rid_start = strrchr(sid, '-')) == NULL)
7680
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   755
			return (1);
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   756
		*rid_start++ = '\0';
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   757
		errno = 0;
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   758
		rid = strtoul(rid_start--, &end, 10);
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   759
		if (errno == 0 && *end == '\0') {
7680
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   760
			if (idmap_init(&idmap_hdl) == IDMAP_SUCCESS &&
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   761
			    idmap_get_create(idmap_hdl, &get_hdl) ==
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   762
			    IDMAP_SUCCESS) {
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   763
				if (user)
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   764
					error = idmap_get_uidbysid(get_hdl,
7369
2d92536a8d73 6727286 libidmap should cache ID mappings
Julian Pullen <Julian.Pullen@Sun.COM>
parents: 7057
diff changeset
   765
					    sid, rid, IDMAP_REQ_FLG_USE_CACHE,
2d92536a8d73 6727286 libidmap should cache ID mappings
Julian Pullen <Julian.Pullen@Sun.COM>
parents: 7057
diff changeset
   766
					    id, &status);
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   767
				else
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   768
					error = idmap_get_gidbysid(get_hdl,
7369
2d92536a8d73 6727286 libidmap should cache ID mappings
Julian Pullen <Julian.Pullen@Sun.COM>
parents: 7057
diff changeset
   769
					    sid, rid, IDMAP_REQ_FLG_USE_CACHE,
2d92536a8d73 6727286 libidmap should cache ID mappings
Julian Pullen <Julian.Pullen@Sun.COM>
parents: 7057
diff changeset
   770
					    id, &status);
7680
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   771
				if (error == IDMAP_SUCCESS) {
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   772
					error = idmap_get_mappings(get_hdl);
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   773
					if (error == IDMAP_SUCCESS &&
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   774
					    status != IDMAP_SUCCESS)
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   775
						error = 1;
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   776
					else
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   777
						error = 0;
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   778
				}
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   779
			} else {
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   780
				error = 1;
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   781
			}
7680
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   782
			if (get_hdl)
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   783
				idmap_get_destroy(get_hdl);
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   784
			if (idmap_hdl)
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   785
				(void) idmap_fini(idmap_hdl);
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   786
		} else {
7680
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   787
			error = 1;
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   788
		}
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   789
		*rid_start = '-'; /* putback character removed earlier */
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   790
	} else {
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   791
		char *name = sid;
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   792
		*domain_start++ = '\0';
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   793
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   794
		if (user)
7369
2d92536a8d73 6727286 libidmap should cache ID mappings
Julian Pullen <Julian.Pullen@Sun.COM>
parents: 7057
diff changeset
   795
			error = idmap_getuidbywinname(name, domain_start,
2d92536a8d73 6727286 libidmap should cache ID mappings
Julian Pullen <Julian.Pullen@Sun.COM>
parents: 7057
diff changeset
   796
			    IDMAP_REQ_FLG_USE_CACHE, id);
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   797
		else
7369
2d92536a8d73 6727286 libidmap should cache ID mappings
Julian Pullen <Julian.Pullen@Sun.COM>
parents: 7057
diff changeset
   798
			error = idmap_getgidbywinname(name, domain_start,
2d92536a8d73 6727286 libidmap should cache ID mappings
Julian Pullen <Julian.Pullen@Sun.COM>
parents: 7057
diff changeset
   799
			    IDMAP_REQ_FLG_USE_CACHE, id);
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   800
		*--domain_start = '@';
7680
35f0ba2a0120 6749458 acl_totext(ACL_SID_FMT) doesn't handle failure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7369
diff changeset
   801
		error = (error == IDMAP_SUCCESS) ? 0 : 1;
7057
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   802
	}
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   803
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   804
	return (error);
d3fa1d6dbef7 PSARC/2008/342 Further SID support
marks
parents: 5331
diff changeset
   805
}