xref: /titanic_53/usr/src/common/zfs/zfs_namecheck.c (revision 1d452cf5123cb6ac0a013a4dbd4dcceeb0da314d)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5*1d452cf5Sahrens  * Common Development and Distribution License (the "License").
6*1d452cf5Sahrens  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22*1d452cf5Sahrens  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens /*
29fa9e4066Sahrens  * Common name validation routines for ZFS.  These routines are shared by the
30fa9e4066Sahrens  * userland code as well as the ioctl() layer to ensure that we don't
31fa9e4066Sahrens  * inadvertently expose a hole through direct ioctl()s that never gets tested.
32fa9e4066Sahrens  * In userland, however, we want significantly more information about _why_ the
33fa9e4066Sahrens  * name is invalid.  In the kernel, we only care whether it's valid or not.
34fa9e4066Sahrens  * Each routine therefore takes a 'namecheck_err_t' which describes exactly why
35fa9e4066Sahrens  * the name failed to validate.
36fa9e4066Sahrens  *
37fa9e4066Sahrens  * Each function returns 0 on success, -1 on error.
38fa9e4066Sahrens  */
39fa9e4066Sahrens 
40fa9e4066Sahrens #if defined(_KERNEL)
41fa9e4066Sahrens #include <sys/systm.h>
42fa9e4066Sahrens #else
43fa9e4066Sahrens #include <string.h>
44fa9e4066Sahrens #endif
45fa9e4066Sahrens 
46b81d61a6Slling #include <sys/param.h>
47fa9e4066Sahrens #include "zfs_namecheck.h"
48fa9e4066Sahrens 
49fa9e4066Sahrens static int
50fa9e4066Sahrens valid_char(char c)
51fa9e4066Sahrens {
52fa9e4066Sahrens 	return ((c >= 'a' && c <= 'z') ||
53fa9e4066Sahrens 	    (c >= 'A' && c <= 'Z') ||
54fa9e4066Sahrens 	    (c >= '0' && c <= '9') ||
55fa9e4066Sahrens 	    c == '-' || c == '_' || c == '.' || c == ':');
56fa9e4066Sahrens }
57fa9e4066Sahrens 
58fa9e4066Sahrens /*
59*1d452cf5Sahrens  * Snapshot names must be made up of alphanumeric characters plus the following
60*1d452cf5Sahrens  * characters:
61*1d452cf5Sahrens  *
62*1d452cf5Sahrens  * 	[-_.:]
63*1d452cf5Sahrens  */
64*1d452cf5Sahrens int
65*1d452cf5Sahrens snapshot_namecheck(const char *path, namecheck_err_t *why, char *what)
66*1d452cf5Sahrens {
67*1d452cf5Sahrens 	const char *loc;
68*1d452cf5Sahrens 
69*1d452cf5Sahrens 	if (strlen(path) >= MAXNAMELEN) {
70*1d452cf5Sahrens 		if (why)
71*1d452cf5Sahrens 			*why = NAME_ERR_TOOLONG;
72*1d452cf5Sahrens 		return (-1);
73*1d452cf5Sahrens 	}
74*1d452cf5Sahrens 
75*1d452cf5Sahrens 	if (path[0] == '\0') {
76*1d452cf5Sahrens 		if (why)
77*1d452cf5Sahrens 			*why = NAME_ERR_EMPTY_COMPONENT;
78*1d452cf5Sahrens 		return (-1);
79*1d452cf5Sahrens 	}
80*1d452cf5Sahrens 
81*1d452cf5Sahrens 	for (loc = path; *loc; loc++) {
82*1d452cf5Sahrens 		if (!valid_char(*loc)) {
83*1d452cf5Sahrens 			if (why) {
84*1d452cf5Sahrens 				*why = NAME_ERR_INVALCHAR;
85*1d452cf5Sahrens 				*what = *loc;
86*1d452cf5Sahrens 			}
87*1d452cf5Sahrens 			return (-1);
88*1d452cf5Sahrens 		}
89*1d452cf5Sahrens 	}
90*1d452cf5Sahrens 	return (0);
91*1d452cf5Sahrens }
92*1d452cf5Sahrens 
93*1d452cf5Sahrens /*
94fa9e4066Sahrens  * Dataset names must be of the following form:
95fa9e4066Sahrens  *
96fa9e4066Sahrens  * 	[component][/]*[component][@component]
97fa9e4066Sahrens  *
98fa9e4066Sahrens  * Where each component is made up of alphanumeric characters plus the following
99fa9e4066Sahrens  * characters:
100fa9e4066Sahrens  *
101fa9e4066Sahrens  * 	[-_.:]
102fa9e4066Sahrens  */
103fa9e4066Sahrens int
104fa9e4066Sahrens dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
105fa9e4066Sahrens {
106fa9e4066Sahrens 	const char *loc, *end;
107fa9e4066Sahrens 	int found_snapshot;
108fa9e4066Sahrens 
109b81d61a6Slling 	/*
110b81d61a6Slling 	 * Make sure the name is not too long.
111b81d61a6Slling 	 *
112b81d61a6Slling 	 * ZFS_MAXNAMELEN is the maximum dataset length used in the userland
113b81d61a6Slling 	 * which is the same as MAXNAMELEN used in the kernel.
114b81d61a6Slling 	 * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all
115b81d61a6Slling 	 * places using MAXNAMELEN.
116b81d61a6Slling 	 */
117b81d61a6Slling 	if (strlen(path) >= MAXNAMELEN) {
118b81d61a6Slling 		if (why)
119b81d61a6Slling 			*why = NAME_ERR_TOOLONG;
120b81d61a6Slling 		return (-1);
121b81d61a6Slling 	}
122b81d61a6Slling 
123fa9e4066Sahrens 	/* Explicitly check for a leading slash.  */
124fa9e4066Sahrens 	if (path[0] == '/') {
125fa9e4066Sahrens 		if (why)
126fa9e4066Sahrens 			*why = NAME_ERR_LEADING_SLASH;
127fa9e4066Sahrens 		return (-1);
128fa9e4066Sahrens 	}
129fa9e4066Sahrens 
130fa9e4066Sahrens 	if (path[0] == '\0') {
131fa9e4066Sahrens 		if (why)
132fa9e4066Sahrens 			*why = NAME_ERR_EMPTY_COMPONENT;
133fa9e4066Sahrens 		return (-1);
134fa9e4066Sahrens 	}
135fa9e4066Sahrens 
136fa9e4066Sahrens 	loc = path;
137fa9e4066Sahrens 	found_snapshot = 0;
138fa9e4066Sahrens 	for (;;) {
139fa9e4066Sahrens 		/* Find the end of this component */
140fa9e4066Sahrens 		end = loc;
141fa9e4066Sahrens 		while (*end != '/' && *end != '@' && *end != '\0')
142fa9e4066Sahrens 			end++;
143fa9e4066Sahrens 
144fa9e4066Sahrens 		if (*end == '\0' && end[-1] == '/') {
145fa9e4066Sahrens 			/* trailing slashes are not allowed */
146fa9e4066Sahrens 			if (why)
147fa9e4066Sahrens 				*why = NAME_ERR_TRAILING_SLASH;
148fa9e4066Sahrens 			return (-1);
149fa9e4066Sahrens 		}
150fa9e4066Sahrens 
151fa9e4066Sahrens 		/* Zero-length components are not allowed */
152fa9e4066Sahrens 		if (loc == end) {
153fa9e4066Sahrens 			if (why)
154fa9e4066Sahrens 				*why = NAME_ERR_EMPTY_COMPONENT;
155fa9e4066Sahrens 			return (-1);
156fa9e4066Sahrens 		}
157fa9e4066Sahrens 
158fa9e4066Sahrens 		/* Validate the contents of this component */
159fa9e4066Sahrens 		while (loc != end) {
160fa9e4066Sahrens 			if (!valid_char(*loc)) {
161fa9e4066Sahrens 				if (why) {
162fa9e4066Sahrens 					*why = NAME_ERR_INVALCHAR;
163fa9e4066Sahrens 					*what = *loc;
164fa9e4066Sahrens 				}
165fa9e4066Sahrens 				return (-1);
166fa9e4066Sahrens 			}
167fa9e4066Sahrens 			loc++;
168fa9e4066Sahrens 		}
169fa9e4066Sahrens 
170fa9e4066Sahrens 		/* If we've reached the end of the string, we're OK */
171fa9e4066Sahrens 		if (*end == '\0')
172fa9e4066Sahrens 			return (0);
173fa9e4066Sahrens 
174fa9e4066Sahrens 		if (*end == '@') {
175fa9e4066Sahrens 			/*
176fa9e4066Sahrens 			 * If we've found an @ symbol, indicate that we're in
177fa9e4066Sahrens 			 * the snapshot component, and report a second '@'
178fa9e4066Sahrens 			 * character as an error.
179fa9e4066Sahrens 			 */
180fa9e4066Sahrens 			if (found_snapshot) {
181fa9e4066Sahrens 				if (why)
182fa9e4066Sahrens 					*why = NAME_ERR_MULTIPLE_AT;
183fa9e4066Sahrens 				return (-1);
184fa9e4066Sahrens 			}
185fa9e4066Sahrens 
186fa9e4066Sahrens 			found_snapshot = 1;
187fa9e4066Sahrens 		}
188fa9e4066Sahrens 
189fa9e4066Sahrens 		/* Update to the next component */
190fa9e4066Sahrens 		loc = end + 1;
191fa9e4066Sahrens 	}
192fa9e4066Sahrens }
193fa9e4066Sahrens 
194fa9e4066Sahrens /*
195fa9e4066Sahrens  * For pool names, we have the same set of valid characters as described in
196fa9e4066Sahrens  * dataset names, with the additional restriction that the pool name must begin
197fa9e4066Sahrens  * with a letter.  The pool names 'raidz' and 'mirror' are also reserved names
198fa9e4066Sahrens  * that cannot be used.
199fa9e4066Sahrens  */
200fa9e4066Sahrens int
201fa9e4066Sahrens pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
202fa9e4066Sahrens {
203fa9e4066Sahrens 	const char *c;
204fa9e4066Sahrens 
205b81d61a6Slling 	/*
206b81d61a6Slling 	 * Make sure the name is not too long.
207b81d61a6Slling 	 *
208b81d61a6Slling 	 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
209b81d61a6Slling 	 * which is the same as MAXNAMELEN used in the kernel.
210b81d61a6Slling 	 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
211b81d61a6Slling 	 * places using MAXNAMELEN.
212b81d61a6Slling 	 */
213b81d61a6Slling 	if (strlen(pool) >= MAXNAMELEN) {
214b81d61a6Slling 		if (why)
215b81d61a6Slling 			*why = NAME_ERR_TOOLONG;
216b81d61a6Slling 		return (-1);
217b81d61a6Slling 	}
218b81d61a6Slling 
219fa9e4066Sahrens 	c = pool;
220fa9e4066Sahrens 	while (*c != '\0') {
221fa9e4066Sahrens 		if (!valid_char(*c)) {
222fa9e4066Sahrens 			if (why) {
223fa9e4066Sahrens 				*why = NAME_ERR_INVALCHAR;
224fa9e4066Sahrens 				*what = *c;
225fa9e4066Sahrens 			}
226fa9e4066Sahrens 			return (-1);
227fa9e4066Sahrens 		}
228fa9e4066Sahrens 		c++;
229fa9e4066Sahrens 	}
230fa9e4066Sahrens 
231fa9e4066Sahrens 	if (!(*pool >= 'a' && *pool <= 'z') &&
232fa9e4066Sahrens 	    !(*pool >= 'A' && *pool <= 'Z')) {
233fa9e4066Sahrens 		if (why)
234fa9e4066Sahrens 			*why = NAME_ERR_NOLETTER;
235fa9e4066Sahrens 		return (-1);
236fa9e4066Sahrens 	}
237fa9e4066Sahrens 
238fa9e4066Sahrens 	if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
239fa9e4066Sahrens 		if (why)
240fa9e4066Sahrens 			*why = NAME_ERR_RESERVED;
241fa9e4066Sahrens 		return (-1);
242fa9e4066Sahrens 	}
243fa9e4066Sahrens 
244fa9e4066Sahrens 	if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
245fa9e4066Sahrens 		if (why)
246fa9e4066Sahrens 			*why = NAME_ERR_DISKLIKE;
247fa9e4066Sahrens 		return (-1);
248fa9e4066Sahrens 	}
249fa9e4066Sahrens 
250fa9e4066Sahrens 	return (0);
251fa9e4066Sahrens }
252fa9e4066Sahrens 
253fa9e4066Sahrens /*
254fa9e4066Sahrens  * Check if the dataset name is private for internal usage.
255fa9e4066Sahrens  * '$' is reserved for internal dataset names. e.g. "$MOS"
256fa9e4066Sahrens  *
257fa9e4066Sahrens  * Return 1 if the given name is used internally.
258fa9e4066Sahrens  * Return 0 if it is not.
259fa9e4066Sahrens  */
260fa9e4066Sahrens int
261fa9e4066Sahrens dataset_name_hidden(const char *name)
262fa9e4066Sahrens {
263fa9e4066Sahrens 	if (strchr(name, '$') != NULL)
264fa9e4066Sahrens 		return (1);
265fa9e4066Sahrens 
266fa9e4066Sahrens 	return (0);
267fa9e4066Sahrens }
268