xref: /illumos-gate/usr/src/common/zfs/zfs_namecheck.c (revision 753d2d2e8e7fd0c9bcf736d9bf2f2faf4d6234cc)
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 2006 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 				*why = NAME_ERR_EMPTY_COMPONENT;
155 			return (-1);
156 		}
157 
158 		/* Validate the contents of this component */
159 		while (loc != end) {
160 			if (!valid_char(*loc)) {
161 				if (why) {
162 					*why = NAME_ERR_INVALCHAR;
163 					*what = *loc;
164 				}
165 				return (-1);
166 			}
167 			loc++;
168 		}
169 
170 		/* If we've reached the end of the string, we're OK */
171 		if (*end == '\0')
172 			return (0);
173 
174 		if (*end == '@') {
175 			/*
176 			 * If we've found an @ symbol, indicate that we're in
177 			 * the snapshot component, and report a second '@'
178 			 * character as an error.
179 			 */
180 			if (found_snapshot) {
181 				if (why)
182 					*why = NAME_ERR_MULTIPLE_AT;
183 				return (-1);
184 			}
185 
186 			found_snapshot = 1;
187 		}
188 
189 		/*
190 		 * If there is a '/' in a snapshot name
191 		 * then report an error
192 		 */
193 		if (*end == '/' && found_snapshot) {
194 			if (why)
195 				*why = NAME_ERR_TRAILING_SLASH;
196 			return (-1);
197 		}
198 
199 		/* Update to the next component */
200 		loc = end + 1;
201 	}
202 }
203 
204 /*
205  * For pool names, we have the same set of valid characters as described in
206  * dataset names, with the additional restriction that the pool name must begin
207  * with a letter.  The pool names 'raidz' and 'mirror' are also reserved names
208  * that cannot be used.
209  */
210 int
211 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
212 {
213 	const char *c;
214 
215 	/*
216 	 * Make sure the name is not too long.
217 	 *
218 	 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
219 	 * which is the same as MAXNAMELEN used in the kernel.
220 	 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
221 	 * places using MAXNAMELEN.
222 	 */
223 	if (strlen(pool) >= MAXNAMELEN) {
224 		if (why)
225 			*why = NAME_ERR_TOOLONG;
226 		return (-1);
227 	}
228 
229 	c = pool;
230 	while (*c != '\0') {
231 		if (!valid_char(*c)) {
232 			if (why) {
233 				*why = NAME_ERR_INVALCHAR;
234 				*what = *c;
235 			}
236 			return (-1);
237 		}
238 		c++;
239 	}
240 
241 	if (!(*pool >= 'a' && *pool <= 'z') &&
242 	    !(*pool >= 'A' && *pool <= 'Z')) {
243 		if (why)
244 			*why = NAME_ERR_NOLETTER;
245 		return (-1);
246 	}
247 
248 	if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
249 		if (why)
250 			*why = NAME_ERR_RESERVED;
251 		return (-1);
252 	}
253 
254 	if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
255 		if (why)
256 			*why = NAME_ERR_DISKLIKE;
257 		return (-1);
258 	}
259 
260 	return (0);
261 }
262 
263 /*
264  * Check if the dataset name is private for internal usage.
265  * '$' is reserved for internal dataset names. e.g. "$MOS"
266  *
267  * Return 1 if the given name is used internally.
268  * Return 0 if it is not.
269  */
270 int
271 dataset_name_hidden(const char *name)
272 {
273 	if (strchr(name, '$') != NULL)
274 		return (1);
275 
276 	return (0);
277 }
278