]>
git.proxmox.com Git - mirror_zfs.git/blob - zfs/lib/libzcommon/zfs_namecheck.c
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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Common name validation routines for ZFS. These routines are shared by the
30 * userland code as well as the ioctl() layer to ensure that we don't
31 * inadvertently expose a hole through direct ioctl()s that never gets tested.
32 * In userland, however, we want significantly more information about _why_ the
33 * name is invalid. In the kernel, we only care whether it's valid or not.
34 * Each routine therefore takes a 'namecheck_err_t' which describes exactly why
35 * the name failed to validate.
37 * Each function returns 0 on success, -1 on error.
41 #include <sys/systm.h>
46 #include <sys/param.h>
47 #include <sys/nvpair.h>
48 #include "zfs_namecheck.h"
49 #include "zfs_deleg.h"
54 return ((c
>= 'a' && c
<= 'z') ||
55 (c
>= 'A' && c
<= 'Z') ||
56 (c
>= '0' && c
<= '9') ||
57 c
== '-' || c
== '_' || c
== '.' || c
== ':' || c
== ' ');
61 * Snapshot names must be made up of alphanumeric characters plus the following
67 snapshot_namecheck(const char *path
, namecheck_err_t
*why
, char *what
)
71 if (strlen(path
) >= MAXNAMELEN
) {
73 *why
= NAME_ERR_TOOLONG
;
77 if (path
[0] == '\0') {
79 *why
= NAME_ERR_EMPTY_COMPONENT
;
83 for (loc
= path
; *loc
; loc
++) {
84 if (!valid_char(*loc
)) {
86 *why
= NAME_ERR_INVALCHAR
;
97 * Permissions set name must start with the letter '@' followed by the
98 * same character restrictions as snapshot names, except that the name
99 * cannot exceed 64 characters.
102 permset_namecheck(const char *path
, namecheck_err_t
*why
, char *what
)
104 if (strlen(path
) >= ZFS_PERMSET_MAXLEN
) {
106 *why
= NAME_ERR_TOOLONG
;
110 if (path
[0] != '@') {
112 *why
= NAME_ERR_NO_AT
;
118 return (snapshot_namecheck(&path
[1], why
, what
));
122 * Dataset names must be of the following form:
124 * [component][/]*[component][@component]
126 * Where each component is made up of alphanumeric characters plus the following
131 * We allow '%' here as we use that character internally to create unique
132 * names for temporary clones (for online recv).
135 dataset_namecheck(const char *path
, namecheck_err_t
*why
, char *what
)
137 const char *loc
, *end
;
141 * Make sure the name is not too long.
143 * ZFS_MAXNAMELEN is the maximum dataset length used in the userland
144 * which is the same as MAXNAMELEN used in the kernel.
145 * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all
146 * places using MAXNAMELEN.
149 if (strlen(path
) >= MAXNAMELEN
) {
151 *why
= NAME_ERR_TOOLONG
;
155 /* Explicitly check for a leading slash. */
156 if (path
[0] == '/') {
158 *why
= NAME_ERR_LEADING_SLASH
;
162 if (path
[0] == '\0') {
164 *why
= NAME_ERR_EMPTY_COMPONENT
;
171 /* Find the end of this component */
173 while (*end
!= '/' && *end
!= '@' && *end
!= '\0')
176 if (*end
== '\0' && end
[-1] == '/') {
177 /* trailing slashes are not allowed */
179 *why
= NAME_ERR_TRAILING_SLASH
;
183 /* Zero-length components are not allowed */
187 * Make sure this is really a zero-length
188 * component and not a '@@'.
190 if (*end
== '@' && found_snapshot
) {
191 *why
= NAME_ERR_MULTIPLE_AT
;
193 *why
= NAME_ERR_EMPTY_COMPONENT
;
200 /* Validate the contents of this component */
202 if (!valid_char(*loc
) && *loc
!= '%') {
204 *why
= NAME_ERR_INVALCHAR
;
212 /* If we've reached the end of the string, we're OK */
218 * If we've found an @ symbol, indicate that we're in
219 * the snapshot component, and report a second '@'
220 * character as an error.
222 if (found_snapshot
) {
224 *why
= NAME_ERR_MULTIPLE_AT
;
232 * If there is a '/' in a snapshot name
233 * then report an error
235 if (*end
== '/' && found_snapshot
) {
237 *why
= NAME_ERR_TRAILING_SLASH
;
241 /* Update to the next component */
248 * mountpoint names must be of the following form:
250 * /[component][/]*[component][/]
253 mountpoint_namecheck(const char *path
, namecheck_err_t
*why
)
255 const char *start
, *end
;
258 * Make sure none of the mountpoint component names are too long.
259 * If a component name is too long then the mkdir of the mountpoint
260 * will fail but then the mountpoint property will be set to a value
261 * that can never be mounted. Better to fail before setting the prop.
262 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
265 if (path
== NULL
|| *path
!= '/') {
267 *why
= NAME_ERR_LEADING_SLASH
;
271 /* Skip leading slash */
275 while (*end
!= '/' && *end
!= '\0')
278 if (end
- start
>= MAXNAMELEN
) {
280 *why
= NAME_ERR_TOOLONG
;
285 } while (*end
!= '\0');
291 * For pool names, we have the same set of valid characters as described in
292 * dataset names, with the additional restriction that the pool name must begin
293 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
294 * that cannot be used.
297 pool_namecheck(const char *pool
, namecheck_err_t
*why
, char *what
)
302 * Make sure the name is not too long.
304 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
305 * which is the same as MAXNAMELEN used in the kernel.
306 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
307 * places using MAXNAMELEN.
309 if (strlen(pool
) >= MAXNAMELEN
) {
311 *why
= NAME_ERR_TOOLONG
;
317 if (!valid_char(*c
)) {
319 *why
= NAME_ERR_INVALCHAR
;
327 if (!(*pool
>= 'a' && *pool
<= 'z') &&
328 !(*pool
>= 'A' && *pool
<= 'Z')) {
330 *why
= NAME_ERR_NOLETTER
;
334 if (strcmp(pool
, "mirror") == 0 || strcmp(pool
, "raidz") == 0) {
336 *why
= NAME_ERR_RESERVED
;
340 if (pool
[0] == 'c' && (pool
[1] >= '0' && pool
[1] <= '9')) {
342 *why
= NAME_ERR_DISKLIKE
;
350 * Check if the dataset name is private for internal usage.
351 * '$' is reserved for internal dataset names. e.g. "$MOS"
353 * Return 1 if the given name is used internally.
354 * Return 0 if it is not.
357 dataset_name_hidden(const char *name
)
359 if (strchr(name
, '$') != NULL
)