xref: /titanic_44/usr/src/common/zfs/zfs_namecheck.c (revision 8d4e547db823a866b8f73efc0acdc423e2963caf)
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 "zfs_namecheck.h"
48 
49 static int
50 valid_char(char c)
51 {
52 	return ((c >= 'a' && c <= 'z') ||
53 	    (c >= 'A' && c <= 'Z') ||
54 	    (c >= '0' && c <= '9') ||
55 	    c == '-' || c == '_' || c == '.' || c == ':');
56 }
57 
58 /*
59  * Snapshot names must be made up of alphanumeric characters plus the following
60  * characters:
61  *
62  * 	[-_.:]
63  */
64 int
65 snapshot_namecheck(const char *path, namecheck_err_t *why, char *what)
66 {
67 	const char *loc;
68 
69 	if (strlen(path) >= MAXNAMELEN) {
70 		if (why)
71 			*why = NAME_ERR_TOOLONG;
72 		return (-1);
73 	}
74 
75 	if (path[0] == '\0') {
76 		if (why)
77 			*why = NAME_ERR_EMPTY_COMPONENT;
78 		return (-1);
79 	}
80 
81 	for (loc = path; *loc; loc++) {
82 		if (!valid_char(*loc)) {
83 			if (why) {
84 				*why = NAME_ERR_INVALCHAR;
85 				*what = *loc;
86 			}
87 			return (-1);
88 		}
89 	}
90 	return (0);
91 }
92 
93 /*
94  * Dataset names must be of the following form:
95  *
96  * 	[component][/]*[component][@component]
97  *
98  * Where each component is made up of alphanumeric characters plus the following
99  * characters:
100  *
101  * 	[-_.:]
102  */
103 int
104 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
105 {
106 	const char *loc, *end;
107 	int found_snapshot;
108 
109 	/*
110 	 * Make sure the name is not too long.
111 	 *
112 	 * ZFS_MAXNAMELEN is the maximum dataset length used in the userland
113 	 * which is the same as MAXNAMELEN used in the kernel.
114 	 * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all
115 	 * places using MAXNAMELEN.
116 	 */
117 	if (strlen(path) >= MAXNAMELEN) {
118 		if (why)
119 			*why = NAME_ERR_TOOLONG;
120 		return (-1);
121 	}
122 
123 	/* Explicitly check for a leading slash.  */
124 	if (path[0] == '/') {
125 		if (why)
126 			*why = NAME_ERR_LEADING_SLASH;
127 		return (-1);
128 	}
129 
130 	if (path[0] == '\0') {
131 		if (why)
132 			*why = NAME_ERR_EMPTY_COMPONENT;
133 		return (-1);
134 	}
135 
136 	loc = path;
137 	found_snapshot = 0;
138 	for (;;) {
139 		/* Find the end of this component */
140 		end = loc;
141 		while (*end != '/' && *end != '@' && *end != '\0')
142 			end++;
143 
144 		if (*end == '\0' && end[-1] == '/') {
145 			/* trailing slashes are not allowed */
146 			if (why)
147 				*why = NAME_ERR_TRAILING_SLASH;
148 			return (-1);
149 		}
150 
151 		/* Zero-length components are not allowed */
152 		if (loc == end) {
153 			if (why) {
154 				/*
155 				 * Make sure this is really a zero-length
156 				 * component and not a '@@'.
157 				 */
158 				if (*end == '@' && found_snapshot) {
159 					*why = NAME_ERR_MULTIPLE_AT;
160 				} else {
161 					*why = NAME_ERR_EMPTY_COMPONENT;
162 				}
163 			}
164 
165 			return (-1);
166 		}
167 
168 		/* Validate the contents of this component */
169 		while (loc != end) {
170 			if (!valid_char(*loc)) {
171 				if (why) {
172 					*why = NAME_ERR_INVALCHAR;
173 					*what = *loc;
174 				}
175 				return (-1);
176 			}
177 			loc++;
178 		}
179 
180 		/* If we've reached the end of the string, we're OK */
181 		if (*end == '\0')
182 			return (0);
183 
184 		if (*end == '@') {
185 			/*
186 			 * If we've found an @ symbol, indicate that we're in
187 			 * the snapshot component, and report a second '@'
188 			 * character as an error.
189 			 */
190 			if (found_snapshot) {
191 				if (why)
192 					*why = NAME_ERR_MULTIPLE_AT;
193 				return (-1);
194 			}
195 
196 			found_snapshot = 1;
197 		}
198 
199 		/*
200 		 * If there is a '/' in a snapshot name
201 		 * then report an error
202 		 */
203 		if (*end == '/' && found_snapshot) {
204 			if (why)
205 				*why = NAME_ERR_TRAILING_SLASH;
206 			return (-1);
207 		}
208 
209 		/* Update to the next component */
210 		loc = end + 1;
211 	}
212 }
213 
214 /*
215  * For pool names, we have the same set of valid characters as described in
216  * dataset names, with the additional restriction that the pool name must begin
217  * with a letter.  The pool names 'raidz' and 'mirror' are also reserved names
218  * that cannot be used.
219  */
220 int
221 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
222 {
223 	const char *c;
224 
225 	/*
226 	 * Make sure the name is not too long.
227 	 *
228 	 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
229 	 * which is the same as MAXNAMELEN used in the kernel.
230 	 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
231 	 * places using MAXNAMELEN.
232 	 */
233 	if (strlen(pool) >= MAXNAMELEN) {
234 		if (why)
235 			*why = NAME_ERR_TOOLONG;
236 		return (-1);
237 	}
238 
239 	c = pool;
240 	while (*c != '\0') {
241 		if (!valid_char(*c)) {
242 			if (why) {
243 				*why = NAME_ERR_INVALCHAR;
244 				*what = *c;
245 			}
246 			return (-1);
247 		}
248 		c++;
249 	}
250 
251 	if (!(*pool >= 'a' && *pool <= 'z') &&
252 	    !(*pool >= 'A' && *pool <= 'Z')) {
253 		if (why)
254 			*why = NAME_ERR_NOLETTER;
255 		return (-1);
256 	}
257 
258 	if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
259 		if (why)
260 			*why = NAME_ERR_RESERVED;
261 		return (-1);
262 	}
263 
264 	if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
265 		if (why)
266 			*why = NAME_ERR_DISKLIKE;
267 		return (-1);
268 	}
269 
270 	return (0);
271 }
272 
273 /*
274  * Check if the dataset name is private for internal usage.
275  * '$' is reserved for internal dataset names. e.g. "$MOS"
276  *
277  * Return 1 if the given name is used internally.
278  * Return 0 if it is not.
279  */
280 int
281 dataset_name_hidden(const char *name)
282 {
283 	if (strchr(name, '$') != NULL)
284 		return (1);
285 
286 	return (0);
287 }
288