xref: /titanic_51/usr/src/common/zfs/zfs_deleg.c (revision 990b4856d0eaada6f8140335733a1b1771ed2746)
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 /*
22ecd6cf80Smarks  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23ecd6cf80Smarks  * Use is subject to license terms.
24ecd6cf80Smarks  */
25ecd6cf80Smarks 
26ecd6cf80Smarks 
27ecd6cf80Smarks #pragma ident	"%Z%%M%	%I%	%E% SMI"
28ecd6cf80Smarks 
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
4091ebeef5Sahrens /* XXX includes zfs_context.h, so why bother with the above? */
41ecd6cf80Smarks #include <sys/dsl_deleg.h>
4291ebeef5Sahrens #include "zfs_prop.h"
43ecd6cf80Smarks #include "zfs_deleg.h"
44ecd6cf80Smarks #include "zfs_namecheck.h"
45ecd6cf80Smarks 
46ecd6cf80Smarks /*
47ecd6cf80Smarks  * permission table
48ecd6cf80Smarks  */
49ecd6cf80Smarks 
50ecd6cf80Smarks char *zfs_deleg_perm_tab[] = {
51ecd6cf80Smarks 	ZFS_DELEG_PERM_CREATE,
52ecd6cf80Smarks 	ZFS_DELEG_PERM_DESTROY,
53ecd6cf80Smarks 	ZFS_DELEG_PERM_SNAPSHOT,
54ecd6cf80Smarks 	ZFS_DELEG_PERM_ROLLBACK,
55ecd6cf80Smarks 	ZFS_DELEG_PERM_CLONE,
56ecd6cf80Smarks 	ZFS_DELEG_PERM_PROMOTE,
57ecd6cf80Smarks 	ZFS_DELEG_PERM_RENAME,
58ecd6cf80Smarks 	ZFS_DELEG_PERM_MOUNT,
59ecd6cf80Smarks 	ZFS_DELEG_PERM_SHARE,
60ecd6cf80Smarks 	ZFS_DELEG_PERM_SEND,
61ecd6cf80Smarks 	ZFS_DELEG_PERM_RECEIVE,
62ecd6cf80Smarks 	ZFS_DELEG_PERM_ALLOW,
63ecd6cf80Smarks 	ZFS_DELEG_PERM_USERPROP,
64ecd6cf80Smarks 	NULL
65ecd6cf80Smarks };
66ecd6cf80Smarks 
67ecd6cf80Smarks static int
6891ebeef5Sahrens zfs_valid_permission_name(const char *perm)
69ecd6cf80Smarks {
7091ebeef5Sahrens 	if (zfs_deleg_canonicalize_perm(perm))
71ecd6cf80Smarks 		return (0);
72ecd6cf80Smarks 
73ecd6cf80Smarks 	return (permset_namecheck(perm, NULL, NULL));
74ecd6cf80Smarks }
75ecd6cf80Smarks 
7691ebeef5Sahrens const char *
7791ebeef5Sahrens zfs_deleg_canonicalize_perm(const char *perm)
7891ebeef5Sahrens {
7991ebeef5Sahrens 	int i;
8091ebeef5Sahrens 	zfs_prop_t prop;
8191ebeef5Sahrens 
8291ebeef5Sahrens 	for (i = 0; zfs_deleg_perm_tab[i] != NULL; i++) {
8391ebeef5Sahrens 		if (strcmp(perm, zfs_deleg_perm_tab[i]) == 0)
8491ebeef5Sahrens 			return (perm);
8591ebeef5Sahrens 	}
8691ebeef5Sahrens 
8791ebeef5Sahrens 	prop = zfs_name_to_prop(perm);
88*990b4856Slling 	if (prop != ZPROP_INVAL && zfs_prop_delegatable(prop))
8991ebeef5Sahrens 		return (zfs_prop_to_name(prop));
9091ebeef5Sahrens 	return (NULL);
9191ebeef5Sahrens 
9291ebeef5Sahrens }
9391ebeef5Sahrens 
94ecd6cf80Smarks static int
95ecd6cf80Smarks zfs_validate_who(char *who)
96ecd6cf80Smarks {
97ecd6cf80Smarks 	char *p;
98ecd6cf80Smarks 
9991ebeef5Sahrens 	if (who[2] != ZFS_DELEG_FIELD_SEP_CHR)
10091ebeef5Sahrens 		return (-1);
10191ebeef5Sahrens 
10291ebeef5Sahrens 	switch (who[0]) {
103ecd6cf80Smarks 	case ZFS_DELEG_USER:
104ecd6cf80Smarks 	case ZFS_DELEG_GROUP:
105ecd6cf80Smarks 	case ZFS_DELEG_USER_SETS:
106ecd6cf80Smarks 	case ZFS_DELEG_GROUP_SETS:
10791ebeef5Sahrens 		if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
10891ebeef5Sahrens 			return (-1);
10991ebeef5Sahrens 		for (p = &who[3]; *p; p++)
11091ebeef5Sahrens 			if (!isdigit(*p))
11191ebeef5Sahrens 				return (-1);
112ecd6cf80Smarks 		break;
113ecd6cf80Smarks 
114ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET:
115ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET_SETS:
11691ebeef5Sahrens 		if (who[1] != ZFS_DELEG_NA)
11791ebeef5Sahrens 			return (-1);
11891ebeef5Sahrens 		return (permset_namecheck(&who[3], NULL, NULL));
119ecd6cf80Smarks 
120ecd6cf80Smarks 	case ZFS_DELEG_CREATE:
121ecd6cf80Smarks 	case ZFS_DELEG_CREATE_SETS:
12291ebeef5Sahrens 		if (who[1] != ZFS_DELEG_NA)
12391ebeef5Sahrens 			return (-1);
12491ebeef5Sahrens 		if (who[3] != '\0')
12591ebeef5Sahrens 			return (-1);
12691ebeef5Sahrens 		break;
12791ebeef5Sahrens 
128ecd6cf80Smarks 	case ZFS_DELEG_EVERYONE:
129ecd6cf80Smarks 	case ZFS_DELEG_EVERYONE_SETS:
13091ebeef5Sahrens 		if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
13191ebeef5Sahrens 			return (-1);
132ecd6cf80Smarks 		if (who[3] != '\0')
133ecd6cf80Smarks 			return (-1);
134ecd6cf80Smarks 		break;
13591ebeef5Sahrens 
136ecd6cf80Smarks 	default:
137ecd6cf80Smarks 		return (-1);
138ecd6cf80Smarks 	}
13991ebeef5Sahrens 
140ecd6cf80Smarks 	return (0);
141ecd6cf80Smarks }
142ecd6cf80Smarks 
143ecd6cf80Smarks int
144ecd6cf80Smarks zfs_deleg_verify_nvlist(nvlist_t *nvp)
145ecd6cf80Smarks {
146ecd6cf80Smarks 	nvpair_t *who, *perm_name;
147ecd6cf80Smarks 	nvlist_t *perms;
148ecd6cf80Smarks 	int error;
149ecd6cf80Smarks 
150ecd6cf80Smarks 	if (nvp == NULL)
151ecd6cf80Smarks 		return (-1);
152ecd6cf80Smarks 
153ecd6cf80Smarks 	who = nvlist_next_nvpair(nvp, NULL);
154ecd6cf80Smarks 	if (who == NULL)
155ecd6cf80Smarks 		return (-1);
156ecd6cf80Smarks 
157ecd6cf80Smarks 	do {
158ecd6cf80Smarks 		if (zfs_validate_who(nvpair_name(who)))
159ecd6cf80Smarks 			return (-1);
160ecd6cf80Smarks 
161ecd6cf80Smarks 		error = nvlist_lookup_nvlist(nvp, nvpair_name(who), &perms);
162ecd6cf80Smarks 
163ecd6cf80Smarks 		if (error && error != ENOENT)
164ecd6cf80Smarks 			return (-1);
165ecd6cf80Smarks 		if (error == ENOENT)
166ecd6cf80Smarks 			continue;
167ecd6cf80Smarks 
168ecd6cf80Smarks 		perm_name = nvlist_next_nvpair(perms, NULL);
169ecd6cf80Smarks 		if (perm_name == NULL) {
170ecd6cf80Smarks 			return (-1);
171ecd6cf80Smarks 		}
172ecd6cf80Smarks 		do {
173ecd6cf80Smarks 			error = zfs_valid_permission_name(
174ecd6cf80Smarks 			    nvpair_name(perm_name));
17591ebeef5Sahrens 			if (error)
176ecd6cf80Smarks 				return (-1);
177ecd6cf80Smarks 		} while (perm_name = nvlist_next_nvpair(perms, perm_name));
178ecd6cf80Smarks 	} while (who = nvlist_next_nvpair(nvp, who));
179ecd6cf80Smarks 	return (0);
180ecd6cf80Smarks }
181ecd6cf80Smarks 
182ecd6cf80Smarks /*
183ecd6cf80Smarks  * Construct the base attribute name.  The base attribute names
184ecd6cf80Smarks  * are the "key" to locate the jump objects which contain the actual
185ecd6cf80Smarks  * permissions.  The base attribute names are encoded based on
186ecd6cf80Smarks  * type of entry and whether it is a local or descendent permission.
187ecd6cf80Smarks  *
188ecd6cf80Smarks  * Arguments:
189ecd6cf80Smarks  * attr - attribute name return string, attribute is assumed to be
190ecd6cf80Smarks  *        ZFS_MAX_DELEG_NAME long.
191ecd6cf80Smarks  * type - type of entry to construct
192ecd6cf80Smarks  * inheritchr - inheritance type (local,descendent, or NA for create and
193ecd6cf80Smarks  *                               permission set definitions
194ecd6cf80Smarks  * data - is either a permission set name or a 64 bit uid/gid.
195ecd6cf80Smarks  */
196ecd6cf80Smarks void
19791ebeef5Sahrens zfs_deleg_whokey(char *attr, zfs_deleg_who_type_t type,
19891ebeef5Sahrens     char inheritchr, void *data)
199ecd6cf80Smarks {
200ecd6cf80Smarks 	int len = ZFS_MAX_DELEG_NAME;
201ecd6cf80Smarks 	uint64_t *id = data;
202ecd6cf80Smarks 
203ecd6cf80Smarks 	switch (type) {
204ecd6cf80Smarks 	case ZFS_DELEG_USER:
205ecd6cf80Smarks 	case ZFS_DELEG_GROUP:
206ecd6cf80Smarks 	case ZFS_DELEG_USER_SETS:
207ecd6cf80Smarks 	case ZFS_DELEG_GROUP_SETS:
208ecd6cf80Smarks 		(void) snprintf(attr, len, "%c%c%c%lld", type, inheritchr,
209ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR, (longlong_t)*id);
210ecd6cf80Smarks 		break;
211ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET_SETS:
212ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET:
213ecd6cf80Smarks 		(void) snprintf(attr, len, "%c-%c%s", type,
214ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR, (char *)data);
215ecd6cf80Smarks 		break;
216ecd6cf80Smarks 	case ZFS_DELEG_CREATE:
217ecd6cf80Smarks 	case ZFS_DELEG_CREATE_SETS:
218ecd6cf80Smarks 		(void) snprintf(attr, len, "%c-%c", type,
219ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR);
220ecd6cf80Smarks 		break;
22191ebeef5Sahrens 	case ZFS_DELEG_EVERYONE:
22291ebeef5Sahrens 	case ZFS_DELEG_EVERYONE_SETS:
223ecd6cf80Smarks 		(void) snprintf(attr, len, "%c%c%c", type, inheritchr,
224ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR);
22591ebeef5Sahrens 		break;
22691ebeef5Sahrens 	default:
22791ebeef5Sahrens 		ASSERT(!"bad zfs_deleg_who_type_t");
228ecd6cf80Smarks 	}
229ecd6cf80Smarks }
230