xref: /illumos-gate/usr/src/common/zfs/zfs_deleg.c (revision f67950b2)
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.
245cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
2588f61deeSIgor Kozhukhov  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
26ecd6cf80Smarks  */
27ecd6cf80Smarks 
2878f17100SMatthew Ahrens #include <sys/zfs_context.h>
2978f17100SMatthew Ahrens 
30ecd6cf80Smarks #if defined(_KERNEL)
31ecd6cf80Smarks #include <sys/systm.h>
32ecd6cf80Smarks #include <sys/sunddi.h>
33ecd6cf80Smarks #include <sys/ctype.h>
34ecd6cf80Smarks #else
35ecd6cf80Smarks #include <stdio.h>
36ecd6cf80Smarks #include <unistd.h>
37ecd6cf80Smarks #include <strings.h>
38ecd6cf80Smarks #include <libnvpair.h>
39ecd6cf80Smarks #include <ctype.h>
40ecd6cf80Smarks #endif
41ecd6cf80Smarks #include <sys/dsl_deleg.h>
4291ebeef5Sahrens #include "zfs_prop.h"
43ecd6cf80Smarks #include "zfs_deleg.h"
44ecd6cf80Smarks #include "zfs_namecheck.h"
45ecd6cf80Smarks 
466949a980Smarks zfs_deleg_perm_tab_t zfs_deleg_perm_tab[] = {
4778f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_ALLOW},
4878f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_BOOKMARK},
4978f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_CLONE},
5078f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_CREATE},
5178f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_DESTROY},
5278f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_DIFF},
5378f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_MOUNT},
5478f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_PROMOTE},
5578f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_RECEIVE},
565cabbc6bSPrashanth Sreenivasa 	{ZFS_DELEG_PERM_REMAP},
5778f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_RENAME},
5878f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_ROLLBACK},
5978f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_SNAPSHOT},
6078f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_SHARE},
6178f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_SEND},
6278f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_USERPROP},
6378f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_USERQUOTA},
6478f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_GROUPQUOTA},
6578f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_USERUSED},
6678f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_GROUPUSED},
67*f67950b2SNasf-Fan 	{ZFS_DELEG_PERM_USEROBJQUOTA},
68*f67950b2SNasf-Fan 	{ZFS_DELEG_PERM_GROUPOBJQUOTA},
69*f67950b2SNasf-Fan 	{ZFS_DELEG_PERM_USEROBJUSED},
70*f67950b2SNasf-Fan 	{ZFS_DELEG_PERM_GROUPOBJUSED},
7178f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_HOLD},
7278f17100SMatthew Ahrens 	{ZFS_DELEG_PERM_RELEASE},
73eb633035STom Caputi 	{ZFS_DELEG_PERM_LOAD_KEY},
74eb633035STom Caputi 	{ZFS_DELEG_PERM_CHANGE_KEY},
75*f67950b2SNasf-Fan 	{ZFS_DELEG_PERM_PROJECTUSED},
76*f67950b2SNasf-Fan 	{ZFS_DELEG_PERM_PROJECTQUOTA},
77*f67950b2SNasf-Fan 	{ZFS_DELEG_PERM_PROJECTOBJUSED},
78*f67950b2SNasf-Fan 	{ZFS_DELEG_PERM_PROJECTOBJQUOTA},
7978f17100SMatthew Ahrens 	{NULL}
80ecd6cf80Smarks };
81ecd6cf80Smarks 
8291ebeef5Sahrens static int
zfs_valid_permission_name(const char * perm)8391ebeef5Sahrens zfs_valid_permission_name(const char *perm)
84ecd6cf80Smarks {
8591ebeef5Sahrens 	if (zfs_deleg_canonicalize_perm(perm))
8691ebeef5Sahrens 		return (0);
8791ebeef5Sahrens 
8891ebeef5Sahrens 	return (permset_namecheck(perm, NULL, NULL));
89ecd6cf80Smarks }
90ecd6cf80Smarks 
9191ebeef5Sahrens const char *
zfs_deleg_canonicalize_perm(const char * perm)9291ebeef5Sahrens zfs_deleg_canonicalize_perm(const char *perm)
93ecd6cf80Smarks {
94ecd6cf80Smarks 	int i;
9591ebeef5Sahrens 	zfs_prop_t prop;
9691ebeef5Sahrens 
976949a980Smarks 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
986949a980Smarks 		if (strcmp(perm, zfs_deleg_perm_tab[i].z_perm) == 0)
9991ebeef5Sahrens 			return (perm);
100ecd6cf80Smarks 	}
101ecd6cf80Smarks 
10291ebeef5Sahrens 	prop = zfs_name_to_prop(perm);
103990b4856Slling 	if (prop != ZPROP_INVAL && zfs_prop_delegatable(prop))
10491ebeef5Sahrens 		return (zfs_prop_to_name(prop));
10591ebeef5Sahrens 	return (NULL);
10691ebeef5Sahrens 
107ecd6cf80Smarks }
108ecd6cf80Smarks 
109ecd6cf80Smarks static int
zfs_validate_who(char * who)110ecd6cf80Smarks zfs_validate_who(char *who)
111ecd6cf80Smarks {
112ecd6cf80Smarks 	char *p;
113ecd6cf80Smarks 
11491ebeef5Sahrens 	if (who[2] != ZFS_DELEG_FIELD_SEP_CHR)
11591ebeef5Sahrens 		return (-1);
11691ebeef5Sahrens 
11791ebeef5Sahrens 	switch (who[0]) {
118ecd6cf80Smarks 	case ZFS_DELEG_USER:
119ecd6cf80Smarks 	case ZFS_DELEG_GROUP:
120ecd6cf80Smarks 	case ZFS_DELEG_USER_SETS:
121ecd6cf80Smarks 	case ZFS_DELEG_GROUP_SETS:
12291ebeef5Sahrens 		if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
12391ebeef5Sahrens 			return (-1);
12491ebeef5Sahrens 		for (p = &who[3]; *p; p++)
12591ebeef5Sahrens 			if (!isdigit(*p))
12691ebeef5Sahrens 				return (-1);
127ecd6cf80Smarks 		break;
12891ebeef5Sahrens 
129ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET:
130ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET_SETS:
13191ebeef5Sahrens 		if (who[1] != ZFS_DELEG_NA)
13291ebeef5Sahrens 			return (-1);
13391ebeef5Sahrens 		return (permset_namecheck(&who[3], NULL, NULL));
134ecd6cf80Smarks 
135ecd6cf80Smarks 	case ZFS_DELEG_CREATE:
136ecd6cf80Smarks 	case ZFS_DELEG_CREATE_SETS:
13791ebeef5Sahrens 		if (who[1] != ZFS_DELEG_NA)
13891ebeef5Sahrens 			return (-1);
139ecd6cf80Smarks 		if (who[3] != '\0')
14091ebeef5Sahrens 			return (-1);
141ecd6cf80Smarks 		break;
142ecd6cf80Smarks 
14391ebeef5Sahrens 	case ZFS_DELEG_EVERYONE:
14491ebeef5Sahrens 	case ZFS_DELEG_EVERYONE_SETS:
14591ebeef5Sahrens 		if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
146ecd6cf80Smarks 			return (-1);
14791ebeef5Sahrens 		if (who[3] != '\0')
148ecd6cf80Smarks 			return (-1);
149ecd6cf80Smarks 		break;
15091ebeef5Sahrens 
15191ebeef5Sahrens 	default:
15291ebeef5Sahrens 		return (-1);
153ecd6cf80Smarks 	}
15491ebeef5Sahrens 
155ecd6cf80Smarks 	return (0);
156ecd6cf80Smarks }
157ecd6cf80Smarks 
158ecd6cf80Smarks int
zfs_deleg_verify_nvlist(nvlist_t * nvp)159ecd6cf80Smarks zfs_deleg_verify_nvlist(nvlist_t *nvp)
160ecd6cf80Smarks {
161ecd6cf80Smarks 	nvpair_t *who, *perm_name;
162ecd6cf80Smarks 	nvlist_t *perms;
163ecd6cf80Smarks 	int error;
164ecd6cf80Smarks 
165ecd6cf80Smarks 	if (nvp == NULL)
166ecd6cf80Smarks 		return (-1);
167ecd6cf80Smarks 
168ecd6cf80Smarks 	who = nvlist_next_nvpair(nvp, NULL);
169ecd6cf80Smarks 	if (who == NULL)
170ecd6cf80Smarks 		return (-1);
171ecd6cf80Smarks 
172ecd6cf80Smarks 	do {
173ecd6cf80Smarks 		if (zfs_validate_who(nvpair_name(who)))
174ecd6cf80Smarks 			return (-1);
175ecd6cf80Smarks 
176ecd6cf80Smarks 		error = nvlist_lookup_nvlist(nvp, nvpair_name(who), &perms);
177ecd6cf80Smarks 
178ecd6cf80Smarks 		if (error && error != ENOENT)
179ecd6cf80Smarks 			return (-1);
180ecd6cf80Smarks 		if (error == ENOENT)
181ecd6cf80Smarks 			continue;
182ecd6cf80Smarks 
183ecd6cf80Smarks 		perm_name = nvlist_next_nvpair(perms, NULL);
184ecd6cf80Smarks 		if (perm_name == NULL) {
185ecd6cf80Smarks 			return (-1);
186ecd6cf80Smarks 		}
187ecd6cf80Smarks 		do {
188ecd6cf80Smarks 			error = zfs_valid_permission_name(
189ecd6cf80Smarks 			    nvpair_name(perm_name));
19091ebeef5Sahrens 			if (error)
191ecd6cf80Smarks 				return (-1);
19288f61deeSIgor Kozhukhov 		} while ((perm_name = nvlist_next_nvpair(perms, perm_name))
19388f61deeSIgor Kozhukhov 		    != NULL);
19488f61deeSIgor Kozhukhov 	} while ((who = nvlist_next_nvpair(nvp, who)) != NULL);
195ecd6cf80Smarks 	return (0);
196ecd6cf80Smarks }
197ecd6cf80Smarks 
198ecd6cf80Smarks /*
199ecd6cf80Smarks  * Construct the base attribute name.  The base attribute names
200ecd6cf80Smarks  * are the "key" to locate the jump objects which contain the actual
201ecd6cf80Smarks  * permissions.  The base attribute names are encoded based on
202ecd6cf80Smarks  * type of entry and whether it is a local or descendent permission.
203ecd6cf80Smarks  *
204ecd6cf80Smarks  * Arguments:
205ecd6cf80Smarks  * attr - attribute name return string, attribute is assumed to be
206ecd6cf80Smarks  *        ZFS_MAX_DELEG_NAME long.
207ecd6cf80Smarks  * type - type of entry to construct
208ecd6cf80Smarks  * inheritchr - inheritance type (local,descendent, or NA for create and
209ecd6cf80Smarks  *                               permission set definitions
210ecd6cf80Smarks  * data - is either a permission set name or a 64 bit uid/gid.
211ecd6cf80Smarks  */
212ecd6cf80Smarks void
zfs_deleg_whokey(char * attr,zfs_deleg_who_type_t type,char inheritchr,void * data)21391ebeef5Sahrens zfs_deleg_whokey(char *attr, zfs_deleg_who_type_t type,
21491ebeef5Sahrens     char inheritchr, void *data)
215ecd6cf80Smarks {
216ecd6cf80Smarks 	int len = ZFS_MAX_DELEG_NAME;
217ecd6cf80Smarks 	uint64_t *id = data;
218ecd6cf80Smarks 
219ecd6cf80Smarks 	switch (type) {
220ecd6cf80Smarks 	case ZFS_DELEG_USER:
221ecd6cf80Smarks 	case ZFS_DELEG_GROUP:
222ecd6cf80Smarks 	case ZFS_DELEG_USER_SETS:
223ecd6cf80Smarks 	case ZFS_DELEG_GROUP_SETS:
224ecd6cf80Smarks 		(void) snprintf(attr, len, "%c%c%c%lld", type, inheritchr,
225ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR, (longlong_t)*id);
226ecd6cf80Smarks 		break;
227ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET_SETS:
228ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET:
229ecd6cf80Smarks 		(void) snprintf(attr, len, "%c-%c%s", type,
230ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR, (char *)data);
231ecd6cf80Smarks 		break;
232ecd6cf80Smarks 	case ZFS_DELEG_CREATE:
233ecd6cf80Smarks 	case ZFS_DELEG_CREATE_SETS:
234ecd6cf80Smarks 		(void) snprintf(attr, len, "%c-%c", type,
235ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR);
236ecd6cf80Smarks 		break;
23791ebeef5Sahrens 	case ZFS_DELEG_EVERYONE:
23891ebeef5Sahrens 	case ZFS_DELEG_EVERYONE_SETS:
239ecd6cf80Smarks 		(void) snprintf(attr, len, "%c%c%c", type, inheritchr,
240ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR);
24191ebeef5Sahrens 		break;
24291ebeef5Sahrens 	default:
24391ebeef5Sahrens 		ASSERT(!"bad zfs_deleg_who_type_t");
244ecd6cf80Smarks 	}
245ecd6cf80Smarks }
246