extattr.c revision 1.3 1 1.3 manu /* $NetBSD: extattr.c,v 1.3 2011/08/03 04:11:17 manu 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.3 manu __RCSID("$NetBSD: extattr.c,v 1.3 2011/08/03 04:11:17 manu 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.3 manu int 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.3 manu for (i = 0; i < 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.3 manu 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.3 manu if (aval != NULL)
164 1.3 manu free(aval);
165 1.3 manu
166 1.3 manu if (alist != NULL)
167 1.3 manu free(alist);
168 1.3 manu
169 1.3 manu return error;
170 1.3 manu }
171 1.3 manu
172 1.3 manu int
173 1.3 manu extattr_copy_file(const char *from, const char *to, int namespace)
174 1.3 manu {
175 1.3 manu ssize_t llen, vlen, maxvlen;
176 1.3 manu size_t alen;
177 1.3 manu void *alist = NULL;
178 1.3 manu void *aval = NULL;
179 1.3 manu int i;
180 1.3 manu int error = -1;
181 1.3 manu
182 1.3 manu llen = extattr_list_file(from, namespace, NULL, 0);
183 1.3 manu if (llen == -1) {
184 1.3 manu /* Silently ignore when EA are not supported */
185 1.3 manu if (errno == EOPNOTSUPP)
186 1.3 manu error = 0;
187 1.3 manu goto out;
188 1.3 manu }
189 1.3 manu
190 1.3 manu if (llen == 0) {
191 1.3 manu error = 0;
192 1.3 manu goto out;
193 1.3 manu }
194 1.3 manu
195 1.3 manu if ((alist = malloc((size_t)llen)) == NULL)
196 1.3 manu goto out;
197 1.3 manu
198 1.3 manu llen = extattr_list_file(from, namespace, alist, (size_t)llen);
199 1.3 manu if (llen == -1)
200 1.3 manu goto out;
201 1.3 manu
202 1.3 manu maxvlen = 1024;
203 1.3 manu if ((aval = malloc((size_t)maxvlen)) == NULL)
204 1.3 manu goto out;
205 1.3 manu
206 1.3 manu for (i = 0; i < llen; i += alen + 1) {
207 1.3 manu char aname[NAME_MAX + 1];
208 1.3 manu char *ap;
209 1.3 manu
210 1.3 manu alen = ((uint8_t *)alist)[i];
211 1.3 manu ap = ((char *)alist) + i + 1;
212 1.3 manu (void)memcpy(aname, ap, alen);
213 1.3 manu aname[alen] = '\0';
214 1.3 manu
215 1.3 manu vlen = extattr_get_file(from, namespace, aname, NULL, 0);
216 1.3 manu if (vlen == -1)
217 1.3 manu goto out;
218 1.3 manu
219 1.3 manu if (vlen > maxvlen) {
220 1.3 manu if ((aval = realloc(aval, (size_t)vlen)) == NULL)
221 1.3 manu goto out;
222 1.3 manu maxvlen = vlen;
223 1.3 manu }
224 1.3 manu
225 1.3 manu if ((vlen = extattr_get_file(from, namespace, aname, aval, (size_t)vlen)) == -1)
226 1.3 manu goto out;
227 1.3 manu
228 1.3 manu if (extattr_set_file(to, namespace, aname,
229 1.3 manu aval, (size_t)vlen) != vlen)
230 1.3 manu goto out;
231 1.3 manu }
232 1.3 manu
233 1.3 manu error = 0;
234 1.3 manu out:
235 1.3 manu if (aval != NULL)
236 1.3 manu free(aval);
237 1.3 manu
238 1.3 manu if (alist != NULL)
239 1.3 manu free(alist);
240 1.3 manu
241 1.3 manu return error;
242 1.3 manu }
243 1.3 manu
244 1.3 manu int
245 1.3 manu extattr_copy_link(const char *from, const char *to, int namespace)
246 1.3 manu {
247 1.3 manu ssize_t llen, vlen, maxvlen;
248 1.3 manu size_t alen;
249 1.3 manu void *alist = NULL;
250 1.3 manu void *aval = NULL;
251 1.3 manu int i;
252 1.3 manu int error = -1;
253 1.3 manu
254 1.3 manu llen = extattr_list_link(from, namespace, NULL, 0);
255 1.3 manu if (llen == -1) {
256 1.3 manu /* Silently ignore when EA are not supported */
257 1.3 manu if (errno == EOPNOTSUPP)
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 (llen == 0) {
263 1.3 manu error = 0;
264 1.3 manu goto out;
265 1.3 manu }
266 1.3 manu
267 1.3 manu if ((alist = malloc((size_t)llen)) == NULL)
268 1.3 manu goto out;
269 1.3 manu
270 1.3 manu llen = extattr_list_link(from, namespace, alist, (size_t)llen);
271 1.3 manu if (llen == -1)
272 1.3 manu goto out;
273 1.3 manu
274 1.3 manu maxvlen = 1024;
275 1.3 manu if ((aval = malloc((size_t)maxvlen)) == NULL)
276 1.3 manu goto out;
277 1.3 manu
278 1.3 manu for (i = 0; i < llen; i += alen + 1) {
279 1.3 manu char aname[NAME_MAX + 1];
280 1.3 manu char *ap;
281 1.3 manu
282 1.3 manu alen = ((uint8_t *)alist)[i];
283 1.3 manu ap = ((char *)alist) + i + 1;
284 1.3 manu (void)memcpy(aname, ap, alen);
285 1.3 manu aname[alen] = '\0';
286 1.3 manu
287 1.3 manu vlen = extattr_get_link(from, namespace, aname, NULL, 0);
288 1.3 manu if (vlen == -1)
289 1.3 manu goto out;
290 1.3 manu
291 1.3 manu if (vlen > maxvlen) {
292 1.3 manu if ((aval = realloc(aval, (size_t)vlen)) == NULL)
293 1.3 manu goto out;
294 1.3 manu maxvlen = vlen;
295 1.3 manu }
296 1.3 manu
297 1.3 manu if ((vlen = extattr_get_link(from, namespace, aname,
298 1.3 manu aval, (size_t)vlen)) == -1)
299 1.3 manu goto out;
300 1.3 manu
301 1.3 manu if (extattr_set_link(to, namespace, aname,
302 1.3 manu aval, (size_t)vlen) != vlen)
303 1.3 manu goto out;
304 1.3 manu }
305 1.3 manu
306 1.3 manu error = 0;
307 1.3 manu out:
308 1.3 manu if (aval != NULL)
309 1.3 manu free(aval);
310 1.3 manu
311 1.3 manu if (alist != NULL)
312 1.3 manu free(alist);
313 1.3 manu
314 1.3 manu return error;
315 1.3 manu }
316 1.3 manu
317 1.3 manu static int
318 1.3 manu extattr_namespace_access(int namespace, int mode)
319 1.3 manu {
320 1.3 manu switch (namespace) {
321 1.3 manu case EXTATTR_NAMESPACE_SYSTEM:
322 1.3 manu if ((mode & (R_OK|W_OK)) && getuid() != 0)
323 1.3 manu return -1;
324 1.3 manu break;
325 1.3 manu default:
326 1.3 manu break;
327 1.3 manu }
328 1.3 manu
329 1.3 manu return 0;
330 1.3 manu }
331 1.3 manu
332 1.3 manu int
333 1.3 manu fcpxattr(int from_fd, int to_fd)
334 1.3 manu {
335 1.3 manu const int *ns;
336 1.3 manu int error;
337 1.3 manu
338 1.3 manu for (ns = extattr_namespaces; *ns; ns++) {
339 1.3 manu if (extattr_namespace_access(*ns, R_OK|W_OK) != 0)
340 1.3 manu continue;
341 1.3 manu
342 1.3 manu if ((error = extattr_copy_fd(from_fd, to_fd, *ns)) != 0)
343 1.3 manu return error;
344 1.3 manu }
345 1.3 manu
346 1.3 manu return 0;
347 1.3 manu }
348 1.3 manu
349 1.3 manu int
350 1.3 manu cpxattr(const char *from, const char *to)
351 1.3 manu {
352 1.3 manu const int *ns;
353 1.3 manu int error;
354 1.3 manu
355 1.3 manu for (ns = extattr_namespaces; *ns; ns++) {
356 1.3 manu if (extattr_namespace_access(*ns, R_OK|W_OK) != 0)
357 1.3 manu continue;
358 1.3 manu
359 1.3 manu if ((error = extattr_copy_file(from, to, *ns)) != 0)
360 1.3 manu return error;
361 1.3 manu }
362 1.3 manu
363 1.3 manu return 0;
364 1.3 manu }
365 1.3 manu
366 1.3 manu int
367 1.3 manu lcpxattr(const char *from, const char *to)
368 1.3 manu {
369 1.3 manu const int *ns;
370 1.3 manu int error;
371 1.3 manu
372 1.3 manu for (ns = extattr_namespaces; *ns; ns++) {
373 1.3 manu if (extattr_namespace_access(*ns, R_OK|W_OK) != 0)
374 1.3 manu continue;
375 1.3 manu
376 1.3 manu if ((error = extattr_copy_link(from, to, *ns)) != 0)
377 1.3 manu return error;
378 1.3 manu }
379 1.3 manu
380 1.3 manu return 0;
381 1.3 manu }
382