]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/zcommon/zfs_deleg.c
OpenZFS 7500 - Simplify dbuf_free_range by removing dn_unlisted_l0_blkid
[FreeBSD/FreeBSD.git] / module / zcommon / zfs_deleg.c
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_USEROBJQUOTA},
66         {ZFS_DELEG_PERM_GROUPOBJQUOTA},
67         {ZFS_DELEG_PERM_USEROBJUSED},
68         {ZFS_DELEG_PERM_GROUPOBJUSED},
69         {ZFS_DELEG_PERM_HOLD},
70         {ZFS_DELEG_PERM_RELEASE},
71         {NULL}
72 };
73
74 static int
75 zfs_valid_permission_name(const char *perm)
76 {
77         if (zfs_deleg_canonicalize_perm(perm))
78                 return (0);
79
80         return (permset_namecheck(perm, NULL, NULL));
81 }
82
83 const char *
84 zfs_deleg_canonicalize_perm(const char *perm)
85 {
86         int i;
87         zfs_prop_t prop;
88
89         for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
90                 if (strcmp(perm, zfs_deleg_perm_tab[i].z_perm) == 0)
91                         return (perm);
92         }
93
94         prop = zfs_name_to_prop(perm);
95         if (prop != ZPROP_INVAL && zfs_prop_delegatable(prop))
96                 return (zfs_prop_to_name(prop));
97         return (NULL);
98
99 }
100
101 static int
102 zfs_validate_who(char *who)
103 {
104         char *p;
105
106         if (who[2] != ZFS_DELEG_FIELD_SEP_CHR)
107                 return (-1);
108
109         switch (who[0]) {
110         case ZFS_DELEG_USER:
111         case ZFS_DELEG_GROUP:
112         case ZFS_DELEG_USER_SETS:
113         case ZFS_DELEG_GROUP_SETS:
114                 if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
115                         return (-1);
116                 for (p = &who[3]; *p; p++)
117                         if (!isdigit(*p))
118                                 return (-1);
119                 break;
120
121         case ZFS_DELEG_NAMED_SET:
122         case ZFS_DELEG_NAMED_SET_SETS:
123                 if (who[1] != ZFS_DELEG_NA)
124                         return (-1);
125                 return (permset_namecheck(&who[3], NULL, NULL));
126
127         case ZFS_DELEG_CREATE:
128         case ZFS_DELEG_CREATE_SETS:
129                 if (who[1] != ZFS_DELEG_NA)
130                         return (-1);
131                 if (who[3] != '\0')
132                         return (-1);
133                 break;
134
135         case ZFS_DELEG_EVERYONE:
136         case ZFS_DELEG_EVERYONE_SETS:
137                 if (who[1] != ZFS_DELEG_LOCAL && who[1] != ZFS_DELEG_DESCENDENT)
138                         return (-1);
139                 if (who[3] != '\0')
140                         return (-1);
141                 break;
142
143         default:
144                 return (-1);
145         }
146
147         return (0);
148 }
149
150 int
151 zfs_deleg_verify_nvlist(nvlist_t *nvp)
152 {
153         nvpair_t *who, *perm_name;
154         nvlist_t *perms;
155         int error;
156
157         if (nvp == NULL)
158                 return (-1);
159
160         who = nvlist_next_nvpair(nvp, NULL);
161         if (who == NULL)
162                 return (-1);
163
164         do {
165                 if (zfs_validate_who(nvpair_name(who)))
166                         return (-1);
167
168                 error = nvlist_lookup_nvlist(nvp, nvpair_name(who), &perms);
169
170                 if (error && error != ENOENT)
171                         return (-1);
172                 if (error == ENOENT)
173                         continue;
174
175                 perm_name = nvlist_next_nvpair(perms, NULL);
176                 if (perm_name == NULL) {
177                         return (-1);
178                 }
179                 do {
180                         error = zfs_valid_permission_name(
181                             nvpair_name(perm_name));
182                         if (error)
183                                 return (-1);
184                 } while ((perm_name = nvlist_next_nvpair(perms, perm_name)));
185         } while ((who = nvlist_next_nvpair(nvp, who)));
186         return (0);
187 }
188
189 /*
190  * Construct the base attribute name.  The base attribute names
191  * are the "key" to locate the jump objects which contain the actual
192  * permissions.  The base attribute names are encoded based on
193  * type of entry and whether it is a local or descendent permission.
194  *
195  * Arguments:
196  * attr - attribute name return string, attribute is assumed to be
197  *        ZFS_MAX_DELEG_NAME long.
198  * type - type of entry to construct
199  * inheritchr - inheritance type (local,descendent, or NA for create and
200  *                               permission set definitions
201  * data - is either a permission set name or a 64 bit uid/gid.
202  */
203 void
204 zfs_deleg_whokey(char *attr, zfs_deleg_who_type_t type,
205     char inheritchr, void *data)
206 {
207         int len = ZFS_MAX_DELEG_NAME;
208         uint64_t *id = data;
209
210         switch (type) {
211         case ZFS_DELEG_USER:
212         case ZFS_DELEG_GROUP:
213         case ZFS_DELEG_USER_SETS:
214         case ZFS_DELEG_GROUP_SETS:
215                 (void) snprintf(attr, len, "%c%c%c%lld", type, inheritchr,
216                     ZFS_DELEG_FIELD_SEP_CHR, (longlong_t)*id);
217                 break;
218         case ZFS_DELEG_NAMED_SET_SETS:
219         case ZFS_DELEG_NAMED_SET:
220                 (void) snprintf(attr, len, "%c-%c%s", type,
221                     ZFS_DELEG_FIELD_SEP_CHR, (char *)data);
222                 break;
223         case ZFS_DELEG_CREATE:
224         case ZFS_DELEG_CREATE_SETS:
225                 (void) snprintf(attr, len, "%c-%c", type,
226                     ZFS_DELEG_FIELD_SEP_CHR);
227                 break;
228         case ZFS_DELEG_EVERYONE:
229         case ZFS_DELEG_EVERYONE_SETS:
230                 (void) snprintf(attr, len, "%c%c%c", type, inheritchr,
231                     ZFS_DELEG_FIELD_SEP_CHR);
232                 break;
233         default:
234                 cmn_err(CE_PANIC, "bad zfs_deleg_who_type_t %d", type);
235         }
236 }
237
238 #if defined(_KERNEL) && defined(HAVE_SPL)
239 EXPORT_SYMBOL(zfs_deleg_verify_nvlist);
240 EXPORT_SYMBOL(zfs_deleg_whokey);
241 EXPORT_SYMBOL(zfs_deleg_canonicalize_perm);
242 #endif