summaryrefslogtreecommitdiffstats
path: root/xlators/system/posix-acl/src/posix-acl-xattr.c
blob: 97ff8e9f41ad6e0daa29fd5f0e8e34aaf0bc64ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/


#include <stdio.h>
#include <stdlib.h>

#include "posix-acl.h"
#include "posix-acl-xattr.h"


int
posix_ace_cmp (const void *val1, const void *val2)
{
        const struct posix_ace *ace1 = NULL;
        const struct posix_ace *ace2 = NULL;
        int                     ret = 0;

        ace1 = val1;
        ace2 = val2;

        ret = (ace1->tag - ace2->tag);
        if (!ret)
                ret = (ace1->id - ace2->id);

        return ret;
}


void
posix_acl_normalize (xlator_t *this, struct posix_acl *acl)
{
        qsort (acl->entries, acl->count, sizeof (struct posix_ace *),
               posix_ace_cmp);
}


struct posix_acl *
posix_acl_from_xattr (xlator_t *this, const char *xattr_buf, int xattr_size)
{
        struct posix_acl_xattr_header   *header = NULL;
        struct posix_acl_xattr_entry    *entry = NULL;
        struct posix_acl                *acl = NULL;
        struct posix_ace                *ace = NULL;
        int                              size = 0;
        int                              count = 0;
        int                              i = 0;

        size = xattr_size;

        if (size < sizeof (*header))
                return NULL;

        size -= sizeof (*header);

        if (size % sizeof (*entry))
                return NULL;

        count = size / sizeof (*entry);

        header = (struct posix_acl_xattr_header *) (xattr_buf);
        entry = (struct posix_acl_xattr_entry *) (header + 1);

        if (header->version != htole32 (POSIX_ACL_VERSION))
                return NULL;

        acl = posix_acl_new (this, count);
        if (!acl)
                return NULL;

        ace = acl->entries;

        for (i = 0; i < count; i++) {
                ace->tag  = letoh16 (entry->tag);
                ace->perm = letoh16 (entry->perm);

                switch (ace->tag) {
                case POSIX_ACL_USER_OBJ:
                case POSIX_ACL_MASK:
                case POSIX_ACL_OTHER:
                        ace->id = POSIX_ACL_UNDEFINED_ID;
                        break;

                case POSIX_ACL_GROUP:
                case POSIX_ACL_USER:
                case POSIX_ACL_GROUP_OBJ:
                        ace->id = letoh32 (entry->id);
                        break;

                default:
                        goto err;
                }

                ace++;
                entry++;
        }

        posix_acl_normalize (this, acl);

        return acl;
err:
        posix_acl_destroy (this, acl);
        return NULL;
}


int
posix_acl_to_xattr (xlator_t *this, struct posix_acl *acl, char *xattr_buf,
                    int xattr_size)
{
        int                             size = 0;
        struct posix_acl_xattr_header  *header = NULL;
        struct posix_acl_xattr_entry   *entry = NULL;
        struct posix_ace               *ace = NULL;
        int                             i = 0;

        size = sizeof (*header) + (acl->count * sizeof (*entry));

        if (xattr_size < size)
                return size;

        header = (struct posix_acl_xattr_header *) (xattr_buf);
        entry = (struct posix_acl_xattr_entry *) (header + 1);
        ace = acl->entries;

        header->version = htole32 (POSIX_ACL_VERSION);

        for (i = 0; i < acl->count; i++) {
                entry->tag   = htole16 (ace->tag);
                entry->perm  = htole16 (ace->perm);

                switch (ace->tag) {
                case POSIX_ACL_USER:
                case POSIX_ACL_GROUP:
                        entry->id  = htole32 (ace->id);
                        break;
                default:
                        entry->id = POSIX_ACL_UNDEFINED_ID;
                        break;
                }

                ace++;
                entry++;
        }

        return 0;
}


int
posix_acl_matches_xattr (xlator_t *this, struct posix_acl *acl, const char *buf,
                         int size)
{
        struct posix_acl  *acl2 = NULL;
        int                ret = 1;

        acl2 = posix_acl_from_xattr (this, buf, size);
        if (!acl2)
                return 0;

        if (acl->count != acl2->count) {
                ret = 0;
                goto out;
        }

        if (memcmp (acl->entries, acl2->entries,
                    (acl->count * sizeof (struct posix_ace))))
                ret = 0;
out:
        posix_acl_destroy (this, acl2);

        return ret;
}