1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 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 /* 24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 25 */ 26 27 #include <errno.h> 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <string.h> 31 #include <unistd.h> 32 33 #include <libzutil.h> 34 35 /* Substring from after the last slash, or the string itself if none */ 36 const char * 37 zfs_basename(const char *path) 38 { 39 const char *bn = strrchr(path, '/'); 40 return (bn ? bn + 1 : path); 41 } 42 43 /* Return index of last slash or -1 if none */ 44 ssize_t 45 zfs_dirnamelen(const char *path) 46 { 47 const char *end = strrchr(path, '/'); 48 return (end ? end - path : -1); 49 } 50 51 /* 52 * Given a shorthand device name check if a file by that name exists in any 53 * of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If 54 * one is found, store its fully qualified path in the 'path' buffer passed 55 * by the caller and return 0, otherwise return an error. 56 */ 57 int 58 zfs_resolve_shortname(const char *name, char *path, size_t len) 59 { 60 const char *env = getenv("ZPOOL_IMPORT_PATH"); 61 char resolved_path[PATH_MAX]; 62 63 if (env) { 64 for (;;) { 65 env += strspn(env, ":"); 66 size_t dirlen = strcspn(env, ":"); 67 if (dirlen) { 68 (void) snprintf(path, len, "%.*s/%s", 69 (int)dirlen, env, name); 70 if (access(path, F_OK) == 0) 71 return (0); 72 73 env += dirlen; 74 } else 75 break; 76 } 77 } else { 78 size_t count; 79 const char *const *zpool_default_import_path = 80 zpool_default_search_paths(&count); 81 82 for (size_t i = 0; i < count; ++i) { 83 (void) snprintf(path, len, "%s/%s", 84 zpool_default_import_path[i], name); 85 if (access(path, F_OK) == 0) 86 return (0); 87 } 88 } 89 90 /* 91 * The user can pass a relative path like ./file1 for the vdev. The path 92 * must contain a directory prefix like './file1' or '../file1'. Simply 93 * passing 'file1' is not allowed, as it may match a block device name. 94 */ 95 if ((strncmp(name, "./", 2) == 0 || strncmp(name, "../", 3) == 0) && 96 realpath(name, resolved_path) != NULL) { 97 if (access(resolved_path, F_OK) == 0) { 98 if (strlen(resolved_path) + 1 <= len) { 99 if (strlcpy(path, resolved_path, len) < len) 100 return (0); /* success */ 101 } 102 } 103 } 104 return (errno = ENOENT); 105 } 106 107 /* 108 * Given a shorthand device name look for a match against 'cmp_name'. This 109 * is done by checking all prefix expansions using either the default 110 * 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment 111 * variable. Proper partition suffixes will be appended if this is a 112 * whole disk. When a match is found 0 is returned otherwise ENOENT. 113 */ 114 static int 115 zfs_strcmp_shortname(const char *name, const char *cmp_name, int wholedisk) 116 { 117 int path_len, cmp_len, i = 0, error = ENOENT; 118 char *dir, *env, *envdup = NULL, *tmp = NULL; 119 char path_name[MAXPATHLEN]; 120 const char *const *zpool_default_import_path = NULL; 121 size_t count; 122 123 cmp_len = strlen(cmp_name); 124 env = getenv("ZPOOL_IMPORT_PATH"); 125 126 if (env) { 127 envdup = strdup(env); 128 dir = strtok_r(envdup, ":", &tmp); 129 } else { 130 zpool_default_import_path = zpool_default_search_paths(&count); 131 dir = (char *)zpool_default_import_path[i]; 132 } 133 134 while (dir) { 135 /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */ 136 if (env) { 137 while (dir[strlen(dir)-1] == '/') 138 dir[strlen(dir)-1] = '\0'; 139 } 140 141 path_len = snprintf(path_name, MAXPATHLEN, "%s/%s", dir, name); 142 if (wholedisk) 143 path_len = zfs_append_partition(path_name, MAXPATHLEN); 144 145 if ((path_len == cmp_len) && strcmp(path_name, cmp_name) == 0) { 146 error = 0; 147 break; 148 } 149 150 if (env) { 151 dir = strtok_r(NULL, ":", &tmp); 152 } else if (++i < count) { 153 dir = (char *)zpool_default_import_path[i]; 154 } else { 155 dir = NULL; 156 } 157 } 158 159 if (env) 160 free(envdup); 161 162 return (error); 163 } 164 165 /* 166 * Given either a shorthand or fully qualified path name look for a match 167 * against 'cmp'. The passed name will be expanded as needed for comparison 168 * purposes and redundant slashes stripped to ensure an accurate match. 169 */ 170 int 171 zfs_strcmp_pathname(const char *name, const char *cmp, int wholedisk) 172 { 173 int path_len, cmp_len; 174 char path_name[MAXPATHLEN]; 175 char cmp_name[MAXPATHLEN]; 176 char *dir, *tmp = NULL; 177 178 /* Strip redundant slashes if they exist due to ZPOOL_IMPORT_PATH */ 179 cmp_name[0] = '\0'; 180 (void) strlcpy(path_name, cmp, sizeof (path_name)); 181 for (dir = strtok_r(path_name, "/", &tmp); 182 dir != NULL; 183 dir = strtok_r(NULL, "/", &tmp)) { 184 strlcat(cmp_name, "/", sizeof (cmp_name)); 185 strlcat(cmp_name, dir, sizeof (cmp_name)); 186 } 187 188 if (name[0] != '/') 189 return (zfs_strcmp_shortname(name, cmp_name, wholedisk)); 190 191 (void) strlcpy(path_name, name, MAXPATHLEN); 192 path_len = strlen(path_name); 193 cmp_len = strlen(cmp_name); 194 195 if (wholedisk) { 196 path_len = zfs_append_partition(path_name, MAXPATHLEN); 197 if (path_len == -1) 198 return (ENOMEM); 199 } 200 201 if ((path_len != cmp_len) || strcmp(path_name, cmp_name)) 202 return (ENOENT); 203 204 return (0); 205 } 206