fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens * Common Development and Distribution License (the "License").
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Use is subject to license terms.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Common name validation routines for ZFS. These routines are shared by the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * userland code as well as the ioctl() layer to ensure that we don't
fa9e4066f08beec538e775443c5be79dd423fcabahrens * inadvertently expose a hole through direct ioctl()s that never gets tested.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * In userland, however, we want significantly more information about _why_ the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * name is invalid. In the kernel, we only care whether it's valid or not.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Each routine therefore takes a 'namecheck_err_t' which describes exactly why
fa9e4066f08beec538e775443c5be79dd423fcabahrens * the name failed to validate.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Each function returns 0 on success, -1 on error.
6d1e0b89f3e34478a022854df6b0cfab82ccce66marks c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens * Snapshot names must be made up of alphanumeric characters plus the following
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens * characters:
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrenszfs_component_namecheck(const char *path, namecheck_err_t *why, char *what)
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens return (-1);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens return (-1);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens return (-1);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens return (0);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks * Permissions set name must start with the letter '@' followed by the
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks * same character restrictions as snapshot names, except that the name
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks * cannot exceed 64 characters.
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarkspermset_namecheck(const char *path, namecheck_err_t *why, char *what)
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks return (-1);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks return (-1);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (zfs_component_namecheck(&path[1], why, what));
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka * Entity names must be of the following form:
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka * [component/]*[component][(@|#)component]?
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Where each component is made up of alphanumeric characters plus the following
fa9e4066f08beec538e775443c5be79dd423fcabahrens * characters:
f0ed2251df2f0c744e0d198d9b79ae5ed937de7bek * We allow '%' here as we use that character internally to create unique
f0ed2251df2f0c744e0d198d9b79ae5ed937de7bek * names for temporary clones (for online recv).
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telkaentity_namecheck(const char *path, namecheck_err_t *why, char *what)
b81d61a68b235e0529ebadc18e14d9d1dd52a258lling * Make sure the name is not too long.
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
b81d61a68b235e0529ebadc18e14d9d1dd52a258lling return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Explicitly check for a leading slash. */
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Find the end of this component */
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka while (*end != '/' && *end != '@' && *end != '#' &&
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* trailing slashes are not allowed */
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Validate the contents of this component */
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka for (const char *loc = start; loc != end; loc++) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka /* Snapshot or bookmark delimiter found */
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka /* Multiple delimiters are not allowed */
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka /* Zero-length components are not allowed */
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka return (-1);
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka /* If we've reached the end of the string, we're OK */
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka * If there is a '/' in a snapshot or bookmark name
98579b20de8e05c5117968705a18979f8b75b863nd * then report an error
98579b20de8e05c5117968705a18979f8b75b863nd return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Update to the next component */
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka * Dataset is any entity, except bookmark
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telkadataset_namecheck(const char *path, namecheck_err_t *why, char *what)
edb901aab9c738b5eb15aa55933e82b0f2f9d9a2Marcel Telka return (-1);
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrm * mountpoint names must be of the following form:
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrm * /[component][/]*[component][/]
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrmmountpoint_namecheck(const char *path, namecheck_err_t *why)
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrm * Make sure none of the mountpoint component names are too long.
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrm * If a component name is too long then the mkdir of the mountpoint
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrm * will fail but then the mountpoint property will be set to a value
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrm * that can never be mounted. Better to fail before setting the prop.
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrm * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrm return (-1);
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrm /* Skip leading slash */
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens if (end - start >= ZFS_MAX_DATASET_NAME_LEN) {
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrm return (-1);
89eef05efc3ad959ce1fcc4fbf4d428b4fc015adrm return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * For pool names, we have the same set of valid characters as described in
fa9e4066f08beec538e775443c5be79dd423fcabahrens * dataset names, with the additional restriction that the pool name must begin
fa9e4066f08beec538e775443c5be79dd423fcabahrens * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
fa9e4066f08beec538e775443c5be79dd423fcabahrens * that cannot be used.
fa9e4066f08beec538e775443c5be79dd423fcabahrenspool_namecheck(const char *pool, namecheck_err_t *why, char *what)
fa9e4066f08beec538e775443c5be79dd423fcabahrens const char *c;
b81d61a68b235e0529ebadc18e14d9d1dd52a258lling * Make sure the name is not too long.
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens if (strlen(pool) >= ZFS_MAX_DATASET_NAME_LEN) {
b81d61a68b235e0529ebadc18e14d9d1dd52a258lling return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens while (*c != '\0') {
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);