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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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.
35 * Each function returns 0 on success, -1 on error.
39 #include <sys/systm.h>
44 #include <sys/param.h>
45 #include <sys/nvpair.h>
46 #include "zfs_namecheck.h"
47 #include "zfs_deleg.h"
52 return ((c >= 'a' && c <= 'z') ||
53 (c >= 'A' && c <= 'Z') ||
54 (c >= '0' && c <= '9') ||
55 c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
59 * Snapshot names must be made up of alphanumeric characters plus the following
65 snapshot_namecheck(const char *path, namecheck_err_t *why, char *what)
69 if (strlen(path) >= MAXNAMELEN) {
71 *why = NAME_ERR_TOOLONG;
75 if (path[0] == '\0') {
77 *why = NAME_ERR_EMPTY_COMPONENT;
81 for (loc = path; *loc; loc++) {
82 if (!valid_char(*loc)) {
84 *why = NAME_ERR_INVALCHAR;
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.
100 permset_namecheck(const char *path, namecheck_err_t *why, char *what)
102 if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
104 *why = NAME_ERR_TOOLONG;
108 if (path[0] != '@') {
110 *why = NAME_ERR_NO_AT;
116 return (snapshot_namecheck(&path[1], why, what));
120 * Dataset names must be of the following form:
122 * [component][/]*[component][@component]
124 * Where each component is made up of alphanumeric characters plus the following
129 * We allow '%' here as we use that character internally to create unique
130 * names for temporary clones (for online recv).
133 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
135 const char *loc, *end;
139 * Make sure the name is not too long.
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.
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.
153 * <pool>/<dataset> + <partition> + <newline>
156 #ifdef HAVE_KOBJ_NAME_LEN
157 if (strlen(path) > 18) {
159 if (strlen(path) >= MAXNAMELEN) {
160 #endif /* HAVE_KOBJ_NAME_LEN */
162 *why = NAME_ERR_TOOLONG;
166 /* Explicitly check for a leading slash. */
167 if (path[0] == '/') {
169 *why = NAME_ERR_LEADING_SLASH;
173 if (path[0] == '\0') {
175 *why = NAME_ERR_EMPTY_COMPONENT;
182 /* Find the end of this component */
184 while (*end != '/' && *end != '@' && *end != '\0')
187 if (*end == '\0' && end[-1] == '/') {
188 /* trailing slashes are not allowed */
190 *why = NAME_ERR_TRAILING_SLASH;
194 /* Zero-length components are not allowed */
198 * Make sure this is really a zero-length
199 * component and not a '@@'.
201 if (*end == '@' && found_snapshot) {
202 *why = NAME_ERR_MULTIPLE_AT;
204 *why = NAME_ERR_EMPTY_COMPONENT;
211 /* Validate the contents of this component */
213 if (!valid_char(*loc) && *loc != '%') {
215 *why = NAME_ERR_INVALCHAR;
223 /* If we've reached the end of the string, we're OK */
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.
233 if (found_snapshot) {
235 *why = NAME_ERR_MULTIPLE_AT;
243 * If there is a '/' in a snapshot name
244 * then report an error
246 if (*end == '/' && found_snapshot) {
248 *why = NAME_ERR_TRAILING_SLASH;
252 /* Update to the next component */
259 * mountpoint names must be of the following form:
261 * /[component][/]*[component][/]
264 mountpoint_namecheck(const char *path, namecheck_err_t *why)
266 const char *start, *end;
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.
276 if (path == NULL || *path != '/') {
278 *why = NAME_ERR_LEADING_SLASH;
282 /* Skip leading slash */
286 while (*end != '/' && *end != '\0')
289 if (end - start >= MAXNAMELEN) {
291 *why = NAME_ERR_TOOLONG;
296 } while (*end != '\0');
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.
308 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
313 * Make sure the name is not too long.
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.
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.
327 * <pool>/<dataset> + <partition> + <newline>
330 #ifdef HAVE_KOBJ_NAME_LEN
331 if (strlen(pool) > 8) {
333 if (strlen(pool) >= MAXNAMELEN) {
334 #endif /* HAVE_KOBJ_NAME_LEN */
336 *why = NAME_ERR_TOOLONG;
342 if (!valid_char(*c)) {
344 *why = NAME_ERR_INVALCHAR;
352 if (!(*pool >= 'a' && *pool <= 'z') &&
353 !(*pool >= 'A' && *pool <= 'Z')) {
355 *why = NAME_ERR_NOLETTER;
359 if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
361 *why = NAME_ERR_RESERVED;
365 if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
367 *why = NAME_ERR_DISKLIKE;
374 #if defined(_KERNEL) && defined(HAVE_SPL)
375 EXPORT_SYMBOL(snapshot_namecheck);
376 EXPORT_SYMBOL(pool_namecheck);
377 EXPORT_SYMBOL(dataset_namecheck);