zfs_namecheck.c revision b81d61a68b235e0529ebadc18e14d9d1dd52a258
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * CDDL HEADER START
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * The contents of this file are subject to the terms of the
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Common Development and Distribution License, Version 1.0 only
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * (the "License"). You may not use this file except in compliance
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * with the License.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * See the License for the specific language governing permissions
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * and limitations under the License.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * When distributing Covered Code, include this CDDL HEADER in each
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * If applicable, add the following below this CDDL HEADER, with the
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * fields enclosed by brackets "[]" replaced with your own identifying
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * information: Portions Copyright [yyyy] [name of copyright owner]
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * CDDL HEADER END
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Use is subject to license terms.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys#pragma ident "%Z%%M% %I% %E% SMI"
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Common name validation routines for ZFS. These routines are shared by the
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * userland code as well as the ioctl() layer to ensure that we don't
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * inadvertently expose a hole through direct ioctl()s that never gets tested.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * In userland, however, we want significantly more information about _why_ the
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * name is invalid. In the kernel, we only care whether it's valid or not.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Each routine therefore takes a 'namecheck_err_t' which describes exactly why
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * the name failed to validate.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Each function returns 0 on success, -1 on error.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Dataset names must be of the following form:
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * [component][/]*[component][@component]
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * Where each component is made up of alphanumeric characters plus the following
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * characters:
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllysdataset_namecheck(const char *path, namecheck_err_t *why, char *what)
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Make sure the name is not too long.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * ZFS_MAXNAMELEN is the maximum dataset length used in the userland
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * which is the same as MAXNAMELEN used in the kernel.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * places using MAXNAMELEN.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* Explicitly check for a leading slash. */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* Find the end of this component */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* trailing slashes are not allowed */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* Zero-length components are not allowed */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* Validate the contents of this component */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* If we've reached the end of the string, we're OK */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (0);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * If we've found an @ symbol, indicate that we're in
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * the snapshot component, and report a second '@'
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * character as an error.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* Update to the next component */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * For pool names, we have the same set of valid characters as described in
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * dataset names, with the additional restriction that the pool name must begin
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * that cannot be used.
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllyspool_namecheck(const char *pool, namecheck_err_t *why, char *what)
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys const char *c;
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * Make sure the name is not too long.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * which is the same as MAXNAMELEN used in the kernel.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * places using MAXNAMELEN.
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys while (*c != '\0') {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (0);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * Check if the dataset name is private for internal usage.
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * '$' is reserved for internal dataset names. e.g. "$MOS"
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * Return 1 if the given name is used internally.
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * Return 0 if it is not.