xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_deleg.c (revision 4787:602d3f97842c)
14543Smarks /*
24543Smarks  * CDDL HEADER START
34543Smarks  *
44543Smarks  * The contents of this file are subject to the terms of the
54543Smarks  * Common Development and Distribution License (the "License").
64543Smarks  * You may not use this file except in compliance with the License.
74543Smarks  *
84543Smarks  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94543Smarks  * or http://www.opensolaris.org/os/licensing.
104543Smarks  * See the License for the specific language governing permissions
114543Smarks  * and limitations under the License.
124543Smarks  *
134543Smarks  * When distributing Covered Code, include this CDDL HEADER in each
144543Smarks  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154543Smarks  * If applicable, add the following below this CDDL HEADER, with the
164543Smarks  * fields enclosed by brackets "[]" replaced with your own identifying
174543Smarks  * information: Portions Copyright [yyyy] [name of copyright owner]
184543Smarks  *
194543Smarks  * CDDL HEADER END
204543Smarks  */
214543Smarks /*
224543Smarks  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
234543Smarks  * Use is subject to license terms.
244543Smarks  */
254543Smarks 
264543Smarks /*
274543Smarks  * DSL permissions are stored in a two level zap attribute
284543Smarks  * mechanism.   The first level identifies the "class" of
294543Smarks  * entry.  The class is identified by the first 2 letters of
304543Smarks  * the attribute.  The second letter "l" or "d" identifies whether
314543Smarks  * it is a local or descendent permission.  The first letter
324543Smarks  * identifies the type of entry.
334543Smarks  *
344543Smarks  * ul$<id>    identifies permssions granted locally for this userid.
354543Smarks  * ud$<id>    identifies permissions granted on descendent datasets for
364543Smarks  *            this userid.
374543Smarks  * Ul$<id>    identifies permission sets granted locally for this userid.
384543Smarks  * Ud$<id>    identifies permission sets granted on descendent datasets for
394543Smarks  *            this userid.
404543Smarks  * gl$<id>    identifies permissions granted locally for this groupid.
414543Smarks  * gd$<id>    identifies permissions granted on descendent datasets for
424543Smarks  *            this groupid.
434543Smarks  * Gl$<id>    identifies permission sets granted locally for this groupid.
444543Smarks  * Gd$<id>    identifies permission sets granted on descendent datasets for
454543Smarks  *            this groupid.
464543Smarks  * el$        identifies permissions granted locally for everyone.
474543Smarks  * ed$        identifies permissions granted on descendent datasets
484543Smarks  *            for everyone.
494543Smarks  * El$        identifies permission sets granted locally for everyone.
504543Smarks  * Ed$        identifies permission sets granted to descendent datasets for
514543Smarks  *            everyone.
524543Smarks  * c-$        identifies permission to create at dataset creation time.
534543Smarks  * C-$        identifies permission sets to grant locally at dataset creation
544543Smarks  *            time.
554543Smarks  * s-$@<name> permissions defined in specified set @<name>
564543Smarks  * S-$@<name> Sets defined in named set @<name>
574543Smarks  *
584543Smarks  * Each of the above entiies points to another zap attribute that contains one
594543Smarks  * attribute for each allowed permission, such as create, destroy,...
604543Smarks  * All of the "upper" case class types will specify permission set names
614543Smarks  * rather than permissions.
624543Smarks  *
634543Smarks  * Basically it looks something like this:
644543Smarks  * ul$12 -> ZAP OBJ -> permissions...
654543Smarks  *
664543Smarks  * The ZAP OBJ is referred to as the jump object.
674543Smarks  */
684543Smarks 
694543Smarks #pragma ident	"%Z%%M%	%I%	%E% SMI"
704543Smarks 
714543Smarks #include <sys/dmu.h>
724543Smarks #include <sys/dmu_objset.h>
734543Smarks #include <sys/dmu_tx.h>
744543Smarks #include <sys/dsl_dataset.h>
754543Smarks #include <sys/dsl_dir.h>
764543Smarks #include <sys/dsl_prop.h>
774543Smarks #include <sys/dsl_synctask.h>
784543Smarks #include <sys/dsl_deleg.h>
794543Smarks #include <sys/spa.h>
804543Smarks #include <sys/spa_impl.h>
814543Smarks #include <sys/zio_checksum.h> /* for the default checksum value */
824543Smarks #include <sys/zap.h>
834543Smarks #include <sys/fs/zfs.h>
844543Smarks #include <sys/cred.h>
854543Smarks #include <sys/sunddi.h>
864543Smarks 
874543Smarks #include "zfs_deleg.h"
884543Smarks 
894543Smarks /*
904543Smarks  * Validate that user is allowed to delegate specified permissions.
914543Smarks  *
92*4787Sahrens  * In order to delegate "create" you must have "create"
934543Smarks  * and "allow".
944543Smarks  */
954543Smarks int
964543Smarks dsl_deleg_can_allow(char *ddname, nvlist_t *nvp, cred_t *cr)
974543Smarks {
984543Smarks 	nvpair_t *whopair = NULL;
99*4787Sahrens 	int error;
1004543Smarks 
101*4787Sahrens 	if ((error = dsl_deleg_access(ddname, ZFS_DELEG_PERM_ALLOW, cr)) != 0)
1024543Smarks 		return (error);
1034543Smarks 
1044543Smarks 	while (whopair = nvlist_next_nvpair(nvp, whopair)) {
1054543Smarks 		nvlist_t *perms;
1064543Smarks 		nvpair_t *permpair = NULL;
1074543Smarks 
1084543Smarks 		VERIFY(nvpair_value_nvlist(whopair, &perms) == 0);
1094543Smarks 
1104543Smarks 		while (permpair = nvlist_next_nvpair(perms, permpair)) {
1114543Smarks 			const char *perm = nvpair_name(permpair);
1124543Smarks 
1134543Smarks 			if (strcmp(perm, ZFS_DELEG_PERM_ALLOW) == 0)
1144543Smarks 				return (EPERM);
1154543Smarks 
116*4787Sahrens 			if ((error = dsl_deleg_access(ddname, perm, cr)) != 0)
1174543Smarks 				return (error);
1184543Smarks 		}
1194543Smarks 	}
120*4787Sahrens 	return (0);
1214543Smarks }
1224543Smarks 
1234543Smarks /*
1244543Smarks  * Validate that user is allowed to unallow specified permissions.  They
1254543Smarks  * must have the 'allow' permission, and even then can only unallow
1264543Smarks  * perms for their uid.
1274543Smarks  */
1284543Smarks int
1294543Smarks dsl_deleg_can_unallow(char *ddname, nvlist_t *nvp, cred_t *cr)
1304543Smarks {
1314543Smarks 	nvpair_t *whopair = NULL;
1324543Smarks 	int error;
133*4787Sahrens 	char idstr[32];
1344543Smarks 
135*4787Sahrens 	if ((error = dsl_deleg_access(ddname, ZFS_DELEG_PERM_ALLOW, cr)) != 0)
1364543Smarks 		return (error);
1374543Smarks 
138*4787Sahrens 	(void) snprintf(idstr, sizeof (idstr), "%lld",
139*4787Sahrens 	    (longlong_t)crgetuid(cr));
140*4787Sahrens 
1414543Smarks 	while (whopair = nvlist_next_nvpair(nvp, whopair)) {
1424543Smarks 		zfs_deleg_who_type_t type = nvpair_name(whopair)[0];
1434543Smarks 
1444543Smarks 		if (type != ZFS_DELEG_USER &&
1454543Smarks 		    type != ZFS_DELEG_USER_SETS)
1464543Smarks 			return (EPERM);
1474543Smarks 
1484543Smarks 		if (strcmp(idstr, &nvpair_name(whopair)[3]) != 0)
1494543Smarks 			return (EPERM);
1504543Smarks 	}
1514543Smarks 	return (0);
1524543Smarks }
1534543Smarks 
1544543Smarks typedef struct {
1554543Smarks 	nvlist_t *p_nvp;
1564543Smarks 	boolean_t p_unset;
1574543Smarks } perm_args_t;
1584543Smarks 
1594543Smarks static void
1604543Smarks dsl_deleg_set_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1614543Smarks {
1624543Smarks 	dsl_dir_t *dd = arg1;
1634543Smarks 	perm_args_t *pa = arg2;
1644543Smarks 	objset_t *mos = dd->dd_pool->dp_meta_objset;
1654543Smarks 	nvpair_t *whopair = NULL;
1664543Smarks 	uint64_t zapobj = dd->dd_phys->dd_deleg_zapobj;
1674543Smarks 
1684543Smarks 	if (zapobj == 0) {
1694543Smarks 		if (pa->p_unset)
1704543Smarks 			return;
1714543Smarks 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
1724543Smarks 		zapobj = dd->dd_phys->dd_deleg_zapobj = zap_create(mos,
1734543Smarks 		    DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
1744543Smarks 	}
1754543Smarks 
1764543Smarks 	while (whopair = nvlist_next_nvpair(pa->p_nvp, whopair)) {
1774543Smarks 		const char *whokey = nvpair_name(whopair);
1784543Smarks 		nvlist_t *perms;
1794543Smarks 		nvpair_t *permpair = NULL;
1804543Smarks 		uint64_t jumpobj;
1814543Smarks 
1824543Smarks 		if (nvpair_value_nvlist(whopair, &perms) != 0) {
183*4787Sahrens 			ASSERT(pa->p_unset);
1844543Smarks 			if (zap_lookup(mos, zapobj, whokey, 8,
1854543Smarks 			    1, &jumpobj) == 0) {
1864543Smarks 				(void) zap_remove(mos, zapobj, whokey, tx);
1874543Smarks 				VERIFY(0 == zap_destroy(mos, jumpobj, tx));
1884543Smarks 			}
1894543Smarks 			spa_history_internal_log(LOG_DS_PERM_WHO_REMOVE,
1904543Smarks 			    dd->dd_pool->dp_spa, tx, cr,
1914543Smarks 			    "%s dataset = %llu", whokey,
1924543Smarks 			    dd->dd_phys->dd_head_dataset_obj);
1934543Smarks 			continue;
1944543Smarks 		}
1954543Smarks 
1964543Smarks 		if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) != 0) {
1974543Smarks 			/*
1984543Smarks 			 * If object doesn't exist and we are removing
1994543Smarks 			 * it, then just continue to next item in nvlist
2004543Smarks 			 */
201*4787Sahrens 			if (pa->p_unset)
2024543Smarks 				continue;
2034543Smarks 			jumpobj = zap_create(mos, DMU_OT_DSL_PERMS,
2044543Smarks 			    DMU_OT_NONE, 0, tx);
2054543Smarks 			VERIFY(zap_update(mos, zapobj,
2064543Smarks 			    whokey, 8, 1, &jumpobj, tx) == 0);
2074543Smarks 		}
2084543Smarks 
2094543Smarks 		while (permpair = nvlist_next_nvpair(perms, permpair)) {
2104543Smarks 			const char *perm = nvpair_name(permpair);
2114543Smarks 			uint64_t n = 0;
2124543Smarks 
2134543Smarks 			if (pa->p_unset) {
2144543Smarks 				(void) zap_remove(mos, jumpobj, perm, tx);
2154543Smarks 				if (zap_count(mos, jumpobj, &n) == 0 && !n) {
2164543Smarks 					(void) zap_remove(mos, zapobj,
2174543Smarks 					    whokey, tx);
2184543Smarks 					VERIFY(0 == zap_destroy(mos,
2194543Smarks 					    jumpobj, tx));
2204543Smarks 				}
2214543Smarks 			} else {
2224543Smarks 				VERIFY(zap_update(mos, jumpobj,
2234543Smarks 				    perm, 8, 1, &n, tx) == 0);
2244543Smarks 			}
2254543Smarks 			spa_history_internal_log((pa->p_unset == B_FALSE) ?
2264543Smarks 			    LOG_DS_PERM_UPDATE : LOG_DS_PERM_REMOVE,
2274543Smarks 			    dd->dd_pool->dp_spa, tx, cr,
2284543Smarks 			    "%s %s dataset = %llu", whokey, perm,
2294543Smarks 			    dd->dd_phys->dd_head_dataset_obj);
2304543Smarks 		}
2314543Smarks 	}
2324543Smarks }
2334543Smarks 
2344543Smarks int
2354543Smarks dsl_deleg_set(const char *ddname, nvlist_t *nvp, boolean_t unset)
2364543Smarks {
2374543Smarks 	dsl_dir_t *dd;
2384543Smarks 	int error;
2394543Smarks 	perm_args_t pa;
2404543Smarks 	nvpair_t *whopair = NULL;
2414543Smarks 	int blocks_modified = 0;
2424543Smarks 
2434543Smarks 	error = dsl_dir_open(ddname, FTAG, &dd, NULL);
2444543Smarks 	if (error)
2454543Smarks 		return (error);
2464543Smarks 
2474543Smarks 	if (spa_version(dmu_objset_spa(dd->dd_pool->dp_meta_objset)) <
2484543Smarks 	    ZFS_VERSION_DELEGATED_PERMS) {
2494543Smarks 		dsl_dir_close(dd, FTAG);
2504543Smarks 		return (ENOTSUP);
2514543Smarks 	}
2524543Smarks 
2534543Smarks 	while (whopair = nvlist_next_nvpair(nvp, whopair))
2544543Smarks 		blocks_modified++;
2554543Smarks 
2564543Smarks 	pa.p_nvp = nvp;
2574543Smarks 	pa.p_unset = unset;
2584543Smarks 
2594543Smarks 	error = dsl_sync_task_do(dd->dd_pool, NULL, dsl_deleg_set_sync,
2604543Smarks 	    dd, &pa, blocks_modified);
2614543Smarks 	dsl_dir_close(dd, FTAG);
2624543Smarks 
2634543Smarks 	return (error);
2644543Smarks }
2654543Smarks 
2664543Smarks /*
2674543Smarks  * Find all 'allow' permissions from a given point and then continue
2684543Smarks  * traversing up to the root.
2694543Smarks  *
2704543Smarks  * This function constructs an nvlist of nvlists.
2714543Smarks  * each setpoint is an nvlist composed of an nvlist of an nvlist
2724543Smarks  * of the individual * users/groups/everyone/create
2734543Smarks  * permissions.
2744543Smarks  *
2754543Smarks  * The nvlist will look like this.
2764543Smarks  *
2774543Smarks  * { source fsname -> { whokeys { permissions,...}, ...}}
2784543Smarks  *
2794543Smarks  * The fsname nvpairs will be arranged in a bottom up order.  For example,
2804543Smarks  * if we have the following structure a/b/c then the nvpairs for the fsnames
2814543Smarks  * will be ordered a/b/c, a/b, a.
2824543Smarks  */
2834543Smarks int
2844543Smarks dsl_deleg_get(const char *ddname, nvlist_t **nvp)
2854543Smarks {
2864543Smarks 	dsl_dir_t *dd, *startdd;
2874543Smarks 	dsl_pool_t *dp;
2884543Smarks 	int error;
2894543Smarks 	objset_t *mos;
2904543Smarks 
2914543Smarks 	error = dsl_dir_open(ddname, FTAG, &startdd, NULL);
2924543Smarks 	if (error)
2934543Smarks 		return (error);
2944543Smarks 
2954543Smarks 	dp = startdd->dd_pool;
2964543Smarks 	mos = dp->dp_meta_objset;
2974543Smarks 
2984543Smarks 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2994543Smarks 
3004543Smarks 	rw_enter(&dp->dp_config_rwlock, RW_READER);
3014543Smarks 	for (dd = startdd; dd != NULL; dd = dd->dd_parent) {
3024543Smarks 		zap_cursor_t basezc;
3034543Smarks 		zap_attribute_t baseza;
3044543Smarks 		nvlist_t *sp_nvp;
3054543Smarks 		uint64_t n;
3064543Smarks 		char source[MAXNAMELEN];
3074543Smarks 
3084543Smarks 		if (dd->dd_phys->dd_deleg_zapobj &&
3094543Smarks 		    (zap_count(mos, dd->dd_phys->dd_deleg_zapobj,
3104543Smarks 		    &n) == 0) && n) {
3114543Smarks 			VERIFY(nvlist_alloc(&sp_nvp,
3124543Smarks 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
3134543Smarks 		} else {
3144543Smarks 			continue;
3154543Smarks 		}
3164543Smarks 
3174543Smarks 		for (zap_cursor_init(&basezc, mos,
3184543Smarks 		    dd->dd_phys->dd_deleg_zapobj);
3194543Smarks 		    zap_cursor_retrieve(&basezc, &baseza) == 0;
3204543Smarks 		    zap_cursor_advance(&basezc)) {
3214543Smarks 			zap_cursor_t zc;
3224543Smarks 			zap_attribute_t za;
3234543Smarks 			nvlist_t *perms_nvp;
3244543Smarks 
3254543Smarks 			ASSERT(baseza.za_integer_length == 8);
3264543Smarks 			ASSERT(baseza.za_num_integers == 1);
3274543Smarks 
3284543Smarks 			VERIFY(nvlist_alloc(&perms_nvp,
3294543Smarks 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
3304543Smarks 			for (zap_cursor_init(&zc, mos, baseza.za_first_integer);
3314543Smarks 			    zap_cursor_retrieve(&zc, &za) == 0;
3324543Smarks 			    zap_cursor_advance(&zc)) {
3334543Smarks 				VERIFY(nvlist_add_boolean(perms_nvp,
3344543Smarks 				    za.za_name) == 0);
3354543Smarks 			}
3364543Smarks 			zap_cursor_fini(&zc);
3374543Smarks 			VERIFY(nvlist_add_nvlist(sp_nvp, baseza.za_name,
3384543Smarks 			    perms_nvp) == 0);
3394543Smarks 			nvlist_free(perms_nvp);
3404543Smarks 		}
3414543Smarks 
3424543Smarks 		zap_cursor_fini(&basezc);
3434543Smarks 
3444543Smarks 		dsl_dir_name(dd, source);
3454543Smarks 		VERIFY(nvlist_add_nvlist(*nvp, source, sp_nvp) == 0);
3464543Smarks 		nvlist_free(sp_nvp);
3474543Smarks 	}
3484543Smarks 	rw_exit(&dp->dp_config_rwlock);
3494543Smarks 
3504543Smarks 	dsl_dir_close(startdd, FTAG);
3514543Smarks 	return (0);
3524543Smarks }
3534543Smarks 
3544543Smarks /*
3554543Smarks  * Routines for dsl_deleg_access() -- access checking.
3564543Smarks  */
3574543Smarks typedef struct perm_set {
3584543Smarks 	avl_node_t	p_node;
359*4787Sahrens 	boolean_t	p_matched;
3604543Smarks 	char		p_setname[ZFS_MAX_DELEG_NAME];
3614543Smarks } perm_set_t;
3624543Smarks 
3634543Smarks static int
3644543Smarks perm_set_compare(const void *arg1, const void *arg2)
3654543Smarks {
3664543Smarks 	const perm_set_t *node1 = arg1;
3674543Smarks 	const perm_set_t *node2 = arg2;
3684543Smarks 	int val;
3694543Smarks 
3704543Smarks 	val = strcmp(node1->p_setname, node2->p_setname);
3714543Smarks 	if (val == 0)
3724543Smarks 		return (0);
3734543Smarks 	return (val > 0 ? 1 : -1);
3744543Smarks }
3754543Smarks 
3764543Smarks /*
3774543Smarks  * Determine whether a specified permission exists.
3784543Smarks  *
3794543Smarks  * First the base attribute has to be retrieved.  i.e. ul$12
3804543Smarks  * Once the base object has been retrieved the actual permission
3814543Smarks  * is lookup up in the zap object the base object points to.
3824543Smarks  *
3834543Smarks  * Return 0 if permission exists, ENOENT if there is no whokey, EPERM if
3844543Smarks  * there is no perm in that jumpobj.
3854543Smarks  */
3864543Smarks static int
3874543Smarks dsl_check_access(objset_t *mos, uint64_t zapobj,
3884543Smarks     char type, char checkflag, void *valp, const char *perm)
3894543Smarks {
3904543Smarks 	int error;
3914543Smarks 	uint64_t jumpobj, zero;
3924543Smarks 	char whokey[ZFS_MAX_DELEG_NAME];
3934543Smarks 
3944543Smarks 	zfs_deleg_whokey(whokey, type, checkflag, valp);
3954543Smarks 	error = zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj);
3964543Smarks 	if (error == 0) {
3974543Smarks 		error = zap_lookup(mos, jumpobj, perm, 8, 1, &zero);
3984543Smarks 		if (error == ENOENT)
3994543Smarks 			error = EPERM;
4004543Smarks 	}
4014543Smarks 	return (error);
4024543Smarks }
4034543Smarks 
4044543Smarks /*
4054543Smarks  * check a specified user/group for a requested permission
4064543Smarks  */
4074543Smarks static int
408*4787Sahrens dsl_check_user_access(objset_t *mos, uint64_t zapobj, const char *perm,
4094543Smarks     int checkflag, cred_t *cr)
4104543Smarks {
4114543Smarks 	const	gid_t *gids;
4124543Smarks 	int	ngids;
4134543Smarks 	int	i;
4144543Smarks 	uint64_t id;
4154543Smarks 
4164543Smarks 	/* check for user */
4174543Smarks 	id = crgetuid(cr);
418*4787Sahrens 	if (dsl_check_access(mos, zapobj,
4194543Smarks 	    ZFS_DELEG_USER, checkflag, &id, perm) == 0)
4204543Smarks 		return (0);
4214543Smarks 
4224543Smarks 	/* check for users primary group */
4234543Smarks 	id = crgetgid(cr);
424*4787Sahrens 	if (dsl_check_access(mos, zapobj,
4254543Smarks 	    ZFS_DELEG_GROUP, checkflag, &id, perm) == 0)
4264543Smarks 		return (0);
4274543Smarks 
4284543Smarks 	/* check for everyone entry */
4294543Smarks 	id = -1;
430*4787Sahrens 	if (dsl_check_access(mos, zapobj,
4314543Smarks 	    ZFS_DELEG_EVERYONE, checkflag, &id, perm) == 0)
4324543Smarks 		return (0);
4334543Smarks 
4344543Smarks 	/* check each supplemental group user is a member of */
4354543Smarks 	ngids = crgetngroups(cr);
4364543Smarks 	gids = crgetgroups(cr);
4374543Smarks 	for (i = 0; i != ngids; i++) {
4384543Smarks 		id = gids[i];
439*4787Sahrens 		if (dsl_check_access(mos, zapobj,
4404543Smarks 		    ZFS_DELEG_GROUP, checkflag, &id, perm) == 0)
4414543Smarks 			return (0);
4424543Smarks 	}
4434543Smarks 
4444543Smarks 	return (EPERM);
4454543Smarks }
4464543Smarks 
4474543Smarks /*
4484543Smarks  * Iterate over the sets specified in the specified zapobj
4494543Smarks  * and load them into the permsets avl tree.
4504543Smarks  */
4514543Smarks static int
4524543Smarks dsl_load_sets(objset_t *mos, uint64_t zapobj,
4534543Smarks     char type, char checkflag, void *valp, avl_tree_t *avl)
4544543Smarks {
4554543Smarks 	zap_cursor_t zc;
4564543Smarks 	zap_attribute_t za;
4574543Smarks 	perm_set_t *permnode;
4584543Smarks 	avl_index_t idx;
4594543Smarks 	uint64_t jumpobj;
4604543Smarks 	int error;
4614543Smarks 	char whokey[ZFS_MAX_DELEG_NAME];
4624543Smarks 
4634543Smarks 	zfs_deleg_whokey(whokey, type, checkflag, valp);
4644543Smarks 
4654543Smarks 	error = zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj);
4664543Smarks 	if (error != 0)
4674543Smarks 		return (error);
4684543Smarks 
4694543Smarks 	for (zap_cursor_init(&zc, mos, jumpobj);
4704543Smarks 	    zap_cursor_retrieve(&zc, &za) == 0;
4714543Smarks 	    zap_cursor_advance(&zc)) {
4724543Smarks 		permnode = kmem_alloc(sizeof (perm_set_t), KM_SLEEP);
4734543Smarks 		(void) strlcpy(permnode->p_setname, za.za_name,
4744543Smarks 		    sizeof (permnode->p_setname));
4754543Smarks 		permnode->p_matched = B_FALSE;
4764543Smarks 
4774543Smarks 		if (avl_find(avl, permnode, &idx) == NULL) {
4784543Smarks 			avl_insert(avl, permnode, idx);
4794543Smarks 		} else {
4804543Smarks 			kmem_free(permnode, sizeof (perm_set_t));
4814543Smarks 		}
4824543Smarks 	}
4834543Smarks 	zap_cursor_fini(&zc);
4844543Smarks 	return (0);
4854543Smarks }
4864543Smarks 
4874543Smarks /*
4884543Smarks  * Load all permissions user based on cred belongs to.
4894543Smarks  */
4904543Smarks static void
4914543Smarks dsl_load_user_sets(objset_t *mos, uint64_t zapobj, avl_tree_t *avl,
4924543Smarks     char checkflag, cred_t *cr)
4934543Smarks {
4944543Smarks 	const	gid_t *gids;
4954543Smarks 	int	ngids, i;
4964543Smarks 	uint64_t id;
4974543Smarks 
4984543Smarks 	id = crgetuid(cr);
4994543Smarks 	(void) dsl_load_sets(mos, zapobj,
5004543Smarks 	    ZFS_DELEG_USER_SETS, checkflag, &id, avl);
5014543Smarks 
5024543Smarks 	id = crgetgid(cr);
5034543Smarks 	(void) dsl_load_sets(mos, zapobj,
5044543Smarks 	    ZFS_DELEG_GROUP_SETS, checkflag, &id, avl);
5054543Smarks 
5064543Smarks 	(void) dsl_load_sets(mos, zapobj,
5074543Smarks 	    ZFS_DELEG_EVERYONE_SETS, checkflag, NULL, avl);
5084543Smarks 
5094543Smarks 	ngids = crgetngroups(cr);
5104543Smarks 	gids = crgetgroups(cr);
5114543Smarks 	for (i = 0; i != ngids; i++) {
5124543Smarks 		id = gids[i];
5134543Smarks 		(void) dsl_load_sets(mos, zapobj,
5144543Smarks 		    ZFS_DELEG_GROUP_SETS, checkflag, &id, avl);
5154543Smarks 	}
5164543Smarks }
5174543Smarks 
5184543Smarks /*
5194543Smarks  * Check if user has requested permission.
5204543Smarks  */
5214543Smarks int
5224543Smarks dsl_deleg_access(const char *ddname, const char *perm, cred_t *cr)
5234543Smarks {
5244543Smarks 	dsl_dir_t *dd, *startdd;
5254543Smarks 	dsl_pool_t *dp;
5264543Smarks 	void *cookie;
5274543Smarks 	int	error;
5284543Smarks 	char	checkflag = ZFS_DELEG_LOCAL;
5294543Smarks 	const char *tail;
5304543Smarks 	objset_t *mos;
5314543Smarks 	avl_tree_t permsets;
5324543Smarks 	perm_set_t *setnode;
5334543Smarks 
5344543Smarks 	/*
5354543Smarks 	 * Use tail so that zfs_ioctl() code doesn't have
5364543Smarks 	 * to always to to figure out parent name in order
5374543Smarks 	 * to do access check.  for example renaming a snapshot
5384543Smarks 	 */
5394543Smarks 	error = dsl_dir_open(ddname, FTAG, &startdd, &tail);
5404543Smarks 	if (error)
5414543Smarks 		return (error);
5424543Smarks 
5434543Smarks 	if (tail && tail[0] != '@') {
5444543Smarks 		dsl_dir_close(startdd, FTAG);
5454543Smarks 		return (ENOENT);
5464543Smarks 	}
5474543Smarks 	dp = startdd->dd_pool;
5484543Smarks 	mos = dp->dp_meta_objset;
5494543Smarks 
5504543Smarks 	if (dsl_delegation_on(mos) == B_FALSE) {
5514543Smarks 		dsl_dir_close(startdd, FTAG);
5524543Smarks 		return (ECANCELED);
5534543Smarks 	}
5544543Smarks 
5554543Smarks 	if (spa_version(dmu_objset_spa(dp->dp_meta_objset)) <
5564543Smarks 	    ZFS_VERSION_DELEGATED_PERMS) {
5574543Smarks 		dsl_dir_close(startdd, FTAG);
5584543Smarks 		return (EPERM);
5594543Smarks 	}
5604543Smarks 
5614543Smarks 	avl_create(&permsets, perm_set_compare, sizeof (perm_set_t),
5624543Smarks 	    offsetof(perm_set_t, p_node));
5634543Smarks 
5644543Smarks 	rw_enter(&dp->dp_config_rwlock, RW_READER);
5654543Smarks 	for (dd = startdd; dd != NULL; dd = dd->dd_parent,
5664543Smarks 	    checkflag = ZFS_DELEG_DESCENDENT) {
5674543Smarks 		uint64_t zapobj;
5684543Smarks 		boolean_t expanded;
5694543Smarks 
5704543Smarks 		/*
5714543Smarks 		 * If not in global zone then make sure
5724543Smarks 		 * the zoned property is set
5734543Smarks 		 */
5744543Smarks 		if (!INGLOBALZONE(curproc)) {
5754543Smarks 			uint64_t zoned;
5764543Smarks 
5774543Smarks 			if (dsl_prop_get_ds_locked(dd,
5784543Smarks 			    zfs_prop_to_name(ZFS_PROP_ZONED),
5794543Smarks 			    8, 1, &zoned, NULL) != 0)
5804543Smarks 				break;
5814543Smarks 			if (!zoned)
5824543Smarks 				break;
5834543Smarks 		}
5844543Smarks 		zapobj = dd->dd_phys->dd_deleg_zapobj;
5854543Smarks 
5864543Smarks 		if (zapobj == 0)
5874543Smarks 			continue;
5884543Smarks 
5894543Smarks 		dsl_load_user_sets(mos, zapobj, &permsets, checkflag, cr);
5904543Smarks again:
5914543Smarks 		expanded = B_FALSE;
5924543Smarks 		for (setnode = avl_first(&permsets); setnode;
5934543Smarks 		    setnode = AVL_NEXT(&permsets, setnode)) {
5944543Smarks 			if (setnode->p_matched == B_TRUE)
5954543Smarks 				continue;
5964543Smarks 
5974543Smarks 			/* See if this set directly grants this permission */
5984543Smarks 			error = dsl_check_access(mos, zapobj,
5994543Smarks 			    ZFS_DELEG_NAMED_SET, 0, setnode->p_setname, perm);
6004543Smarks 			if (error == 0)
6014543Smarks 				goto success;
6024543Smarks 			if (error == EPERM)
6034543Smarks 				setnode->p_matched = B_TRUE;
6044543Smarks 
6054543Smarks 			/* See if this set includes other sets */
6064543Smarks 			error = dsl_load_sets(mos, zapobj,
6074543Smarks 			    ZFS_DELEG_NAMED_SET_SETS, 0,
6084543Smarks 			    setnode->p_setname, &permsets);
6094543Smarks 			if (error == 0)
6104543Smarks 				setnode->p_matched = expanded = B_TRUE;
6114543Smarks 		}
6124543Smarks 		/*
6134543Smarks 		 * If we expanded any sets, that will define more sets,
6144543Smarks 		 * which we need to check.
6154543Smarks 		 */
6164543Smarks 		if (expanded)
6174543Smarks 			goto again;
6184543Smarks 
6194543Smarks 		error = dsl_check_user_access(mos, zapobj, perm, checkflag, cr);
6204543Smarks 		if (error == 0)
6214543Smarks 			goto success;
6224543Smarks 	}
6234543Smarks 	error = EPERM;
6244543Smarks success:
6254543Smarks 	rw_exit(&dp->dp_config_rwlock);
6264543Smarks 	dsl_dir_close(startdd, FTAG);
6274543Smarks 
6284543Smarks 	cookie = NULL;
629*4787Sahrens 	while ((setnode = avl_destroy_nodes(&permsets, &cookie)) != NULL)
6304543Smarks 		kmem_free(setnode, sizeof (perm_set_t));
6314543Smarks 
6324543Smarks 	return (error);
6334543Smarks }
6344543Smarks 
6354543Smarks /*
6364543Smarks  * Other routines.
6374543Smarks  */
6384543Smarks 
6394543Smarks static void
640*4787Sahrens copy_create_perms(dsl_dir_t *dd, uint64_t pzapobj,
6414543Smarks     boolean_t dosets, uint64_t uid, dmu_tx_t *tx)
6424543Smarks {
643*4787Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
6444543Smarks 	uint64_t jumpobj, pjumpobj;
6454543Smarks 	uint64_t zapobj = dd->dd_phys->dd_deleg_zapobj;
6464543Smarks 	zap_cursor_t zc;
6474543Smarks 	zap_attribute_t za;
6484543Smarks 	char whokey[ZFS_MAX_DELEG_NAME];
6494543Smarks 
6504543Smarks 	zfs_deleg_whokey(whokey,
6514543Smarks 	    dosets ? ZFS_DELEG_CREATE_SETS : ZFS_DELEG_CREATE,
6524543Smarks 	    ZFS_DELEG_LOCAL, NULL);
653*4787Sahrens 	if (zap_lookup(mos, pzapobj, whokey, 8, 1, &pjumpobj) != 0)
6544543Smarks 		return;
6554543Smarks 
6564543Smarks 	if (zapobj == 0) {
6574543Smarks 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
6584543Smarks 		zapobj = dd->dd_phys->dd_deleg_zapobj = zap_create(mos,
6594543Smarks 		    DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
6604543Smarks 	}
6614543Smarks 
662*4787Sahrens 	zfs_deleg_whokey(whokey,
663*4787Sahrens 	    dosets ? ZFS_DELEG_USER_SETS : ZFS_DELEG_USER,
664*4787Sahrens 	    ZFS_DELEG_LOCAL, &uid);
6654543Smarks 	if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) == ENOENT) {
6664543Smarks 		jumpobj = zap_create(mos, DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
6674543Smarks 		VERIFY(zap_add(mos, zapobj, whokey, 8, 1, &jumpobj, tx) == 0);
6684543Smarks 	}
6694543Smarks 
6704543Smarks 	for (zap_cursor_init(&zc, mos, pjumpobj);
6714543Smarks 	    zap_cursor_retrieve(&zc, &za) == 0;
6724543Smarks 	    zap_cursor_advance(&zc)) {
673*4787Sahrens 		uint64_t zero = 0;
6744543Smarks 		ASSERT(za.za_integer_length == 8 && za.za_num_integers == 1);
6754543Smarks 
6764543Smarks 		VERIFY(zap_update(mos, jumpobj, za.za_name,
6774543Smarks 		    8, 1, &zero, tx) == 0);
6784543Smarks 	}
6794543Smarks 	zap_cursor_fini(&zc);
6804543Smarks }
6814543Smarks 
6824543Smarks /*
6834543Smarks  * set all create time permission on new dataset.
6844543Smarks  */
6854543Smarks void
6864543Smarks dsl_deleg_set_create_perms(dsl_dir_t *sdd, dmu_tx_t *tx, cred_t *cr)
6874543Smarks {
6884543Smarks 	dsl_dir_t *dd;
689*4787Sahrens 	uint64_t uid = crgetuid(cr);
6904543Smarks 
6914543Smarks 	if (spa_version(dmu_objset_spa(sdd->dd_pool->dp_meta_objset)) <
6924543Smarks 	    ZFS_VERSION_DELEGATED_PERMS)
6934543Smarks 		return;
6944543Smarks 
6954543Smarks 	for (dd = sdd->dd_parent; dd != NULL; dd = dd->dd_parent) {
696*4787Sahrens 		uint64_t pzapobj = dd->dd_phys->dd_deleg_zapobj;
6974543Smarks 
698*4787Sahrens 		if (pzapobj == 0)
6994543Smarks 			continue;
7004543Smarks 
701*4787Sahrens 		copy_create_perms(sdd, pzapobj, B_FALSE, uid, tx);
702*4787Sahrens 		copy_create_perms(sdd, pzapobj, B_TRUE, uid, tx);
7034543Smarks 	}
7044543Smarks }
7054543Smarks 
7064543Smarks int
7074543Smarks dsl_deleg_destroy(objset_t *mos, uint64_t zapobj, dmu_tx_t *tx)
7084543Smarks {
7094543Smarks 	zap_cursor_t zc;
7104543Smarks 	zap_attribute_t za;
7114543Smarks 
7124543Smarks 	if (zapobj == 0)
7134543Smarks 		return (0);
7144543Smarks 
7154543Smarks 	for (zap_cursor_init(&zc, mos, zapobj);
7164543Smarks 	    zap_cursor_retrieve(&zc, &za) == 0;
7174543Smarks 	    zap_cursor_advance(&zc)) {
7184543Smarks 		ASSERT(za.za_integer_length == 8 && za.za_num_integers == 1);
7194543Smarks 		VERIFY(0 == zap_destroy(mos, za.za_first_integer, tx));
7204543Smarks 	}
7214543Smarks 	zap_cursor_fini(&zc);
7224543Smarks 	VERIFY(0 == zap_destroy(mos, zapobj, tx));
7234543Smarks 	return (0);
7244543Smarks }
7254543Smarks 
7264543Smarks boolean_t
7274543Smarks dsl_delegation_on(objset_t *os)
7284543Smarks {
7294543Smarks 	return (os->os->os_spa->spa_delegation);
7304543Smarks }
731