xref: /titanic_53/usr/src/common/zfs/zfs_namecheck.c (revision 6d1e0b89f3e34478a022854df6b0cfab82ccce66)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
51d452cf5Sahrens  * Common Development and Distribution License (the "License").
61d452cf5Sahrens  * 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*6d1e0b89Smarks  * Copyright 2008 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>
47ecd6cf80Smarks #include <sys/nvpair.h>
48fa9e4066Sahrens #include "zfs_namecheck.h"
49ecd6cf80Smarks #include "zfs_deleg.h"
50fa9e4066Sahrens 
51fa9e4066Sahrens static int
52fa9e4066Sahrens valid_char(char c)
53fa9e4066Sahrens {
54fa9e4066Sahrens 	return ((c >= 'a' && c <= 'z') ||
55fa9e4066Sahrens 	    (c >= 'A' && c <= 'Z') ||
56fa9e4066Sahrens 	    (c >= '0' && c <= '9') ||
57*6d1e0b89Smarks 	    c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
58fa9e4066Sahrens }
59fa9e4066Sahrens 
60fa9e4066Sahrens /*
611d452cf5Sahrens  * Snapshot names must be made up of alphanumeric characters plus the following
621d452cf5Sahrens  * characters:
631d452cf5Sahrens  *
64f0ed2251Sek110237  * 	[-_.:]
651d452cf5Sahrens  */
661d452cf5Sahrens int
671d452cf5Sahrens snapshot_namecheck(const char *path, namecheck_err_t *why, char *what)
681d452cf5Sahrens {
691d452cf5Sahrens 	const char *loc;
701d452cf5Sahrens 
711d452cf5Sahrens 	if (strlen(path) >= MAXNAMELEN) {
721d452cf5Sahrens 		if (why)
731d452cf5Sahrens 			*why = NAME_ERR_TOOLONG;
741d452cf5Sahrens 		return (-1);
751d452cf5Sahrens 	}
761d452cf5Sahrens 
771d452cf5Sahrens 	if (path[0] == '\0') {
781d452cf5Sahrens 		if (why)
791d452cf5Sahrens 			*why = NAME_ERR_EMPTY_COMPONENT;
801d452cf5Sahrens 		return (-1);
811d452cf5Sahrens 	}
821d452cf5Sahrens 
831d452cf5Sahrens 	for (loc = path; *loc; loc++) {
841d452cf5Sahrens 		if (!valid_char(*loc)) {
851d452cf5Sahrens 			if (why) {
861d452cf5Sahrens 				*why = NAME_ERR_INVALCHAR;
871d452cf5Sahrens 				*what = *loc;
881d452cf5Sahrens 			}
891d452cf5Sahrens 			return (-1);
901d452cf5Sahrens 		}
911d452cf5Sahrens 	}
921d452cf5Sahrens 	return (0);
931d452cf5Sahrens }
941d452cf5Sahrens 
95ecd6cf80Smarks 
96ecd6cf80Smarks /*
97ecd6cf80Smarks  * Permissions set name must start with the letter '@' followed by the
98ecd6cf80Smarks  * same character restrictions as snapshot names, except that the name
99ecd6cf80Smarks  * cannot exceed 64 characters.
100ecd6cf80Smarks  */
101ecd6cf80Smarks int
102ecd6cf80Smarks permset_namecheck(const char *path, namecheck_err_t *why, char *what)
103ecd6cf80Smarks {
104ecd6cf80Smarks 	if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
105ecd6cf80Smarks 		if (why)
106ecd6cf80Smarks 			*why = NAME_ERR_TOOLONG;
107ecd6cf80Smarks 		return (-1);
108ecd6cf80Smarks 	}
109ecd6cf80Smarks 
110ecd6cf80Smarks 	if (path[0] != '@') {
111ecd6cf80Smarks 		if (why) {
112ecd6cf80Smarks 			*why = NAME_ERR_NO_AT;
113ecd6cf80Smarks 			*what = path[0];
114ecd6cf80Smarks 		}
115ecd6cf80Smarks 		return (-1);
116ecd6cf80Smarks 	}
117ecd6cf80Smarks 
118ecd6cf80Smarks 	return (snapshot_namecheck(&path[1], why, what));
119ecd6cf80Smarks }
120ecd6cf80Smarks 
1211d452cf5Sahrens /*
122fa9e4066Sahrens  * Dataset names must be of the following form:
123fa9e4066Sahrens  *
124fa9e4066Sahrens  * 	[component][/]*[component][@component]
125fa9e4066Sahrens  *
126fa9e4066Sahrens  * Where each component is made up of alphanumeric characters plus the following
127fa9e4066Sahrens  * characters:
128fa9e4066Sahrens  *
129f18faf3fSek110237  * 	[-_.:%]
130f0ed2251Sek110237  *
131f0ed2251Sek110237  * We allow '%' here as we use that character internally to create unique
132f0ed2251Sek110237  * names for temporary clones (for online recv).
133fa9e4066Sahrens  */
134fa9e4066Sahrens int
135fa9e4066Sahrens dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
136fa9e4066Sahrens {
137fa9e4066Sahrens 	const char *loc, *end;
138fa9e4066Sahrens 	int found_snapshot;
139fa9e4066Sahrens 
140b81d61a6Slling 	/*
141b81d61a6Slling 	 * Make sure the name is not too long.
142b81d61a6Slling 	 *
143b81d61a6Slling 	 * ZFS_MAXNAMELEN is the maximum dataset length used in the userland
144b81d61a6Slling 	 * which is the same as MAXNAMELEN used in the kernel.
145b81d61a6Slling 	 * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all
146b81d61a6Slling 	 * places using MAXNAMELEN.
147b81d61a6Slling 	 */
148ecd6cf80Smarks 
149b81d61a6Slling 	if (strlen(path) >= MAXNAMELEN) {
150b81d61a6Slling 		if (why)
151b81d61a6Slling 			*why = NAME_ERR_TOOLONG;
152b81d61a6Slling 		return (-1);
153b81d61a6Slling 	}
154b81d61a6Slling 
155fa9e4066Sahrens 	/* Explicitly check for a leading slash.  */
156fa9e4066Sahrens 	if (path[0] == '/') {
157fa9e4066Sahrens 		if (why)
158fa9e4066Sahrens 			*why = NAME_ERR_LEADING_SLASH;
159fa9e4066Sahrens 		return (-1);
160fa9e4066Sahrens 	}
161fa9e4066Sahrens 
162fa9e4066Sahrens 	if (path[0] == '\0') {
163fa9e4066Sahrens 		if (why)
164fa9e4066Sahrens 			*why = NAME_ERR_EMPTY_COMPONENT;
165fa9e4066Sahrens 		return (-1);
166fa9e4066Sahrens 	}
167fa9e4066Sahrens 
168fa9e4066Sahrens 	loc = path;
169fa9e4066Sahrens 	found_snapshot = 0;
170fa9e4066Sahrens 	for (;;) {
171fa9e4066Sahrens 		/* Find the end of this component */
172fa9e4066Sahrens 		end = loc;
173fa9e4066Sahrens 		while (*end != '/' && *end != '@' && *end != '\0')
174fa9e4066Sahrens 			end++;
175fa9e4066Sahrens 
176fa9e4066Sahrens 		if (*end == '\0' && end[-1] == '/') {
177fa9e4066Sahrens 			/* trailing slashes are not allowed */
178fa9e4066Sahrens 			if (why)
179fa9e4066Sahrens 				*why = NAME_ERR_TRAILING_SLASH;
180fa9e4066Sahrens 			return (-1);
181fa9e4066Sahrens 		}
182fa9e4066Sahrens 
183fa9e4066Sahrens 		/* Zero-length components are not allowed */
184adab9adaSpd144616 		if (loc == end) {
185adab9adaSpd144616 			if (why) {
186adab9adaSpd144616 				/*
187adab9adaSpd144616 				 * Make sure this is really a zero-length
188adab9adaSpd144616 				 * component and not a '@@'.
189adab9adaSpd144616 				 */
190adab9adaSpd144616 				if (*end == '@' && found_snapshot) {
191adab9adaSpd144616 					*why = NAME_ERR_MULTIPLE_AT;
192adab9adaSpd144616 				} else {
193fa9e4066Sahrens 					*why = NAME_ERR_EMPTY_COMPONENT;
194adab9adaSpd144616 				}
195adab9adaSpd144616 			}
196adab9adaSpd144616 
197fa9e4066Sahrens 			return (-1);
198fa9e4066Sahrens 		}
199fa9e4066Sahrens 
200fa9e4066Sahrens 		/* Validate the contents of this component */
201fa9e4066Sahrens 		while (loc != end) {
202f0ed2251Sek110237 			if (!valid_char(*loc) && *loc != '%') {
203fa9e4066Sahrens 				if (why) {
204fa9e4066Sahrens 					*why = NAME_ERR_INVALCHAR;
205fa9e4066Sahrens 					*what = *loc;
206fa9e4066Sahrens 				}
207fa9e4066Sahrens 				return (-1);
208fa9e4066Sahrens 			}
209fa9e4066Sahrens 			loc++;
210fa9e4066Sahrens 		}
211fa9e4066Sahrens 
212fa9e4066Sahrens 		/* If we've reached the end of the string, we're OK */
213fa9e4066Sahrens 		if (*end == '\0')
214fa9e4066Sahrens 			return (0);
215fa9e4066Sahrens 
216fa9e4066Sahrens 		if (*end == '@') {
217fa9e4066Sahrens 			/*
218fa9e4066Sahrens 			 * If we've found an @ symbol, indicate that we're in
219fa9e4066Sahrens 			 * the snapshot component, and report a second '@'
220fa9e4066Sahrens 			 * character as an error.
221fa9e4066Sahrens 			 */
222fa9e4066Sahrens 			if (found_snapshot) {
223fa9e4066Sahrens 				if (why)
224fa9e4066Sahrens 					*why = NAME_ERR_MULTIPLE_AT;
225fa9e4066Sahrens 				return (-1);
226fa9e4066Sahrens 			}
227fa9e4066Sahrens 
228fa9e4066Sahrens 			found_snapshot = 1;
229fa9e4066Sahrens 		}
230fa9e4066Sahrens 
23198579b20Snd150628 		/*
23298579b20Snd150628 		 * If there is a '/' in a snapshot name
23398579b20Snd150628 		 * then report an error
23498579b20Snd150628 		 */
23598579b20Snd150628 		if (*end == '/' && found_snapshot) {
23698579b20Snd150628 			if (why)
23798579b20Snd150628 				*why = NAME_ERR_TRAILING_SLASH;
23898579b20Snd150628 			return (-1);
23998579b20Snd150628 		}
24098579b20Snd150628 
241fa9e4066Sahrens 		/* Update to the next component */
242fa9e4066Sahrens 		loc = end + 1;
243fa9e4066Sahrens 	}
244fa9e4066Sahrens }
245fa9e4066Sahrens 
24689eef05eSrm160521 
24789eef05eSrm160521 /*
24889eef05eSrm160521  * mountpoint names must be of the following form:
24989eef05eSrm160521  *
25089eef05eSrm160521  *	/[component][/]*[component][/]
25189eef05eSrm160521  */
25289eef05eSrm160521 int
25389eef05eSrm160521 mountpoint_namecheck(const char *path, namecheck_err_t *why)
25489eef05eSrm160521 {
25589eef05eSrm160521 	const char *start, *end;
25689eef05eSrm160521 
25789eef05eSrm160521 	/*
25889eef05eSrm160521 	 * Make sure none of the mountpoint component names are too long.
25989eef05eSrm160521 	 * If a component name is too long then the mkdir of the mountpoint
26089eef05eSrm160521 	 * will fail but then the mountpoint property will be set to a value
26189eef05eSrm160521 	 * that can never be mounted.  Better to fail before setting the prop.
26289eef05eSrm160521 	 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
26389eef05eSrm160521 	 */
26489eef05eSrm160521 
26589eef05eSrm160521 	if (path == NULL || *path != '/') {
26689eef05eSrm160521 		if (why)
26789eef05eSrm160521 			*why = NAME_ERR_LEADING_SLASH;
26889eef05eSrm160521 		return (-1);
26989eef05eSrm160521 	}
27089eef05eSrm160521 
27189eef05eSrm160521 	/* Skip leading slash  */
27289eef05eSrm160521 	start = &path[1];
27389eef05eSrm160521 	do {
27489eef05eSrm160521 		end = start;
27589eef05eSrm160521 		while (*end != '/' && *end != '\0')
27689eef05eSrm160521 			end++;
27789eef05eSrm160521 
27889eef05eSrm160521 		if (end - start >= MAXNAMELEN) {
27989eef05eSrm160521 			if (why)
28089eef05eSrm160521 				*why = NAME_ERR_TOOLONG;
28189eef05eSrm160521 			return (-1);
28289eef05eSrm160521 		}
28389eef05eSrm160521 		start = end + 1;
28489eef05eSrm160521 
28589eef05eSrm160521 	} while (*end != '\0');
28689eef05eSrm160521 
28789eef05eSrm160521 	return (0);
28889eef05eSrm160521 }
28989eef05eSrm160521 
290fa9e4066Sahrens /*
291fa9e4066Sahrens  * For pool names, we have the same set of valid characters as described in
292fa9e4066Sahrens  * dataset names, with the additional restriction that the pool name must begin
293fa9e4066Sahrens  * with a letter.  The pool names 'raidz' and 'mirror' are also reserved names
294fa9e4066Sahrens  * that cannot be used.
295fa9e4066Sahrens  */
296fa9e4066Sahrens int
297fa9e4066Sahrens pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
298fa9e4066Sahrens {
299fa9e4066Sahrens 	const char *c;
300fa9e4066Sahrens 
301b81d61a6Slling 	/*
302b81d61a6Slling 	 * Make sure the name is not too long.
303b81d61a6Slling 	 *
304b81d61a6Slling 	 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
305b81d61a6Slling 	 * which is the same as MAXNAMELEN used in the kernel.
306b81d61a6Slling 	 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
307b81d61a6Slling 	 * places using MAXNAMELEN.
308b81d61a6Slling 	 */
309b81d61a6Slling 	if (strlen(pool) >= MAXNAMELEN) {
310b81d61a6Slling 		if (why)
311b81d61a6Slling 			*why = NAME_ERR_TOOLONG;
312b81d61a6Slling 		return (-1);
313b81d61a6Slling 	}
314b81d61a6Slling 
315fa9e4066Sahrens 	c = pool;
316fa9e4066Sahrens 	while (*c != '\0') {
317fa9e4066Sahrens 		if (!valid_char(*c)) {
318fa9e4066Sahrens 			if (why) {
319fa9e4066Sahrens 				*why = NAME_ERR_INVALCHAR;
320fa9e4066Sahrens 				*what = *c;
321fa9e4066Sahrens 			}
322fa9e4066Sahrens 			return (-1);
323fa9e4066Sahrens 		}
324fa9e4066Sahrens 		c++;
325fa9e4066Sahrens 	}
326fa9e4066Sahrens 
327fa9e4066Sahrens 	if (!(*pool >= 'a' && *pool <= 'z') &&
328fa9e4066Sahrens 	    !(*pool >= 'A' && *pool <= 'Z')) {
329fa9e4066Sahrens 		if (why)
330fa9e4066Sahrens 			*why = NAME_ERR_NOLETTER;
331fa9e4066Sahrens 		return (-1);
332fa9e4066Sahrens 	}
333fa9e4066Sahrens 
334fa9e4066Sahrens 	if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
335fa9e4066Sahrens 		if (why)
336fa9e4066Sahrens 			*why = NAME_ERR_RESERVED;
337fa9e4066Sahrens 		return (-1);
338fa9e4066Sahrens 	}
339fa9e4066Sahrens 
340fa9e4066Sahrens 	if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
341fa9e4066Sahrens 		if (why)
342fa9e4066Sahrens 			*why = NAME_ERR_DISKLIKE;
343fa9e4066Sahrens 		return (-1);
344fa9e4066Sahrens 	}
345fa9e4066Sahrens 
346fa9e4066Sahrens 	return (0);
347fa9e4066Sahrens }
348fa9e4066Sahrens 
349fa9e4066Sahrens /*
350fa9e4066Sahrens  * Check if the dataset name is private for internal usage.
351fa9e4066Sahrens  * '$' is reserved for internal dataset names. e.g. "$MOS"
352fa9e4066Sahrens  *
353fa9e4066Sahrens  * Return 1 if the given name is used internally.
354fa9e4066Sahrens  * Return 0 if it is not.
355fa9e4066Sahrens  */
356fa9e4066Sahrens int
357fa9e4066Sahrens dataset_name_hidden(const char *name)
358fa9e4066Sahrens {
359fa9e4066Sahrens 	if (strchr(name, '$') != NULL)
360fa9e4066Sahrens 		return (1);
361fa9e4066Sahrens 
362fa9e4066Sahrens 	return (0);
363fa9e4066Sahrens }
364