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