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