4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
27 * DSL permissions are stored in a two level zap attribute
28 * mechanism. The first level identifies the "class" of
29 * entry. The class is identified by the first 2 letters of
30 * the attribute. The second letter "l" or "d" identifies whether
31 * it is a local or descendent permission. The first letter
32 * identifies the type of entry.
34 * ul$<id> identifies permissions granted locally for this userid.
35 * ud$<id> identifies permissions granted on descendent datasets for
37 * Ul$<id> identifies permission sets granted locally for this userid.
38 * Ud$<id> identifies permission sets granted on descendent datasets for
40 * gl$<id> identifies permissions granted locally for this groupid.
41 * gd$<id> identifies permissions granted on descendent datasets for
43 * Gl$<id> identifies permission sets granted locally for this groupid.
44 * Gd$<id> identifies permission sets granted on descendent datasets for
46 * el$ identifies permissions granted locally for everyone.
47 * ed$ identifies permissions granted on descendent datasets
49 * El$ identifies permission sets granted locally for everyone.
50 * Ed$ identifies permission sets granted to descendent datasets for
52 * c-$ identifies permission to create at dataset creation time.
53 * C-$ identifies permission sets to grant locally at dataset creation
55 * s-$@<name> permissions defined in specified set @<name>
56 * S-$@<name> Sets defined in named set @<name>
58 * Each of the above entities points to another zap attribute that contains one
59 * attribute for each allowed permission, such as create, destroy,...
60 * All of the "upper" case class types will specify permission set names
61 * rather than permissions.
63 * Basically it looks something like this:
64 * ul$12 -> ZAP OBJ -> permissions...
66 * The ZAP OBJ is referred to as the jump object.
70 #include <sys/dmu_objset.h>
71 #include <sys/dmu_tx.h>
72 #include <sys/dsl_dataset.h>
73 #include <sys/dsl_dir.h>
74 #include <sys/dsl_prop.h>
75 #include <sys/dsl_synctask.h>
76 #include <sys/dsl_deleg.h>
79 #include <sys/fs/zfs.h>
81 #include <sys/sunddi.h>
83 #include "zfs_deleg.h"
86 * Validate that user is allowed to delegate specified permissions.
88 * In order to delegate "create" you must have "create"
92 dsl_deleg_can_allow(char *ddname, nvlist_t *nvp, cred_t *cr)
94 nvpair_t *whopair = NULL;
97 if ((error = dsl_deleg_access(ddname, ZFS_DELEG_PERM_ALLOW, cr)) != 0)
100 while (whopair = nvlist_next_nvpair(nvp, whopair)) {
102 nvpair_t *permpair = NULL;
104 VERIFY(nvpair_value_nvlist(whopair, &perms) == 0);
106 while (permpair = nvlist_next_nvpair(perms, permpair)) {
107 const char *perm = nvpair_name(permpair);
109 if (strcmp(perm, ZFS_DELEG_PERM_ALLOW) == 0)
112 if ((error = dsl_deleg_access(ddname, perm, cr)) != 0)
120 * Validate that user is allowed to unallow specified permissions. They
121 * must have the 'allow' permission, and even then can only unallow
122 * perms for their uid.
125 dsl_deleg_can_unallow(char *ddname, nvlist_t *nvp, cred_t *cr)
127 nvpair_t *whopair = NULL;
131 if ((error = dsl_deleg_access(ddname, ZFS_DELEG_PERM_ALLOW, cr)) != 0)
134 (void) snprintf(idstr, sizeof (idstr), "%lld",
135 (longlong_t)crgetuid(cr));
137 while (whopair = nvlist_next_nvpair(nvp, whopair)) {
138 zfs_deleg_who_type_t type = nvpair_name(whopair)[0];
140 if (type != ZFS_DELEG_USER &&
141 type != ZFS_DELEG_USER_SETS)
144 if (strcmp(idstr, &nvpair_name(whopair)[3]) != 0)
151 dsl_deleg_set_sync(void *arg1, void *arg2, dmu_tx_t *tx)
153 dsl_dir_t *dd = arg1;
154 nvlist_t *nvp = arg2;
155 objset_t *mos = dd->dd_pool->dp_meta_objset;
156 nvpair_t *whopair = NULL;
157 uint64_t zapobj = dd->dd_phys->dd_deleg_zapobj;
160 dmu_buf_will_dirty(dd->dd_dbuf, tx);
161 zapobj = dd->dd_phys->dd_deleg_zapobj = zap_create(mos,
162 DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
165 while (whopair = nvlist_next_nvpair(nvp, whopair)) {
166 const char *whokey = nvpair_name(whopair);
168 nvpair_t *permpair = NULL;
171 VERIFY(nvpair_value_nvlist(whopair, &perms) == 0);
173 if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) != 0) {
174 jumpobj = zap_create_link(mos, DMU_OT_DSL_PERMS,
178 while (permpair = nvlist_next_nvpair(perms, permpair)) {
179 const char *perm = nvpair_name(permpair);
182 VERIFY(zap_update(mos, jumpobj,
183 perm, 8, 1, &n, tx) == 0);
184 spa_history_log_internal(LOG_DS_PERM_UPDATE,
185 dd->dd_pool->dp_spa, tx,
186 "%s %s dataset = %llu", whokey, perm,
187 dd->dd_phys->dd_head_dataset_obj);
193 dsl_deleg_unset_sync(void *arg1, void *arg2, dmu_tx_t *tx)
195 dsl_dir_t *dd = arg1;
196 nvlist_t *nvp = arg2;
197 objset_t *mos = dd->dd_pool->dp_meta_objset;
198 nvpair_t *whopair = NULL;
199 uint64_t zapobj = dd->dd_phys->dd_deleg_zapobj;
204 while (whopair = nvlist_next_nvpair(nvp, whopair)) {
205 const char *whokey = nvpair_name(whopair);
207 nvpair_t *permpair = NULL;
210 if (nvpair_value_nvlist(whopair, &perms) != 0) {
211 if (zap_lookup(mos, zapobj, whokey, 8,
213 (void) zap_remove(mos, zapobj, whokey, tx);
214 VERIFY(0 == zap_destroy(mos, jumpobj, tx));
216 spa_history_log_internal(LOG_DS_PERM_WHO_REMOVE,
217 dd->dd_pool->dp_spa, tx,
218 "%s dataset = %llu", whokey,
219 dd->dd_phys->dd_head_dataset_obj);
223 if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) != 0)
226 while (permpair = nvlist_next_nvpair(perms, permpair)) {
227 const char *perm = nvpair_name(permpair);
230 (void) zap_remove(mos, jumpobj, perm, tx);
231 if (zap_count(mos, jumpobj, &n) == 0 && n == 0) {
232 (void) zap_remove(mos, zapobj,
234 VERIFY(0 == zap_destroy(mos,
237 spa_history_log_internal(LOG_DS_PERM_REMOVE,
238 dd->dd_pool->dp_spa, tx,
239 "%s %s dataset = %llu", whokey, perm,
240 dd->dd_phys->dd_head_dataset_obj);
246 dsl_deleg_set(const char *ddname, nvlist_t *nvp, boolean_t unset)
250 nvpair_t *whopair = NULL;
251 int blocks_modified = 0;
253 error = dsl_dir_open(ddname, FTAG, &dd, NULL);
257 if (spa_version(dmu_objset_spa(dd->dd_pool->dp_meta_objset)) <
258 SPA_VERSION_DELEGATED_PERMS) {
259 dsl_dir_close(dd, FTAG);
263 while (whopair = nvlist_next_nvpair(nvp, whopair))
266 error = dsl_sync_task_do(dd->dd_pool, NULL,
267 unset ? dsl_deleg_unset_sync : dsl_deleg_set_sync,
268 dd, nvp, blocks_modified);
269 dsl_dir_close(dd, FTAG);
275 * Find all 'allow' permissions from a given point and then continue
276 * traversing up to the root.
278 * This function constructs an nvlist of nvlists.
279 * each setpoint is an nvlist composed of an nvlist of an nvlist
280 * of the individual * users/groups/everyone/create
283 * The nvlist will look like this.
285 * { source fsname -> { whokeys { permissions,...}, ...}}
287 * The fsname nvpairs will be arranged in a bottom up order. For example,
288 * if we have the following structure a/b/c then the nvpairs for the fsnames
289 * will be ordered a/b/c, a/b, a.
292 dsl_deleg_get(const char *ddname, nvlist_t **nvp)
294 dsl_dir_t *dd, *startdd;
299 error = dsl_dir_open(ddname, FTAG, &startdd, NULL);
303 dp = startdd->dd_pool;
304 mos = dp->dp_meta_objset;
306 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
308 rw_enter(&dp->dp_config_rwlock, RW_READER);
309 for (dd = startdd; dd != NULL; dd = dd->dd_parent) {
311 zap_attribute_t baseza;
314 char source[MAXNAMELEN];
316 if (dd->dd_phys->dd_deleg_zapobj &&
317 (zap_count(mos, dd->dd_phys->dd_deleg_zapobj,
319 VERIFY(nvlist_alloc(&sp_nvp,
320 NV_UNIQUE_NAME, KM_SLEEP) == 0);
325 for (zap_cursor_init(&basezc, mos,
326 dd->dd_phys->dd_deleg_zapobj);
327 zap_cursor_retrieve(&basezc, &baseza) == 0;
328 zap_cursor_advance(&basezc)) {
333 ASSERT(baseza.za_integer_length == 8);
334 ASSERT(baseza.za_num_integers == 1);
336 VERIFY(nvlist_alloc(&perms_nvp,
337 NV_UNIQUE_NAME, KM_SLEEP) == 0);
338 for (zap_cursor_init(&zc, mos, baseza.za_first_integer);
339 zap_cursor_retrieve(&zc, &za) == 0;
340 zap_cursor_advance(&zc)) {
341 VERIFY(nvlist_add_boolean(perms_nvp,
344 zap_cursor_fini(&zc);
345 VERIFY(nvlist_add_nvlist(sp_nvp, baseza.za_name,
347 nvlist_free(perms_nvp);
350 zap_cursor_fini(&basezc);
352 dsl_dir_name(dd, source);
353 VERIFY(nvlist_add_nvlist(*nvp, source, sp_nvp) == 0);
356 rw_exit(&dp->dp_config_rwlock);
358 dsl_dir_close(startdd, FTAG);
363 * Routines for dsl_deleg_access() -- access checking.
365 typedef struct perm_set {
368 char p_setname[ZFS_MAX_DELEG_NAME];
372 perm_set_compare(const void *arg1, const void *arg2)
374 const perm_set_t *node1 = arg1;
375 const perm_set_t *node2 = arg2;
378 val = strcmp(node1->p_setname, node2->p_setname);
381 return (val > 0 ? 1 : -1);
385 * Determine whether a specified permission exists.
387 * First the base attribute has to be retrieved. i.e. ul$12
388 * Once the base object has been retrieved the actual permission
389 * is lookup up in the zap object the base object points to.
391 * Return 0 if permission exists, ENOENT if there is no whokey, EPERM if
392 * there is no perm in that jumpobj.
395 dsl_check_access(objset_t *mos, uint64_t zapobj,
396 char type, char checkflag, void *valp, const char *perm)
399 uint64_t jumpobj, zero;
400 char whokey[ZFS_MAX_DELEG_NAME];
402 zfs_deleg_whokey(whokey, type, checkflag, valp);
403 error = zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj);
405 error = zap_lookup(mos, jumpobj, perm, 8, 1, &zero);
413 * check a specified user/group for a requested permission
416 dsl_check_user_access(objset_t *mos, uint64_t zapobj, const char *perm,
417 int checkflag, cred_t *cr)
426 if (dsl_check_access(mos, zapobj,
427 ZFS_DELEG_USER, checkflag, &id, perm) == 0)
430 /* check for users primary group */
432 if (dsl_check_access(mos, zapobj,
433 ZFS_DELEG_GROUP, checkflag, &id, perm) == 0)
436 /* check for everyone entry */
438 if (dsl_check_access(mos, zapobj,
439 ZFS_DELEG_EVERYONE, checkflag, &id, perm) == 0)
442 /* check each supplemental group user is a member of */
443 ngids = crgetngroups(cr);
444 gids = crgetgroups(cr);
445 for (i = 0; i != ngids; i++) {
447 if (dsl_check_access(mos, zapobj,
448 ZFS_DELEG_GROUP, checkflag, &id, perm) == 0)
456 * Iterate over the sets specified in the specified zapobj
457 * and load them into the permsets avl tree.
460 dsl_load_sets(objset_t *mos, uint64_t zapobj,
461 char type, char checkflag, void *valp, avl_tree_t *avl)
465 perm_set_t *permnode;
469 char whokey[ZFS_MAX_DELEG_NAME];
471 zfs_deleg_whokey(whokey, type, checkflag, valp);
473 error = zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj);
477 for (zap_cursor_init(&zc, mos, jumpobj);
478 zap_cursor_retrieve(&zc, &za) == 0;
479 zap_cursor_advance(&zc)) {
480 permnode = kmem_alloc(sizeof (perm_set_t), KM_SLEEP);
481 (void) strlcpy(permnode->p_setname, za.za_name,
482 sizeof (permnode->p_setname));
483 permnode->p_matched = B_FALSE;
485 if (avl_find(avl, permnode, &idx) == NULL) {
486 avl_insert(avl, permnode, idx);
488 kmem_free(permnode, sizeof (perm_set_t));
491 zap_cursor_fini(&zc);
496 * Load all permissions user based on cred belongs to.
499 dsl_load_user_sets(objset_t *mos, uint64_t zapobj, avl_tree_t *avl,
500 char checkflag, cred_t *cr)
507 (void) dsl_load_sets(mos, zapobj,
508 ZFS_DELEG_USER_SETS, checkflag, &id, avl);
511 (void) dsl_load_sets(mos, zapobj,
512 ZFS_DELEG_GROUP_SETS, checkflag, &id, avl);
514 (void) dsl_load_sets(mos, zapobj,
515 ZFS_DELEG_EVERYONE_SETS, checkflag, NULL, avl);
517 ngids = crgetngroups(cr);
518 gids = crgetgroups(cr);
519 for (i = 0; i != ngids; i++) {
521 (void) dsl_load_sets(mos, zapobj,
522 ZFS_DELEG_GROUP_SETS, checkflag, &id, avl);
527 * Check if user has requested permission. If descendent is set, must have
531 dsl_deleg_access_impl(dsl_dataset_t *ds, boolean_t descendent, const char *perm,
543 dp = ds->ds_dir->dd_pool;
544 mos = dp->dp_meta_objset;
546 if (dsl_delegation_on(mos) == B_FALSE)
549 if (spa_version(dmu_objset_spa(dp->dp_meta_objset)) <
550 SPA_VERSION_DELEGATED_PERMS)
553 if (dsl_dataset_is_snapshot(ds) || descendent) {
555 * Snapshots are treated as descendents only,
556 * local permissions do not apply.
558 checkflag = ZFS_DELEG_DESCENDENT;
560 checkflag = ZFS_DELEG_LOCAL;
563 avl_create(&permsets, perm_set_compare, sizeof (perm_set_t),
564 offsetof(perm_set_t, p_node));
566 rw_enter(&dp->dp_config_rwlock, RW_READER);
567 for (dd = ds->ds_dir; dd != NULL; dd = dd->dd_parent,
568 checkflag = ZFS_DELEG_DESCENDENT) {
573 * If not in global zone then make sure
574 * the zoned property is set
576 if (!INGLOBALZONE(curthread)) {
579 if (dsl_prop_get_dd(dd,
580 zfs_prop_to_name(ZFS_PROP_ZONED),
581 8, 1, &zoned, NULL, B_FALSE) != 0)
586 zapobj = dd->dd_phys->dd_deleg_zapobj;
591 dsl_load_user_sets(mos, zapobj, &permsets, checkflag, cr);
594 for (setnode = avl_first(&permsets); setnode;
595 setnode = AVL_NEXT(&permsets, setnode)) {
596 if (setnode->p_matched == B_TRUE)
599 /* See if this set directly grants this permission */
600 error = dsl_check_access(mos, zapobj,
601 ZFS_DELEG_NAMED_SET, 0, setnode->p_setname, perm);
605 setnode->p_matched = B_TRUE;
607 /* See if this set includes other sets */
608 error = dsl_load_sets(mos, zapobj,
609 ZFS_DELEG_NAMED_SET_SETS, 0,
610 setnode->p_setname, &permsets);
612 setnode->p_matched = expanded = B_TRUE;
615 * If we expanded any sets, that will define more sets,
616 * which we need to check.
621 error = dsl_check_user_access(mos, zapobj, perm, checkflag, cr);
627 rw_exit(&dp->dp_config_rwlock);
630 while ((setnode = avl_destroy_nodes(&permsets, &cookie)) != NULL)
631 kmem_free(setnode, sizeof (perm_set_t));
637 dsl_deleg_access(const char *dsname, const char *perm, cred_t *cr)
642 error = dsl_dataset_hold(dsname, FTAG, &ds);
646 error = dsl_deleg_access_impl(ds, B_FALSE, perm, cr);
647 dsl_dataset_rele(ds, FTAG);
657 copy_create_perms(dsl_dir_t *dd, uint64_t pzapobj,
658 boolean_t dosets, uint64_t uid, dmu_tx_t *tx)
660 objset_t *mos = dd->dd_pool->dp_meta_objset;
661 uint64_t jumpobj, pjumpobj;
662 uint64_t zapobj = dd->dd_phys->dd_deleg_zapobj;
665 char whokey[ZFS_MAX_DELEG_NAME];
667 zfs_deleg_whokey(whokey,
668 dosets ? ZFS_DELEG_CREATE_SETS : ZFS_DELEG_CREATE,
669 ZFS_DELEG_LOCAL, NULL);
670 if (zap_lookup(mos, pzapobj, whokey, 8, 1, &pjumpobj) != 0)
674 dmu_buf_will_dirty(dd->dd_dbuf, tx);
675 zapobj = dd->dd_phys->dd_deleg_zapobj = zap_create(mos,
676 DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
679 zfs_deleg_whokey(whokey,
680 dosets ? ZFS_DELEG_USER_SETS : ZFS_DELEG_USER,
681 ZFS_DELEG_LOCAL, &uid);
682 if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) == ENOENT) {
683 jumpobj = zap_create(mos, DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
684 VERIFY(zap_add(mos, zapobj, whokey, 8, 1, &jumpobj, tx) == 0);
687 for (zap_cursor_init(&zc, mos, pjumpobj);
688 zap_cursor_retrieve(&zc, &za) == 0;
689 zap_cursor_advance(&zc)) {
691 ASSERT(za.za_integer_length == 8 && za.za_num_integers == 1);
693 VERIFY(zap_update(mos, jumpobj, za.za_name,
694 8, 1, &zero, tx) == 0);
696 zap_cursor_fini(&zc);
700 * set all create time permission on new dataset.
703 dsl_deleg_set_create_perms(dsl_dir_t *sdd, dmu_tx_t *tx, cred_t *cr)
706 uint64_t uid = crgetuid(cr);
708 if (spa_version(dmu_objset_spa(sdd->dd_pool->dp_meta_objset)) <
709 SPA_VERSION_DELEGATED_PERMS)
712 for (dd = sdd->dd_parent; dd != NULL; dd = dd->dd_parent) {
713 uint64_t pzapobj = dd->dd_phys->dd_deleg_zapobj;
718 copy_create_perms(sdd, pzapobj, B_FALSE, uid, tx);
719 copy_create_perms(sdd, pzapobj, B_TRUE, uid, tx);
724 dsl_deleg_destroy(objset_t *mos, uint64_t zapobj, dmu_tx_t *tx)
732 for (zap_cursor_init(&zc, mos, zapobj);
733 zap_cursor_retrieve(&zc, &za) == 0;
734 zap_cursor_advance(&zc)) {
735 ASSERT(za.za_integer_length == 8 && za.za_num_integers == 1);
736 VERIFY(0 == zap_destroy(mos, za.za_first_integer, tx));
738 zap_cursor_fini(&zc);
739 VERIFY(0 == zap_destroy(mos, zapobj, tx));
744 dsl_delegation_on(objset_t *os)
746 return (!!spa_delegation(os->os_spa));