Debian Codegraph @

xlators/system/posix-acl/src/posix-acl-xattr.c

raw symbols SCIP
1 /*
2 Copyright (c) 2011-2013 Red Hat, Inc. <http://www.redhat.com>
3 This file is part of GlusterFS.
4
5 This file is licensed to you under your choice of the GNU Lesser
6 General Public License, version 3 or any later version (LGPLv3 or
7 later), or the GNU General Public License, version 2 (GPLv2), in all
8 cases as published by the Free Software Foundation.
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #ifdef __FreeBSD__
14 #include <sys/endian.h>
15 #else
16 #include <endian.h>
17 #endif
18 #include <glusterfs/xlator.h>
19 #include "posix-acl.h"
20 #include "posix-acl-xattr.h"
21
22 int
23 posix_ace_cmp(const void *val1, const void *val2)
24 {
25 const struct posix_ace *ace1 = NULL;
26 const struct posix_ace *ace2 = NULL;
27 int ret = 0;
28
29 ace1 = val1;
30 ace2 = val2;
31
32 ret = (ace1->tag - ace2->tag);
33 if (!ret)
34 ret = (ace1->id - ace2->id);
35
36 return ret;
37 }
38
39 void
40 posix_acl_normalize(xlator_t *this, struct posix_acl *acl)
41 {
42 qsort(acl->entries, acl->count, sizeof(struct posix_ace *), posix_ace_cmp);
43 }
44
45 struct posix_acl *
46 posix_acl_from_xattr(xlator_t *this, const char *xattr_buf, int xattr_size)
47 {
48 struct posix_acl_xattr_header *header = NULL;
49 struct posix_acl_xattr_entry *entry = NULL;
50 struct posix_acl *acl = NULL;
51 struct posix_ace *ace = NULL;
52 int size = 0;
53 int count = 0;
54 int i = 0;
55
56 size = xattr_size;
57
58 if (size < sizeof(*header))
59 return NULL;
60
61 size -= sizeof(*header);
62
63 if (size % sizeof(*entry))
64 return NULL;
65
66 count = size / sizeof(*entry);
67
68 header = (struct posix_acl_xattr_header *)(xattr_buf);
69 entry = (struct posix_acl_xattr_entry *)(header + 1);
70
71 if (header->version != htole32(POSIX_ACL_XATTR_VERSION))
72 return NULL;
73
74 acl = posix_acl_new(this, count);
75 if (!acl)
76 return NULL;
77
78 ace = acl->entries;
79
80 for (i = 0; i < count; i++) {
81 ace->tag = le16toh(entry->tag);
82 ace->perm = le16toh(entry->perm);
83
84 switch (ace->tag) {
85 case POSIX_ACL_USER_OBJ:
86 case POSIX_ACL_MASK:
87 case POSIX_ACL_OTHER:
88 ace->id = POSIX_ACL_UNDEFINED_ID;
89 break;
90
91 case POSIX_ACL_GROUP:
92 case POSIX_ACL_USER:
93 case POSIX_ACL_GROUP_OBJ:
94 ace->id = le32toh(entry->id);
95 break;
96
97 default:
98 goto err;
99 }
100
101 ace++;
102 entry++;
103 }
104
105 posix_acl_normalize(this, acl);
106
107 return acl;
108 err:
109 posix_acl_destroy(this, acl);
110 return NULL;
111 }
112
113 int
114 posix_acl_to_xattr(xlator_t *this, struct posix_acl *acl, char *xattr_buf,
115 int xattr_size)
116 {
117 int size = 0;
118 struct posix_acl_xattr_header *header = NULL;
119 struct posix_acl_xattr_entry *entry = NULL;
120 struct posix_ace *ace = NULL;
121 int i = 0;
122
123 size = sizeof(*header) + (acl->count * sizeof(*entry));
124
125 if (xattr_size < size)
126 return size;
127
128 header = (struct posix_acl_xattr_header *)(xattr_buf);
129 entry = (struct posix_acl_xattr_entry *)(header + 1);
130 ace = acl->entries;
131
132 header->version = htole32(POSIX_ACL_XATTR_VERSION);
133
134 for (i = 0; i < acl->count; i++) {
135 entry->tag = htole16(ace->tag);
136 entry->perm = htole16(ace->perm);
137
138 switch (ace->tag) {
139 case POSIX_ACL_USER:
140 case POSIX_ACL_GROUP:
141 entry->id = htole32(ace->id);
142 break;
143 default:
144 entry->id = POSIX_ACL_UNDEFINED_ID;
145 break;
146 }
147
148 ace++;
149 entry++;
150 }
151
152 return 0;
153 }
154
155 int
156 posix_acl_matches_xattr(xlator_t *this, struct posix_acl *acl, const char *buf,
157 int size)
158 {
159 struct posix_acl *acl2 = NULL;
160 int ret = 1;
161
162 acl2 = posix_acl_from_xattr(this, buf, size);
163 if (!acl2)
164 return 0;
165
166 if (acl->count != acl2->count) {
167 ret = 0;
168 goto out;
169 }
170
171 if (memcmp(acl->entries, acl2->entries,
172 (acl->count * sizeof(struct posix_ace))))
173 ret = 0;
174 out:
175 posix_acl_destroy(this, acl2);
176
177 return ret;
178 }