1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 51d452cf5Sahrens * Common Development and Distribution License (the "License"). 61d452cf5Sahrens * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22*14843421SMatthew Ahrens * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens /* 27fa9e4066Sahrens * Common name validation routines for ZFS. These routines are shared by the 28fa9e4066Sahrens * userland code as well as the ioctl() layer to ensure that we don't 29fa9e4066Sahrens * inadvertently expose a hole through direct ioctl()s that never gets tested. 30fa9e4066Sahrens * In userland, however, we want significantly more information about _why_ the 31fa9e4066Sahrens * name is invalid. In the kernel, we only care whether it's valid or not. 32fa9e4066Sahrens * Each routine therefore takes a 'namecheck_err_t' which describes exactly why 33fa9e4066Sahrens * the name failed to validate. 34fa9e4066Sahrens * 35fa9e4066Sahrens * Each function returns 0 on success, -1 on error. 36fa9e4066Sahrens */ 37fa9e4066Sahrens 38fa9e4066Sahrens #if defined(_KERNEL) 39fa9e4066Sahrens #include <sys/systm.h> 40fa9e4066Sahrens #else 41fa9e4066Sahrens #include <string.h> 42fa9e4066Sahrens #endif 43fa9e4066Sahrens 44b81d61a6Slling #include <sys/param.h> 45ecd6cf80Smarks #include <sys/nvpair.h> 46fa9e4066Sahrens #include "zfs_namecheck.h" 47ecd6cf80Smarks #include "zfs_deleg.h" 48fa9e4066Sahrens 49fa9e4066Sahrens static int 50fa9e4066Sahrens valid_char(char c) 51fa9e4066Sahrens { 52fa9e4066Sahrens return ((c >= 'a' && c <= 'z') || 53fa9e4066Sahrens (c >= 'A' && c <= 'Z') || 54fa9e4066Sahrens (c >= '0' && c <= '9') || 556d1e0b89Smarks c == '-' || c == '_' || c == '.' || c == ':' || c == ' '); 56fa9e4066Sahrens } 57fa9e4066Sahrens 58fa9e4066Sahrens /* 591d452cf5Sahrens * Snapshot names must be made up of alphanumeric characters plus the following 601d452cf5Sahrens * characters: 611d452cf5Sahrens * 62f0ed2251Sek110237 * [-_.:] 631d452cf5Sahrens */ 641d452cf5Sahrens int 651d452cf5Sahrens snapshot_namecheck(const char *path, namecheck_err_t *why, char *what) 661d452cf5Sahrens { 671d452cf5Sahrens const char *loc; 681d452cf5Sahrens 691d452cf5Sahrens if (strlen(path) >= MAXNAMELEN) { 701d452cf5Sahrens if (why) 711d452cf5Sahrens *why = NAME_ERR_TOOLONG; 721d452cf5Sahrens return (-1); 731d452cf5Sahrens } 741d452cf5Sahrens 751d452cf5Sahrens if (path[0] == '\0') { 761d452cf5Sahrens if (why) 771d452cf5Sahrens *why = NAME_ERR_EMPTY_COMPONENT; 781d452cf5Sahrens return (-1); 791d452cf5Sahrens } 801d452cf5Sahrens 811d452cf5Sahrens for (loc = path; *loc; loc++) { 821d452cf5Sahrens if (!valid_char(*loc)) { 831d452cf5Sahrens if (why) { 841d452cf5Sahrens *why = NAME_ERR_INVALCHAR; 851d452cf5Sahrens *what = *loc; 861d452cf5Sahrens } 871d452cf5Sahrens return (-1); 881d452cf5Sahrens } 891d452cf5Sahrens } 901d452cf5Sahrens return (0); 911d452cf5Sahrens } 921d452cf5Sahrens 93ecd6cf80Smarks 94ecd6cf80Smarks /* 95ecd6cf80Smarks * Permissions set name must start with the letter '@' followed by the 96ecd6cf80Smarks * same character restrictions as snapshot names, except that the name 97ecd6cf80Smarks * cannot exceed 64 characters. 98ecd6cf80Smarks */ 99ecd6cf80Smarks int 100ecd6cf80Smarks permset_namecheck(const char *path, namecheck_err_t *why, char *what) 101ecd6cf80Smarks { 102ecd6cf80Smarks if (strlen(path) >= ZFS_PERMSET_MAXLEN) { 103ecd6cf80Smarks if (why) 104ecd6cf80Smarks *why = NAME_ERR_TOOLONG; 105ecd6cf80Smarks return (-1); 106ecd6cf80Smarks } 107ecd6cf80Smarks 108ecd6cf80Smarks if (path[0] != '@') { 109ecd6cf80Smarks if (why) { 110ecd6cf80Smarks *why = NAME_ERR_NO_AT; 111ecd6cf80Smarks *what = path[0]; 112ecd6cf80Smarks } 113ecd6cf80Smarks return (-1); 114ecd6cf80Smarks } 115ecd6cf80Smarks 116ecd6cf80Smarks return (snapshot_namecheck(&path[1], why, what)); 117ecd6cf80Smarks } 118ecd6cf80Smarks 1191d452cf5Sahrens /* 120fa9e4066Sahrens * Dataset names must be of the following form: 121fa9e4066Sahrens * 122fa9e4066Sahrens * [component][/]*[component][@component] 123fa9e4066Sahrens * 124fa9e4066Sahrens * Where each component is made up of alphanumeric characters plus the following 125fa9e4066Sahrens * characters: 126fa9e4066Sahrens * 127f18faf3fSek110237 * [-_.:%] 128f0ed2251Sek110237 * 129f0ed2251Sek110237 * We allow '%' here as we use that character internally to create unique 130f0ed2251Sek110237 * names for temporary clones (for online recv). 131fa9e4066Sahrens */ 132fa9e4066Sahrens int 133fa9e4066Sahrens dataset_namecheck(const char *path, namecheck_err_t *why, char *what) 134fa9e4066Sahrens { 135fa9e4066Sahrens const char *loc, *end; 136fa9e4066Sahrens int found_snapshot; 137fa9e4066Sahrens 138b81d61a6Slling /* 139b81d61a6Slling * Make sure the name is not too long. 140b81d61a6Slling * 141b81d61a6Slling * ZFS_MAXNAMELEN is the maximum dataset length used in the userland 142b81d61a6Slling * which is the same as MAXNAMELEN used in the kernel. 143b81d61a6Slling * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all 144b81d61a6Slling * places using MAXNAMELEN. 145b81d61a6Slling */ 146ecd6cf80Smarks 147b81d61a6Slling if (strlen(path) >= MAXNAMELEN) { 148b81d61a6Slling if (why) 149b81d61a6Slling *why = NAME_ERR_TOOLONG; 150b81d61a6Slling return (-1); 151b81d61a6Slling } 152b81d61a6Slling 153fa9e4066Sahrens /* Explicitly check for a leading slash. */ 154fa9e4066Sahrens if (path[0] == '/') { 155fa9e4066Sahrens if (why) 156fa9e4066Sahrens *why = NAME_ERR_LEADING_SLASH; 157fa9e4066Sahrens return (-1); 158fa9e4066Sahrens } 159fa9e4066Sahrens 160fa9e4066Sahrens if (path[0] == '\0') { 161fa9e4066Sahrens if (why) 162fa9e4066Sahrens *why = NAME_ERR_EMPTY_COMPONENT; 163fa9e4066Sahrens return (-1); 164fa9e4066Sahrens } 165fa9e4066Sahrens 166fa9e4066Sahrens loc = path; 167fa9e4066Sahrens found_snapshot = 0; 168fa9e4066Sahrens for (;;) { 169fa9e4066Sahrens /* Find the end of this component */ 170fa9e4066Sahrens end = loc; 171fa9e4066Sahrens while (*end != '/' && *end != '@' && *end != '\0') 172fa9e4066Sahrens end++; 173fa9e4066Sahrens 174fa9e4066Sahrens if (*end == '\0' && end[-1] == '/') { 175fa9e4066Sahrens /* trailing slashes are not allowed */ 176fa9e4066Sahrens if (why) 177fa9e4066Sahrens *why = NAME_ERR_TRAILING_SLASH; 178fa9e4066Sahrens return (-1); 179fa9e4066Sahrens } 180fa9e4066Sahrens 181fa9e4066Sahrens /* Zero-length components are not allowed */ 182adab9adaSpd144616 if (loc == end) { 183adab9adaSpd144616 if (why) { 184adab9adaSpd144616 /* 185adab9adaSpd144616 * Make sure this is really a zero-length 186adab9adaSpd144616 * component and not a '@@'. 187adab9adaSpd144616 */ 188adab9adaSpd144616 if (*end == '@' && found_snapshot) { 189adab9adaSpd144616 *why = NAME_ERR_MULTIPLE_AT; 190adab9adaSpd144616 } else { 191fa9e4066Sahrens *why = NAME_ERR_EMPTY_COMPONENT; 192adab9adaSpd144616 } 193adab9adaSpd144616 } 194adab9adaSpd144616 195fa9e4066Sahrens return (-1); 196fa9e4066Sahrens } 197fa9e4066Sahrens 198fa9e4066Sahrens /* Validate the contents of this component */ 199fa9e4066Sahrens while (loc != end) { 200f0ed2251Sek110237 if (!valid_char(*loc) && *loc != '%') { 201fa9e4066Sahrens if (why) { 202fa9e4066Sahrens *why = NAME_ERR_INVALCHAR; 203fa9e4066Sahrens *what = *loc; 204fa9e4066Sahrens } 205fa9e4066Sahrens return (-1); 206fa9e4066Sahrens } 207fa9e4066Sahrens loc++; 208fa9e4066Sahrens } 209fa9e4066Sahrens 210fa9e4066Sahrens /* If we've reached the end of the string, we're OK */ 211fa9e4066Sahrens if (*end == '\0') 212fa9e4066Sahrens return (0); 213fa9e4066Sahrens 214fa9e4066Sahrens if (*end == '@') { 215fa9e4066Sahrens /* 216fa9e4066Sahrens * If we've found an @ symbol, indicate that we're in 217fa9e4066Sahrens * the snapshot component, and report a second '@' 218fa9e4066Sahrens * character as an error. 219fa9e4066Sahrens */ 220fa9e4066Sahrens if (found_snapshot) { 221fa9e4066Sahrens if (why) 222fa9e4066Sahrens *why = NAME_ERR_MULTIPLE_AT; 223fa9e4066Sahrens return (-1); 224fa9e4066Sahrens } 225fa9e4066Sahrens 226fa9e4066Sahrens found_snapshot = 1; 227fa9e4066Sahrens } 228fa9e4066Sahrens 22998579b20Snd150628 /* 23098579b20Snd150628 * If there is a '/' in a snapshot name 23198579b20Snd150628 * then report an error 23298579b20Snd150628 */ 23398579b20Snd150628 if (*end == '/' && found_snapshot) { 23498579b20Snd150628 if (why) 23598579b20Snd150628 *why = NAME_ERR_TRAILING_SLASH; 23698579b20Snd150628 return (-1); 23798579b20Snd150628 } 23898579b20Snd150628 239fa9e4066Sahrens /* Update to the next component */ 240fa9e4066Sahrens loc = end + 1; 241fa9e4066Sahrens } 242fa9e4066Sahrens } 243fa9e4066Sahrens 24489eef05eSrm160521 24589eef05eSrm160521 /* 24689eef05eSrm160521 * mountpoint names must be of the following form: 24789eef05eSrm160521 * 24889eef05eSrm160521 * /[component][/]*[component][/] 24989eef05eSrm160521 */ 25089eef05eSrm160521 int 25189eef05eSrm160521 mountpoint_namecheck(const char *path, namecheck_err_t *why) 25289eef05eSrm160521 { 25389eef05eSrm160521 const char *start, *end; 25489eef05eSrm160521 25589eef05eSrm160521 /* 25689eef05eSrm160521 * Make sure none of the mountpoint component names are too long. 25789eef05eSrm160521 * If a component name is too long then the mkdir of the mountpoint 25889eef05eSrm160521 * will fail but then the mountpoint property will be set to a value 25989eef05eSrm160521 * that can never be mounted. Better to fail before setting the prop. 26089eef05eSrm160521 * Extra slashes are OK, they will be tossed by the mountpoint mkdir. 26189eef05eSrm160521 */ 26289eef05eSrm160521 26389eef05eSrm160521 if (path == NULL || *path != '/') { 26489eef05eSrm160521 if (why) 26589eef05eSrm160521 *why = NAME_ERR_LEADING_SLASH; 26689eef05eSrm160521 return (-1); 26789eef05eSrm160521 } 26889eef05eSrm160521 26989eef05eSrm160521 /* Skip leading slash */ 27089eef05eSrm160521 start = &path[1]; 27189eef05eSrm160521 do { 27289eef05eSrm160521 end = start; 27389eef05eSrm160521 while (*end != '/' && *end != '\0') 27489eef05eSrm160521 end++; 27589eef05eSrm160521 27689eef05eSrm160521 if (end - start >= MAXNAMELEN) { 27789eef05eSrm160521 if (why) 27889eef05eSrm160521 *why = NAME_ERR_TOOLONG; 27989eef05eSrm160521 return (-1); 28089eef05eSrm160521 } 28189eef05eSrm160521 start = end + 1; 28289eef05eSrm160521 28389eef05eSrm160521 } while (*end != '\0'); 28489eef05eSrm160521 28589eef05eSrm160521 return (0); 28689eef05eSrm160521 } 28789eef05eSrm160521 288fa9e4066Sahrens /* 289fa9e4066Sahrens * For pool names, we have the same set of valid characters as described in 290fa9e4066Sahrens * dataset names, with the additional restriction that the pool name must begin 291fa9e4066Sahrens * with a letter. The pool names 'raidz' and 'mirror' are also reserved names 292fa9e4066Sahrens * that cannot be used. 293fa9e4066Sahrens */ 294fa9e4066Sahrens int 295fa9e4066Sahrens pool_namecheck(const char *pool, namecheck_err_t *why, char *what) 296fa9e4066Sahrens { 297fa9e4066Sahrens const char *c; 298fa9e4066Sahrens 299b81d61a6Slling /* 300b81d61a6Slling * Make sure the name is not too long. 301b81d61a6Slling * 302b81d61a6Slling * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland 303b81d61a6Slling * which is the same as MAXNAMELEN used in the kernel. 304b81d61a6Slling * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all 305b81d61a6Slling * places using MAXNAMELEN. 306b81d61a6Slling */ 307b81d61a6Slling if (strlen(pool) >= MAXNAMELEN) { 308b81d61a6Slling if (why) 309b81d61a6Slling *why = NAME_ERR_TOOLONG; 310b81d61a6Slling return (-1); 311b81d61a6Slling } 312b81d61a6Slling 313fa9e4066Sahrens c = pool; 314fa9e4066Sahrens while (*c != '\0') { 315fa9e4066Sahrens if (!valid_char(*c)) { 316fa9e4066Sahrens if (why) { 317fa9e4066Sahrens *why = NAME_ERR_INVALCHAR; 318fa9e4066Sahrens *what = *c; 319fa9e4066Sahrens } 320fa9e4066Sahrens return (-1); 321fa9e4066Sahrens } 322fa9e4066Sahrens c++; 323fa9e4066Sahrens } 324fa9e4066Sahrens 325fa9e4066Sahrens if (!(*pool >= 'a' && *pool <= 'z') && 326fa9e4066Sahrens !(*pool >= 'A' && *pool <= 'Z')) { 327fa9e4066Sahrens if (why) 328fa9e4066Sahrens *why = NAME_ERR_NOLETTER; 329fa9e4066Sahrens return (-1); 330fa9e4066Sahrens } 331fa9e4066Sahrens 332fa9e4066Sahrens if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) { 333fa9e4066Sahrens if (why) 334fa9e4066Sahrens *why = NAME_ERR_RESERVED; 335fa9e4066Sahrens return (-1); 336fa9e4066Sahrens } 337fa9e4066Sahrens 338fa9e4066Sahrens if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) { 339fa9e4066Sahrens if (why) 340fa9e4066Sahrens *why = NAME_ERR_DISKLIKE; 341fa9e4066Sahrens return (-1); 342fa9e4066Sahrens } 343fa9e4066Sahrens 344fa9e4066Sahrens return (0); 345fa9e4066Sahrens } 346