1ecd6cf80Smarks /*
2ecd6cf80Smarks * CDDL HEADER START
3ecd6cf80Smarks *
4ecd6cf80Smarks * The contents of this file are subject to the terms of the
5ecd6cf80Smarks * Common Development and Distribution License (the "License").
6ecd6cf80Smarks * You may not use this file except in compliance with the License.
7ecd6cf80Smarks *
8ecd6cf80Smarks * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9ecd6cf80Smarks * or http://www.opensolaris.org/os/licensing.
10ecd6cf80Smarks * See the License for the specific language governing permissions
11ecd6cf80Smarks * and limitations under the License.
12ecd6cf80Smarks *
13ecd6cf80Smarks * When distributing Covered Code, include this CDDL HEADER in each
14ecd6cf80Smarks * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15ecd6cf80Smarks * If applicable, add the following below this CDDL HEADER, with the
16ecd6cf80Smarks * fields enclosed by brackets "[]" replaced with your own identifying
17ecd6cf80Smarks * information: Portions Copyright [yyyy] [name of copyright owner]
18ecd6cf80Smarks *
19ecd6cf80Smarks * CDDL HEADER END
20ecd6cf80Smarks */
21ecd6cf80Smarks /*
2299d5e173STim Haley * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
231af68beaSAlexander Stetsenko * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
24*78f17100SMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved.
25ecd6cf80Smarks */
26ecd6cf80Smarks
27*78f17100SMatthew Ahrens #include <sys/zfs_context.h>
28*78f17100SMatthew Ahrens
29ecd6cf80Smarks #if defined(_KERNEL)
30ecd6cf80Smarks #include <sys/systm.h>
31ecd6cf80Smarks #include <sys/sunddi.h>
32ecd6cf80Smarks #include <sys/ctype.h>
33ecd6cf80Smarks #else
34ecd6cf80Smarks #include <stdio.h>
35ecd6cf80Smarks #include <unistd.h>
36ecd6cf80Smarks #include <strings.h>
37ecd6cf80Smarks #include <libnvpair.h>
38ecd6cf80Smarks #include <ctype.h>
39ecd6cf80Smarks #endif
40ecd6cf80Smarks #include <sys/dsl_deleg.h>
4191ebeef5Sahrens #include "zfs_prop.h"
42ecd6cf80Smarks #include "zfs_deleg.h"
43ecd6cf80Smarks #include "zfs_namecheck.h"
44ecd6cf80Smarks
456949a980Smarks zfs_deleg_perm_tab_t zfs_deleg_perm_tab[] = {
46*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_ALLOW},
47*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_BOOKMARK},
48*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_CLONE},
49*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_CREATE},
50*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_DESTROY},
51*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_DIFF},
52*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_MOUNT},
53*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_PROMOTE},
54*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_RECEIVE},
55*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_RENAME},
56*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_ROLLBACK},
57*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_SNAPSHOT},
58*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_SHARE},
59*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_SEND},
60*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_USERPROP},
61*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_USERQUOTA},
62*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_GROUPQUOTA},
63*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_USERUSED},
64*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_GROUPUSED},
65*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_HOLD},
66*78f17100SMatthew Ahrens {ZFS_DELEG_PERM_RELEASE},
67*78f17100SMatthew Ahrens {NULL}
68ecd6cf80Smarks };
69ecd6cf80Smarks
70ecd6cf80Smarks static int
zfs_valid_permission_name(const char * perm)7191ebeef5Sahrens zfs_valid_permission_name(const char *perm)
72ecd6cf80Smarks {
7391ebeef5Sahrens if (zfs_deleg_canonicalize_perm(perm))
74ecd6cf80Smarks return (0);
75ecd6cf80Smarks
76ecd6cf80Smarks return (permset_namecheck(perm, NULL, NULL));
77ecd6cf80Smarks }
78ecd6cf80Smarks
7991ebeef5Sahrens const char *
zfs_deleg_canonicalize_perm(const char * perm)8091ebeef5Sahrens zfs_deleg_canonicalize_perm(const char *perm)
8191ebeef5Sahrens {
8291ebeef5Sahrens int i;
8391ebeef5Sahrens zfs_prop_t prop;
8491ebeef5Sahrens
856949a980Smarks for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
866949a980Smarks if (strcmp(perm, zfs_deleg_perm_tab[i].z_perm) == 0)
8791ebeef5Sahrens return (perm);
8891ebeef5Sahrens }
8991ebeef5Sahrens
9091ebeef5Sahrens prop = zfs_name_to_prop(perm);
91990b4856Slling if (prop != ZPROP_INVAL && zfs_prop_delegatable(prop))
9291ebeef5Sahrens return (zfs_prop_to_name(prop));
9391ebeef5Sahrens return (NULL);
9491ebeef5Sahrens
9591ebeef5Sahrens }
9691ebeef5Sahrens
97ecd6cf80Smarks static int
zfs_validate_who(char * who)98ecd6cf80Smarks zfs_validate_who(char *who)
99ecd6cf80Smarks {
100ecd6cf80Smarks char *p;
101ecd6cf80Smarks
10291ebeef5Sahrens if (who[2] != ZFS_DELEG_FIELD_SEP_CHR)
10391ebeef5Sahrens return (-1);
10491ebeef5Sahrens
10591ebeef5Sahrens switch (who[0]) {
106ecd6cf80Smarks case ZFS_DELEG_USER:
107ecd6cf80Smarks case ZFS_DELEG_GROUP:
108ecd6cf80Smarks case ZFS_DELEG_USER_SETS:
109ecd6cf80Smarks case ZFS_DELEG_GROUP_SETS:
11091ebeef5Sahrens if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
11191ebeef5Sahrens return (-1);
11291ebeef5Sahrens for (p = &who[3]; *p; p++)
11391ebeef5Sahrens if (!isdigit(*p))
11491ebeef5Sahrens return (-1);
115ecd6cf80Smarks break;
116ecd6cf80Smarks
117ecd6cf80Smarks case ZFS_DELEG_NAMED_SET:
118ecd6cf80Smarks case ZFS_DELEG_NAMED_SET_SETS:
11991ebeef5Sahrens if (who[1] != ZFS_DELEG_NA)
12091ebeef5Sahrens return (-1);
12191ebeef5Sahrens return (permset_namecheck(&who[3], NULL, NULL));
122ecd6cf80Smarks
123ecd6cf80Smarks case ZFS_DELEG_CREATE:
124ecd6cf80Smarks case ZFS_DELEG_CREATE_SETS:
12591ebeef5Sahrens if (who[1] != ZFS_DELEG_NA)
12691ebeef5Sahrens return (-1);
12791ebeef5Sahrens if (who[3] != '\0')
12891ebeef5Sahrens return (-1);
12991ebeef5Sahrens break;
13091ebeef5Sahrens
131ecd6cf80Smarks case ZFS_DELEG_EVERYONE:
132ecd6cf80Smarks case ZFS_DELEG_EVERYONE_SETS:
13391ebeef5Sahrens if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
13491ebeef5Sahrens return (-1);
135ecd6cf80Smarks if (who[3] != '\0')
136ecd6cf80Smarks return (-1);
137ecd6cf80Smarks break;
13891ebeef5Sahrens
139ecd6cf80Smarks default:
140ecd6cf80Smarks return (-1);
141ecd6cf80Smarks }
14291ebeef5Sahrens
143ecd6cf80Smarks return (0);
144ecd6cf80Smarks }
145ecd6cf80Smarks
146ecd6cf80Smarks int
zfs_deleg_verify_nvlist(nvlist_t * nvp)147ecd6cf80Smarks zfs_deleg_verify_nvlist(nvlist_t *nvp)
148ecd6cf80Smarks {
149ecd6cf80Smarks nvpair_t *who, *perm_name;
150ecd6cf80Smarks nvlist_t *perms;
151ecd6cf80Smarks int error;
152ecd6cf80Smarks
153ecd6cf80Smarks if (nvp == NULL)
154ecd6cf80Smarks return (-1);
155ecd6cf80Smarks
156ecd6cf80Smarks who = nvlist_next_nvpair(nvp, NULL);
157ecd6cf80Smarks if (who == NULL)
158ecd6cf80Smarks return (-1);
159ecd6cf80Smarks
160ecd6cf80Smarks do {
161ecd6cf80Smarks if (zfs_validate_who(nvpair_name(who)))
162ecd6cf80Smarks return (-1);
163ecd6cf80Smarks
164ecd6cf80Smarks error = nvlist_lookup_nvlist(nvp, nvpair_name(who), &perms);
165ecd6cf80Smarks
166ecd6cf80Smarks if (error && error != ENOENT)
167ecd6cf80Smarks return (-1);
168ecd6cf80Smarks if (error == ENOENT)
169ecd6cf80Smarks continue;
170ecd6cf80Smarks
171ecd6cf80Smarks perm_name = nvlist_next_nvpair(perms, NULL);
172ecd6cf80Smarks if (perm_name == NULL) {
173ecd6cf80Smarks return (-1);
174ecd6cf80Smarks }
175ecd6cf80Smarks do {
176ecd6cf80Smarks error = zfs_valid_permission_name(
177ecd6cf80Smarks nvpair_name(perm_name));
17891ebeef5Sahrens if (error)
179ecd6cf80Smarks return (-1);
180ecd6cf80Smarks } while (perm_name = nvlist_next_nvpair(perms, perm_name));
181ecd6cf80Smarks } while (who = nvlist_next_nvpair(nvp, who));
182ecd6cf80Smarks return (0);
183ecd6cf80Smarks }
184ecd6cf80Smarks
185ecd6cf80Smarks /*
186ecd6cf80Smarks * Construct the base attribute name. The base attribute names
187ecd6cf80Smarks * are the "key" to locate the jump objects which contain the actual
188ecd6cf80Smarks * permissions. The base attribute names are encoded based on
189ecd6cf80Smarks * type of entry and whether it is a local or descendent permission.
190ecd6cf80Smarks *
191ecd6cf80Smarks * Arguments:
192ecd6cf80Smarks * attr - attribute name return string, attribute is assumed to be
193ecd6cf80Smarks * ZFS_MAX_DELEG_NAME long.
194ecd6cf80Smarks * type - type of entry to construct
195ecd6cf80Smarks * inheritchr - inheritance type (local,descendent, or NA for create and
196ecd6cf80Smarks * permission set definitions
197ecd6cf80Smarks * data - is either a permission set name or a 64 bit uid/gid.
198ecd6cf80Smarks */
199ecd6cf80Smarks void
zfs_deleg_whokey(char * attr,zfs_deleg_who_type_t type,char inheritchr,void * data)20091ebeef5Sahrens zfs_deleg_whokey(char *attr, zfs_deleg_who_type_t type,
20191ebeef5Sahrens char inheritchr, void *data)
202ecd6cf80Smarks {
203ecd6cf80Smarks int len = ZFS_MAX_DELEG_NAME;
204ecd6cf80Smarks uint64_t *id = data;
205ecd6cf80Smarks
206ecd6cf80Smarks switch (type) {
207ecd6cf80Smarks case ZFS_DELEG_USER:
208ecd6cf80Smarks case ZFS_DELEG_GROUP:
209ecd6cf80Smarks case ZFS_DELEG_USER_SETS:
210ecd6cf80Smarks case ZFS_DELEG_GROUP_SETS:
211ecd6cf80Smarks (void) snprintf(attr, len, "%c%c%c%lld", type, inheritchr,
212ecd6cf80Smarks ZFS_DELEG_FIELD_SEP_CHR, (longlong_t)*id);
213ecd6cf80Smarks break;
214ecd6cf80Smarks case ZFS_DELEG_NAMED_SET_SETS:
215ecd6cf80Smarks case ZFS_DELEG_NAMED_SET:
216ecd6cf80Smarks (void) snprintf(attr, len, "%c-%c%s", type,
217ecd6cf80Smarks ZFS_DELEG_FIELD_SEP_CHR, (char *)data);
218ecd6cf80Smarks break;
219ecd6cf80Smarks case ZFS_DELEG_CREATE:
220ecd6cf80Smarks case ZFS_DELEG_CREATE_SETS:
221ecd6cf80Smarks (void) snprintf(attr, len, "%c-%c", type,
222ecd6cf80Smarks ZFS_DELEG_FIELD_SEP_CHR);
223ecd6cf80Smarks break;
22491ebeef5Sahrens case ZFS_DELEG_EVERYONE:
22591ebeef5Sahrens case ZFS_DELEG_EVERYONE_SETS:
226ecd6cf80Smarks (void) snprintf(attr, len, "%c%c%c", type, inheritchr,
227ecd6cf80Smarks ZFS_DELEG_FIELD_SEP_CHR);
22891ebeef5Sahrens break;
22991ebeef5Sahrens default:
23091ebeef5Sahrens ASSERT(!"bad zfs_deleg_who_type_t");
231ecd6cf80Smarks }
232ecd6cf80Smarks }
233