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.
26 * DSL permissions are stored in a two level zap attribute
27 * mechanism. The first level identifies the "class" of
28 * entry. The class is identified by the first 2 letters of
29 * the attribute. The second letter "l" or "d" identifies whether
30 * it is a local or descendent permission. The first letter
31 * identifies the type of entry.
33 * ul$<id> identifies permissions granted locally for this userid.
34 * ud$<id> identifies permissions granted on descendent datasets for
36 * Ul$<id> identifies permission sets granted locally for this userid.
37 * Ud$<id> identifies permission sets granted on descendent datasets for
39 * gl$<id> identifies permissions granted locally for this groupid.
40 * gd$<id> identifies permissions granted on descendent datasets for
42 * Gl$<id> identifies permission sets granted locally for this groupid.
43 * Gd$<id> identifies permission sets granted on descendent datasets for
45 * el$ identifies permissions granted locally for everyone.
46 * ed$ identifies permissions granted on descendent datasets
48 * El$ identifies permission sets granted locally for everyone.
49 * Ed$ identifies permission sets granted to descendent datasets for
51 * c-$ identifies permission to create at dataset creation time.
52 * C-$ identifies permission sets to grant locally at dataset creation
54 * s-$@<name> permissions defined in specified set @<name>
55 * S-$@<name> Sets defined in named set @<name>
57 * Each of the above entities points to another zap attribute that contains one
58 * attribute for each allowed permission, such as create, destroy,...
59 * All of the "upper" case class types will specify permission set names
60 * rather than permissions.
62 * Basically it looks something like this:
63 * ul$12 -> ZAP OBJ -> permissions...
65 * The ZAP OBJ is referred to as the jump object.
69 #include <sys/dmu_objset.h>
70 #include <sys/dmu_tx.h>
71 #include <sys/dsl_dataset.h>
72 #include <sys/dsl_dir.h>
73 #include <sys/dsl_prop.h>
74 #include <sys/dsl_synctask.h>
75 #include <sys/dsl_deleg.h>
78 #include <sys/fs/zfs.h>
80 #include <sys/sunddi.h>
82 #include "zfs_deleg.h"
85 * Validate that user is allowed to delegate specified permissions.
87 * In order to delegate "create" you must have "create"
91 dsl_deleg_can_allow(char *ddname
, nvlist_t
*nvp
, cred_t
*cr
)
93 nvpair_t
*whopair
= NULL
;
96 if ((error
= dsl_deleg_access(ddname
, ZFS_DELEG_PERM_ALLOW
, cr
)) != 0)
99 while ((whopair
= nvlist_next_nvpair(nvp
, whopair
))) {
101 nvpair_t
*permpair
= NULL
;
103 VERIFY(nvpair_value_nvlist(whopair
, &perms
) == 0);
105 while ((permpair
= nvlist_next_nvpair(perms
, permpair
))) {
106 const char *perm
= nvpair_name(permpair
);
108 if (strcmp(perm
, ZFS_DELEG_PERM_ALLOW
) == 0)
111 if ((error
= dsl_deleg_access(ddname
, perm
, cr
)) != 0)
119 * Validate that user is allowed to unallow specified permissions. They
120 * must have the 'allow' permission, and even then can only unallow
121 * perms for their uid.
124 dsl_deleg_can_unallow(char *ddname
, nvlist_t
*nvp
, cred_t
*cr
)
126 nvpair_t
*whopair
= NULL
;
130 if ((error
= dsl_deleg_access(ddname
, ZFS_DELEG_PERM_ALLOW
, cr
)) != 0)
133 (void) snprintf(idstr
, sizeof (idstr
), "%lld",
134 (longlong_t
)crgetuid(cr
));
136 while ((whopair
= nvlist_next_nvpair(nvp
, whopair
))) {
137 zfs_deleg_who_type_t type
= nvpair_name(whopair
)[0];
139 if (type
!= ZFS_DELEG_USER
&&
140 type
!= ZFS_DELEG_USER_SETS
)
143 if (strcmp(idstr
, &nvpair_name(whopair
)[3]) != 0)
150 dsl_deleg_set_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
152 dsl_dir_t
*dd
= arg1
;
153 nvlist_t
*nvp
= arg2
;
154 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
155 nvpair_t
*whopair
= NULL
;
156 uint64_t zapobj
= dd
->dd_phys
->dd_deleg_zapobj
;
159 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
160 zapobj
= dd
->dd_phys
->dd_deleg_zapobj
= zap_create(mos
,
161 DMU_OT_DSL_PERMS
, DMU_OT_NONE
, 0, tx
);
164 while ((whopair
= nvlist_next_nvpair(nvp
, whopair
))) {
165 const char *whokey
= nvpair_name(whopair
);
167 nvpair_t
*permpair
= NULL
;
170 VERIFY(nvpair_value_nvlist(whopair
, &perms
) == 0);
172 if (zap_lookup(mos
, zapobj
, whokey
, 8, 1, &jumpobj
) != 0) {
173 jumpobj
= zap_create(mos
, DMU_OT_DSL_PERMS
,
175 VERIFY(zap_update(mos
, zapobj
,
176 whokey
, 8, 1, &jumpobj
, tx
) == 0);
179 while ((permpair
= nvlist_next_nvpair(perms
, permpair
))) {
180 const char *perm
= nvpair_name(permpair
);
183 VERIFY(zap_update(mos
, jumpobj
,
184 perm
, 8, 1, &n
, tx
) == 0);
185 spa_history_log_internal(LOG_DS_PERM_UPDATE
,
186 dd
->dd_pool
->dp_spa
, tx
,
187 "%s %s dataset = %llu", whokey
, perm
,
188 dd
->dd_phys
->dd_head_dataset_obj
);
194 dsl_deleg_unset_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
196 dsl_dir_t
*dd
= arg1
;
197 nvlist_t
*nvp
= arg2
;
198 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
199 nvpair_t
*whopair
= NULL
;
200 uint64_t zapobj
= dd
->dd_phys
->dd_deleg_zapobj
;
205 while ((whopair
= nvlist_next_nvpair(nvp
, whopair
))) {
206 const char *whokey
= nvpair_name(whopair
);
208 nvpair_t
*permpair
= NULL
;
211 if (nvpair_value_nvlist(whopair
, &perms
) != 0) {
212 if (zap_lookup(mos
, zapobj
, whokey
, 8,
214 (void) zap_remove(mos
, zapobj
, whokey
, tx
);
215 VERIFY(0 == zap_destroy(mos
, jumpobj
, tx
));
217 spa_history_log_internal(LOG_DS_PERM_WHO_REMOVE
,
218 dd
->dd_pool
->dp_spa
, tx
,
219 "%s dataset = %llu", whokey
,
220 dd
->dd_phys
->dd_head_dataset_obj
);
224 if (zap_lookup(mos
, zapobj
, whokey
, 8, 1, &jumpobj
) != 0)
227 while ((permpair
= nvlist_next_nvpair(perms
, permpair
))) {
228 const char *perm
= nvpair_name(permpair
);
231 (void) zap_remove(mos
, jumpobj
, perm
, tx
);
232 if (zap_count(mos
, jumpobj
, &n
) == 0 && n
== 0) {
233 (void) zap_remove(mos
, zapobj
,
235 VERIFY(0 == zap_destroy(mos
,
238 spa_history_log_internal(LOG_DS_PERM_REMOVE
,
239 dd
->dd_pool
->dp_spa
, tx
,
240 "%s %s dataset = %llu", whokey
, perm
,
241 dd
->dd_phys
->dd_head_dataset_obj
);
247 dsl_deleg_set(const char *ddname
, nvlist_t
*nvp
, boolean_t unset
)
251 nvpair_t
*whopair
= NULL
;
252 int blocks_modified
= 0;
254 error
= dsl_dir_open(ddname
, FTAG
, &dd
, NULL
);
258 if (spa_version(dmu_objset_spa(dd
->dd_pool
->dp_meta_objset
)) <
259 SPA_VERSION_DELEGATED_PERMS
) {
260 dsl_dir_close(dd
, FTAG
);
264 while ((whopair
= nvlist_next_nvpair(nvp
, whopair
)))
267 error
= dsl_sync_task_do(dd
->dd_pool
, NULL
,
268 unset
? dsl_deleg_unset_sync
: dsl_deleg_set_sync
,
269 dd
, nvp
, blocks_modified
);
270 dsl_dir_close(dd
, FTAG
);
276 * Find all 'allow' permissions from a given point and then continue
277 * traversing up to the root.
279 * This function constructs an nvlist of nvlists.
280 * each setpoint is an nvlist composed of an nvlist of an nvlist
281 * of the individual * users/groups/everyone/create
284 * The nvlist will look like this.
286 * { source fsname -> { whokeys { permissions,...}, ...}}
288 * The fsname nvpairs will be arranged in a bottom up order. For example,
289 * if we have the following structure a/b/c then the nvpairs for the fsnames
290 * will be ordered a/b/c, a/b, a.
293 dsl_deleg_get(const char *ddname
, nvlist_t
**nvp
)
295 dsl_dir_t
*dd
, *startdd
;
299 zap_cursor_t
*basezc
, *zc
;
300 zap_attribute_t
*baseza
, *za
;
303 error
= dsl_dir_open(ddname
, FTAG
, &startdd
, NULL
);
307 dp
= startdd
->dd_pool
;
308 mos
= dp
->dp_meta_objset
;
310 zc
= kmem_alloc(sizeof(zap_cursor_t
), KM_SLEEP
);
311 za
= kmem_alloc(sizeof(zap_attribute_t
), KM_SLEEP
);
312 basezc
= kmem_alloc(sizeof(zap_cursor_t
), KM_SLEEP
);
313 baseza
= kmem_alloc(sizeof(zap_attribute_t
), KM_SLEEP
);
314 source
= kmem_alloc(MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1, KM_SLEEP
);
315 VERIFY(nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
317 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
318 for (dd
= startdd
; dd
!= NULL
; dd
= dd
->dd_parent
) {
322 if (dd
->dd_phys
->dd_deleg_zapobj
&&
323 (zap_count(mos
, dd
->dd_phys
->dd_deleg_zapobj
,
325 VERIFY(nvlist_alloc(&sp_nvp
,
326 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
331 for (zap_cursor_init(basezc
, mos
,
332 dd
->dd_phys
->dd_deleg_zapobj
);
333 zap_cursor_retrieve(basezc
, baseza
) == 0;
334 zap_cursor_advance(basezc
)) {
337 ASSERT(baseza
->za_integer_length
== 8);
338 ASSERT(baseza
->za_num_integers
== 1);
340 VERIFY(nvlist_alloc(&perms_nvp
,
341 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
342 for (zap_cursor_init(zc
, mos
, baseza
->za_first_integer
);
343 zap_cursor_retrieve(zc
, za
) == 0;
344 zap_cursor_advance(zc
)) {
345 VERIFY(nvlist_add_boolean(perms_nvp
,
349 VERIFY(nvlist_add_nvlist(sp_nvp
, baseza
->za_name
,
351 nvlist_free(perms_nvp
);
354 zap_cursor_fini(basezc
);
356 dsl_dir_name(dd
, source
);
357 VERIFY(nvlist_add_nvlist(*nvp
, source
, sp_nvp
) == 0);
360 rw_exit(&dp
->dp_config_rwlock
);
362 kmem_free(source
, MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1);
363 kmem_free(baseza
, sizeof(zap_attribute_t
));
364 kmem_free(basezc
, sizeof(zap_cursor_t
));
365 kmem_free(za
, sizeof(zap_attribute_t
));
366 kmem_free(zc
, sizeof(zap_cursor_t
));
368 dsl_dir_close(startdd
, FTAG
);
373 * Routines for dsl_deleg_access() -- access checking.
375 typedef struct perm_set
{
378 char p_setname
[ZFS_MAX_DELEG_NAME
];
382 perm_set_compare(const void *arg1
, const void *arg2
)
384 const perm_set_t
*node1
= arg1
;
385 const perm_set_t
*node2
= arg2
;
388 val
= strcmp(node1
->p_setname
, node2
->p_setname
);
391 return (val
> 0 ? 1 : -1);
395 * Determine whether a specified permission exists.
397 * First the base attribute has to be retrieved. i.e. ul$12
398 * Once the base object has been retrieved the actual permission
399 * is lookup up in the zap object the base object points to.
401 * Return 0 if permission exists, ENOENT if there is no whokey, EPERM if
402 * there is no perm in that jumpobj.
405 dsl_check_access(objset_t
*mos
, uint64_t zapobj
,
406 char type
, char checkflag
, void *valp
, const char *perm
)
409 uint64_t jumpobj
, zero
;
410 char whokey
[ZFS_MAX_DELEG_NAME
];
412 zfs_deleg_whokey(whokey
, type
, checkflag
, valp
);
413 error
= zap_lookup(mos
, zapobj
, whokey
, 8, 1, &jumpobj
);
415 error
= zap_lookup(mos
, jumpobj
, perm
, 8, 1, &zero
);
423 * check a specified user/group for a requested permission
426 dsl_check_user_access(objset_t
*mos
, uint64_t zapobj
, const char *perm
,
427 int checkflag
, cred_t
*cr
)
436 if (dsl_check_access(mos
, zapobj
,
437 ZFS_DELEG_USER
, checkflag
, &id
, perm
) == 0)
440 /* check for users primary group */
442 if (dsl_check_access(mos
, zapobj
,
443 ZFS_DELEG_GROUP
, checkflag
, &id
, perm
) == 0)
446 /* check for everyone entry */
448 if (dsl_check_access(mos
, zapobj
,
449 ZFS_DELEG_EVERYONE
, checkflag
, &id
, perm
) == 0)
452 /* check each supplemental group user is a member of */
453 ngids
= crgetngroups(cr
);
454 gids
= crgetgroups(cr
);
455 for (i
= 0; i
!= ngids
; i
++) {
457 if (dsl_check_access(mos
, zapobj
,
458 ZFS_DELEG_GROUP
, checkflag
, &id
, perm
) == 0)
466 * Iterate over the sets specified in the specified zapobj
467 * and load them into the permsets avl tree.
470 dsl_load_sets(objset_t
*mos
, uint64_t zapobj
,
471 char type
, char checkflag
, void *valp
, avl_tree_t
*avl
)
475 perm_set_t
*permnode
;
479 char whokey
[ZFS_MAX_DELEG_NAME
];
481 zfs_deleg_whokey(whokey
, type
, checkflag
, valp
);
483 error
= zap_lookup(mos
, zapobj
, whokey
, 8, 1, &jumpobj
);
487 for (zap_cursor_init(&zc
, mos
, jumpobj
);
488 zap_cursor_retrieve(&zc
, &za
) == 0;
489 zap_cursor_advance(&zc
)) {
490 permnode
= kmem_alloc(sizeof (perm_set_t
), KM_SLEEP
);
491 (void) strlcpy(permnode
->p_setname
, za
.za_name
,
492 sizeof (permnode
->p_setname
));
493 permnode
->p_matched
= B_FALSE
;
495 if (avl_find(avl
, permnode
, &idx
) == NULL
) {
496 avl_insert(avl
, permnode
, idx
);
498 kmem_free(permnode
, sizeof (perm_set_t
));
501 zap_cursor_fini(&zc
);
506 * Load all permissions user based on cred belongs to.
509 dsl_load_user_sets(objset_t
*mos
, uint64_t zapobj
, avl_tree_t
*avl
,
510 char checkflag
, cred_t
*cr
)
517 (void) dsl_load_sets(mos
, zapobj
,
518 ZFS_DELEG_USER_SETS
, checkflag
, &id
, avl
);
521 (void) dsl_load_sets(mos
, zapobj
,
522 ZFS_DELEG_GROUP_SETS
, checkflag
, &id
, avl
);
524 (void) dsl_load_sets(mos
, zapobj
,
525 ZFS_DELEG_EVERYONE_SETS
, checkflag
, NULL
, avl
);
527 ngids
= crgetngroups(cr
);
528 gids
= crgetgroups(cr
);
529 for (i
= 0; i
!= ngids
; i
++) {
531 (void) dsl_load_sets(mos
, zapobj
,
532 ZFS_DELEG_GROUP_SETS
, checkflag
, &id
, avl
);
537 * Check if user has requested permission.
540 dsl_deleg_access_impl(dsl_dataset_t
*ds
, const char *perm
, cred_t
*cr
)
551 dp
= ds
->ds_dir
->dd_pool
;
552 mos
= dp
->dp_meta_objset
;
554 if (dsl_delegation_on(mos
) == B_FALSE
)
557 if (spa_version(dmu_objset_spa(dp
->dp_meta_objset
)) <
558 SPA_VERSION_DELEGATED_PERMS
)
561 if (dsl_dataset_is_snapshot(ds
)) {
563 * Snapshots are treated as descendents only,
564 * local permissions do not apply.
566 checkflag
= ZFS_DELEG_DESCENDENT
;
568 checkflag
= ZFS_DELEG_LOCAL
;
571 avl_create(&permsets
, perm_set_compare
, sizeof (perm_set_t
),
572 offsetof(perm_set_t
, p_node
));
574 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
575 for (dd
= ds
->ds_dir
; dd
!= NULL
; dd
= dd
->dd_parent
,
576 checkflag
= ZFS_DELEG_DESCENDENT
) {
581 * If not in global zone then make sure
582 * the zoned property is set
584 if (!INGLOBALZONE(curproc
)) {
587 if (dsl_prop_get_dd(dd
,
588 zfs_prop_to_name(ZFS_PROP_ZONED
),
589 8, 1, &zoned
, NULL
, B_FALSE
) != 0)
594 zapobj
= dd
->dd_phys
->dd_deleg_zapobj
;
599 dsl_load_user_sets(mos
, zapobj
, &permsets
, checkflag
, cr
);
602 for (setnode
= avl_first(&permsets
); setnode
;
603 setnode
= AVL_NEXT(&permsets
, setnode
)) {
604 if (setnode
->p_matched
== B_TRUE
)
607 /* See if this set directly grants this permission */
608 error
= dsl_check_access(mos
, zapobj
,
609 ZFS_DELEG_NAMED_SET
, 0, setnode
->p_setname
, perm
);
613 setnode
->p_matched
= B_TRUE
;
615 /* See if this set includes other sets */
616 error
= dsl_load_sets(mos
, zapobj
,
617 ZFS_DELEG_NAMED_SET_SETS
, 0,
618 setnode
->p_setname
, &permsets
);
620 setnode
->p_matched
= expanded
= B_TRUE
;
623 * If we expanded any sets, that will define more sets,
624 * which we need to check.
629 error
= dsl_check_user_access(mos
, zapobj
, perm
, checkflag
, cr
);
635 rw_exit(&dp
->dp_config_rwlock
);
638 while ((setnode
= avl_destroy_nodes(&permsets
, &cookie
)) != NULL
)
639 kmem_free(setnode
, sizeof (perm_set_t
));
645 dsl_deleg_access(const char *dsname
, const char *perm
, cred_t
*cr
)
650 error
= dsl_dataset_hold(dsname
, FTAG
, &ds
);
654 error
= dsl_deleg_access_impl(ds
, perm
, cr
);
655 dsl_dataset_rele(ds
, FTAG
);
665 copy_create_perms(dsl_dir_t
*dd
, uint64_t pzapobj
,
666 boolean_t dosets
, uint64_t uid
, dmu_tx_t
*tx
)
668 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
669 uint64_t jumpobj
, pjumpobj
;
670 uint64_t zapobj
= dd
->dd_phys
->dd_deleg_zapobj
;
673 char whokey
[ZFS_MAX_DELEG_NAME
];
675 zfs_deleg_whokey(whokey
,
676 dosets
? ZFS_DELEG_CREATE_SETS
: ZFS_DELEG_CREATE
,
677 ZFS_DELEG_LOCAL
, NULL
);
678 if (zap_lookup(mos
, pzapobj
, whokey
, 8, 1, &pjumpobj
) != 0)
682 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
683 zapobj
= dd
->dd_phys
->dd_deleg_zapobj
= zap_create(mos
,
684 DMU_OT_DSL_PERMS
, DMU_OT_NONE
, 0, tx
);
687 zfs_deleg_whokey(whokey
,
688 dosets
? ZFS_DELEG_USER_SETS
: ZFS_DELEG_USER
,
689 ZFS_DELEG_LOCAL
, &uid
);
690 if (zap_lookup(mos
, zapobj
, whokey
, 8, 1, &jumpobj
) == ENOENT
) {
691 jumpobj
= zap_create(mos
, DMU_OT_DSL_PERMS
, DMU_OT_NONE
, 0, tx
);
692 VERIFY(zap_add(mos
, zapobj
, whokey
, 8, 1, &jumpobj
, tx
) == 0);
695 for (zap_cursor_init(&zc
, mos
, pjumpobj
);
696 zap_cursor_retrieve(&zc
, &za
) == 0;
697 zap_cursor_advance(&zc
)) {
699 ASSERT(za
.za_integer_length
== 8 && za
.za_num_integers
== 1);
701 VERIFY(zap_update(mos
, jumpobj
, za
.za_name
,
702 8, 1, &zero
, tx
) == 0);
704 zap_cursor_fini(&zc
);
708 * set all create time permission on new dataset.
711 dsl_deleg_set_create_perms(dsl_dir_t
*sdd
, dmu_tx_t
*tx
, cred_t
*cr
)
714 uint64_t uid
= crgetuid(cr
);
716 if (spa_version(dmu_objset_spa(sdd
->dd_pool
->dp_meta_objset
)) <
717 SPA_VERSION_DELEGATED_PERMS
)
720 for (dd
= sdd
->dd_parent
; dd
!= NULL
; dd
= dd
->dd_parent
) {
721 uint64_t pzapobj
= dd
->dd_phys
->dd_deleg_zapobj
;
726 copy_create_perms(sdd
, pzapobj
, B_FALSE
, uid
, tx
);
727 copy_create_perms(sdd
, pzapobj
, B_TRUE
, uid
, tx
);
732 dsl_deleg_destroy(objset_t
*mos
, uint64_t zapobj
, dmu_tx_t
*tx
)
740 for (zap_cursor_init(&zc
, mos
, zapobj
);
741 zap_cursor_retrieve(&zc
, &za
) == 0;
742 zap_cursor_advance(&zc
)) {
743 ASSERT(za
.za_integer_length
== 8 && za
.za_num_integers
== 1);
744 VERIFY(0 == zap_destroy(mos
, za
.za_first_integer
, tx
));
746 zap_cursor_fini(&zc
);
747 VERIFY(0 == zap_destroy(mos
, zapobj
, tx
));
752 dsl_delegation_on(objset_t
*os
)
754 return (!!spa_delegation(os
->os_spa
));