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.
147 if (strlen(path) >= MAXNAMELEN) {
149 *why = NAME_ERR_TOOLONG;
153 /* Explicitly check for a leading slash. */
154 if (path[0] == '/') {
156 *why = NAME_ERR_LEADING_SLASH;
160 if (path[0] == '\0') {
162 *why = NAME_ERR_EMPTY_COMPONENT;
169 /* Find the end of this component */
171 while (*end != '/' && *end != '@' && *end != '\0')
174 if (*end == '\0' && end[-1] == '/') {
175 /* trailing slashes are not allowed */
177 *why = NAME_ERR_TRAILING_SLASH;
181 /* Zero-length components are not allowed */
185 * Make sure this is really a zero-length
186 * component and not a '@@'.
188 if (*end == '@' && found_snapshot) {
189 *why = NAME_ERR_MULTIPLE_AT;
191 *why = NAME_ERR_EMPTY_COMPONENT;
198 /* Validate the contents of this component */
200 if (!valid_char(*loc) && *loc != '%') {
202 *why = NAME_ERR_INVALCHAR;
210 /* If we've reached the end of the string, we're OK */
216 * If we've found an @ symbol, indicate that we're in
217 * the snapshot component, and report a second '@'
218 * character as an error.
220 if (found_snapshot) {
222 *why = NAME_ERR_MULTIPLE_AT;
230 * If there is a '/' in a snapshot name
231 * then report an error
233 if (*end == '/' && found_snapshot) {
235 *why = NAME_ERR_TRAILING_SLASH;
239 /* Update to the next component */
246 * mountpoint names must be of the following form:
248 * /[component][/]*[component][/]
251 mountpoint_namecheck(const char *path, namecheck_err_t *why)
253 const char *start, *end;
256 * Make sure none of the mountpoint component names are too long.
257 * If a component name is too long then the mkdir of the mountpoint
258 * will fail but then the mountpoint property will be set to a value
259 * that can never be mounted. Better to fail before setting the prop.
260 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
263 if (path == NULL || *path != '/') {
265 *why = NAME_ERR_LEADING_SLASH;
269 /* Skip leading slash */
273 while (*end != '/' && *end != '\0')
276 if (end - start >= MAXNAMELEN) {
278 *why = NAME_ERR_TOOLONG;
283 } while (*end != '\0');
289 * For pool names, we have the same set of valid characters as described in
290 * dataset names, with the additional restriction that the pool name must begin
291 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
292 * that cannot be used.
295 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
300 * Make sure the name is not too long.
302 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
303 * which is the same as MAXNAMELEN used in the kernel.
304 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
305 * places using MAXNAMELEN.
307 if (strlen(pool) >= MAXNAMELEN) {
309 *why = NAME_ERR_TOOLONG;
315 if (!valid_char(*c)) {
317 *why = NAME_ERR_INVALCHAR;
325 if (!(*pool >= 'a' && *pool <= 'z') &&
326 !(*pool >= 'A' && *pool <= 'Z')) {
328 *why = NAME_ERR_NOLETTER;
332 if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
334 *why = NAME_ERR_RESERVED;
338 if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
340 *why = NAME_ERR_DISKLIKE;