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