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