zfs_namecheck.c revision b81d61a68b235e0529ebadc18e14d9d1dd52a258
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys/*
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * CDDL HEADER START
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys *
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 *
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * or http://www.opensolaris.org/os/licensing.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * See the License for the specific language governing permissions
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * and limitations under the License.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys *
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 *
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * CDDL HEADER END
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys */
a44b45c8fa54b3a79a2de076aa2d8c48cd75dee4Huie-Ying Lee/*
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Use is subject to license terms.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys#pragma ident "%Z%%M% %I% %E% SMI"
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys/*
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 *
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Each function returns 0 on success, -1 on error.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys#if defined(_KERNEL)
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys#include <sys/systm.h>
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys#else
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys#include <string.h>
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys#endif
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys#include <sys/param.h>
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys#include "zfs_namecheck.h"
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllysstatic int
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllysvalid_char(char c)
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys{
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return ((c >= 'a' && c <= 'z') ||
d00756ccb34596a328f8a15d1965da5412d366d0wyllys (c >= 'A' && c <= 'Z') ||
d00756ccb34596a328f8a15d1965da5412d366d0wyllys (c >= '0' && c <= '9') ||
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys c == '-' || c == '_' || c == '.' || c == ':');
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys}
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys/*
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Dataset names must be of the following form:
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys *
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * [component][/]*[component][@component]
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * Where each component is made up of alphanumeric characters plus the following
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * characters:
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * [-_.:]
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllysint
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllysdataset_namecheck(const char *path, namecheck_err_t *why, char *what)
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys{
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys const char *loc, *end;
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys int found_snapshot;
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /*
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Make sure the name is not too long.
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *
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.
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if (strlen(path) >= MAXNAMELEN) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (why)
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys *why = NAME_ERR_TOOLONG;
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* Explicitly check for a leading slash. */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if (path[0] == '/') {
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if (why)
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys *why = NAME_ERR_LEADING_SLASH;
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys }
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if (path[0] == '\0') {
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if (why)
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *why = NAME_ERR_EMPTY_COMPONENT;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys loc = path;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys found_snapshot = 0;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys for (;;) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* Find the end of this component */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys end = loc;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys while (*end != '/' && *end != '@' && *end != '\0')
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys end++;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (*end == '\0' && end[-1] == '/') {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* trailing slashes are not allowed */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (why)
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *why = NAME_ERR_TRAILING_SLASH;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* Zero-length components are not allowed */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (loc == end) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (why)
fa60c371cd00bdca17de2ff18fe3e64d051ae61bwyllys *why = NAME_ERR_EMPTY_COMPONENT;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* Validate the contents of this component */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys while (loc != end) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (!valid_char(*loc)) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (why) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *why = NAME_ERR_INVALCHAR;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *what = *loc;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys loc++;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys /* If we've reached the end of the string, we're OK */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (*end == '\0')
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (0);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if (*end == '@') {
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /*
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.
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if (found_snapshot) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (why)
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *why = NAME_ERR_MULTIPLE_AT;
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys found_snapshot = 1;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* Update to the next component */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys loc = end + 1;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys}
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys/*
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.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllysint
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllyspool_namecheck(const char *pool, namecheck_err_t *why, char *what)
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys{
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys const char *c;
d00756ccb34596a328f8a15d1965da5412d366d0wyllys
d00756ccb34596a328f8a15d1965da5412d366d0wyllys /*
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * Make sure the name is not too long.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys *
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.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (strlen(pool) >= MAXNAMELEN) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (why)
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *why = NAME_ERR_TOOLONG;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys c = pool;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys while (*c != '\0') {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (!valid_char(*c)) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (why) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *why = NAME_ERR_INVALCHAR;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *what = *c;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys c++;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (!(*pool >= 'a' && *pool <= 'z') &&
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys !(*pool >= 'A' && *pool <= 'Z')) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (why)
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *why = NAME_ERR_NOLETTER;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if (why)
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys *why = NAME_ERR_RESERVED;
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys if (why)
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *why = NAME_ERR_DISKLIKE;
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (-1);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys }
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys return (0);
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys}
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys/*
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * Check if the dataset name is private for internal usage.
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * '$' is reserved for internal dataset names. e.g. "$MOS"
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys *
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * Return 1 if the given name is used internally.
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys * Return 0 if it is not.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys */
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllysint
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllysdataset_name_hidden(const char *name)
30a5e8fa1253cb33980ee4514743cf683f584b4ewyllys{
a44b45c8fa54b3a79a2de076aa2d8c48cd75dee4Huie-Ying Lee if (strchr(name, '$') != NULL)
a44b45c8fa54b3a79a2de076aa2d8c48cd75dee4Huie-Ying Lee return (1);
a44b45c8fa54b3a79a2de076aa2d8c48cd75dee4Huie-Ying Lee
a44b45c8fa54b3a79a2de076aa2d8c48cd75dee4Huie-Ying Lee return (0);
a44b45c8fa54b3a79a2de076aa2d8c48cd75dee4Huie-Ying Lee}
a44b45c8fa54b3a79a2de076aa2d8c48cd75dee4Huie-Ying Lee