xref: /titanic_44/usr/src/common/zfs/zfs_deleg.c (revision 5fd03bc0f2e00e7ba02316c2e08f45d52aab15db)
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 by Delphix. All rights reserved.
25  */
26 
27 #include <sys/zfs_context.h>
28 
29 #if defined(_KERNEL)
30 #include <sys/systm.h>
31 #include <sys/sunddi.h>
32 #include <sys/ctype.h>
33 #else
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <strings.h>
37 #include <libnvpair.h>
38 #include <ctype.h>
39 #endif
40 #include <sys/dsl_deleg.h>
41 #include "zfs_prop.h"
42 #include "zfs_deleg.h"
43 #include "zfs_namecheck.h"
44 
45 zfs_deleg_perm_tab_t zfs_deleg_perm_tab[] = {
46 	{ZFS_DELEG_PERM_ALLOW},
47 	{ZFS_DELEG_PERM_BOOKMARK},
48 	{ZFS_DELEG_PERM_CLONE},
49 	{ZFS_DELEG_PERM_CREATE},
50 	{ZFS_DELEG_PERM_DESTROY},
51 	{ZFS_DELEG_PERM_DIFF},
52 	{ZFS_DELEG_PERM_MOUNT},
53 	{ZFS_DELEG_PERM_PROMOTE},
54 	{ZFS_DELEG_PERM_RECEIVE},
55 	{ZFS_DELEG_PERM_RENAME},
56 	{ZFS_DELEG_PERM_ROLLBACK},
57 	{ZFS_DELEG_PERM_SNAPSHOT},
58 	{ZFS_DELEG_PERM_SHARE},
59 	{ZFS_DELEG_PERM_SEND},
60 	{ZFS_DELEG_PERM_USERPROP},
61 	{ZFS_DELEG_PERM_USERQUOTA},
62 	{ZFS_DELEG_PERM_GROUPQUOTA},
63 	{ZFS_DELEG_PERM_USERUSED},
64 	{ZFS_DELEG_PERM_GROUPUSED},
65 	{ZFS_DELEG_PERM_HOLD},
66 	{ZFS_DELEG_PERM_RELEASE},
67 	{NULL}
68 };
69 
70 static int
71 zfs_valid_permission_name(const char *perm)
72 {
73 	if (zfs_deleg_canonicalize_perm(perm))
74 		return (0);
75 
76 	return (permset_namecheck(perm, NULL, NULL));
77 }
78 
79 const char *
80 zfs_deleg_canonicalize_perm(const char *perm)
81 {
82 	int i;
83 	zfs_prop_t prop;
84 
85 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
86 		if (strcmp(perm, zfs_deleg_perm_tab[i].z_perm) == 0)
87 			return (perm);
88 	}
89 
90 	prop = zfs_name_to_prop(perm);
91 	if (prop != ZPROP_INVAL && zfs_prop_delegatable(prop))
92 		return (zfs_prop_to_name(prop));
93 	return (NULL);
94 
95 }
96 
97 static int
98 zfs_validate_who(char *who)
99 {
100 	char *p;
101 
102 	if (who[2] != ZFS_DELEG_FIELD_SEP_CHR)
103 		return (-1);
104 
105 	switch (who[0]) {
106 	case ZFS_DELEG_USER:
107 	case ZFS_DELEG_GROUP:
108 	case ZFS_DELEG_USER_SETS:
109 	case ZFS_DELEG_GROUP_SETS:
110 		if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
111 			return (-1);
112 		for (p = &who[3]; *p; p++)
113 			if (!isdigit(*p))
114 				return (-1);
115 		break;
116 
117 	case ZFS_DELEG_NAMED_SET:
118 	case ZFS_DELEG_NAMED_SET_SETS:
119 		if (who[1] != ZFS_DELEG_NA)
120 			return (-1);
121 		return (permset_namecheck(&who[3], NULL, NULL));
122 
123 	case ZFS_DELEG_CREATE:
124 	case ZFS_DELEG_CREATE_SETS:
125 		if (who[1] != ZFS_DELEG_NA)
126 			return (-1);
127 		if (who[3] != '\0')
128 			return (-1);
129 		break;
130 
131 	case ZFS_DELEG_EVERYONE:
132 	case ZFS_DELEG_EVERYONE_SETS:
133 		if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
134 			return (-1);
135 		if (who[3] != '\0')
136 			return (-1);
137 		break;
138 
139 	default:
140 		return (-1);
141 	}
142 
143 	return (0);
144 }
145 
146 int
147 zfs_deleg_verify_nvlist(nvlist_t *nvp)
148 {
149 	nvpair_t *who, *perm_name;
150 	nvlist_t *perms;
151 	int error;
152 
153 	if (nvp == NULL)
154 		return (-1);
155 
156 	who = nvlist_next_nvpair(nvp, NULL);
157 	if (who == NULL)
158 		return (-1);
159 
160 	do {
161 		if (zfs_validate_who(nvpair_name(who)))
162 			return (-1);
163 
164 		error = nvlist_lookup_nvlist(nvp, nvpair_name(who), &perms);
165 
166 		if (error && error != ENOENT)
167 			return (-1);
168 		if (error == ENOENT)
169 			continue;
170 
171 		perm_name = nvlist_next_nvpair(perms, NULL);
172 		if (perm_name == NULL) {
173 			return (-1);
174 		}
175 		do {
176 			error = zfs_valid_permission_name(
177 			    nvpair_name(perm_name));
178 			if (error)
179 				return (-1);
180 		} while (perm_name = nvlist_next_nvpair(perms, perm_name));
181 	} while (who = nvlist_next_nvpair(nvp, who));
182 	return (0);
183 }
184 
185 /*
186  * Construct the base attribute name.  The base attribute names
187  * are the "key" to locate the jump objects which contain the actual
188  * permissions.  The base attribute names are encoded based on
189  * type of entry and whether it is a local or descendent permission.
190  *
191  * Arguments:
192  * attr - attribute name return string, attribute is assumed to be
193  *        ZFS_MAX_DELEG_NAME long.
194  * type - type of entry to construct
195  * inheritchr - inheritance type (local,descendent, or NA for create and
196  *                               permission set definitions
197  * data - is either a permission set name or a 64 bit uid/gid.
198  */
199 void
200 zfs_deleg_whokey(char *attr, zfs_deleg_who_type_t type,
201     char inheritchr, void *data)
202 {
203 	int len = ZFS_MAX_DELEG_NAME;
204 	uint64_t *id = data;
205 
206 	switch (type) {
207 	case ZFS_DELEG_USER:
208 	case ZFS_DELEG_GROUP:
209 	case ZFS_DELEG_USER_SETS:
210 	case ZFS_DELEG_GROUP_SETS:
211 		(void) snprintf(attr, len, "%c%c%c%lld", type, inheritchr,
212 		    ZFS_DELEG_FIELD_SEP_CHR, (longlong_t)*id);
213 		break;
214 	case ZFS_DELEG_NAMED_SET_SETS:
215 	case ZFS_DELEG_NAMED_SET:
216 		(void) snprintf(attr, len, "%c-%c%s", type,
217 		    ZFS_DELEG_FIELD_SEP_CHR, (char *)data);
218 		break;
219 	case ZFS_DELEG_CREATE:
220 	case ZFS_DELEG_CREATE_SETS:
221 		(void) snprintf(attr, len, "%c-%c", type,
222 		    ZFS_DELEG_FIELD_SEP_CHR);
223 		break;
224 	case ZFS_DELEG_EVERYONE:
225 	case ZFS_DELEG_EVERYONE_SETS:
226 		(void) snprintf(attr, len, "%c%c%c", type, inheritchr,
227 		    ZFS_DELEG_FIELD_SEP_CHR);
228 		break;
229 	default:
230 		ASSERT(!"bad zfs_deleg_who_type_t");
231 	}
232 }
233