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