]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/archive_read_disk_entry_from_file.c
If we can't stat a file, return the correct ARCHIVE_FAILED (this entry can't
[FreeBSD/FreeBSD.git] / lib / libarchive / archive_read_disk_entry_from_file.c
1 /*-
2  * Copyright (c) 2003-2009 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
28
29 #ifdef HAVE_SYS_TYPES_H
30 /* Mac OSX requires sys/types.h before sys/acl.h. */
31 #include <sys/types.h>
32 #endif
33 #ifdef HAVE_SYS_ACL_H
34 #include <sys/acl.h>
35 #endif
36 #ifdef HAVE_SYS_EXTATTR_H
37 #include <sys/extattr.h>
38 #endif
39 #ifdef HAVE_SYS_PARAM_H
40 #include <sys/param.h>
41 #endif
42 #ifdef HAVE_SYS_STAT_H
43 #include <sys/stat.h>
44 #endif
45 #ifdef HAVE_SYS_XATTR_H
46 #include <sys/xattr.h>
47 #endif
48 #ifdef HAVE_ACL_LIBACL_H
49 #include <acl/libacl.h>
50 #endif
51 #ifdef HAVE_ATTR_XATTR_H
52 #include <attr/xattr.h>
53 #endif
54 #ifdef HAVE_ERRNO_H
55 #include <errno.h>
56 #endif
57 #ifdef HAVE_LIMITS_H
58 #include <limits.h>
59 #endif
60 #ifdef HAVE_WINDOWS_H
61 #include <windows.h>
62 #endif
63
64 #include "archive.h"
65 #include "archive_entry.h"
66 #include "archive_private.h"
67 #include "archive_read_disk_private.h"
68
69 /*
70  * Linux and FreeBSD plug this obvious hole in POSIX.1e in
71  * different ways.
72  */
73 #if HAVE_ACL_GET_PERM
74 #define ACL_GET_PERM acl_get_perm
75 #elif HAVE_ACL_GET_PERM_NP
76 #define ACL_GET_PERM acl_get_perm_np
77 #endif
78
79 static int setup_acls_posix1e(struct archive_read_disk *,
80     struct archive_entry *, int fd);
81 static int setup_xattrs(struct archive_read_disk *,
82     struct archive_entry *, int fd);
83
84 int
85 archive_read_disk_entry_from_file(struct archive *_a,
86     struct archive_entry *entry,
87     int fd, const struct stat *st)
88 {
89         struct archive_read_disk *a = (struct archive_read_disk *)_a;
90         const char *path, *name;
91         struct stat s;
92         int initial_fd = fd;
93         int r, r1;
94
95         archive_clear_error(_a);
96         path = archive_entry_sourcepath(entry);
97         if (path == NULL)
98                 path = archive_entry_pathname(entry);
99
100 #ifdef EXT2_IOC_GETFLAGS
101         /* Linux requires an extra ioctl to pull the flags.  Although
102          * this is an extra step, it has a nice side-effect: We get an
103          * open file descriptor which we can use in the subsequent lookups. */
104         if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode))) {
105                 if (fd < 0)
106                         fd = open(pathname, O_RDONLY | O_NONBLOCK | O_BINARY);
107                 if (fd >= 0) {
108                         unsigned long stflags;
109                         int r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags);
110                         if (r == 0 && stflags != 0)
111                                 archive_entry_set_fflags(entry, stflags, 0);
112                 }
113         }
114 #endif
115
116         if (st == NULL) {
117                 /* TODO: On Windows, use GetFileInfoByHandle() here.
118                  * Using Windows stat() call is badly broken, but
119                  * even the stat() wrapper has problems because
120                  * 'struct stat' is broken on Windows.
121                  */
122 #if HAVE_FSTAT
123                 if (fd >= 0) {
124                         if (fstat(fd, &s) != 0) {
125                                 archive_set_error(&a->archive, errno,
126                                     "Can't fstat");
127                                 return (ARCHIVE_FAILED);
128                         }
129                 } else
130 #endif
131 #if HAVE_LSTAT
132                 if (!a->follow_symlinks) {
133                         if (lstat(path, &s) != 0) {
134                                 archive_set_error(&a->archive, errno,
135                                     "Can't lstat %s", path);
136                                 return (ARCHIVE_FAILED);
137                         }
138                 } else
139 #endif
140                 if (stat(path, &s) != 0) {
141                         archive_set_error(&a->archive, errno,
142                             "Can't stat %s", path);
143                         return (ARCHIVE_FAILED);
144                 }
145                 st = &s;
146         }
147         archive_entry_copy_stat(entry, st);
148
149         /* Lookup uname/gname */
150         name = archive_read_disk_uname(_a, archive_entry_uid(entry));
151         if (name != NULL)
152                 archive_entry_copy_uname(entry, name);
153         name = archive_read_disk_gname(_a, archive_entry_gid(entry));
154         if (name != NULL)
155                 archive_entry_copy_gname(entry, name);
156
157 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
158         /* On FreeBSD, we get flags for free with the stat. */
159         /* TODO: Does this belong in copy_stat()? */
160         if (st->st_flags != 0)
161                 archive_entry_set_fflags(entry, st->st_flags, 0);
162 #endif
163
164 #ifdef HAVE_READLINK
165         if (S_ISLNK(st->st_mode)) {
166                 char linkbuffer[PATH_MAX + 1];
167                 int lnklen = readlink(path, linkbuffer, PATH_MAX);
168                 if (lnklen < 0) {
169                         archive_set_error(&a->archive, errno,
170                             "Couldn't read link data");
171                         return (ARCHIVE_FAILED);
172                 }
173                 linkbuffer[lnklen] = 0;
174                 archive_entry_set_symlink(entry, linkbuffer);
175         }
176 #endif
177
178         r = setup_acls_posix1e(a, entry, fd);
179         r1 = setup_xattrs(a, entry, fd);
180         if (r1 < r)
181                 r = r1;
182         /* If we opened the file earlier in this function, close it. */
183         if (initial_fd != fd)
184                 close(fd);
185         return (r);
186 }
187
188 #ifdef HAVE_POSIX_ACL
189 static void setup_acl_posix1e(struct archive_read_disk *a,
190     struct archive_entry *entry, acl_t acl, int archive_entry_acl_type);
191
192 static int
193 setup_acls_posix1e(struct archive_read_disk *a,
194     struct archive_entry *entry, int fd)
195 {
196         const char      *accpath;
197         acl_t            acl;
198
199         accpath = archive_entry_sourcepath(entry);
200         if (accpath == NULL)
201                 accpath = archive_entry_pathname(entry);
202
203         archive_entry_acl_clear(entry);
204
205         /* Retrieve access ACL from file. */
206         if (fd >= 0)
207                 acl = acl_get_fd(fd);
208 #if HAVE_ACL_GET_LINK_NP
209         else if (!a->follow_symlinks)
210                 acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS);
211 #endif
212         else
213                 acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
214         if (acl != NULL) {
215                 setup_acl_posix1e(a, entry, acl,
216                     ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
217                 acl_free(acl);
218         }
219
220         /* Only directories can have default ACLs. */
221         if (S_ISDIR(archive_entry_mode(entry))) {
222                 acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
223                 if (acl != NULL) {
224                         setup_acl_posix1e(a, entry, acl,
225                             ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
226                         acl_free(acl);
227                 }
228         }
229         return (ARCHIVE_OK);
230 }
231
232 /*
233  * Translate POSIX.1e ACL into libarchive internal structure.
234  */
235 static void
236 setup_acl_posix1e(struct archive_read_disk *a,
237     struct archive_entry *entry, acl_t acl, int archive_entry_acl_type)
238 {
239         acl_tag_t        acl_tag;
240         acl_entry_t      acl_entry;
241         acl_permset_t    acl_permset;
242         int              s, ae_id, ae_tag, ae_perm;
243         const char      *ae_name;
244
245         s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
246         while (s == 1) {
247                 ae_id = -1;
248                 ae_name = NULL;
249
250                 acl_get_tag_type(acl_entry, &acl_tag);
251                 if (acl_tag == ACL_USER) {
252                         ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
253                         ae_name = archive_read_disk_uname(&a->archive, ae_id);
254                         ae_tag = ARCHIVE_ENTRY_ACL_USER;
255                 } else if (acl_tag == ACL_GROUP) {
256                         ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
257                         ae_name = archive_read_disk_gname(&a->archive, ae_id);
258                         ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
259                 } else if (acl_tag == ACL_MASK) {
260                         ae_tag = ARCHIVE_ENTRY_ACL_MASK;
261                 } else if (acl_tag == ACL_USER_OBJ) {
262                         ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
263                 } else if (acl_tag == ACL_GROUP_OBJ) {
264                         ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
265                 } else if (acl_tag == ACL_OTHER) {
266                         ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
267                 } else {
268                         /* Skip types that libarchive can't support. */
269                         continue;
270                 }
271
272                 acl_get_permset(acl_entry, &acl_permset);
273                 ae_perm = 0;
274                 /*
275                  * acl_get_perm() is spelled differently on different
276                  * platforms; see above.
277                  */
278                 if (ACL_GET_PERM(acl_permset, ACL_EXECUTE))
279                         ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE;
280                 if (ACL_GET_PERM(acl_permset, ACL_READ))
281                         ae_perm |= ARCHIVE_ENTRY_ACL_READ;
282                 if (ACL_GET_PERM(acl_permset, ACL_WRITE))
283                         ae_perm |= ARCHIVE_ENTRY_ACL_WRITE;
284
285                 archive_entry_acl_add_entry(entry,
286                     archive_entry_acl_type, ae_perm, ae_tag,
287                     ae_id, ae_name);
288
289                 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
290         }
291 }
292 #else
293 static int
294 setup_acls_posix1e(struct archive_read_disk *a,
295     struct archive_entry *entry, int fd)
296 {
297         (void)a;      /* UNUSED */
298         (void)entry;  /* UNUSED */
299         (void)fd;     /* UNUSED */
300         return (ARCHIVE_OK);
301 }
302 #endif
303
304 #if HAVE_LISTXATTR && HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR
305
306 /*
307  * Linux extended attribute support.
308  *
309  * TODO:  By using a stack-allocated buffer for the first
310  * call to getxattr(), we might be able to avoid the second
311  * call entirely.  We only need the second call if the
312  * stack-allocated buffer is too small.  But a modest buffer
313  * of 1024 bytes or so will often be big enough.  Same applies
314  * to listxattr().
315  */
316
317
318 static int
319 setup_xattr(struct archive_read_disk *a,
320     struct archive_entry *entry, const char *name, int fd)
321 {
322         ssize_t size;
323         void *value = NULL;
324         const char *accpath;
325
326         (void)fd; /* UNUSED */
327
328         accpath = archive_entry_sourcepath(entry);
329         if (accpath == NULL)
330                 accpath = archive_entry_pathname(entry);
331
332         if (!a->follow_symlinks)
333                 size = lgetxattr(accpath, name, NULL, 0);
334         else
335                 size = getxattr(accpath, name, NULL, 0);
336
337         if (size == -1) {
338                 archive_set_error(&a->archive, errno,
339                     "Couldn't query extended attribute");
340                 return (ARCHIVE_WARN);
341         }
342
343         if (size > 0 && (value = malloc(size)) == NULL) {
344                 archive_set_error(&a->archive, errno, "Out of memory");
345                 return (ARCHIVE_FATAL);
346         }
347
348         if (!a->follow_symlinks)
349                 size = lgetxattr(accpath, name, value, size);
350         else
351                 size = getxattr(accpath, name, value, size);
352
353         if (size == -1) {
354                 archive_set_error(&a->archive, errno,
355                     "Couldn't read extended attribute");
356                 return (ARCHIVE_WARN);
357         }
358
359         archive_entry_xattr_add_entry(entry, name, value, size);
360
361         free(value);
362         return (ARCHIVE_OK);
363 }
364
365 static int
366 setup_xattrs(struct archive_read_disk *a,
367     struct archive_entry *entry, int fd)
368 {
369         char *list, *p;
370         const char *path;
371         ssize_t list_size;
372
373
374         path = archive_entry_sourcepath(entry);
375         if (path == NULL)
376                 path = archive_entry_pathname(entry);
377
378         if (!a->follow_symlinks)
379                 list_size = llistxattr(path, NULL, 0);
380         else
381                 list_size = listxattr(path, NULL, 0);
382
383         if (list_size == -1) {
384                 if (errno == ENOTSUP)
385                         return (ARCHIVE_OK);
386                 archive_set_error(&a->archive, errno,
387                         "Couldn't list extended attributes");
388                 return (ARCHIVE_WARN);
389         }
390
391         if (list_size == 0)
392                 return (ARCHIVE_OK);
393
394         if ((list = malloc(list_size)) == NULL) {
395                 archive_set_error(&a->archive, errno, "Out of memory");
396                 return (ARCHIVE_FATAL);
397         }
398
399         if (!a->follow_symlinks)
400                 list_size = llistxattr(path, list, list_size);
401         else
402                 list_size = listxattr(path, list, list_size);
403
404         if (list_size == -1) {
405                 archive_set_error(&a->archive, errno,
406                         "Couldn't retrieve extended attributes");
407                 free(list);
408                 return (ARCHIVE_WARN);
409         }
410
411         for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
412                 if (strncmp(p, "system.", 7) == 0 ||
413                                 strncmp(p, "xfsroot.", 8) == 0)
414                         continue;
415                 setup_xattr(a, entry, p, fd);
416         }
417
418         free(list);
419         return (ARCHIVE_OK);
420 }
421
422 #elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE
423
424 /*
425  * FreeBSD extattr interface.
426  */
427
428 /* TODO: Implement this.  Follow the Linux model above, but
429  * with FreeBSD-specific system calls, of course.  Be careful
430  * to not include the system extattrs that hold ACLs; we handle
431  * those separately.
432  */
433 int
434 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
435     int namespace, const char *name, const char *fullname, int fd);
436
437 int
438 setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
439     int namespace, const char *name, const char *fullname, int fd)
440 {
441         ssize_t size;
442         void *value = NULL;
443         const char *accpath;
444
445         (void)fd; /* UNUSED */
446
447         accpath = archive_entry_sourcepath(entry);
448         if (accpath == NULL)
449                 accpath = archive_entry_pathname(entry);
450
451         if (!a->follow_symlinks)
452                 size = extattr_get_link(accpath, namespace, name, NULL, 0);
453         else
454                 size = extattr_get_file(accpath, namespace, name, NULL, 0);
455
456         if (size == -1) {
457                 archive_set_error(&a->archive, errno,
458                     "Couldn't query extended attribute");
459                 return (ARCHIVE_WARN);
460         }
461
462         if (size > 0 && (value = malloc(size)) == NULL) {
463                 archive_set_error(&a->archive, errno, "Out of memory");
464                 return (ARCHIVE_FATAL);
465         }
466
467         if (!a->follow_symlinks)
468                 size = extattr_get_link(accpath, namespace, name, value, size);
469         else
470                 size = extattr_get_file(accpath, namespace, name, value, size);
471
472         if (size == -1) {
473                 archive_set_error(&a->archive, errno,
474                     "Couldn't read extended attribute");
475                 return (ARCHIVE_WARN);
476         }
477
478         archive_entry_xattr_add_entry(entry, fullname, value, size);
479
480         free(value);
481         return (ARCHIVE_OK);
482 }
483
484 static int
485 setup_xattrs(struct archive_read_disk *a,
486     struct archive_entry *entry, int fd)
487 {
488         char buff[512];
489         char *list, *p;
490         ssize_t list_size;
491         const char *path;
492         int namespace = EXTATTR_NAMESPACE_USER;
493
494         path = archive_entry_sourcepath(entry);
495         if (path == NULL)
496                 path = archive_entry_pathname(entry);
497
498         if (!a->follow_symlinks)
499                 list_size = extattr_list_link(path, namespace, NULL, 0);
500         else
501                 list_size = extattr_list_file(path, namespace, NULL, 0);
502
503         if (list_size == -1 && errno == EOPNOTSUPP)
504                 return (ARCHIVE_OK);
505         if (list_size == -1) {
506                 archive_set_error(&a->archive, errno,
507                         "Couldn't list extended attributes");
508                 return (ARCHIVE_WARN);
509         }
510
511         if (list_size == 0)
512                 return (ARCHIVE_OK);
513
514         if ((list = malloc(list_size)) == NULL) {
515                 archive_set_error(&a->archive, errno, "Out of memory");
516                 return (ARCHIVE_FATAL);
517         }
518
519         if (!a->follow_symlinks)
520                 list_size = extattr_list_link(path, namespace, list, list_size);
521         else
522                 list_size = extattr_list_file(path, namespace, list, list_size);
523
524         if (list_size == -1) {
525                 archive_set_error(&a->archive, errno,
526                         "Couldn't retrieve extended attributes");
527                 free(list);
528                 return (ARCHIVE_WARN);
529         }
530
531         p = list;
532         while ((p - list) < list_size) {
533                 size_t len = 255 & (int)*p;
534                 char *name;
535
536                 strcpy(buff, "user.");
537                 name = buff + strlen(buff);
538                 memcpy(name, p + 1, len);
539                 name[len] = '\0';
540                 setup_xattr(a, entry, namespace, name, buff, fd);
541                 p += 1 + len;
542         }
543
544         free(list);
545         return (ARCHIVE_OK);
546 }
547
548 #else
549
550 /*
551  * Generic (stub) extended attribute support.
552  */
553 static int
554 setup_xattrs(struct archive_read_disk *a,
555     struct archive_entry *entry, int fd)
556 {
557         (void)a;     /* UNUSED */
558         (void)entry; /* UNUSED */
559         (void)fd;    /* UNUSED */
560         return (ARCHIVE_OK);
561 }
562
563 #endif