]> git.proxmox.com Git - mirror_zfs.git/blame - module/zcommon/zfs_deleg.c
Fix vd_path and error in spa_vdev_remove()
[mirror_zfs.git] / module / zcommon / zfs_deleg.c
CommitLineData
34dc7c2f
BB
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/*
572e2857 22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
0b7936d5 23 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
a1d477c2 24 * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
23d70cde 25 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
34dc7c2f
BB
26 */
27
da536844
MA
28#include <sys/zfs_context.h>
29
34dc7c2f 30#if defined(_KERNEL)
34dc7c2f
BB
31#include <sys/sunddi.h>
32#include <sys/ctype.h>
33#else
34#include <stdio.h>
35#include <unistd.h>
34dc7c2f
BB
36#include <libnvpair.h>
37#include <ctype.h>
38#endif
93ce2b4c 39#include <sys/strings.h>
34dc7c2f
BB
40#include <sys/dsl_deleg.h>
41#include "zfs_prop.h"
42#include "zfs_deleg.h"
43#include "zfs_namecheck.h"
44
34dc7c2f 45zfs_deleg_perm_tab_t zfs_deleg_perm_tab[] = {
da536844
MA
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},
a1d477c2 55 {ZFS_DELEG_PERM_REMAP},
da536844
MA
56 {ZFS_DELEG_PERM_RENAME},
57 {ZFS_DELEG_PERM_ROLLBACK},
58 {ZFS_DELEG_PERM_SNAPSHOT},
59 {ZFS_DELEG_PERM_SHARE},
60 {ZFS_DELEG_PERM_SEND},
61 {ZFS_DELEG_PERM_USERPROP},
62 {ZFS_DELEG_PERM_USERQUOTA},
63 {ZFS_DELEG_PERM_GROUPQUOTA},
64 {ZFS_DELEG_PERM_USERUSED},
65 {ZFS_DELEG_PERM_GROUPUSED},
1de321e6
JX
66 {ZFS_DELEG_PERM_USEROBJQUOTA},
67 {ZFS_DELEG_PERM_GROUPOBJQUOTA},
68 {ZFS_DELEG_PERM_USEROBJUSED},
69 {ZFS_DELEG_PERM_GROUPOBJUSED},
da536844
MA
70 {ZFS_DELEG_PERM_HOLD},
71 {ZFS_DELEG_PERM_RELEASE},
b5256303
TC
72 {ZFS_DELEG_PERM_LOAD_KEY},
73 {ZFS_DELEG_PERM_CHANGE_KEY},
9c5167d1
NF
74 {ZFS_DELEG_PERM_PROJECTUSED},
75 {ZFS_DELEG_PERM_PROJECTQUOTA},
76 {ZFS_DELEG_PERM_PROJECTOBJUSED},
77 {ZFS_DELEG_PERM_PROJECTOBJQUOTA},
da536844 78 {NULL}
34dc7c2f
BB
79};
80
81static int
82zfs_valid_permission_name(const char *perm)
83{
84 if (zfs_deleg_canonicalize_perm(perm))
85 return (0);
86
87 return (permset_namecheck(perm, NULL, NULL));
88}
89
90const char *
91zfs_deleg_canonicalize_perm(const char *perm)
92{
93 int i;
94 zfs_prop_t prop;
95
96 for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
97 if (strcmp(perm, zfs_deleg_perm_tab[i].z_perm) == 0)
98 return (perm);
99 }
100
101 prop = zfs_name_to_prop(perm);
102 if (prop != ZPROP_INVAL && zfs_prop_delegatable(prop))
103 return (zfs_prop_to_name(prop));
104 return (NULL);
105
106}
107
108static int
109zfs_validate_who(char *who)
110{
111 char *p;
112
113 if (who[2] != ZFS_DELEG_FIELD_SEP_CHR)
114 return (-1);
115
116 switch (who[0]) {
117 case ZFS_DELEG_USER:
118 case ZFS_DELEG_GROUP:
119 case ZFS_DELEG_USER_SETS:
120 case ZFS_DELEG_GROUP_SETS:
121 if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
122 return (-1);
123 for (p = &who[3]; *p; p++)
124 if (!isdigit(*p))
125 return (-1);
126 break;
127
128 case ZFS_DELEG_NAMED_SET:
129 case ZFS_DELEG_NAMED_SET_SETS:
130 if (who[1] != ZFS_DELEG_NA)
131 return (-1);
132 return (permset_namecheck(&who[3], NULL, NULL));
133
134 case ZFS_DELEG_CREATE:
135 case ZFS_DELEG_CREATE_SETS:
136 if (who[1] != ZFS_DELEG_NA)
137 return (-1);
138 if (who[3] != '\0')
139 return (-1);
140 break;
141
142 case ZFS_DELEG_EVERYONE:
143 case ZFS_DELEG_EVERYONE_SETS:
144 if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
145 return (-1);
146 if (who[3] != '\0')
147 return (-1);
148 break;
149
150 default:
151 return (-1);
152 }
153
154 return (0);
155}
156
157int
158zfs_deleg_verify_nvlist(nvlist_t *nvp)
159{
160 nvpair_t *who, *perm_name;
161 nvlist_t *perms;
162 int error;
163
164 if (nvp == NULL)
165 return (-1);
166
167 who = nvlist_next_nvpair(nvp, NULL);
168 if (who == NULL)
169 return (-1);
170
171 do {
172 if (zfs_validate_who(nvpair_name(who)))
173 return (-1);
174
175 error = nvlist_lookup_nvlist(nvp, nvpair_name(who), &perms);
176
177 if (error && error != ENOENT)
178 return (-1);
179 if (error == ENOENT)
180 continue;
181
182 perm_name = nvlist_next_nvpair(perms, NULL);
183 if (perm_name == NULL) {
184 return (-1);
185 }
186 do {
187 error = zfs_valid_permission_name(
188 nvpair_name(perm_name));
189 if (error)
190 return (-1);
23d70cde
GM
191 } while ((perm_name = nvlist_next_nvpair(perms, perm_name))
192 != NULL);
193 } while ((who = nvlist_next_nvpair(nvp, who)) != NULL);
34dc7c2f
BB
194 return (0);
195}
196
197/*
198 * Construct the base attribute name. The base attribute names
199 * are the "key" to locate the jump objects which contain the actual
200 * permissions. The base attribute names are encoded based on
201 * type of entry and whether it is a local or descendent permission.
202 *
203 * Arguments:
204 * attr - attribute name return string, attribute is assumed to be
205 * ZFS_MAX_DELEG_NAME long.
206 * type - type of entry to construct
207 * inheritchr - inheritance type (local,descendent, or NA for create and
208 * permission set definitions
209 * data - is either a permission set name or a 64 bit uid/gid.
210 */
211void
212zfs_deleg_whokey(char *attr, zfs_deleg_who_type_t type,
213 char inheritchr, void *data)
214{
215 int len = ZFS_MAX_DELEG_NAME;
216 uint64_t *id = data;
217
218 switch (type) {
219 case ZFS_DELEG_USER:
220 case ZFS_DELEG_GROUP:
221 case ZFS_DELEG_USER_SETS:
222 case ZFS_DELEG_GROUP_SETS:
223 (void) snprintf(attr, len, "%c%c%c%lld", type, inheritchr,
224 ZFS_DELEG_FIELD_SEP_CHR, (longlong_t)*id);
225 break;
226 case ZFS_DELEG_NAMED_SET_SETS:
227 case ZFS_DELEG_NAMED_SET:
228 (void) snprintf(attr, len, "%c-%c%s", type,
229 ZFS_DELEG_FIELD_SEP_CHR, (char *)data);
230 break;
231 case ZFS_DELEG_CREATE:
232 case ZFS_DELEG_CREATE_SETS:
233 (void) snprintf(attr, len, "%c-%c", type,
234 ZFS_DELEG_FIELD_SEP_CHR);
235 break;
236 case ZFS_DELEG_EVERYONE:
237 case ZFS_DELEG_EVERYONE_SETS:
238 (void) snprintf(attr, len, "%c%c%c", type, inheritchr,
239 ZFS_DELEG_FIELD_SEP_CHR);
240 break;
241 default:
46364cb2 242 ASSERT(!"bad zfs_deleg_who_type_t");
34dc7c2f
BB
243 }
244}
c28b2279 245
93ce2b4c 246#if defined(_KERNEL)
c28b2279
BB
247EXPORT_SYMBOL(zfs_deleg_verify_nvlist);
248EXPORT_SYMBOL(zfs_deleg_whokey);
249EXPORT_SYMBOL(zfs_deleg_canonicalize_perm);
250#endif