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 2007 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 "zfs_namecheck.h"
52 return ((c >= 'a' && c <= 'z') ||
53 (c >= 'A' && c <= 'Z') ||
54 (c >= '0' && c <= '9') ||
55 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;
94 * Dataset names must be of the following form:
96 * [component][/]*[component][@component]
98 * Where each component is made up of alphanumeric characters plus the following
104 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
106 const char *loc, *end;
110 * Make sure the name is not too long.
112 * ZFS_MAXNAMELEN is the maximum dataset length used in the userland
113 * which is the same as MAXNAMELEN used in the kernel.
114 * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all
115 * places using MAXNAMELEN.
117 if (strlen(path) >= MAXNAMELEN) {
119 *why = NAME_ERR_TOOLONG;
123 /* Explicitly check for a leading slash. */
124 if (path[0] == '/') {
126 *why = NAME_ERR_LEADING_SLASH;
130 if (path[0] == '\0') {
132 *why = NAME_ERR_EMPTY_COMPONENT;
139 /* Find the end of this component */
141 while (*end != '/' && *end != '@' && *end != '\0')
144 if (*end == '\0' && end[-1] == '/') {
145 /* trailing slashes are not allowed */
147 *why = NAME_ERR_TRAILING_SLASH;
151 /* Zero-length components are not allowed */
155 * Make sure this is really a zero-length
156 * component and not a '@@'.
158 if (*end == '@' && found_snapshot) {
159 *why = NAME_ERR_MULTIPLE_AT;
161 *why = NAME_ERR_EMPTY_COMPONENT;
168 /* Validate the contents of this component */
170 if (!valid_char(*loc)) {
172 *why = NAME_ERR_INVALCHAR;
180 /* If we've reached the end of the string, we're OK */
186 * If we've found an @ symbol, indicate that we're in
187 * the snapshot component, and report a second '@'
188 * character as an error.
190 if (found_snapshot) {
192 *why = NAME_ERR_MULTIPLE_AT;
200 * If there is a '/' in a snapshot name
201 * then report an error
203 if (*end == '/' && found_snapshot) {
205 *why = NAME_ERR_TRAILING_SLASH;
209 /* Update to the next component */
215 * For pool names, we have the same set of valid characters as described in
216 * dataset names, with the additional restriction that the pool name must begin
217 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
218 * that cannot be used.
221 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
226 * Make sure the name is not too long.
228 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
229 * which is the same as MAXNAMELEN used in the kernel.
230 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
231 * places using MAXNAMELEN.
233 if (strlen(pool) >= MAXNAMELEN) {
235 *why = NAME_ERR_TOOLONG;
241 if (!valid_char(*c)) {
243 *why = NAME_ERR_INVALCHAR;
251 if (!(*pool >= 'a' && *pool <= 'z') &&
252 !(*pool >= 'A' && *pool <= 'Z')) {
254 *why = NAME_ERR_NOLETTER;
258 if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
260 *why = NAME_ERR_RESERVED;
264 if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
266 *why = NAME_ERR_DISKLIKE;
274 * Check if the dataset name is private for internal usage.
275 * '$' is reserved for internal dataset names. e.g. "$MOS"
277 * Return 1 if the given name is used internally.
278 * Return 0 if it is not.
281 dataset_name_hidden(const char *name)
283 if (strchr(name, '$') != NULL)