]> granicus.if.org Git - zfs/blob - module/zcommon/zfs_namecheck.c
Rework of fletcher_4 module
[zfs] / module / zcommon / zfs_namecheck.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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2013 by Delphix. All rights reserved.
27  */
28
29 /*
30  * Common name validation routines for ZFS.  These routines are shared by the
31  * userland code as well as the ioctl() layer to ensure that we don't
32  * inadvertently expose a hole through direct ioctl()s that never gets tested.
33  * In userland, however, we want significantly more information about _why_ the
34  * name is invalid.  In the kernel, we only care whether it's valid or not.
35  * Each routine therefore takes a 'namecheck_err_t' which describes exactly why
36  * the name failed to validate.
37  *
38  * Each function returns 0 on success, -1 on error.
39  */
40
41 #if defined(_KERNEL)
42 #include <sys/systm.h>
43 #else
44 #include <string.h>
45 #endif
46
47 #include <sys/param.h>
48 #include <sys/nvpair.h>
49 #include "zfs_namecheck.h"
50 #include "zfs_deleg.h"
51
52 static int
53 valid_char(char c)
54 {
55         return ((c >= 'a' && c <= 'z') ||
56             (c >= 'A' && c <= 'Z') ||
57             (c >= '0' && c <= '9') ||
58             c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
59 }
60
61 /*
62  * Snapshot names must be made up of alphanumeric characters plus the following
63  * characters:
64  *
65  *      [-_.: ]
66  */
67 int
68 zfs_component_namecheck(const char *path, namecheck_err_t *why, char *what)
69 {
70         const char *loc;
71
72         if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
73                 if (why)
74                         *why = NAME_ERR_TOOLONG;
75                 return (-1);
76         }
77
78         if (path[0] == '\0') {
79                 if (why)
80                         *why = NAME_ERR_EMPTY_COMPONENT;
81                 return (-1);
82         }
83
84         for (loc = path; *loc; loc++) {
85                 if (!valid_char(*loc)) {
86                         if (why) {
87                                 *why = NAME_ERR_INVALCHAR;
88                                 *what = *loc;
89                         }
90                         return (-1);
91                 }
92         }
93         return (0);
94 }
95
96
97 /*
98  * Permissions set name must start with the letter '@' followed by the
99  * same character restrictions as snapshot names, except that the name
100  * cannot exceed 64 characters.
101  */
102 int
103 permset_namecheck(const char *path, namecheck_err_t *why, char *what)
104 {
105         if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
106                 if (why)
107                         *why = NAME_ERR_TOOLONG;
108                 return (-1);
109         }
110
111         if (path[0] != '@') {
112                 if (why) {
113                         *why = NAME_ERR_NO_AT;
114                         *what = path[0];
115                 }
116                 return (-1);
117         }
118
119         return (zfs_component_namecheck(&path[1], why, what));
120 }
121
122 /*
123  * Dataset names must be of the following form:
124  *
125  *      [component][/]*[component][@component]
126  *
127  * Where each component is made up of alphanumeric characters plus the following
128  * characters:
129  *
130  *      [-_.:%]
131  *
132  * We allow '%' here as we use that character internally to create unique
133  * names for temporary clones (for online recv).
134  */
135 int
136 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
137 {
138         const char *loc, *end;
139         int found_snapshot;
140
141         /*
142          * Make sure the name is not too long.
143          */
144         if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
145                 if (why)
146                         *why = NAME_ERR_TOOLONG;
147                 return (-1);
148         }
149
150         /* Explicitly check for a leading slash.  */
151         if (path[0] == '/') {
152                 if (why)
153                         *why = NAME_ERR_LEADING_SLASH;
154                 return (-1);
155         }
156
157         if (path[0] == '\0') {
158                 if (why)
159                         *why = NAME_ERR_EMPTY_COMPONENT;
160                 return (-1);
161         }
162
163         loc = path;
164         found_snapshot = 0;
165         for (;;) {
166                 /* Find the end of this component */
167                 end = loc;
168                 while (*end != '/' && *end != '@' && *end != '\0')
169                         end++;
170
171                 if (*end == '\0' && end[-1] == '/') {
172                         /* trailing slashes are not allowed */
173                         if (why)
174                                 *why = NAME_ERR_TRAILING_SLASH;
175                         return (-1);
176                 }
177
178                 /* Zero-length components are not allowed */
179                 if (loc == end) {
180                         if (why) {
181                                 /*
182                                  * Make sure this is really a zero-length
183                                  * component and not a '@@'.
184                                  */
185                                 if (*end == '@' && found_snapshot) {
186                                         *why = NAME_ERR_MULTIPLE_AT;
187                                 } else {
188                                         *why = NAME_ERR_EMPTY_COMPONENT;
189                                 }
190                         }
191
192                         return (-1);
193                 }
194
195                 /* Validate the contents of this component */
196                 while (loc != end) {
197                         if (!valid_char(*loc) && *loc != '%') {
198                                 if (why) {
199                                         *why = NAME_ERR_INVALCHAR;
200                                         *what = *loc;
201                                 }
202                                 return (-1);
203                         }
204                         loc++;
205                 }
206
207                 /* If we've reached the end of the string, we're OK */
208                 if (*end == '\0')
209                         return (0);
210
211                 if (*end == '@') {
212                         /*
213                          * If we've found an @ symbol, indicate that we're in
214                          * the snapshot component, and report a second '@'
215                          * character as an error.
216                          */
217                         if (found_snapshot) {
218                                 if (why)
219                                         *why = NAME_ERR_MULTIPLE_AT;
220                                 return (-1);
221                         }
222
223                         found_snapshot = 1;
224                 }
225
226                 /*
227                  * If there is a '/' in a snapshot name
228                  * then report an error
229                  */
230                 if (*end == '/' && found_snapshot) {
231                         if (why)
232                                 *why = NAME_ERR_TRAILING_SLASH;
233                         return (-1);
234                 }
235
236                 /* Update to the next component */
237                 loc = end + 1;
238         }
239 }
240
241
242 /*
243  * mountpoint names must be of the following form:
244  *
245  *      /[component][/]*[component][/]
246  */
247 int
248 mountpoint_namecheck(const char *path, namecheck_err_t *why)
249 {
250         const char *start, *end;
251
252         /*
253          * Make sure none of the mountpoint component names are too long.
254          * If a component name is too long then the mkdir of the mountpoint
255          * will fail but then the mountpoint property will be set to a value
256          * that can never be mounted.  Better to fail before setting the prop.
257          * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
258          */
259
260         if (path == NULL || *path != '/') {
261                 if (why)
262                         *why = NAME_ERR_LEADING_SLASH;
263                 return (-1);
264         }
265
266         /* Skip leading slash  */
267         start = &path[1];
268         do {
269                 end = start;
270                 while (*end != '/' && *end != '\0')
271                         end++;
272
273                 if (end - start >= ZFS_MAX_DATASET_NAME_LEN) {
274                         if (why)
275                                 *why = NAME_ERR_TOOLONG;
276                         return (-1);
277                 }
278                 start = end + 1;
279
280         } while (*end != '\0');
281
282         return (0);
283 }
284
285 /*
286  * For pool names, we have the same set of valid characters as described in
287  * dataset names, with the additional restriction that the pool name must begin
288  * with a letter.  The pool names 'raidz' and 'mirror' are also reserved names
289  * that cannot be used.
290  */
291 int
292 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
293 {
294         const char *c;
295
296         /*
297          * Make sure the name is not too long.
298          */
299         if (strlen(pool) >= ZFS_MAX_DATASET_NAME_LEN) {
300                 if (why)
301                         *why = NAME_ERR_TOOLONG;
302                 return (-1);
303         }
304
305         c = pool;
306         while (*c != '\0') {
307                 if (!valid_char(*c)) {
308                         if (why) {
309                                 *why = NAME_ERR_INVALCHAR;
310                                 *what = *c;
311                         }
312                         return (-1);
313                 }
314                 c++;
315         }
316
317         if (!(*pool >= 'a' && *pool <= 'z') &&
318             !(*pool >= 'A' && *pool <= 'Z')) {
319                 if (why)
320                         *why = NAME_ERR_NOLETTER;
321                 return (-1);
322         }
323
324         if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
325                 if (why)
326                         *why = NAME_ERR_RESERVED;
327                 return (-1);
328         }
329
330         if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
331                 if (why)
332                         *why = NAME_ERR_DISKLIKE;
333                 return (-1);
334         }
335
336         return (0);
337 }
338
339 #if defined(_KERNEL) && defined(HAVE_SPL)
340 EXPORT_SYMBOL(pool_namecheck);
341 EXPORT_SYMBOL(dataset_namecheck);
342 EXPORT_SYMBOL(zfs_component_namecheck);
343 #endif