xref: /freebsd-src/sys/contrib/openzfs/module/zcommon/zfs_deleg.c (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23eda14cbcSMatt Macy  * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
25eda14cbcSMatt Macy  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
26eda14cbcSMatt Macy  */
27eda14cbcSMatt Macy 
28eda14cbcSMatt Macy #include <sys/zfs_context.h>
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy #if defined(_KERNEL)
31eda14cbcSMatt Macy #include <sys/sunddi.h>
32eda14cbcSMatt Macy #include <sys/ctype.h>
33eda14cbcSMatt Macy #else
34eda14cbcSMatt Macy #include <stdio.h>
35eda14cbcSMatt Macy #include <unistd.h>
36eda14cbcSMatt Macy #include <libnvpair.h>
37eda14cbcSMatt Macy #include <ctype.h>
38eda14cbcSMatt Macy #endif
39da5137abSMartin Matuska #include <sys/string.h>
40eda14cbcSMatt Macy #include <sys/dsl_deleg.h>
41eda14cbcSMatt Macy #include "zfs_prop.h"
42eda14cbcSMatt Macy #include "zfs_deleg.h"
43eda14cbcSMatt Macy #include "zfs_namecheck.h"
44eda14cbcSMatt Macy 
45e92ffd9bSMartin Matuska const zfs_deleg_perm_tab_t zfs_deleg_perm_tab[] = {
46eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_ALLOW},
47eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_BOOKMARK},
48eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_CLONE},
49eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_CREATE},
50eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_DESTROY},
51eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_DIFF},
52eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_MOUNT},
53eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_PROMOTE},
54eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_RECEIVE},
55eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_RENAME},
56eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_ROLLBACK},
57eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_SNAPSHOT},
58eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_SHARE},
59eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_SEND},
60eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_USERPROP},
61eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_USERQUOTA},
62eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_GROUPQUOTA},
63eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_USERUSED},
64eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_GROUPUSED},
65eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_USEROBJQUOTA},
66eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_GROUPOBJQUOTA},
67eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_USEROBJUSED},
68eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_GROUPOBJUSED},
69eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_HOLD},
70eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_RELEASE},
71eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_LOAD_KEY},
72eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_CHANGE_KEY},
73eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_PROJECTUSED},
74eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_PROJECTQUOTA},
75eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_PROJECTOBJUSED},
76eda14cbcSMatt Macy 	{ZFS_DELEG_PERM_PROJECTOBJQUOTA},
77eda14cbcSMatt Macy 	{NULL}
78eda14cbcSMatt Macy };
79eda14cbcSMatt Macy 
80eda14cbcSMatt Macy static int
zfs_valid_permission_name(const char * perm)81eda14cbcSMatt Macy zfs_valid_permission_name(const char *perm)
82eda14cbcSMatt Macy {
83eda14cbcSMatt Macy 	if (zfs_deleg_canonicalize_perm(perm))
84eda14cbcSMatt Macy 		return (0);
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy 	return (permset_namecheck(perm, NULL, NULL));
87eda14cbcSMatt Macy }
88eda14cbcSMatt Macy 
89eda14cbcSMatt Macy const char *
zfs_deleg_canonicalize_perm(const char * perm)90eda14cbcSMatt Macy zfs_deleg_canonicalize_perm(const char *perm)
91eda14cbcSMatt Macy {
92e92ffd9bSMartin Matuska 	for (int i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
93eda14cbcSMatt Macy 		if (strcmp(perm, zfs_deleg_perm_tab[i].z_perm) == 0)
94eda14cbcSMatt Macy 			return (perm);
95eda14cbcSMatt Macy 	}
96eda14cbcSMatt Macy 
97e92ffd9bSMartin Matuska 	zfs_prop_t prop = zfs_name_to_prop(perm);
98eda14cbcSMatt Macy 	if (prop != ZPROP_INVAL && zfs_prop_delegatable(prop))
99eda14cbcSMatt Macy 		return (zfs_prop_to_name(prop));
100eda14cbcSMatt Macy 	return (NULL);
101eda14cbcSMatt Macy 
102eda14cbcSMatt Macy }
103eda14cbcSMatt Macy 
104eda14cbcSMatt Macy static int
zfs_validate_who(const char * who)105*2a58b312SMartin Matuska zfs_validate_who(const char *who)
106eda14cbcSMatt Macy {
107*2a58b312SMartin Matuska 	const char *p;
108eda14cbcSMatt Macy 
109eda14cbcSMatt Macy 	if (who[2] != ZFS_DELEG_FIELD_SEP_CHR)
110eda14cbcSMatt Macy 		return (-1);
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy 	switch (who[0]) {
113eda14cbcSMatt Macy 	case ZFS_DELEG_USER:
114eda14cbcSMatt Macy 	case ZFS_DELEG_GROUP:
115eda14cbcSMatt Macy 	case ZFS_DELEG_USER_SETS:
116eda14cbcSMatt Macy 	case ZFS_DELEG_GROUP_SETS:
117eda14cbcSMatt Macy 		if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
118eda14cbcSMatt Macy 			return (-1);
119eda14cbcSMatt Macy 		for (p = &who[3]; *p; p++)
120eda14cbcSMatt Macy 			if (!isdigit(*p))
121eda14cbcSMatt Macy 				return (-1);
122eda14cbcSMatt Macy 		break;
123eda14cbcSMatt Macy 
124eda14cbcSMatt Macy 	case ZFS_DELEG_NAMED_SET:
125eda14cbcSMatt Macy 	case ZFS_DELEG_NAMED_SET_SETS:
126eda14cbcSMatt Macy 		if (who[1] != ZFS_DELEG_NA)
127eda14cbcSMatt Macy 			return (-1);
128eda14cbcSMatt Macy 		return (permset_namecheck(&who[3], NULL, NULL));
129eda14cbcSMatt Macy 
130eda14cbcSMatt Macy 	case ZFS_DELEG_CREATE:
131eda14cbcSMatt Macy 	case ZFS_DELEG_CREATE_SETS:
132eda14cbcSMatt Macy 		if (who[1] != ZFS_DELEG_NA)
133eda14cbcSMatt Macy 			return (-1);
134eda14cbcSMatt Macy 		if (who[3] != '\0')
135eda14cbcSMatt Macy 			return (-1);
136eda14cbcSMatt Macy 		break;
137eda14cbcSMatt Macy 
138eda14cbcSMatt Macy 	case ZFS_DELEG_EVERYONE:
139eda14cbcSMatt Macy 	case ZFS_DELEG_EVERYONE_SETS:
140eda14cbcSMatt Macy 		if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
141eda14cbcSMatt Macy 			return (-1);
142eda14cbcSMatt Macy 		if (who[3] != '\0')
143eda14cbcSMatt Macy 			return (-1);
144eda14cbcSMatt Macy 		break;
145eda14cbcSMatt Macy 
146eda14cbcSMatt Macy 	default:
147eda14cbcSMatt Macy 		return (-1);
148eda14cbcSMatt Macy 	}
149eda14cbcSMatt Macy 
150eda14cbcSMatt Macy 	return (0);
151eda14cbcSMatt Macy }
152eda14cbcSMatt Macy 
153eda14cbcSMatt Macy int
zfs_deleg_verify_nvlist(nvlist_t * nvp)154eda14cbcSMatt Macy zfs_deleg_verify_nvlist(nvlist_t *nvp)
155eda14cbcSMatt Macy {
156eda14cbcSMatt Macy 	nvpair_t *who, *perm_name;
157eda14cbcSMatt Macy 	nvlist_t *perms;
158eda14cbcSMatt Macy 	int error;
159eda14cbcSMatt Macy 
160eda14cbcSMatt Macy 	if (nvp == NULL)
161eda14cbcSMatt Macy 		return (-1);
162eda14cbcSMatt Macy 
163eda14cbcSMatt Macy 	who = nvlist_next_nvpair(nvp, NULL);
164eda14cbcSMatt Macy 	if (who == NULL)
165eda14cbcSMatt Macy 		return (-1);
166eda14cbcSMatt Macy 
167eda14cbcSMatt Macy 	do {
168eda14cbcSMatt Macy 		if (zfs_validate_who(nvpair_name(who)))
169eda14cbcSMatt Macy 			return (-1);
170eda14cbcSMatt Macy 
171eda14cbcSMatt Macy 		error = nvlist_lookup_nvlist(nvp, nvpair_name(who), &perms);
172eda14cbcSMatt Macy 
173eda14cbcSMatt Macy 		if (error && error != ENOENT)
174eda14cbcSMatt Macy 			return (-1);
175eda14cbcSMatt Macy 		if (error == ENOENT)
176eda14cbcSMatt Macy 			continue;
177eda14cbcSMatt Macy 
178eda14cbcSMatt Macy 		perm_name = nvlist_next_nvpair(perms, NULL);
179eda14cbcSMatt Macy 		if (perm_name == NULL) {
180eda14cbcSMatt Macy 			return (-1);
181eda14cbcSMatt Macy 		}
182eda14cbcSMatt Macy 		do {
183eda14cbcSMatt Macy 			error = zfs_valid_permission_name(
184eda14cbcSMatt Macy 			    nvpair_name(perm_name));
185eda14cbcSMatt Macy 			if (error)
186eda14cbcSMatt Macy 				return (-1);
187eda14cbcSMatt Macy 		} while ((perm_name = nvlist_next_nvpair(perms, perm_name))
188eda14cbcSMatt Macy 		    != NULL);
189eda14cbcSMatt Macy 	} while ((who = nvlist_next_nvpair(nvp, who)) != NULL);
190eda14cbcSMatt Macy 	return (0);
191eda14cbcSMatt Macy }
192eda14cbcSMatt Macy 
193eda14cbcSMatt Macy /*
194eda14cbcSMatt Macy  * Construct the base attribute name.  The base attribute names
195eda14cbcSMatt Macy  * are the "key" to locate the jump objects which contain the actual
196eda14cbcSMatt Macy  * permissions.  The base attribute names are encoded based on
197eda14cbcSMatt Macy  * type of entry and whether it is a local or descendent permission.
198eda14cbcSMatt Macy  *
199eda14cbcSMatt Macy  * Arguments:
200eda14cbcSMatt Macy  * attr - attribute name return string, attribute is assumed to be
201eda14cbcSMatt Macy  *        ZFS_MAX_DELEG_NAME long.
202eda14cbcSMatt Macy  * type - type of entry to construct
203eda14cbcSMatt Macy  * inheritchr - inheritance type (local,descendent, or NA for create and
204eda14cbcSMatt Macy  *                               permission set definitions
205eda14cbcSMatt Macy  * data - is either a permission set name or a 64 bit uid/gid.
206eda14cbcSMatt Macy  */
207eda14cbcSMatt Macy void
zfs_deleg_whokey(char * attr,zfs_deleg_who_type_t type,char inheritchr,void * data)208eda14cbcSMatt Macy zfs_deleg_whokey(char *attr, zfs_deleg_who_type_t type,
209eda14cbcSMatt Macy     char inheritchr, void *data)
210eda14cbcSMatt Macy {
211eda14cbcSMatt Macy 	int len = ZFS_MAX_DELEG_NAME;
212eda14cbcSMatt Macy 	uint64_t *id = data;
213eda14cbcSMatt Macy 
214eda14cbcSMatt Macy 	switch (type) {
215eda14cbcSMatt Macy 	case ZFS_DELEG_USER:
216eda14cbcSMatt Macy 	case ZFS_DELEG_GROUP:
217eda14cbcSMatt Macy 	case ZFS_DELEG_USER_SETS:
218eda14cbcSMatt Macy 	case ZFS_DELEG_GROUP_SETS:
219eda14cbcSMatt Macy 		(void) snprintf(attr, len, "%c%c%c%lld", type, inheritchr,
220eda14cbcSMatt Macy 		    ZFS_DELEG_FIELD_SEP_CHR, (longlong_t)*id);
221eda14cbcSMatt Macy 		break;
222eda14cbcSMatt Macy 	case ZFS_DELEG_NAMED_SET_SETS:
223eda14cbcSMatt Macy 	case ZFS_DELEG_NAMED_SET:
224eda14cbcSMatt Macy 		(void) snprintf(attr, len, "%c-%c%s", type,
225eda14cbcSMatt Macy 		    ZFS_DELEG_FIELD_SEP_CHR, (char *)data);
226eda14cbcSMatt Macy 		break;
227eda14cbcSMatt Macy 	case ZFS_DELEG_CREATE:
228eda14cbcSMatt Macy 	case ZFS_DELEG_CREATE_SETS:
229eda14cbcSMatt Macy 		(void) snprintf(attr, len, "%c-%c", type,
230eda14cbcSMatt Macy 		    ZFS_DELEG_FIELD_SEP_CHR);
231eda14cbcSMatt Macy 		break;
232eda14cbcSMatt Macy 	case ZFS_DELEG_EVERYONE:
233eda14cbcSMatt Macy 	case ZFS_DELEG_EVERYONE_SETS:
234eda14cbcSMatt Macy 		(void) snprintf(attr, len, "%c%c%c", type, inheritchr,
235eda14cbcSMatt Macy 		    ZFS_DELEG_FIELD_SEP_CHR);
236eda14cbcSMatt Macy 		break;
237eda14cbcSMatt Macy 	default:
238eda14cbcSMatt Macy 		ASSERT(!"bad zfs_deleg_who_type_t");
239eda14cbcSMatt Macy 	}
240eda14cbcSMatt Macy }
241eda14cbcSMatt Macy 
242eda14cbcSMatt Macy #if defined(_KERNEL)
243eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_deleg_verify_nvlist);
244eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_deleg_whokey);
245eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_deleg_canonicalize_perm);
246eda14cbcSMatt Macy #endif
247