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 /* Update to the next component */ 190 loc = end + 1; 191 } 192 } 193 194 /* 195 * For pool names, we have the same set of valid characters as described in 196 * dataset names, with the additional restriction that the pool name must begin 197 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names 198 * that cannot be used. 199 */ 200 int 201 pool_namecheck(const char *pool, namecheck_err_t *why, char *what) 202 { 203 const char *c; 204 205 /* 206 * Make sure the name is not too long. 207 * 208 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland 209 * which is the same as MAXNAMELEN used in the kernel. 210 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all 211 * places using MAXNAMELEN. 212 */ 213 if (strlen(pool) >= MAXNAMELEN) { 214 if (why) 215 *why = NAME_ERR_TOOLONG; 216 return (-1); 217 } 218 219 c = pool; 220 while (*c != '\0') { 221 if (!valid_char(*c)) { 222 if (why) { 223 *why = NAME_ERR_INVALCHAR; 224 *what = *c; 225 } 226 return (-1); 227 } 228 c++; 229 } 230 231 if (!(*pool >= 'a' && *pool <= 'z') && 232 !(*pool >= 'A' && *pool <= 'Z')) { 233 if (why) 234 *why = NAME_ERR_NOLETTER; 235 return (-1); 236 } 237 238 if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) { 239 if (why) 240 *why = NAME_ERR_RESERVED; 241 return (-1); 242 } 243 244 if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) { 245 if (why) 246 *why = NAME_ERR_DISKLIKE; 247 return (-1); 248 } 249 250 return (0); 251 } 252 253 /* 254 * Check if the dataset name is private for internal usage. 255 * '$' is reserved for internal dataset names. e.g. "$MOS" 256 * 257 * Return 1 if the given name is used internally. 258 * Return 0 if it is not. 259 */ 260 int 261 dataset_name_hidden(const char *name) 262 { 263 if (strchr(name, '$') != NULL) 264 return (1); 265 266 return (0); 267 } 268