Debian Codegraph @

lib/sysxattrs.c

raw symbols SCIP
1 /*
2 * Extended attribute support for rsync.
3 *
4 * Copyright (C) 2004 Red Hat, Inc.
5 * Copyright (C) 2003-2022 Wayne Davison
6 * Written by Jay Fenlason.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22 #include "rsync.h"
23 #include "sysxattrs.h"
24
25 #ifdef SUPPORT_XATTRS
26
27 #ifdef HAVE_OSX_XATTRS
28 #define GETXATTR_FETCH_LIMIT (64*1024*1024)
29 #endif
30
31 #if defined HAVE_LINUX_XATTRS
32
33 ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
34 {
35 return lgetxattr(path, name, value, size);
36 }
37
38 ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
39 {
40 return fgetxattr(filedes, name, value, size);
41 }
42
43 int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
44 {
45 return lsetxattr(path, name, value, size, 0);
46 }
47
48 int sys_fsetxattr(int filedes, const char *name, const void *value, size_t size)
49 {
50 return fsetxattr(filedes, name, value, size, 0);
51 }
52
53 int sys_lremovexattr(const char *path, const char *name)
54 {
55 return lremovexattr(path, name);
56 }
57
58 int sys_fremovexattr(int filedes, const char *name)
59 {
60 return fremovexattr(filedes, name);
61 }
62
63 ssize_t sys_llistxattr(const char *path, char *list, size_t size)
64 {
65 return llistxattr(path, list, size);
66 }
67
68 ssize_t sys_flistxattr(int filedes, char *list, size_t size)
69 {
70 return flistxattr(filedes, list, size);
71 }
72
73 #elif HAVE_OSX_XATTRS
74
75 ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
76 {
77 ssize_t len = getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
78
79 /* If we're retrieving data, handle resource forks > 64MB specially */
80 if (value != NULL && len == GETXATTR_FETCH_LIMIT && (size_t)len < size) {
81 /* getxattr will only return 64MB of data at a time, need to call again with a new offset */
82 u_int32_t offset = len;
83 size_t data_retrieved = len;
84 while (data_retrieved < size) {
85 len = getxattr(path, name, (char*)value + offset, size - data_retrieved, offset, XATTR_NOFOLLOW);
86 if (len <= 0)
87 break;
88 data_retrieved += len;
89 offset += (u_int32_t)len;
90 }
91 len = data_retrieved;
92 }
93
94 return len;
95 }
96
97 ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
98 {
99 return fgetxattr(filedes, name, value, size, 0, 0);
100 }
101
102 int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
103 {
104 return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
105 }
106
107 int sys_fsetxattr(int filedes, const char *name, const void *value, size_t size)
108 {
109 return fsetxattr(filedes, name, value, size, 0, 0);
110 }
111
112 int sys_lremovexattr(const char *path, const char *name)
113 {
114 return removexattr(path, name, XATTR_NOFOLLOW);
115 }
116
117 int sys_fremovexattr(int filedes, const char *name)
118 {
119 return fremovexattr(filedes, name, 0);
120 }
121
122 ssize_t sys_llistxattr(const char *path, char *list, size_t size)
123 {
124 return listxattr(path, list, size, XATTR_NOFOLLOW);
125 }
126
127 ssize_t sys_flistxattr(int filedes, char *list, size_t size)
128 {
129 return flistxattr(filedes, list, size, 0);
130 }
131
132 #elif HAVE_FREEBSD_XATTRS
133
134 ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
135 {
136 return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
137 }
138
139 ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
140 {
141 return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
142 }
143
144 int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
145 {
146 return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
147 }
148
149 int sys_fsetxattr(int filedes, const char *name, const void *value, size_t size)
150 {
151 return extattr_set_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
152 }
153
154 int sys_lremovexattr(const char *path, const char *name)
155 {
156 return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
157 }
158
159 int sys_fremovexattr(int filedes, const char *name)
160 {
161 return extattr_delete_fd(filedes, EXTATTR_NAMESPACE_USER, name);
162 }
163
164 /* Turn the FreeBSD extattr_list_xx() output (a single length byte before each
165 * name, no '\0' terminator) into the series of null-terminated strings that the
166 * rest of rsync expects. Since the size is unchanged, transform in place.
167 * Shared by the path and fd list variants. */
168 static ssize_t freebsd_list_finish(char *list, size_t size, ssize_t len)
169 {
170 unsigned char keylen;
171 ssize_t off;
172
173 if (len <= 0 || size == 0)
174 return len;
175
176 if ((size_t)len >= size) {
177 /* FreeBSD extattr_list_xx() returns 'size' as 'len' in case there are
178 more data available, truncating the output, we solve this by signalling
179 ERANGE in case len == size so that the code in xattrs.c will retry with
180 a bigger buffer */
181 errno = ERANGE;
182 return -1;
183 }
184
185 for (off = 0; off < len; off += keylen + 1) {
186 keylen = ((unsigned char*)list)[off];
187 if (off + keylen >= len) {
188 /* Should be impossible, but bugs happen! */
189 errno = EINVAL;
190 return -1;
191 }
192 memmove(list+off, list+off+1, keylen);
193 list[off+keylen] = '\0';
194 }
195
196 return len;
197 }
198
199 ssize_t sys_llistxattr(const char *path, char *list, size_t size)
200 {
201 return freebsd_list_finish(list, size,
202 extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size));
203 }
204
205 ssize_t sys_flistxattr(int filedes, char *list, size_t size)
206 {
207 return freebsd_list_finish(list, size,
208 extattr_list_fd(filedes, EXTATTR_NAMESPACE_USER, list, size));
209 }
210
211 #elif HAVE_SOLARIS_XATTRS
212
213 static ssize_t read_xattr(int attrfd, void *buf, size_t buflen)
214 {
215 STRUCT_STAT sb;
216 ssize_t ret;
217
218 if (fstat(attrfd, &sb) < 0)
219 ret = -1;
220 else if (sb.st_size > SSIZE_MAX) {
221 errno = ERANGE;
222 ret = -1;
223 } else if (buflen == 0)
224 ret = sb.st_size;
225 else if (sb.st_size > buflen) {
226 errno = ERANGE;
227 ret = -1;
228 } else {
229 size_t bufpos;
230 for (bufpos = 0; bufpos < sb.st_size; ) {
231 ssize_t cnt = read(attrfd, (char*)buf + bufpos, sb.st_size - bufpos);
232 if (cnt <= 0) {
233 if (cnt < 0 && errno == EINTR)
234 continue;
235 bufpos = -1;
236 break;
237 }
238 bufpos += cnt;
239 }
240 ret = bufpos;
241 }
242
243 close(attrfd);
244
245 return ret;
246 }
247
248 ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
249 {
250 int attrfd;
251
252 if ((attrfd = attropen(path, name, O_RDONLY)) < 0) {
253 errno = ENOATTR;
254 return -1;
255 }
256
257 return read_xattr(attrfd, value, size);
258 }
259
260 ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
261 {
262 int attrfd;
263
264 if ((attrfd = openat(filedes, name, O_RDONLY|O_XATTR, 0)) < 0) {
265 errno = ENOATTR;
266 return -1;
267 }
268
269 return read_xattr(attrfd, value, size);
270 }
271
272 /* Write a datum to the already-opened attribute fd, closing it. Shared by the
273 * path- and fd-keyed setters below. */
274 static int write_xattr(int attrfd, const void *value, size_t size)
275 {
276 size_t bufpos;
277 int ret = 0, saved_errno = 0;
278
279 for (bufpos = 0; bufpos < size; ) {
280 ssize_t cnt = write(attrfd, (const char *)value + bufpos, size - bufpos);
281 if (cnt < 0) {
282 if (errno == EINTR)
283 continue;
284 ret = -1;
285 saved_errno = errno;
286 break;
287 }
288 if (cnt == 0) {
289 ret = -1;
290 saved_errno = EIO;
291 break;
292 }
293 bufpos += cnt;
294 }
295
296 /* Don't let close() clobber the write error; do report a close() failure. */
297 if (close(attrfd) < 0 && ret == 0)
298 return -1;
299 if (ret < 0 && saved_errno)
300 errno = saved_errno;
301
302 return ret;
303 }
304
305 int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
306 {
307 int attrfd;
308 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
309
310 if ((attrfd = attropen(path, name, O_CREAT|O_TRUNC|O_WRONLY, mode)) < 0)
311 return -1;
312
313 return write_xattr(attrfd, value, size);
314 }
315
316 int sys_fsetxattr(int filedes, const char *name, const void *value, size_t size)
317 {
318 int attrfd;
319 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
320
321 if ((attrfd = openat(filedes, name, O_CREAT|O_TRUNC|O_WRONLY|O_XATTR, mode)) < 0)
322 return -1;
323
324 return write_xattr(attrfd, value, size);
325 }
326
327 int sys_lremovexattr(const char *path, const char *name)
328 {
329 int attrdirfd;
330 int ret;
331
332 if ((attrdirfd = attropen(path, ".", O_RDONLY)) < 0)
333 return -1;
334
335 ret = unlinkat(attrdirfd, name, 0);
336
337 close(attrdirfd);
338
339 return ret;
340 }
341
342 int sys_fremovexattr(int filedes, const char *name)
343 {
344 int attrdirfd;
345 int ret;
346
347 if ((attrdirfd = openat(filedes, ".", O_RDONLY|O_XATTR, 0)) < 0)
348 return -1;
349
350 ret = unlinkat(attrdirfd, name, 0);
351
352 close(attrdirfd);
353
354 return ret;
355 }
356
357 /* List the names in an already-opened attribute-dir fd, consuming it. Shared
358 * by the path- and fd-keyed listers below. */
359 static ssize_t list_xattr(int attrdirfd, char *list, size_t size)
360 {
361 DIR *dirp;
362 struct dirent *dp;
363 ssize_t ret = 0;
364
365 if ((dirp = fdopendir(attrdirfd)) == NULL) {
366 close(attrdirfd);
367 return -1;
368 }
369
370 while ((dp = readdir(dirp))) {
371 int len = strlen(dp->d_name);
372
373 if (dp->d_name[0] == '.' && (len == 1 || (len == 2 && dp->d_name[1] == '.')))
374 continue;
375 if (len == 11 && dp->d_name[0] == 'S' && strncmp(dp->d_name, "SUNWattr_r", 10) == 0
376 && (dp->d_name[10] == 'o' || dp->d_name[10] == 'w'))
377 continue;
378
379 ret += len + 1;
380 if ((size_t)ret > size) {
381 if (size == 0)
382 continue;
383 ret = -1;
384 errno = ERANGE;
385 break;
386 }
387 memcpy(list, dp->d_name, len+1);
388 list += len+1;
389 }
390
391 closedir(dirp);
392
393 return ret;
394 }
395
396 ssize_t sys_llistxattr(const char *path, char *list, size_t size)
397 {
398 int attrdirfd;
399
400 if ((attrdirfd = attropen(path, ".", O_RDONLY)) < 0) {
401 errno = ENOTSUP;
402 return -1;
403 }
404
405 return list_xattr(attrdirfd, list, size);
406 }
407
408 ssize_t sys_flistxattr(int filedes, char *list, size_t size)
409 {
410 int attrdirfd;
411
412 if ((attrdirfd = openat(filedes, ".", O_RDONLY|O_XATTR, 0)) < 0) {
413 errno = ENOTSUP;
414 return -1;
415 }
416
417 return list_xattr(attrdirfd, list, size);
418 }
419
420 #else
421
422 #error You need to create xattr compatibility functions.
423
424 #endif
425
426 #endif /* SUPPORT_XATTRS */