xref: /titanic_51/usr/src/common/zfs/zfs_deleg.c (revision 1af68beac368dae20a9c5d738cf6b6320cf9d63a)
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.
23*1af68beaSAlexander Stetsenko  * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
24ecd6cf80Smarks  */
25ecd6cf80Smarks 
26ecd6cf80Smarks #if defined(_KERNEL)
27ecd6cf80Smarks #include <sys/systm.h>
28ecd6cf80Smarks #include <sys/sunddi.h>
29ecd6cf80Smarks #include <sys/ctype.h>
30ecd6cf80Smarks #else
31ecd6cf80Smarks #include <stdio.h>
32ecd6cf80Smarks #include <unistd.h>
33ecd6cf80Smarks #include <strings.h>
34ecd6cf80Smarks #include <libnvpair.h>
35ecd6cf80Smarks #include <ctype.h>
36ecd6cf80Smarks #endif
3791ebeef5Sahrens /* XXX includes zfs_context.h, so why bother with the above? */
38ecd6cf80Smarks #include <sys/dsl_deleg.h>
3991ebeef5Sahrens #include "zfs_prop.h"
40ecd6cf80Smarks #include "zfs_deleg.h"
41ecd6cf80Smarks #include "zfs_namecheck.h"
42ecd6cf80Smarks 
43ecd6cf80Smarks /*
44ecd6cf80Smarks  * permission table
456949a980Smarks  *
466949a980Smarks  * Keep this table in sorted order
476949a980Smarks  *
486949a980Smarks  * This table is used for displaying all permissions for
496949a980Smarks  * zfs allow
50ecd6cf80Smarks  */
51ecd6cf80Smarks 
526949a980Smarks zfs_deleg_perm_tab_t zfs_deleg_perm_tab[] = {
536949a980Smarks 	{ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW},
546949a980Smarks 	{ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
556949a980Smarks 	{ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
566949a980Smarks 	{ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
576949a980Smarks 	{ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
586949a980Smarks 	{ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
596949a980Smarks 	{ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
606949a980Smarks 	{ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
616949a980Smarks 	{ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
626949a980Smarks 	{ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
636949a980Smarks 	{ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
64*1af68beaSAlexander Stetsenko 	{ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
656949a980Smarks 	{ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
6614843421SMatthew Ahrens 	{ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
6714843421SMatthew Ahrens 	{ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
6814843421SMatthew Ahrens 	{ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
6914843421SMatthew Ahrens 	{ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
70842727c2SChris Kirby 	{ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
71842727c2SChris Kirby 	{ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
7299d5e173STim Haley 	{ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
736949a980Smarks 	{NULL, ZFS_DELEG_NOTE_NONE }
74ecd6cf80Smarks };
75ecd6cf80Smarks 
76ecd6cf80Smarks static int
7791ebeef5Sahrens zfs_valid_permission_name(const char *perm)
78ecd6cf80Smarks {
7991ebeef5Sahrens 	if (zfs_deleg_canonicalize_perm(perm))
80ecd6cf80Smarks 		return (0);
81ecd6cf80Smarks 
82ecd6cf80Smarks 	return (permset_namecheck(perm, NULL, NULL));
83ecd6cf80Smarks }
84ecd6cf80Smarks 
8591ebeef5Sahrens const char *
8691ebeef5Sahrens zfs_deleg_canonicalize_perm(const char *perm)
8791ebeef5Sahrens {
8891ebeef5Sahrens 	int i;
8991ebeef5Sahrens 	zfs_prop_t prop;
9091ebeef5Sahrens 
916949a980Smarks 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
926949a980Smarks 		if (strcmp(perm, zfs_deleg_perm_tab[i].z_perm) == 0)
9391ebeef5Sahrens 			return (perm);
9491ebeef5Sahrens 	}
9591ebeef5Sahrens 
9691ebeef5Sahrens 	prop = zfs_name_to_prop(perm);
97990b4856Slling 	if (prop != ZPROP_INVAL && zfs_prop_delegatable(prop))
9891ebeef5Sahrens 		return (zfs_prop_to_name(prop));
9991ebeef5Sahrens 	return (NULL);
10091ebeef5Sahrens 
10191ebeef5Sahrens }
10291ebeef5Sahrens 
103ecd6cf80Smarks static int
104ecd6cf80Smarks zfs_validate_who(char *who)
105ecd6cf80Smarks {
106ecd6cf80Smarks 	char *p;
107ecd6cf80Smarks 
10891ebeef5Sahrens 	if (who[2] != ZFS_DELEG_FIELD_SEP_CHR)
10991ebeef5Sahrens 		return (-1);
11091ebeef5Sahrens 
11191ebeef5Sahrens 	switch (who[0]) {
112ecd6cf80Smarks 	case ZFS_DELEG_USER:
113ecd6cf80Smarks 	case ZFS_DELEG_GROUP:
114ecd6cf80Smarks 	case ZFS_DELEG_USER_SETS:
115ecd6cf80Smarks 	case ZFS_DELEG_GROUP_SETS:
11691ebeef5Sahrens 		if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
11791ebeef5Sahrens 			return (-1);
11891ebeef5Sahrens 		for (p = &who[3]; *p; p++)
11991ebeef5Sahrens 			if (!isdigit(*p))
12091ebeef5Sahrens 				return (-1);
121ecd6cf80Smarks 		break;
122ecd6cf80Smarks 
123ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET:
124ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET_SETS:
12591ebeef5Sahrens 		if (who[1] != ZFS_DELEG_NA)
12691ebeef5Sahrens 			return (-1);
12791ebeef5Sahrens 		return (permset_namecheck(&who[3], NULL, NULL));
128ecd6cf80Smarks 
129ecd6cf80Smarks 	case ZFS_DELEG_CREATE:
130ecd6cf80Smarks 	case ZFS_DELEG_CREATE_SETS:
13191ebeef5Sahrens 		if (who[1] != ZFS_DELEG_NA)
13291ebeef5Sahrens 			return (-1);
13391ebeef5Sahrens 		if (who[3] != '\0')
13491ebeef5Sahrens 			return (-1);
13591ebeef5Sahrens 		break;
13691ebeef5Sahrens 
137ecd6cf80Smarks 	case ZFS_DELEG_EVERYONE:
138ecd6cf80Smarks 	case ZFS_DELEG_EVERYONE_SETS:
13991ebeef5Sahrens 		if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
14091ebeef5Sahrens 			return (-1);
141ecd6cf80Smarks 		if (who[3] != '\0')
142ecd6cf80Smarks 			return (-1);
143ecd6cf80Smarks 		break;
14491ebeef5Sahrens 
145ecd6cf80Smarks 	default:
146ecd6cf80Smarks 		return (-1);
147ecd6cf80Smarks 	}
14891ebeef5Sahrens 
149ecd6cf80Smarks 	return (0);
150ecd6cf80Smarks }
151ecd6cf80Smarks 
152ecd6cf80Smarks int
153ecd6cf80Smarks zfs_deleg_verify_nvlist(nvlist_t *nvp)
154ecd6cf80Smarks {
155ecd6cf80Smarks 	nvpair_t *who, *perm_name;
156ecd6cf80Smarks 	nvlist_t *perms;
157ecd6cf80Smarks 	int error;
158ecd6cf80Smarks 
159ecd6cf80Smarks 	if (nvp == NULL)
160ecd6cf80Smarks 		return (-1);
161ecd6cf80Smarks 
162ecd6cf80Smarks 	who = nvlist_next_nvpair(nvp, NULL);
163ecd6cf80Smarks 	if (who == NULL)
164ecd6cf80Smarks 		return (-1);
165ecd6cf80Smarks 
166ecd6cf80Smarks 	do {
167ecd6cf80Smarks 		if (zfs_validate_who(nvpair_name(who)))
168ecd6cf80Smarks 			return (-1);
169ecd6cf80Smarks 
170ecd6cf80Smarks 		error = nvlist_lookup_nvlist(nvp, nvpair_name(who), &perms);
171ecd6cf80Smarks 
172ecd6cf80Smarks 		if (error && error != ENOENT)
173ecd6cf80Smarks 			return (-1);
174ecd6cf80Smarks 		if (error == ENOENT)
175ecd6cf80Smarks 			continue;
176ecd6cf80Smarks 
177ecd6cf80Smarks 		perm_name = nvlist_next_nvpair(perms, NULL);
178ecd6cf80Smarks 		if (perm_name == NULL) {
179ecd6cf80Smarks 			return (-1);
180ecd6cf80Smarks 		}
181ecd6cf80Smarks 		do {
182ecd6cf80Smarks 			error = zfs_valid_permission_name(
183ecd6cf80Smarks 			    nvpair_name(perm_name));
18491ebeef5Sahrens 			if (error)
185ecd6cf80Smarks 				return (-1);
186ecd6cf80Smarks 		} while (perm_name = nvlist_next_nvpair(perms, perm_name));
187ecd6cf80Smarks 	} while (who = nvlist_next_nvpair(nvp, who));
188ecd6cf80Smarks 	return (0);
189ecd6cf80Smarks }
190ecd6cf80Smarks 
191ecd6cf80Smarks /*
192ecd6cf80Smarks  * Construct the base attribute name.  The base attribute names
193ecd6cf80Smarks  * are the "key" to locate the jump objects which contain the actual
194ecd6cf80Smarks  * permissions.  The base attribute names are encoded based on
195ecd6cf80Smarks  * type of entry and whether it is a local or descendent permission.
196ecd6cf80Smarks  *
197ecd6cf80Smarks  * Arguments:
198ecd6cf80Smarks  * attr - attribute name return string, attribute is assumed to be
199ecd6cf80Smarks  *        ZFS_MAX_DELEG_NAME long.
200ecd6cf80Smarks  * type - type of entry to construct
201ecd6cf80Smarks  * inheritchr - inheritance type (local,descendent, or NA for create and
202ecd6cf80Smarks  *                               permission set definitions
203ecd6cf80Smarks  * data - is either a permission set name or a 64 bit uid/gid.
204ecd6cf80Smarks  */
205ecd6cf80Smarks void
20691ebeef5Sahrens zfs_deleg_whokey(char *attr, zfs_deleg_who_type_t type,
20791ebeef5Sahrens     char inheritchr, void *data)
208ecd6cf80Smarks {
209ecd6cf80Smarks 	int len = ZFS_MAX_DELEG_NAME;
210ecd6cf80Smarks 	uint64_t *id = data;
211ecd6cf80Smarks 
212ecd6cf80Smarks 	switch (type) {
213ecd6cf80Smarks 	case ZFS_DELEG_USER:
214ecd6cf80Smarks 	case ZFS_DELEG_GROUP:
215ecd6cf80Smarks 	case ZFS_DELEG_USER_SETS:
216ecd6cf80Smarks 	case ZFS_DELEG_GROUP_SETS:
217ecd6cf80Smarks 		(void) snprintf(attr, len, "%c%c%c%lld", type, inheritchr,
218ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR, (longlong_t)*id);
219ecd6cf80Smarks 		break;
220ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET_SETS:
221ecd6cf80Smarks 	case ZFS_DELEG_NAMED_SET:
222ecd6cf80Smarks 		(void) snprintf(attr, len, "%c-%c%s", type,
223ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR, (char *)data);
224ecd6cf80Smarks 		break;
225ecd6cf80Smarks 	case ZFS_DELEG_CREATE:
226ecd6cf80Smarks 	case ZFS_DELEG_CREATE_SETS:
227ecd6cf80Smarks 		(void) snprintf(attr, len, "%c-%c", type,
228ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR);
229ecd6cf80Smarks 		break;
23091ebeef5Sahrens 	case ZFS_DELEG_EVERYONE:
23191ebeef5Sahrens 	case ZFS_DELEG_EVERYONE_SETS:
232ecd6cf80Smarks 		(void) snprintf(attr, len, "%c%c%c", type, inheritchr,
233ecd6cf80Smarks 		    ZFS_DELEG_FIELD_SEP_CHR);
23491ebeef5Sahrens 		break;
23591ebeef5Sahrens 	default:
23691ebeef5Sahrens 		ASSERT(!"bad zfs_deleg_who_type_t");
237ecd6cf80Smarks 	}
238ecd6cf80Smarks }
239