xref: /titanic_50/usr/src/common/zfs/zfs_namecheck.c (revision 89eef05efc3ad959ce1fcc4fbf4d428b4fc015ad)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Common name validation routines for ZFS.  These routines are shared by the
30  * userland code as well as the ioctl() layer to ensure that we don't
31  * inadvertently expose a hole through direct ioctl()s that never gets tested.
32  * In userland, however, we want significantly more information about _why_ the
33  * name is invalid.  In the kernel, we only care whether it's valid or not.
34  * Each routine therefore takes a 'namecheck_err_t' which describes exactly why
35  * the name failed to validate.
36  *
37  * Each function returns 0 on success, -1 on error.
38  */
39 
40 #if defined(_KERNEL)
41 #include <sys/systm.h>
42 #else
43 #include <string.h>
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/nvpair.h>
48 #include "zfs_namecheck.h"
49 #include "zfs_deleg.h"
50 
51 static int
52 valid_char(char c)
53 {
54 	return ((c >= 'a' && c <= 'z') ||
55 	    (c >= 'A' && c <= 'Z') ||
56 	    (c >= '0' && c <= '9') ||
57 	    c == '-' || c == '_' || c == '.' || c == ':');
58 }
59 
60 /*
61  * Snapshot names must be made up of alphanumeric characters plus the following
62  * characters:
63  *
64  * 	[-_.:]
65  */
66 int
67 snapshot_namecheck(const char *path, namecheck_err_t *why, char *what)
68 {
69 	const char *loc;
70 
71 	if (strlen(path) >= MAXNAMELEN) {
72 		if (why)
73 			*why = NAME_ERR_TOOLONG;
74 		return (-1);
75 	}
76 
77 	if (path[0] == '\0') {
78 		if (why)
79 			*why = NAME_ERR_EMPTY_COMPONENT;
80 		return (-1);
81 	}
82 
83 	for (loc = path; *loc; loc++) {
84 		if (!valid_char(*loc)) {
85 			if (why) {
86 				*why = NAME_ERR_INVALCHAR;
87 				*what = *loc;
88 			}
89 			return (-1);
90 		}
91 	}
92 	return (0);
93 }
94 
95 
96 /*
97  * Permissions set name must start with the letter '@' followed by the
98  * same character restrictions as snapshot names, except that the name
99  * cannot exceed 64 characters.
100  */
101 int
102 permset_namecheck(const char *path, namecheck_err_t *why, char *what)
103 {
104 	if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
105 		if (why)
106 			*why = NAME_ERR_TOOLONG;
107 		return (-1);
108 	}
109 
110 	if (path[0] != '@') {
111 		if (why) {
112 			*why = NAME_ERR_NO_AT;
113 			*what = path[0];
114 		}
115 		return (-1);
116 	}
117 
118 	return (snapshot_namecheck(&path[1], why, what));
119 }
120 
121 /*
122  * Dataset names must be of the following form:
123  *
124  * 	[component][/]*[component][@component]
125  *
126  * Where each component is made up of alphanumeric characters plus the following
127  * characters:
128  *
129  * 	[-_.:]
130  */
131 int
132 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
133 {
134 	const char *loc, *end;
135 	int found_snapshot;
136 
137 	/*
138 	 * Make sure the name is not too long.
139 	 *
140 	 * ZFS_MAXNAMELEN is the maximum dataset length used in the userland
141 	 * which is the same as MAXNAMELEN used in the kernel.
142 	 * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all
143 	 * places using MAXNAMELEN.
144 	 */
145 
146 	if (strlen(path) >= MAXNAMELEN) {
147 		if (why)
148 			*why = NAME_ERR_TOOLONG;
149 		return (-1);
150 	}
151 
152 	/* Explicitly check for a leading slash.  */
153 	if (path[0] == '/') {
154 		if (why)
155 			*why = NAME_ERR_LEADING_SLASH;
156 		return (-1);
157 	}
158 
159 	if (path[0] == '\0') {
160 		if (why)
161 			*why = NAME_ERR_EMPTY_COMPONENT;
162 		return (-1);
163 	}
164 
165 	loc = path;
166 	found_snapshot = 0;
167 	for (;;) {
168 		/* Find the end of this component */
169 		end = loc;
170 		while (*end != '/' && *end != '@' && *end != '\0')
171 			end++;
172 
173 		if (*end == '\0' && end[-1] == '/') {
174 			/* trailing slashes are not allowed */
175 			if (why)
176 				*why = NAME_ERR_TRAILING_SLASH;
177 			return (-1);
178 		}
179 
180 		/* Zero-length components are not allowed */
181 		if (loc == end) {
182 			if (why) {
183 				/*
184 				 * Make sure this is really a zero-length
185 				 * component and not a '@@'.
186 				 */
187 				if (*end == '@' && found_snapshot) {
188 					*why = NAME_ERR_MULTIPLE_AT;
189 				} else {
190 					*why = NAME_ERR_EMPTY_COMPONENT;
191 				}
192 			}
193 
194 			return (-1);
195 		}
196 
197 		/* Validate the contents of this component */
198 		while (loc != end) {
199 			if (!valid_char(*loc)) {
200 				if (why) {
201 					*why = NAME_ERR_INVALCHAR;
202 					*what = *loc;
203 				}
204 				return (-1);
205 			}
206 			loc++;
207 		}
208 
209 		/* If we've reached the end of the string, we're OK */
210 		if (*end == '\0')
211 			return (0);
212 
213 		if (*end == '@') {
214 			/*
215 			 * If we've found an @ symbol, indicate that we're in
216 			 * the snapshot component, and report a second '@'
217 			 * character as an error.
218 			 */
219 			if (found_snapshot) {
220 				if (why)
221 					*why = NAME_ERR_MULTIPLE_AT;
222 				return (-1);
223 			}
224 
225 			found_snapshot = 1;
226 		}
227 
228 		/*
229 		 * If there is a '/' in a snapshot name
230 		 * then report an error
231 		 */
232 		if (*end == '/' && found_snapshot) {
233 			if (why)
234 				*why = NAME_ERR_TRAILING_SLASH;
235 			return (-1);
236 		}
237 
238 		/* Update to the next component */
239 		loc = end + 1;
240 	}
241 }
242 
243 
244 /*
245  * mountpoint names must be of the following form:
246  *
247  *	/[component][/]*[component][/]
248  */
249 int
250 mountpoint_namecheck(const char *path, namecheck_err_t *why)
251 {
252 	const char *start, *end;
253 
254 	/*
255 	 * Make sure none of the mountpoint component names are too long.
256 	 * If a component name is too long then the mkdir of the mountpoint
257 	 * will fail but then the mountpoint property will be set to a value
258 	 * that can never be mounted.  Better to fail before setting the prop.
259 	 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
260 	 */
261 
262 	if (path == NULL || *path != '/') {
263 		if (why)
264 			*why = NAME_ERR_LEADING_SLASH;
265 		return (-1);
266 	}
267 
268 	/* Skip leading slash  */
269 	start = &path[1];
270 	do {
271 		end = start;
272 		while (*end != '/' && *end != '\0')
273 			end++;
274 
275 		if (end - start >= MAXNAMELEN) {
276 			if (why)
277 				*why = NAME_ERR_TOOLONG;
278 			return (-1);
279 		}
280 		start = end + 1;
281 
282 	} while (*end != '\0');
283 
284 	return (0);
285 }
286 
287 /*
288  * For pool names, we have the same set of valid characters as described in
289  * dataset names, with the additional restriction that the pool name must begin
290  * with a letter.  The pool names 'raidz' and 'mirror' are also reserved names
291  * that cannot be used.
292  */
293 int
294 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
295 {
296 	const char *c;
297 
298 	/*
299 	 * Make sure the name is not too long.
300 	 *
301 	 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
302 	 * which is the same as MAXNAMELEN used in the kernel.
303 	 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
304 	 * places using MAXNAMELEN.
305 	 */
306 	if (strlen(pool) >= MAXNAMELEN) {
307 		if (why)
308 			*why = NAME_ERR_TOOLONG;
309 		return (-1);
310 	}
311 
312 	c = pool;
313 	while (*c != '\0') {
314 		if (!valid_char(*c)) {
315 			if (why) {
316 				*why = NAME_ERR_INVALCHAR;
317 				*what = *c;
318 			}
319 			return (-1);
320 		}
321 		c++;
322 	}
323 
324 	if (!(*pool >= 'a' && *pool <= 'z') &&
325 	    !(*pool >= 'A' && *pool <= 'Z')) {
326 		if (why)
327 			*why = NAME_ERR_NOLETTER;
328 		return (-1);
329 	}
330 
331 	if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
332 		if (why)
333 			*why = NAME_ERR_RESERVED;
334 		return (-1);
335 	}
336 
337 	if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
338 		if (why)
339 			*why = NAME_ERR_DISKLIKE;
340 		return (-1);
341 	}
342 
343 	return (0);
344 }
345 
346 /*
347  * Check if the dataset name is private for internal usage.
348  * '$' is reserved for internal dataset names. e.g. "$MOS"
349  *
350  * Return 1 if the given name is used internally.
351  * Return 0 if it is not.
352  */
353 int
354 dataset_name_hidden(const char *name)
355 {
356 	if (strchr(name, '$') != NULL)
357 		return (1);
358 
359 	return (0);
360 }
361