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 * For pool names, we have the same set of valid characters as described in 245 * dataset names, with the additional restriction that the pool name must begin 246 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names 247 * that cannot be used. 248 */ 249 int 250 pool_namecheck(const char *pool, namecheck_err_t *why, char *what) 251 { 252 const char *c; 253 254 /* 255 * Make sure the name is not too long. 256 * 257 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland 258 * which is the same as MAXNAMELEN used in the kernel. 259 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all 260 * places using MAXNAMELEN. 261 */ 262 if (strlen(pool) >= MAXNAMELEN) { 263 if (why) 264 *why = NAME_ERR_TOOLONG; 265 return (-1); 266 } 267 268 c = pool; 269 while (*c != '\0') { 270 if (!valid_char(*c)) { 271 if (why) { 272 *why = NAME_ERR_INVALCHAR; 273 *what = *c; 274 } 275 return (-1); 276 } 277 c++; 278 } 279 280 if (!(*pool >= 'a' && *pool <= 'z') && 281 !(*pool >= 'A' && *pool <= 'Z')) { 282 if (why) 283 *why = NAME_ERR_NOLETTER; 284 return (-1); 285 } 286 287 if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) { 288 if (why) 289 *why = NAME_ERR_RESERVED; 290 return (-1); 291 } 292 293 if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) { 294 if (why) 295 *why = NAME_ERR_DISKLIKE; 296 return (-1); 297 } 298 299 return (0); 300 } 301 302 /* 303 * Check if the dataset name is private for internal usage. 304 * '$' is reserved for internal dataset names. e.g. "$MOS" 305 * 306 * Return 1 if the given name is used internally. 307 * Return 0 if it is not. 308 */ 309 int 310 dataset_name_hidden(const char *name) 311 { 312 if (strchr(name, '$') != NULL) 313 return (1); 314 315 return (0); 316 } 317