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