1 1.5 maya /* $NetBSD: extattr.c,v 1.5 2017/03/09 11:39:41 maya Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /*- 4 1.1 thorpej * Copyright (c) 2001 Robert N. M. Watson 5 1.1 thorpej * All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * Redistribution and use in source and binary forms, with or without 8 1.1 thorpej * modification, are permitted provided that the following conditions 9 1.1 thorpej * are met: 10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 11 1.1 thorpej * notice, this list of conditions and the following disclaimer. 12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 14 1.1 thorpej * documentation and/or other materials provided with the distribution. 15 1.1 thorpej * 16 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 thorpej * SUCH DAMAGE. 27 1.1 thorpej */ 28 1.1 thorpej 29 1.1 thorpej /* 30 1.1 thorpej * TrustedBSD: Utility functions for extended attributes. 31 1.1 thorpej */ 32 1.1 thorpej 33 1.1 thorpej #include <sys/cdefs.h> 34 1.1 thorpej #if defined(LIBC_SCCS) && !defined(lint) 35 1.5 maya __RCSID("$NetBSD: extattr.c,v 1.5 2017/03/09 11:39:41 maya Exp $"); 36 1.1 thorpej #endif /* LIBC_SCCS and not lint */ 37 1.1 thorpej 38 1.2 kleink #include "namespace.h" 39 1.1 thorpej #include <sys/types.h> 40 1.3 manu #include <sys/param.h> 41 1.1 thorpej #include <sys/extattr.h> 42 1.1 thorpej 43 1.1 thorpej #include <errno.h> 44 1.3 manu #include <unistd.h> 45 1.3 manu #include <stdlib.h> 46 1.1 thorpej #include <string.h> 47 1.1 thorpej 48 1.3 manu const int extattr_namespaces[] = { 49 1.3 manu EXTATTR_NAMESPACE_USER, 50 1.3 manu EXTATTR_NAMESPACE_SYSTEM, 51 1.3 manu 0, 52 1.3 manu }; 53 1.3 manu 54 1.1 thorpej int 55 1.1 thorpej extattr_namespace_to_string(int attrnamespace, char **string) 56 1.1 thorpej { 57 1.1 thorpej 58 1.1 thorpej switch(attrnamespace) { 59 1.1 thorpej case EXTATTR_NAMESPACE_USER: 60 1.1 thorpej if (string != NULL) { 61 1.1 thorpej if ((*string = 62 1.1 thorpej strdup(EXTATTR_NAMESPACE_USER_STRING)) == NULL) 63 1.1 thorpej return (-1); 64 1.1 thorpej } 65 1.1 thorpej return (0); 66 1.1 thorpej 67 1.1 thorpej case EXTATTR_NAMESPACE_SYSTEM: 68 1.1 thorpej if (string != NULL) 69 1.1 thorpej if ((*string = 70 1.1 thorpej strdup(EXTATTR_NAMESPACE_SYSTEM_STRING)) == NULL) 71 1.1 thorpej return (-1); 72 1.1 thorpej return (0); 73 1.1 thorpej 74 1.1 thorpej default: 75 1.1 thorpej errno = EINVAL; 76 1.1 thorpej return (-1); 77 1.1 thorpej } 78 1.1 thorpej } 79 1.1 thorpej 80 1.1 thorpej int 81 1.1 thorpej extattr_string_to_namespace(const char *string, int *attrnamespace) 82 1.1 thorpej { 83 1.1 thorpej 84 1.1 thorpej if (strcmp(string, EXTATTR_NAMESPACE_USER_STRING) == 0) { 85 1.1 thorpej if (attrnamespace != NULL) 86 1.1 thorpej *attrnamespace = EXTATTR_NAMESPACE_USER; 87 1.1 thorpej return (0); 88 1.1 thorpej } else if (strcmp(string, EXTATTR_NAMESPACE_SYSTEM_STRING) == 0) { 89 1.1 thorpej if (attrnamespace != NULL) 90 1.1 thorpej *attrnamespace = EXTATTR_NAMESPACE_SYSTEM; 91 1.1 thorpej return (0); 92 1.1 thorpej } else { 93 1.1 thorpej errno = EINVAL; 94 1.1 thorpej return (-1); 95 1.1 thorpej } 96 1.1 thorpej } 97 1.3 manu 98 1.3 manu 99 1.3 manu int 100 1.3 manu extattr_copy_fd(int from_fd, int to_fd, int namespace) 101 1.3 manu { 102 1.3 manu ssize_t llen, vlen, maxvlen; 103 1.3 manu size_t alen; 104 1.3 manu void *alist = NULL; 105 1.3 manu void *aval = NULL; 106 1.4 christos size_t i; 107 1.3 manu int error = -1; 108 1.3 manu 109 1.3 manu llen = extattr_list_fd(from_fd, namespace, NULL, 0); 110 1.3 manu if (llen == -1) { 111 1.3 manu /* Silently ignore when EA are not supported */ 112 1.3 manu if (errno == EOPNOTSUPP) 113 1.3 manu error = 0; 114 1.3 manu goto out; 115 1.3 manu } 116 1.3 manu 117 1.3 manu if (llen == 0) { 118 1.3 manu error = 0; 119 1.3 manu goto out; 120 1.3 manu } 121 1.3 manu 122 1.3 manu if ((alist = malloc((size_t)llen)) == NULL) 123 1.3 manu goto out; 124 1.3 manu 125 1.3 manu llen = extattr_list_fd(from_fd, namespace, alist, (size_t)llen); 126 1.3 manu if (llen == -1) 127 1.3 manu goto out; 128 1.3 manu 129 1.3 manu maxvlen = 1024; 130 1.3 manu if ((aval = malloc((size_t)maxvlen)) == NULL) 131 1.3 manu goto out; 132 1.3 manu 133 1.4 christos for (i = 0; i < (size_t)llen; i += alen + 1) { 134 1.3 manu char aname[NAME_MAX + 1]; 135 1.3 manu char *ap; 136 1.3 manu 137 1.3 manu alen = ((uint8_t *)alist)[i]; 138 1.3 manu ap = ((char *)alist) + i + 1; 139 1.3 manu (void)memcpy(aname, ap, alen); 140 1.3 manu aname[alen] = '\0'; 141 1.3 manu 142 1.3 manu vlen = extattr_get_fd(from_fd, namespace, aname, NULL, 0); 143 1.3 manu if (vlen == -1) 144 1.3 manu goto out; 145 1.3 manu 146 1.3 manu if (vlen > maxvlen) { 147 1.3 manu if ((aval = realloc(aval, (size_t)vlen)) == NULL) 148 1.3 manu goto out; 149 1.3 manu maxvlen = vlen; 150 1.3 manu } 151 1.3 manu 152 1.3 manu if ((vlen = extattr_get_fd(from_fd, namespace, aname, 153 1.5 maya aval, (size_t)vlen)) == -1) 154 1.3 manu goto out; 155 1.3 manu 156 1.3 manu if (extattr_set_fd(to_fd, namespace, aname, 157 1.3 manu aval, (size_t)vlen) != vlen) 158 1.3 manu goto out; 159 1.3 manu } 160 1.3 manu 161 1.3 manu error = 0; 162 1.3 manu out: 163 1.5 maya free(aval); 164 1.5 maya free(alist); 165 1.5 maya 166 1.3 manu return error; 167 1.3 manu } 168 1.3 manu 169 1.3 manu int 170 1.3 manu extattr_copy_file(const char *from, const char *to, int namespace) 171 1.3 manu { 172 1.3 manu ssize_t llen, vlen, maxvlen; 173 1.3 manu size_t alen; 174 1.3 manu void *alist = NULL; 175 1.3 manu void *aval = NULL; 176 1.4 christos size_t i; 177 1.3 manu int error = -1; 178 1.3 manu 179 1.3 manu llen = extattr_list_file(from, namespace, NULL, 0); 180 1.3 manu if (llen == -1) { 181 1.3 manu /* Silently ignore when EA are not supported */ 182 1.3 manu if (errno == EOPNOTSUPP) 183 1.3 manu error = 0; 184 1.3 manu goto out; 185 1.3 manu } 186 1.3 manu 187 1.3 manu if (llen == 0) { 188 1.3 manu error = 0; 189 1.3 manu goto out; 190 1.3 manu } 191 1.3 manu 192 1.3 manu if ((alist = malloc((size_t)llen)) == NULL) 193 1.3 manu goto out; 194 1.3 manu 195 1.3 manu llen = extattr_list_file(from, namespace, alist, (size_t)llen); 196 1.3 manu if (llen == -1) 197 1.3 manu goto out; 198 1.3 manu 199 1.3 manu maxvlen = 1024; 200 1.3 manu if ((aval = malloc((size_t)maxvlen)) == NULL) 201 1.3 manu goto out; 202 1.3 manu 203 1.4 christos for (i = 0; i < (size_t)llen; i += alen + 1) { 204 1.3 manu char aname[NAME_MAX + 1]; 205 1.3 manu char *ap; 206 1.3 manu 207 1.3 manu alen = ((uint8_t *)alist)[i]; 208 1.3 manu ap = ((char *)alist) + i + 1; 209 1.3 manu (void)memcpy(aname, ap, alen); 210 1.3 manu aname[alen] = '\0'; 211 1.3 manu 212 1.3 manu vlen = extattr_get_file(from, namespace, aname, NULL, 0); 213 1.3 manu if (vlen == -1) 214 1.3 manu goto out; 215 1.3 manu 216 1.3 manu if (vlen > maxvlen) { 217 1.3 manu if ((aval = realloc(aval, (size_t)vlen)) == NULL) 218 1.3 manu goto out; 219 1.3 manu maxvlen = vlen; 220 1.3 manu } 221 1.3 manu 222 1.5 maya if ((vlen = extattr_get_file(from, namespace, aname, 223 1.5 maya aval, (size_t)vlen)) == -1) 224 1.3 manu goto out; 225 1.3 manu 226 1.3 manu if (extattr_set_file(to, namespace, aname, 227 1.3 manu aval, (size_t)vlen) != vlen) 228 1.3 manu goto out; 229 1.3 manu } 230 1.3 manu 231 1.3 manu error = 0; 232 1.3 manu out: 233 1.5 maya free(aval); 234 1.5 maya free(alist); 235 1.5 maya 236 1.3 manu return error; 237 1.3 manu } 238 1.3 manu 239 1.3 manu int 240 1.3 manu extattr_copy_link(const char *from, const char *to, int namespace) 241 1.3 manu { 242 1.3 manu ssize_t llen, vlen, maxvlen; 243 1.3 manu size_t alen; 244 1.3 manu void *alist = NULL; 245 1.3 manu void *aval = NULL; 246 1.4 christos size_t i; 247 1.3 manu int error = -1; 248 1.3 manu 249 1.3 manu llen = extattr_list_link(from, namespace, NULL, 0); 250 1.3 manu if (llen == -1) { 251 1.3 manu /* Silently ignore when EA are not supported */ 252 1.3 manu if (errno == EOPNOTSUPP) 253 1.3 manu error = 0; 254 1.3 manu goto out; 255 1.3 manu } 256 1.3 manu 257 1.3 manu if (llen == 0) { 258 1.3 manu error = 0; 259 1.3 manu goto out; 260 1.3 manu } 261 1.3 manu 262 1.3 manu if ((alist = malloc((size_t)llen)) == NULL) 263 1.3 manu goto out; 264 1.3 manu 265 1.3 manu llen = extattr_list_link(from, namespace, alist, (size_t)llen); 266 1.3 manu if (llen == -1) 267 1.3 manu goto out; 268 1.3 manu 269 1.3 manu maxvlen = 1024; 270 1.3 manu if ((aval = malloc((size_t)maxvlen)) == NULL) 271 1.3 manu goto out; 272 1.3 manu 273 1.4 christos for (i = 0; i < (size_t)llen; i += alen + 1) { 274 1.3 manu char aname[NAME_MAX + 1]; 275 1.3 manu char *ap; 276 1.3 manu 277 1.3 manu alen = ((uint8_t *)alist)[i]; 278 1.3 manu ap = ((char *)alist) + i + 1; 279 1.3 manu (void)memcpy(aname, ap, alen); 280 1.3 manu aname[alen] = '\0'; 281 1.3 manu 282 1.3 manu vlen = extattr_get_link(from, namespace, aname, NULL, 0); 283 1.3 manu if (vlen == -1) 284 1.3 manu goto out; 285 1.3 manu 286 1.3 manu if (vlen > maxvlen) { 287 1.3 manu if ((aval = realloc(aval, (size_t)vlen)) == NULL) 288 1.3 manu goto out; 289 1.3 manu maxvlen = vlen; 290 1.3 manu } 291 1.3 manu 292 1.3 manu if ((vlen = extattr_get_link(from, namespace, aname, 293 1.3 manu aval, (size_t)vlen)) == -1) 294 1.3 manu goto out; 295 1.3 manu 296 1.3 manu if (extattr_set_link(to, namespace, aname, 297 1.3 manu aval, (size_t)vlen) != vlen) 298 1.3 manu goto out; 299 1.3 manu } 300 1.3 manu 301 1.3 manu error = 0; 302 1.3 manu out: 303 1.5 maya free(aval); 304 1.5 maya free(alist); 305 1.5 maya 306 1.3 manu return error; 307 1.3 manu } 308 1.3 manu 309 1.3 manu static int 310 1.3 manu extattr_namespace_access(int namespace, int mode) 311 1.3 manu { 312 1.3 manu switch (namespace) { 313 1.3 manu case EXTATTR_NAMESPACE_SYSTEM: 314 1.3 manu if ((mode & (R_OK|W_OK)) && getuid() != 0) 315 1.3 manu return -1; 316 1.3 manu break; 317 1.3 manu default: 318 1.3 manu break; 319 1.3 manu } 320 1.3 manu 321 1.3 manu return 0; 322 1.3 manu } 323 1.3 manu 324 1.3 manu int 325 1.3 manu fcpxattr(int from_fd, int to_fd) 326 1.3 manu { 327 1.3 manu const int *ns; 328 1.3 manu int error; 329 1.3 manu 330 1.3 manu for (ns = extattr_namespaces; *ns; ns++) { 331 1.3 manu if (extattr_namespace_access(*ns, R_OK|W_OK) != 0) 332 1.3 manu continue; 333 1.3 manu 334 1.3 manu if ((error = extattr_copy_fd(from_fd, to_fd, *ns)) != 0) 335 1.3 manu return error; 336 1.3 manu } 337 1.3 manu 338 1.3 manu return 0; 339 1.3 manu } 340 1.3 manu 341 1.3 manu int 342 1.3 manu cpxattr(const char *from, const char *to) 343 1.3 manu { 344 1.3 manu const int *ns; 345 1.3 manu int error; 346 1.3 manu 347 1.3 manu for (ns = extattr_namespaces; *ns; ns++) { 348 1.3 manu if (extattr_namespace_access(*ns, R_OK|W_OK) != 0) 349 1.3 manu continue; 350 1.3 manu 351 1.3 manu if ((error = extattr_copy_file(from, to, *ns)) != 0) 352 1.3 manu return error; 353 1.3 manu } 354 1.3 manu 355 1.3 manu return 0; 356 1.3 manu } 357 1.3 manu 358 1.3 manu int 359 1.3 manu lcpxattr(const char *from, const char *to) 360 1.3 manu { 361 1.3 manu const int *ns; 362 1.3 manu int error; 363 1.3 manu 364 1.3 manu for (ns = extattr_namespaces; *ns; ns++) { 365 1.3 manu if (extattr_namespace_access(*ns, R_OK|W_OK) != 0) 366 1.3 manu continue; 367 1.3 manu 368 1.3 manu if ((error = extattr_copy_link(from, to, *ns)) != 0) 369 1.3 manu return error; 370 1.3 manu } 371 1.3 manu 372 1.3 manu return 0; 373 1.3 manu } 374