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.
26 * Copyright (c) 2013 by Delphix. All rights reserved.
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.
38 * Each function returns 0 on success, -1 on error.
42 #include <sys/systm.h>
47 #include <sys/dsl_dir.h>
48 #include <sys/param.h>
49 #include <sys/nvpair.h>
50 #include "zfs_namecheck.h"
51 #include "zfs_deleg.h"
56 return ((c
>= 'a' && c
<= 'z') ||
57 (c
>= 'A' && c
<= 'Z') ||
58 (c
>= '0' && c
<= '9') ||
59 c
== '-' || c
== '_' || c
== '.' || c
== ':' || c
== ' ');
63 * Snapshot names must be made up of alphanumeric characters plus the following
69 zfs_component_namecheck(const char *path
, namecheck_err_t
*why
, char *what
)
73 if (strlen(path
) >= ZFS_MAX_DATASET_NAME_LEN
) {
75 *why
= NAME_ERR_TOOLONG
;
79 if (path
[0] == '\0') {
81 *why
= NAME_ERR_EMPTY_COMPONENT
;
85 for (loc
= path
; *loc
; loc
++) {
86 if (!valid_char(*loc
)) {
88 *why
= NAME_ERR_INVALCHAR
;
99 * Permissions set name must start with the letter '@' followed by the
100 * same character restrictions as snapshot names, except that the name
101 * cannot exceed 64 characters.
104 permset_namecheck(const char *path
, namecheck_err_t
*why
, char *what
)
106 if (strlen(path
) >= ZFS_PERMSET_MAXLEN
) {
108 *why
= NAME_ERR_TOOLONG
;
112 if (path
[0] != '@') {
114 *why
= NAME_ERR_NO_AT
;
120 return (zfs_component_namecheck(&path
[1], why
, what
));
124 * Entity names must be of the following form:
126 * [component/]*[component][(@|#)component]?
128 * Where each component is made up of alphanumeric characters plus the following
133 * We allow '%' here as we use that character internally to create unique
134 * names for temporary clones (for online recv).
137 entity_namecheck(const char *path
, namecheck_err_t
*why
, char *what
)
139 const char *start
, *end
;
143 * Make sure the name is not too long.
146 if (strlen(path
) >= ZFS_MAX_DATASET_NAME_LEN
) {
148 *why
= NAME_ERR_TOOLONG
;
152 /* Explicitly check for a leading slash. */
153 if (path
[0] == '/') {
155 *why
= NAME_ERR_LEADING_SLASH
;
159 if (path
[0] == '\0') {
161 *why
= NAME_ERR_EMPTY_COMPONENT
;
168 /* Find the end of this component */
170 while (*end
!= '/' && *end
!= '@' && *end
!= '#' &&
174 if (*end
== '\0' && end
[-1] == '/') {
175 /* trailing slashes are not allowed */
177 *why
= NAME_ERR_TRAILING_SLASH
;
181 /* Validate the contents of this component */
182 for (const char *loc
= start
; loc
!= end
; loc
++) {
183 if (!valid_char(*loc
) && *loc
!= '%') {
185 *why
= NAME_ERR_INVALCHAR
;
192 /* Snapshot or bookmark delimiter found */
193 if (*end
== '@' || *end
== '#') {
194 /* Multiple delimiters are not allowed */
195 if (found_delim
!= 0) {
197 *why
= NAME_ERR_MULTIPLE_DELIMITERS
;
204 /* Zero-length components are not allowed */
207 *why
= NAME_ERR_EMPTY_COMPONENT
;
211 /* If we've reached the end of the string, we're OK */
216 * If there is a '/' in a snapshot or bookmark name
217 * then report an error
219 if (*end
== '/' && found_delim
!= 0) {
221 *why
= NAME_ERR_TRAILING_SLASH
;
225 /* Update to the next component */
231 * Dataset is any entity, except bookmark
234 dataset_namecheck(const char *path
, namecheck_err_t
*why
, char *what
)
236 int ret
= entity_namecheck(path
, why
, what
);
238 if (ret
== 0 && strchr(path
, '#') != NULL
) {
240 *why
= NAME_ERR_INVALCHAR
;
250 * mountpoint names must be of the following form:
252 * /[component][/]*[component][/]
255 mountpoint_namecheck(const char *path
, namecheck_err_t
*why
)
257 const char *start
, *end
;
260 * Make sure none of the mountpoint component names are too long.
261 * If a component name is too long then the mkdir of the mountpoint
262 * will fail but then the mountpoint property will be set to a value
263 * that can never be mounted. Better to fail before setting the prop.
264 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
267 if (path
== NULL
|| *path
!= '/') {
269 *why
= NAME_ERR_LEADING_SLASH
;
273 /* Skip leading slash */
277 while (*end
!= '/' && *end
!= '\0')
280 if (end
- start
>= ZFS_MAX_DATASET_NAME_LEN
) {
282 *why
= NAME_ERR_TOOLONG
;
287 } while (*end
!= '\0');
293 * For pool names, we have the same set of valid characters as described in
294 * dataset names, with the additional restriction that the pool name must begin
295 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
296 * that cannot be used.
299 pool_namecheck(const char *pool
, namecheck_err_t
*why
, char *what
)
304 * Make sure the name is not too long.
305 * If we're creating a pool with version >= SPA_VERSION_DSL_SCRUB (v11)
306 * we need to account for additional space needed by the origin ds which
307 * will also be snapshotted: "poolname"+"/"+"$ORIGIN"+"@"+"$ORIGIN".
308 * Play it safe and enforce this limit even if the pool version is < 11
309 * so it can be upgraded without issues.
311 if (strlen(pool
) >= (ZFS_MAX_DATASET_NAME_LEN
- 2 -
312 strlen(ORIGIN_DIR_NAME
) * 2)) {
314 *why
= NAME_ERR_TOOLONG
;
320 if (!valid_char(*c
)) {
322 *why
= NAME_ERR_INVALCHAR
;
330 if (!(*pool
>= 'a' && *pool
<= 'z') &&
331 !(*pool
>= 'A' && *pool
<= 'Z')) {
333 *why
= NAME_ERR_NOLETTER
;
337 if (strcmp(pool
, "mirror") == 0 || strcmp(pool
, "raidz") == 0) {
339 *why
= NAME_ERR_RESERVED
;
343 if (pool
[0] == 'c' && (pool
[1] >= '0' && pool
[1] <= '9')) {
345 *why
= NAME_ERR_DISKLIKE
;