]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/archive_write_disk_acl.c
MFC r299529,r299540,r299576,r299896:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / archive_write_disk_acl.c
1 /*-
2  * Copyright (c) 2003-2010 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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_disk.c 201159 2009-12-29 05:35:40Z kientzle $");
29
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33 #ifdef HAVE_SYS_ACL_H
34 #define _ACL_PRIVATE /* For debugging */
35 #include <sys/acl.h>
36 #endif
37 #ifdef HAVE_ERRNO_H
38 #include <errno.h>
39 #endif
40
41 #include "archive.h"
42 #include "archive_entry.h"
43 #include "archive_acl_private.h"
44 #include "archive_write_disk_private.h"
45
46 #ifndef HAVE_POSIX_ACL
47 /* Default empty function body to satisfy mainline code. */
48 int
49 archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
50          struct archive_acl *abstract_acl)
51 {
52         (void)a; /* UNUSED */
53         (void)fd; /* UNUSED */
54         (void)name; /* UNUSED */
55         (void)abstract_acl; /* UNUSED */
56         return (ARCHIVE_OK);
57 }
58
59 #else
60
61 static int      set_acl(struct archive *, int fd, const char *,
62                         struct archive_acl *,
63                         acl_type_t, int archive_entry_acl_type, const char *tn);
64
65 /*
66  * XXX TODO: What about ACL types other than ACCESS and DEFAULT?
67  */
68 int
69 archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
70          struct archive_acl *abstract_acl)
71 {
72         int              ret;
73
74         if (archive_acl_count(abstract_acl, ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) > 0) {
75                 ret = set_acl(a, fd, name, abstract_acl, ACL_TYPE_ACCESS,
76                     ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
77                 if (ret != ARCHIVE_OK)
78                         return (ret);
79                 ret = set_acl(a, fd, name, abstract_acl, ACL_TYPE_DEFAULT,
80                     ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
81                 return (ret);
82 #ifdef ACL_TYPE_NFS4
83         } else if (archive_acl_count(abstract_acl, ARCHIVE_ENTRY_ACL_TYPE_NFS4) > 0) {
84                 ret = set_acl(a, fd, name, abstract_acl, ACL_TYPE_NFS4,
85                     ARCHIVE_ENTRY_ACL_TYPE_NFS4, "nfs4");
86                 return (ret);
87 #endif
88         } else
89                 return ARCHIVE_OK;
90 }
91
92 static struct {
93         int archive_perm;
94         int platform_perm;
95 } acl_perm_map[] = {
96         {ARCHIVE_ENTRY_ACL_EXECUTE, ACL_EXECUTE},
97         {ARCHIVE_ENTRY_ACL_WRITE, ACL_WRITE},
98         {ARCHIVE_ENTRY_ACL_READ, ACL_READ},
99 #ifdef ACL_TYPE_NFS4
100         {ARCHIVE_ENTRY_ACL_READ_DATA, ACL_READ_DATA},
101         {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, ACL_LIST_DIRECTORY},
102         {ARCHIVE_ENTRY_ACL_WRITE_DATA, ACL_WRITE_DATA},
103         {ARCHIVE_ENTRY_ACL_ADD_FILE, ACL_ADD_FILE},
104         {ARCHIVE_ENTRY_ACL_APPEND_DATA, ACL_APPEND_DATA},
105         {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, ACL_ADD_SUBDIRECTORY},
106         {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
107         {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
108         {ARCHIVE_ENTRY_ACL_DELETE_CHILD, ACL_DELETE_CHILD},
109         {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, ACL_READ_ATTRIBUTES},
110         {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
111         {ARCHIVE_ENTRY_ACL_DELETE, ACL_DELETE},
112         {ARCHIVE_ENTRY_ACL_READ_ACL, ACL_READ_ACL},
113         {ARCHIVE_ENTRY_ACL_WRITE_ACL, ACL_WRITE_ACL},
114         {ARCHIVE_ENTRY_ACL_WRITE_OWNER, ACL_WRITE_OWNER},
115         {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, ACL_SYNCHRONIZE}
116 #endif
117 };
118
119 #ifdef ACL_TYPE_NFS4
120 static struct {
121         int archive_inherit;
122         int platform_inherit;
123 } acl_inherit_map[] = {
124         {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_FILE_INHERIT},
125         {ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT},
126         {ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_NO_PROPAGATE_INHERIT},
127         {ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, ACL_ENTRY_INHERIT_ONLY}
128 };
129 #endif
130
131 static int
132 set_acl(struct archive *a, int fd, const char *name,
133     struct archive_acl *abstract_acl,
134     acl_type_t acl_type, int ae_requested_type, const char *tname)
135 {
136         acl_t            acl;
137         acl_entry_t      acl_entry;
138         acl_permset_t    acl_permset;
139 #ifdef ACL_TYPE_NFS4
140         acl_flagset_t    acl_flagset;
141 #endif
142         int              ret;
143         int              ae_type, ae_permset, ae_tag, ae_id;
144         uid_t            ae_uid;
145         gid_t            ae_gid;
146         const char      *ae_name;
147         int              entries;
148         int              i;
149
150         ret = ARCHIVE_OK;
151         entries = archive_acl_reset(abstract_acl, ae_requested_type);
152         if (entries == 0)
153                 return (ARCHIVE_OK);
154         acl = acl_init(entries);
155         while (archive_acl_next(a, abstract_acl, ae_requested_type, &ae_type,
156                    &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
157                 acl_create_entry(&acl, &acl_entry);
158
159                 switch (ae_tag) {
160                 case ARCHIVE_ENTRY_ACL_USER:
161                         acl_set_tag_type(acl_entry, ACL_USER);
162                         ae_uid = archive_write_disk_uid(a, ae_name, ae_id);
163                         acl_set_qualifier(acl_entry, &ae_uid);
164                         break;
165                 case ARCHIVE_ENTRY_ACL_GROUP:
166                         acl_set_tag_type(acl_entry, ACL_GROUP);
167                         ae_gid = archive_write_disk_gid(a, ae_name, ae_id);
168                         acl_set_qualifier(acl_entry, &ae_gid);
169                         break;
170                 case ARCHIVE_ENTRY_ACL_USER_OBJ:
171                         acl_set_tag_type(acl_entry, ACL_USER_OBJ);
172                         break;
173                 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
174                         acl_set_tag_type(acl_entry, ACL_GROUP_OBJ);
175                         break;
176                 case ARCHIVE_ENTRY_ACL_MASK:
177                         acl_set_tag_type(acl_entry, ACL_MASK);
178                         break;
179                 case ARCHIVE_ENTRY_ACL_OTHER:
180                         acl_set_tag_type(acl_entry, ACL_OTHER);
181                         break;
182 #ifdef ACL_TYPE_NFS4
183                 case ARCHIVE_ENTRY_ACL_EVERYONE:
184                         acl_set_tag_type(acl_entry, ACL_EVERYONE);
185                         break;
186 #endif
187                 default:
188                         /* XXX */
189                         break;
190                 }
191
192 #ifdef ACL_TYPE_NFS4
193                 switch (ae_type) {
194                 case ARCHIVE_ENTRY_ACL_TYPE_ALLOW:
195                         acl_set_entry_type_np(acl_entry, ACL_ENTRY_TYPE_ALLOW);
196                         break;
197                 case ARCHIVE_ENTRY_ACL_TYPE_DENY:
198                         acl_set_entry_type_np(acl_entry, ACL_ENTRY_TYPE_DENY);
199                         break;
200                 case ARCHIVE_ENTRY_ACL_TYPE_AUDIT:
201                         acl_set_entry_type_np(acl_entry, ACL_ENTRY_TYPE_AUDIT);
202                         break;
203                 case ARCHIVE_ENTRY_ACL_TYPE_ALARM:
204                         acl_set_entry_type_np(acl_entry, ACL_ENTRY_TYPE_ALARM);
205                         break;
206                 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
207                 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
208                         // These don't translate directly into the system ACL.
209                         break;
210                 default:
211                         // XXX error handling here.
212                         break;
213                 }
214 #endif
215
216                 acl_get_permset(acl_entry, &acl_permset);
217                 acl_clear_perms(acl_permset);
218
219                 for (i = 0; i < (int)(sizeof(acl_perm_map) / sizeof(acl_perm_map[0])); ++i) {
220                         if (ae_permset & acl_perm_map[i].archive_perm)
221                                 acl_add_perm(acl_permset,
222                                              acl_perm_map[i].platform_perm);
223                 }
224
225 #ifdef ACL_TYPE_NFS4
226                 acl_get_flagset_np(acl_entry, &acl_flagset);
227                 acl_clear_flags_np(acl_flagset);
228                 for (i = 0; i < (int)(sizeof(acl_inherit_map) / sizeof(acl_inherit_map[0])); ++i) {
229                         if (ae_permset & acl_inherit_map[i].archive_inherit)
230                                 acl_add_flag_np(acl_flagset,
231                                                 acl_inherit_map[i].platform_inherit);
232                 }
233 #endif
234         }
235
236         /* Try restoring the ACL through 'fd' if we can. */
237 #if HAVE_ACL_SET_FD
238         if (fd >= 0 && acl_type == ACL_TYPE_ACCESS && acl_set_fd(fd, acl) == 0)
239                 ret = ARCHIVE_OK;
240         else
241 #else
242 #if HAVE_ACL_SET_FD_NP
243         if (fd >= 0 && acl_set_fd_np(fd, acl, acl_type) == 0)
244                 ret = ARCHIVE_OK;
245         else
246 #endif
247 #endif
248 #if HAVE_ACL_SET_LINK_NP
249           if (acl_set_link_np(name, acl_type, acl) != 0) {
250                 archive_set_error(a, errno, "Failed to set %s acl", tname);
251                 ret = ARCHIVE_WARN;
252           }
253 #else
254         /* TODO: Skip this if 'name' is a symlink. */
255         if (acl_set_file(name, acl_type, acl) != 0) {
256                 archive_set_error(a, errno, "Failed to set %s acl", tname);
257                 ret = ARCHIVE_WARN;
258         }
259 #endif
260         acl_free(acl);
261         return (ret);
262 }
263 #endif