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;
300 error = dsl_dir_open(ddname, FTAG, &startdd, NULL);
304 dp = startdd->dd_pool;
305 mos = dp->dp_meta_objset;
307 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
309 rw_enter(&dp->dp_config_rwlock, RW_READER);
310 for (dd = startdd; dd != NULL; dd = dd->dd_parent) {
312 zap_attribute_t baseza;
315 char source[MAXNAMELEN];
317 if (dd->dd_phys->dd_deleg_zapobj &&
318 (zap_count(mos, dd->dd_phys->dd_deleg_zapobj,
320 VERIFY(nvlist_alloc(&sp_nvp,
321 NV_UNIQUE_NAME, KM_SLEEP) == 0);
326 for (zap_cursor_init(&basezc, mos,
327 dd->dd_phys->dd_deleg_zapobj);
328 zap_cursor_retrieve(&basezc, &baseza) == 0;
329 zap_cursor_advance(&basezc)) {
334 ASSERT(baseza.za_integer_length == 8);
335 ASSERT(baseza.za_num_integers == 1);
337 VERIFY(nvlist_alloc(&perms_nvp,
338 NV_UNIQUE_NAME, KM_SLEEP) == 0);
339 for (zap_cursor_init(&zc, mos, baseza.za_first_integer);
340 zap_cursor_retrieve(&zc, &za) == 0;
341 zap_cursor_advance(&zc)) {
342 VERIFY(nvlist_add_boolean(perms_nvp,
345 zap_cursor_fini(&zc);
346 VERIFY(nvlist_add_nvlist(sp_nvp, baseza.za_name,
348 nvlist_free(perms_nvp);
351 zap_cursor_fini(&basezc);
353 dsl_dir_name(dd, source);
354 VERIFY(nvlist_add_nvlist(*nvp, source, sp_nvp) == 0);
357 rw_exit(&dp->dp_config_rwlock);
359 dsl_dir_close(startdd, FTAG);
364 * Routines for dsl_deleg_access() -- access checking.
366 typedef struct perm_set {
369 char p_setname[ZFS_MAX_DELEG_NAME];
373 perm_set_compare(const void *arg1, const void *arg2)
375 const perm_set_t *node1 = arg1;
376 const perm_set_t *node2 = arg2;
379 val = strcmp(node1->p_setname, node2->p_setname);
382 return (val > 0 ? 1 : -1);
386 * Determine whether a specified permission exists.
388 * First the base attribute has to be retrieved. i.e. ul$12
389 * Once the base object has been retrieved the actual permission
390 * is lookup up in the zap object the base object points to.
392 * Return 0 if permission exists, ENOENT if there is no whokey, EPERM if
393 * there is no perm in that jumpobj.
396 dsl_check_access(objset_t *mos, uint64_t zapobj,
397 char type, char checkflag, void *valp, const char *perm)
400 uint64_t jumpobj, zero;
401 char whokey[ZFS_MAX_DELEG_NAME];
403 zfs_deleg_whokey(whokey, type, checkflag, valp);
404 error = zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj);
406 error = zap_lookup(mos, jumpobj, perm, 8, 1, &zero);
414 * check a specified user/group for a requested permission
417 dsl_check_user_access(objset_t *mos, uint64_t zapobj, const char *perm,
418 int checkflag, cred_t *cr)
427 if (dsl_check_access(mos, zapobj,
428 ZFS_DELEG_USER, checkflag, &id, perm) == 0)
431 /* check for users primary group */
433 if (dsl_check_access(mos, zapobj,
434 ZFS_DELEG_GROUP, checkflag, &id, perm) == 0)
437 /* check for everyone entry */
439 if (dsl_check_access(mos, zapobj,
440 ZFS_DELEG_EVERYONE, checkflag, &id, perm) == 0)
443 /* check each supplemental group user is a member of */
444 ngids = crgetngroups(cr);
445 gids = crgetgroups(cr);
446 for (i = 0; i != ngids; i++) {
448 if (dsl_check_access(mos, zapobj,
449 ZFS_DELEG_GROUP, checkflag, &id, perm) == 0)
457 * Iterate over the sets specified in the specified zapobj
458 * and load them into the permsets avl tree.
461 dsl_load_sets(objset_t *mos, uint64_t zapobj,
462 char type, char checkflag, void *valp, avl_tree_t *avl)
466 perm_set_t *permnode;
470 char whokey[ZFS_MAX_DELEG_NAME];
472 zfs_deleg_whokey(whokey, type, checkflag, valp);
474 error = zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj);
478 for (zap_cursor_init(&zc, mos, jumpobj);
479 zap_cursor_retrieve(&zc, &za) == 0;
480 zap_cursor_advance(&zc)) {
481 permnode = kmem_alloc(sizeof (perm_set_t), KM_SLEEP);
482 (void) strlcpy(permnode->p_setname, za.za_name,
483 sizeof (permnode->p_setname));
484 permnode->p_matched = B_FALSE;
486 if (avl_find(avl, permnode, &idx) == NULL) {
487 avl_insert(avl, permnode, idx);
489 kmem_free(permnode, sizeof (perm_set_t));
492 zap_cursor_fini(&zc);
497 * Load all permissions user based on cred belongs to.
500 dsl_load_user_sets(objset_t *mos, uint64_t zapobj, avl_tree_t *avl,
501 char checkflag, cred_t *cr)
508 (void) dsl_load_sets(mos, zapobj,
509 ZFS_DELEG_USER_SETS, checkflag, &id, avl);
512 (void) dsl_load_sets(mos, zapobj,
513 ZFS_DELEG_GROUP_SETS, checkflag, &id, avl);
515 (void) dsl_load_sets(mos, zapobj,
516 ZFS_DELEG_EVERYONE_SETS, checkflag, NULL, avl);
518 ngids = crgetngroups(cr);
519 gids = crgetgroups(cr);
520 for (i = 0; i != ngids; i++) {
522 (void) dsl_load_sets(mos, zapobj,
523 ZFS_DELEG_GROUP_SETS, checkflag, &id, avl);
528 * Check if user has requested permission.
531 dsl_deleg_access_impl(dsl_dataset_t *ds, const char *perm, cred_t *cr)
542 dp = ds->ds_dir->dd_pool;
543 mos = dp->dp_meta_objset;
545 if (dsl_delegation_on(mos) == B_FALSE)
548 if (spa_version(dmu_objset_spa(dp->dp_meta_objset)) <
549 SPA_VERSION_DELEGATED_PERMS)
552 if (dsl_dataset_is_snapshot(ds)) {
554 * Snapshots are treated as descendents only,
555 * local permissions do not apply.
557 checkflag = ZFS_DELEG_DESCENDENT;
559 checkflag = ZFS_DELEG_LOCAL;
562 avl_create(&permsets, perm_set_compare, sizeof (perm_set_t),
563 offsetof(perm_set_t, p_node));
565 rw_enter(&dp->dp_config_rwlock, RW_READER);
566 for (dd = ds->ds_dir; dd != NULL; dd = dd->dd_parent,
567 checkflag = ZFS_DELEG_DESCENDENT) {
572 * If not in global zone then make sure
573 * the zoned property is set
575 if (!INGLOBALZONE(curthread)) {
578 if (dsl_prop_get_dd(dd,
579 zfs_prop_to_name(ZFS_PROP_ZONED),
580 8, 1, &zoned, NULL, B_FALSE) != 0)
585 zapobj = dd->dd_phys->dd_deleg_zapobj;
590 dsl_load_user_sets(mos, zapobj, &permsets, checkflag, cr);
593 for (setnode = avl_first(&permsets); setnode;
594 setnode = AVL_NEXT(&permsets, setnode)) {
595 if (setnode->p_matched == B_TRUE)
598 /* See if this set directly grants this permission */
599 error = dsl_check_access(mos, zapobj,
600 ZFS_DELEG_NAMED_SET, 0, setnode->p_setname, perm);
604 setnode->p_matched = B_TRUE;
606 /* See if this set includes other sets */
607 error = dsl_load_sets(mos, zapobj,
608 ZFS_DELEG_NAMED_SET_SETS, 0,
609 setnode->p_setname, &permsets);
611 setnode->p_matched = expanded = B_TRUE;
614 * If we expanded any sets, that will define more sets,
615 * which we need to check.
620 error = dsl_check_user_access(mos, zapobj, perm, checkflag, cr);
626 rw_exit(&dp->dp_config_rwlock);
629 while ((setnode = avl_destroy_nodes(&permsets, &cookie)) != NULL)
630 kmem_free(setnode, sizeof (perm_set_t));
636 dsl_deleg_access(const char *dsname, const char *perm, cred_t *cr)
641 error = dsl_dataset_hold(dsname, FTAG, &ds);
645 error = dsl_deleg_access_impl(ds, perm, cr);
646 dsl_dataset_rele(ds, FTAG);
656 copy_create_perms(dsl_dir_t *dd, uint64_t pzapobj,
657 boolean_t dosets, uint64_t uid, dmu_tx_t *tx)
659 objset_t *mos = dd->dd_pool->dp_meta_objset;
660 uint64_t jumpobj, pjumpobj;
661 uint64_t zapobj = dd->dd_phys->dd_deleg_zapobj;
664 char whokey[ZFS_MAX_DELEG_NAME];
666 zfs_deleg_whokey(whokey,
667 dosets ? ZFS_DELEG_CREATE_SETS : ZFS_DELEG_CREATE,
668 ZFS_DELEG_LOCAL, NULL);
669 if (zap_lookup(mos, pzapobj, whokey, 8, 1, &pjumpobj) != 0)
673 dmu_buf_will_dirty(dd->dd_dbuf, tx);
674 zapobj = dd->dd_phys->dd_deleg_zapobj = zap_create(mos,
675 DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
678 zfs_deleg_whokey(whokey,
679 dosets ? ZFS_DELEG_USER_SETS : ZFS_DELEG_USER,
680 ZFS_DELEG_LOCAL, &uid);
681 if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) == ENOENT) {
682 jumpobj = zap_create(mos, DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
683 VERIFY(zap_add(mos, zapobj, whokey, 8, 1, &jumpobj, tx) == 0);
686 for (zap_cursor_init(&zc, mos, pjumpobj);
687 zap_cursor_retrieve(&zc, &za) == 0;
688 zap_cursor_advance(&zc)) {
690 ASSERT(za.za_integer_length == 8 && za.za_num_integers == 1);
692 VERIFY(zap_update(mos, jumpobj, za.za_name,
693 8, 1, &zero, tx) == 0);
695 zap_cursor_fini(&zc);
699 * set all create time permission on new dataset.
702 dsl_deleg_set_create_perms(dsl_dir_t *sdd, dmu_tx_t *tx, cred_t *cr)
705 uint64_t uid = crgetuid(cr);
707 if (spa_version(dmu_objset_spa(sdd->dd_pool->dp_meta_objset)) <
708 SPA_VERSION_DELEGATED_PERMS)
711 for (dd = sdd->dd_parent; dd != NULL; dd = dd->dd_parent) {
712 uint64_t pzapobj = dd->dd_phys->dd_deleg_zapobj;
717 copy_create_perms(sdd, pzapobj, B_FALSE, uid, tx);
718 copy_create_perms(sdd, pzapobj, B_TRUE, uid, tx);
723 dsl_deleg_destroy(objset_t *mos, uint64_t zapobj, dmu_tx_t *tx)
731 for (zap_cursor_init(&zc, mos, zapobj);
732 zap_cursor_retrieve(&zc, &za) == 0;
733 zap_cursor_advance(&zc)) {
734 ASSERT(za.za_integer_length == 8 && za.za_num_integers == 1);
735 VERIFY(0 == zap_destroy(mos, za.za_first_integer, tx));
737 zap_cursor_fini(&zc);
738 VERIFY(0 == zap_destroy(mos, zapobj, tx));
743 dsl_delegation_on(objset_t *os)
745 return (!!spa_delegation(os->os_spa));