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