xref: /titanic_41/usr/src/common/zfs/zfs_namecheck.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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 "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  * Dataset names must be of the following form:
60  *
61  * 	[component][/]*[component][@component]
62  *
63  * Where each component is made up of alphanumeric characters plus the following
64  * characters:
65  *
66  * 	[-_.:]
67  */
68 int
69 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
70 {
71 	const char *loc, *end;
72 	int found_snapshot;
73 
74 	/* Explicitly check for a leading slash.  */
75 	if (path[0] == '/') {
76 		if (why)
77 			*why = NAME_ERR_LEADING_SLASH;
78 		return (-1);
79 	}
80 
81 	if (path[0] == '\0') {
82 		if (why)
83 			*why = NAME_ERR_EMPTY_COMPONENT;
84 		return (-1);
85 	}
86 
87 	loc = path;
88 	found_snapshot = 0;
89 	for (;;) {
90 		/* Find the end of this component */
91 		end = loc;
92 		while (*end != '/' && *end != '@' && *end != '\0')
93 			end++;
94 
95 		if (*end == '\0' && end[-1] == '/') {
96 			/* trailing slashes are not allowed */
97 			if (why)
98 				*why = NAME_ERR_TRAILING_SLASH;
99 			return (-1);
100 		}
101 
102 		/* Zero-length components are not allowed */
103 		if (loc == end) {
104 			if (why)
105 				*why = NAME_ERR_EMPTY_COMPONENT;
106 			return (-1);
107 		}
108 
109 		/* Validate the contents of this component */
110 		while (loc != end) {
111 			if (!valid_char(*loc)) {
112 				if (why) {
113 					*why = NAME_ERR_INVALCHAR;
114 					*what = *loc;
115 				}
116 				return (-1);
117 			}
118 			loc++;
119 		}
120 
121 		/* If we've reached the end of the string, we're OK */
122 		if (*end == '\0')
123 			return (0);
124 
125 		if (*end == '@') {
126 			/*
127 			 * If we've found an @ symbol, indicate that we're in
128 			 * the snapshot component, and report a second '@'
129 			 * character as an error.
130 			 */
131 			if (found_snapshot) {
132 				if (why)
133 					*why = NAME_ERR_MULTIPLE_AT;
134 				return (-1);
135 			}
136 
137 			found_snapshot = 1;
138 		}
139 
140 		/* Update to the next component */
141 		loc = end + 1;
142 	}
143 }
144 
145 /*
146  * For pool names, we have the same set of valid characters as described in
147  * dataset names, with the additional restriction that the pool name must begin
148  * with a letter.  The pool names 'raidz' and 'mirror' are also reserved names
149  * that cannot be used.
150  */
151 int
152 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
153 {
154 	const char *c;
155 
156 	c = pool;
157 	while (*c != '\0') {
158 		if (!valid_char(*c)) {
159 			if (why) {
160 				*why = NAME_ERR_INVALCHAR;
161 				*what = *c;
162 			}
163 			return (-1);
164 		}
165 		c++;
166 	}
167 
168 	if (!(*pool >= 'a' && *pool <= 'z') &&
169 	    !(*pool >= 'A' && *pool <= 'Z')) {
170 		if (why)
171 			*why = NAME_ERR_NOLETTER;
172 		return (-1);
173 	}
174 
175 	if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
176 		if (why)
177 			*why = NAME_ERR_RESERVED;
178 		return (-1);
179 	}
180 
181 	if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
182 		if (why)
183 			*why = NAME_ERR_DISKLIKE;
184 		return (-1);
185 	}
186 
187 	return (0);
188 }
189 
190 /*
191  * Check if the dataset name is private for internal usage.
192  * '$' is reserved for internal dataset names. e.g. "$MOS"
193  *
194  * Return 1 if the given name is used internally.
195  * Return 0 if it is not.
196  */
197 int
198 dataset_name_hidden(const char *name)
199 {
200 	if (strchr(name, '$') != NULL)
201 		return (1);
202 
203 	return (0);
204 }
205