]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/setfacl/remove.c
This commit was generated by cvs2svn to compensate for changes in r98530,
[FreeBSD/FreeBSD.git] / bin / setfacl / remove.c
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
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 AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE VOICES IN HIS HEAD BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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  * $FreeBSD$
27  */
28
29 #include <sys/types.h>
30 #include <sys/acl.h>
31 #include <sys/stat.h>
32
33 #include <err.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #include "setfacl.h"
38
39 /*
40  * remove ACL entries from an ACL
41  */
42 int
43 remove_acl(acl_t acl, acl_t *prev_acl)
44 {
45         acl_entry_t     entry;
46         acl_t           acl_new;
47         acl_tag_t       tag;
48         int             carried_error, entry_id;
49
50         carried_error = 0;
51
52         if (acl_type == ACL_TYPE_ACCESS)
53                 acl_new = acl_dup(prev_acl[ACCESS_ACL]);
54         else
55                 acl_new = acl_dup(prev_acl[DEFAULT_ACL]);
56         if (acl_new == NULL)
57                 err(1, "acl_dup() failed");
58
59         tag = ACL_UNDEFINED_TAG;
60
61         /* find and delete the entry */
62         entry_id = ACL_FIRST_ENTRY;
63         while (acl_get_entry(acl, entry_id, &entry) == 1) {
64                 entry_id = ACL_NEXT_ENTRY;
65                 if (acl_get_tag_type(entry, &tag) == -1)
66                         err(1, "acl_get_tag_type() failed");
67                 if (tag == ACL_MASK)
68                         have_mask++;
69                 if (acl_delete_entry(acl_new, entry) == -1) {
70                         carried_error++;
71                         warnx("cannot remove non-existent acl entry");
72                 }
73         }
74
75         if (acl_type == ACL_TYPE_ACCESS) {
76                 acl_free(prev_acl[ACCESS_ACL]);
77                 prev_acl[ACCESS_ACL] = acl_new;
78         } else {
79                 acl_free(prev_acl[DEFAULT_ACL]);
80                 prev_acl[DEFAULT_ACL] = acl_new;
81         }
82
83         if (carried_error)
84                 return (-1);
85
86         return (0);
87 }
88
89 /*
90  * remove default entries
91  */
92 int
93 remove_default(acl_t *prev_acl)
94 {
95
96         if (prev_acl[1]) {
97                 acl_free(prev_acl[1]);
98                 prev_acl[1] = acl_init(ACL_MAX_ENTRIES);
99                 if (prev_acl[1] == NULL)
100                         err(1, "acl_init() failed");
101         } else {
102                 warn("cannot remove default ACL");
103                 return (-1);
104         }
105         return (0);
106 }
107
108 /*
109  * remove extended entries
110  */
111 void
112 remove_ext(acl_t *prev_acl)
113 {
114         acl_t acl_new, acl_old;
115         acl_entry_t entry, entry_new;
116         acl_permset_t perm;
117         acl_tag_t tag;
118         int entry_id, have_mask_entry;
119
120         if (acl_type == ACL_TYPE_ACCESS)
121                 acl_old = acl_dup(prev_acl[ACCESS_ACL]);
122         else
123                 acl_old = acl_dup(prev_acl[DEFAULT_ACL]);
124         if (acl_old == NULL)
125                 err(1, "acl_dup() failed");
126
127         have_mask_entry = 0;
128         acl_new = acl_init(ACL_MAX_ENTRIES);
129         if (acl_new == NULL)
130                 err(1, "acl_init() failed");
131         tag = ACL_UNDEFINED_TAG;
132
133         /* only save the default user/group/other entries */
134         entry_id = ACL_FIRST_ENTRY;
135         while (acl_get_entry(acl_old, entry_id, &entry) == 1) {
136                 entry_id = ACL_NEXT_ENTRY;
137
138                 if (acl_get_tag_type(entry, &tag) == -1)
139                         err(1, "acl_get_tag_type() failed");
140
141                 switch(tag) {
142                 case ACL_USER_OBJ:
143                 case ACL_GROUP_OBJ:
144                 case ACL_OTHER:
145                         if (acl_get_tag_type(entry, &tag) == -1)
146                                 err(1, "acl_get_tag_type() failed");
147                         if (acl_get_permset(entry, &perm) == -1)
148                                 err(1, "acl_get_permset() failed");
149                         if (acl_create_entry(&acl_new, &entry_new) == -1)
150                                 err(1, "acl_create_entry() failed");
151                         if (acl_set_tag_type(entry_new, tag) == -1)
152                                 err(1, "acl_set_tag_type() failed");
153                         if (acl_set_permset(entry_new, perm) == -1)
154                                 err(1, "acl_get_permset() failed");
155                         if (acl_copy_entry(entry_new, entry) == -1)
156                                 err(1, "acl_copy_entry() failed");
157                         break;
158                 case ACL_MASK:
159                         have_mask_entry = 1;
160                         break;
161                 default:
162                         break;
163                 }
164         }
165         if (have_mask_entry && n_flag == 0) {
166                 if (acl_calc_mask(&acl_new) == -1)
167                         err(1, "acl_calc_mask() failed");
168         } else {
169                 have_mask = 1;
170         }
171
172         if (acl_type == ACL_TYPE_ACCESS) {
173                 acl_free(prev_acl[ACCESS_ACL]);
174                 prev_acl[ACCESS_ACL] = acl_new;
175         } else {
176                 acl_free(prev_acl[DEFAULT_ACL]);
177                 prev_acl[DEFAULT_ACL] = acl_new;
178         }
179 }