Deleted Added
full compact
remove.c (139969) remove.c (196936)
1/*-
2 * Copyright (c) 2001 Chris D. Faulhaber
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

--- 11 unchanged lines hidden (view full) ---

20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2001 Chris D. Faulhaber
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

--- 11 unchanged lines hidden (view full) ---

20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/bin/setfacl/remove.c 139969 2005-01-10 08:39:26Z imp $");
28__FBSDID("$FreeBSD: head/bin/setfacl/remove.c 196936 2009-09-07 16:19:32Z trasz $");
29
30#include <sys/types.h>
31#include <sys/acl.h>
32#include <sys/stat.h>
33
34#include <err.h>
35#include <stdio.h>
36#include <string.h>
37
38#include "setfacl.h"
39
40/*
41 * remove ACL entries from an ACL
42 */
43int
29
30#include <sys/types.h>
31#include <sys/acl.h>
32#include <sys/stat.h>
33
34#include <err.h>
35#include <stdio.h>
36#include <string.h>
37
38#include "setfacl.h"
39
40/*
41 * remove ACL entries from an ACL
42 */
43int
44remove_acl(acl_t acl, acl_t *prev_acl)
44remove_acl(acl_t acl, acl_t *prev_acl, const char *filename)
45{
46 acl_entry_t entry;
47 acl_t acl_new;
48 acl_tag_t tag;
45{
46 acl_entry_t entry;
47 acl_t acl_new;
48 acl_tag_t tag;
49 int carried_error, entry_id;
49 int carried_error, entry_id, acl_brand, prev_acl_brand;
50
51 carried_error = 0;
52
50
51 carried_error = 0;
52
53 if (acl_type == ACL_TYPE_ACCESS)
54 acl_new = acl_dup(prev_acl[ACCESS_ACL]);
55 else
56 acl_new = acl_dup(prev_acl[DEFAULT_ACL]);
53 acl_get_brand_np(acl, &acl_brand);
54 acl_get_brand_np(*prev_acl, &prev_acl_brand);
55
56 if (acl_brand != prev_acl_brand) {
57 warnx("%s: branding mismatch; existing ACL is %s, "
58 "entry to be removed is %s", filename,
59 prev_acl_brand == ACL_BRAND_NFS4 ? "NFSv4" : "POSIX.1e",
60 acl_brand == ACL_BRAND_NFS4 ? "NFSv4" : "POSIX.1e");
61 return (-1);
62 }
63
64 carried_error = 0;
65
66 acl_new = acl_dup(*prev_acl);
57 if (acl_new == NULL)
67 if (acl_new == NULL)
58 err(1, "acl_dup() failed");
68 err(1, "%s: acl_dup() failed", filename);
59
60 tag = ACL_UNDEFINED_TAG;
61
62 /* find and delete the entry */
63 entry_id = ACL_FIRST_ENTRY;
64 while (acl_get_entry(acl, entry_id, &entry) == 1) {
65 entry_id = ACL_NEXT_ENTRY;
66 if (acl_get_tag_type(entry, &tag) == -1)
69
70 tag = ACL_UNDEFINED_TAG;
71
72 /* find and delete the entry */
73 entry_id = ACL_FIRST_ENTRY;
74 while (acl_get_entry(acl, entry_id, &entry) == 1) {
75 entry_id = ACL_NEXT_ENTRY;
76 if (acl_get_tag_type(entry, &tag) == -1)
67 err(1, "acl_get_tag_type() failed");
77 err(1, "%s: acl_get_tag_type() failed", filename);
68 if (tag == ACL_MASK)
69 have_mask++;
70 if (acl_delete_entry(acl_new, entry) == -1) {
71 carried_error++;
78 if (tag == ACL_MASK)
79 have_mask++;
80 if (acl_delete_entry(acl_new, entry) == -1) {
81 carried_error++;
72 warnx("cannot remove non-existent acl entry");
82 warnx("%s: cannot remove non-existent ACL entry",
83 filename);
73 }
74 }
75
84 }
85 }
86
76 if (acl_type == ACL_TYPE_ACCESS) {
77 acl_free(prev_acl[ACCESS_ACL]);
78 prev_acl[ACCESS_ACL] = acl_new;
79 } else {
80 acl_free(prev_acl[DEFAULT_ACL]);
81 prev_acl[DEFAULT_ACL] = acl_new;
87 acl_free(*prev_acl);
88 *prev_acl = acl_new;
89
90 if (carried_error)
91 return (-1);
92
93 return (0);
94}
95
96int
97remove_by_number(uint entry_number, acl_t *prev_acl, const char *filename)
98{
99 acl_entry_t entry;
100 acl_t acl_new;
101 acl_tag_t tag;
102 int carried_error, entry_id;
103 uint i;
104
105 carried_error = 0;
106
107 acl_new = acl_dup(*prev_acl);
108 if (acl_new == NULL)
109 err(1, "%s: acl_dup() failed", filename);
110
111 tag = ACL_UNDEFINED_TAG;
112
113 /*
114 * Find out whether we're removing the mask entry,
115 * to behave the same as the routine above.
116 *
117 * XXX: Is this loop actually needed?
118 */
119 entry_id = ACL_FIRST_ENTRY;
120 i = 0;
121 while (acl_get_entry(acl_new, entry_id, &entry) == 1) {
122 entry_id = ACL_NEXT_ENTRY;
123 if (i != entry_number)
124 continue;
125 if (acl_get_tag_type(entry, &tag) == -1)
126 err(1, "%s: acl_get_tag_type() failed", filename);
127 if (tag == ACL_MASK)
128 have_mask++;
82 }
83
129 }
130
131 if (acl_delete_entry_np(acl_new, entry_number) == -1) {
132 carried_error++;
133 warn("%s: acl_delete_entry_np() failed", filename);
134 }
135
136 acl_free(*prev_acl);
137 *prev_acl = acl_new;
138
84 if (carried_error)
85 return (-1);
86
87 return (0);
88}
89
90/*
91 * remove default entries
92 */
93int
139 if (carried_error)
140 return (-1);
141
142 return (0);
143}
144
145/*
146 * remove default entries
147 */
148int
94remove_default(acl_t *prev_acl)
149remove_default(acl_t *prev_acl, const char *filename)
95{
96
150{
151
97 if (prev_acl[1]) {
98 acl_free(prev_acl[1]);
99 prev_acl[1] = acl_init(ACL_MAX_ENTRIES);
100 if (prev_acl[1] == NULL)
101 err(1, "acl_init() failed");
102 } else {
103 warn("cannot remove default ACL");
104 return (-1);
105 }
152 acl_free(*prev_acl);
153 *prev_acl = acl_init(ACL_MAX_ENTRIES);
154 if (*prev_acl == NULL)
155 err(1, "%s: acl_init() failed", filename);
156
106 return (0);
107}
108
109/*
110 * remove extended entries
111 */
112void
157 return (0);
158}
159
160/*
161 * remove extended entries
162 */
163void
113remove_ext(acl_t *prev_acl)
164remove_ext(acl_t *prev_acl, const char *filename)
114{
165{
115 acl_t acl_new, acl_old;
116 acl_entry_t entry, entry_new;
117 acl_permset_t perm;
118 acl_tag_t tag;
119 int entry_id, have_mask_entry;
166 acl_t acl_new;
120
167
121 if (acl_type == ACL_TYPE_ACCESS)
122 acl_old = acl_dup(prev_acl[ACCESS_ACL]);
123 else
124 acl_old = acl_dup(prev_acl[DEFAULT_ACL]);
125 if (acl_old == NULL)
126 err(1, "acl_dup() failed");
127
128 have_mask_entry = 0;
129 acl_new = acl_init(ACL_MAX_ENTRIES);
168 acl_new = acl_strip_np(*prev_acl, !n_flag);
130 if (acl_new == NULL)
169 if (acl_new == NULL)
131 err(1, "acl_init() failed");
132 tag = ACL_UNDEFINED_TAG;
170 err(1, "%s: acl_strip_np() failed", filename);
133
171
134 /* only save the default user/group/other entries */
135 entry_id = ACL_FIRST_ENTRY;
136 while (acl_get_entry(acl_old, entry_id, &entry) == 1) {
137 entry_id = ACL_NEXT_ENTRY;
138
139 if (acl_get_tag_type(entry, &tag) == -1)
140 err(1, "acl_get_tag_type() failed");
141
142 switch(tag) {
143 case ACL_USER_OBJ:
144 case ACL_GROUP_OBJ:
145 case ACL_OTHER:
146 if (acl_get_tag_type(entry, &tag) == -1)
147 err(1, "acl_get_tag_type() failed");
148 if (acl_get_permset(entry, &perm) == -1)
149 err(1, "acl_get_permset() failed");
150 if (acl_create_entry(&acl_new, &entry_new) == -1)
151 err(1, "acl_create_entry() failed");
152 if (acl_set_tag_type(entry_new, tag) == -1)
153 err(1, "acl_set_tag_type() failed");
154 if (acl_set_permset(entry_new, perm) == -1)
155 err(1, "acl_get_permset() failed");
156 if (acl_copy_entry(entry_new, entry) == -1)
157 err(1, "acl_copy_entry() failed");
158 break;
159 case ACL_MASK:
160 have_mask_entry = 1;
161 break;
162 default:
163 break;
164 }
165 }
166 if (have_mask_entry && n_flag == 0) {
167 if (acl_calc_mask(&acl_new) == -1)
168 err(1, "acl_calc_mask() failed");
169 } else {
170 have_mask = 1;
171 }
172
173 if (acl_type == ACL_TYPE_ACCESS) {
174 acl_free(prev_acl[ACCESS_ACL]);
175 prev_acl[ACCESS_ACL] = acl_new;
176 } else {
177 acl_free(prev_acl[DEFAULT_ACL]);
178 prev_acl[DEFAULT_ACL] = acl_new;
179 }
172 acl_free(*prev_acl);
173 *prev_acl = acl_new;
180}
174}