7386 zfs get does not work properly with bookmarks
[unleashed.git] / usr / src / common / zfs / zfs_namecheck.c
blob950d41f353acf187ac38d2c93576631f33be9045
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright (c) 2013 by Delphix. All rights reserved.
30 * Common name validation routines for ZFS. These routines are shared by the
31 * userland code as well as the ioctl() layer to ensure that we don't
32 * inadvertently expose a hole through direct ioctl()s that never gets tested.
33 * In userland, however, we want significantly more information about _why_ the
34 * name is invalid. In the kernel, we only care whether it's valid or not.
35 * Each routine therefore takes a 'namecheck_err_t' which describes exactly why
36 * the name failed to validate.
38 * Each function returns 0 on success, -1 on error.
41 #if defined(_KERNEL)
42 #include <sys/systm.h>
43 #else
44 #include <string.h>
45 #endif
47 #include <sys/param.h>
48 #include <sys/nvpair.h>
49 #include "zfs_namecheck.h"
50 #include "zfs_deleg.h"
52 static int
53 valid_char(char c)
55 return ((c >= 'a' && c <= 'z') ||
56 (c >= 'A' && c <= 'Z') ||
57 (c >= '0' && c <= '9') ||
58 c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
62 * Snapshot names must be made up of alphanumeric characters plus the following
63 * characters:
65 * [-_.: ]
67 int
68 zfs_component_namecheck(const char *path, namecheck_err_t *why, char *what)
70 const char *loc;
72 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
73 if (why)
74 *why = NAME_ERR_TOOLONG;
75 return (-1);
78 if (path[0] == '\0') {
79 if (why)
80 *why = NAME_ERR_EMPTY_COMPONENT;
81 return (-1);
84 for (loc = path; *loc; loc++) {
85 if (!valid_char(*loc)) {
86 if (why) {
87 *why = NAME_ERR_INVALCHAR;
88 *what = *loc;
90 return (-1);
93 return (0);
98 * Permissions set name must start with the letter '@' followed by the
99 * same character restrictions as snapshot names, except that the name
100 * cannot exceed 64 characters.
103 permset_namecheck(const char *path, namecheck_err_t *why, char *what)
105 if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
106 if (why)
107 *why = NAME_ERR_TOOLONG;
108 return (-1);
111 if (path[0] != '@') {
112 if (why) {
113 *why = NAME_ERR_NO_AT;
114 *what = path[0];
116 return (-1);
119 return (zfs_component_namecheck(&path[1], why, what));
123 * Entity names must be of the following form:
125 * [component/]*[component][(@|#)component]?
127 * Where each component is made up of alphanumeric characters plus the following
128 * characters:
130 * [-_.:%]
132 * We allow '%' here as we use that character internally to create unique
133 * names for temporary clones (for online recv).
136 entity_namecheck(const char *path, namecheck_err_t *why, char *what)
138 const char *start, *end;
139 int found_delim;
142 * Make sure the name is not too long.
145 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
146 if (why)
147 *why = NAME_ERR_TOOLONG;
148 return (-1);
151 /* Explicitly check for a leading slash. */
152 if (path[0] == '/') {
153 if (why)
154 *why = NAME_ERR_LEADING_SLASH;
155 return (-1);
158 if (path[0] == '\0') {
159 if (why)
160 *why = NAME_ERR_EMPTY_COMPONENT;
161 return (-1);
164 start = path;
165 found_delim = 0;
166 for (;;) {
167 /* Find the end of this component */
168 end = start;
169 while (*end != '/' && *end != '@' && *end != '#' &&
170 *end != '\0')
171 end++;
173 if (*end == '\0' && end[-1] == '/') {
174 /* trailing slashes are not allowed */
175 if (why)
176 *why = NAME_ERR_TRAILING_SLASH;
177 return (-1);
180 /* Validate the contents of this component */
181 for (const char *loc = start; loc != end; loc++) {
182 if (!valid_char(*loc) && *loc != '%') {
183 if (why) {
184 *why = NAME_ERR_INVALCHAR;
185 *what = *loc;
187 return (-1);
191 /* Snapshot or bookmark delimiter found */
192 if (*end == '@' || *end == '#') {
193 /* Multiple delimiters are not allowed */
194 if (found_delim != 0) {
195 if (why)
196 *why = NAME_ERR_MULTIPLE_DELIMITERS;
197 return (-1);
200 found_delim = 1;
203 /* Zero-length components are not allowed */
204 if (start == end) {
205 if (why)
206 *why = NAME_ERR_EMPTY_COMPONENT;
207 return (-1);
210 /* If we've reached the end of the string, we're OK */
211 if (*end == '\0')
212 return (0);
215 * If there is a '/' in a snapshot or bookmark name
216 * then report an error
218 if (*end == '/' && found_delim != 0) {
219 if (why)
220 *why = NAME_ERR_TRAILING_SLASH;
221 return (-1);
224 /* Update to the next component */
225 start = end + 1;
230 * Dataset is any entity, except bookmark
233 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
235 int ret = entity_namecheck(path, why, what);
237 if (ret == 0 && strchr(path, '#') != NULL) {
238 if (why != NULL) {
239 *why = NAME_ERR_INVALCHAR;
240 *what = '#';
242 return (-1);
245 return (ret);
249 * mountpoint names must be of the following form:
251 * /[component][/]*[component][/]
254 mountpoint_namecheck(const char *path, namecheck_err_t *why)
256 const char *start, *end;
259 * Make sure none of the mountpoint component names are too long.
260 * If a component name is too long then the mkdir of the mountpoint
261 * will fail but then the mountpoint property will be set to a value
262 * that can never be mounted. Better to fail before setting the prop.
263 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
266 if (path == NULL || *path != '/') {
267 if (why)
268 *why = NAME_ERR_LEADING_SLASH;
269 return (-1);
272 /* Skip leading slash */
273 start = &path[1];
274 do {
275 end = start;
276 while (*end != '/' && *end != '\0')
277 end++;
279 if (end - start >= ZFS_MAX_DATASET_NAME_LEN) {
280 if (why)
281 *why = NAME_ERR_TOOLONG;
282 return (-1);
284 start = end + 1;
286 } while (*end != '\0');
288 return (0);
292 * For pool names, we have the same set of valid characters as described in
293 * dataset names, with the additional restriction that the pool name must begin
294 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
295 * that cannot be used.
298 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
300 const char *c;
303 * Make sure the name is not too long.
305 if (strlen(pool) >= ZFS_MAX_DATASET_NAME_LEN) {
306 if (why)
307 *why = NAME_ERR_TOOLONG;
308 return (-1);
311 c = pool;
312 while (*c != '\0') {
313 if (!valid_char(*c)) {
314 if (why) {
315 *why = NAME_ERR_INVALCHAR;
316 *what = *c;
318 return (-1);
320 c++;
323 if (!(*pool >= 'a' && *pool <= 'z') &&
324 !(*pool >= 'A' && *pool <= 'Z')) {
325 if (why)
326 *why = NAME_ERR_NOLETTER;
327 return (-1);
330 if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
331 if (why)
332 *why = NAME_ERR_RESERVED;
333 return (-1);
336 if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
337 if (why)
338 *why = NAME_ERR_DISKLIKE;
339 return (-1);
342 return (0);