summaryrefslogtreecommitdiffstats
path: root/kio/kio/posixacladdons.cpp
blob: bae51592b1d852efb4bf5a856c193c85ef906989 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/***************************************************************************
 *   Copyright (C) 2005 by Markus Brueffer <markus@brueffer.de>            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by  the Free Software Foundation; either version 2 of the   *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "posixacladdons.h"

#if defined(USE_POSIX_ACL) && !defined(HAVE_NON_POSIX_ACL_EXTENSIONS)

#include <errno.h>
#include <sys/stat.h>

#include <tqptrlist.h>

class SortedEntryList : public TQPtrList<acl_entry_t>
{
protected:
    int compareItems( TQPtrCollection::Item i1,
                      TQPtrCollection::Item i2 )
    {
        acl_entry_t *e1 = static_cast<acl_entry_t*>( i1 );
        acl_entry_t *e2 = static_cast<acl_entry_t*>( i2 );

        acl_tag_t tag1, tag2;
        uid_t uid1 = 0, uid2 = 0;

        acl_get_tag_type( *e1, &tag1 );
        acl_get_tag_type( *e2, &tag2 );

        if ( tag1 == ACL_USER || tag1 == ACL_GROUP )
                uid1 = *( (uid_t*) acl_get_qualifier( *e1 ) );

        if ( tag2 == ACL_USER || tag2 == ACL_GROUP )
                uid2 = *( (uid_t*) acl_get_qualifier( *e2 ) );

        if ( tag1 < tag2 )
            return -1;
        else if ( tag1 > tag2 )
            return 1;

        if ( uid1 < uid2 )
            return -1;
        else if ( uid1 > uid2 )
            return 1;

        return 0;
    }
};

int acl_cmp(acl_t acl1, acl_t acl2)
{
    if ( !acl1 || !acl2 )
        return -1;

    SortedEntryList entries1, entries2;
    entries1.setAutoDelete( true );
    entries2.setAutoDelete( true );

    /* Add ACL entries to vectors */
    acl_entry_t *entry = new acl_entry_t;
    int ret = acl_get_entry( acl1, ACL_FIRST_ENTRY, entry );
    while( ret == 1 ) {
        entries1.append( entry );
        entry = new acl_entry_t;
        ret = acl_get_entry( acl1, ACL_NEXT_ENTRY, entry );
    }
    delete entry;

    entry = new acl_entry_t;
    ret = acl_get_entry( acl2, ACL_FIRST_ENTRY, entry );
    while ( ret == 1 ) {
        entries2.append( entry );
        entry = new acl_entry_t;
        ret = acl_get_entry( acl2, ACL_NEXT_ENTRY, entry );
    }
    delete entry;

    /* If the entry count differs, we are done */
    if ( entries1.count() != entries2.count() )
        return 1;

    /* Sort vectors */
    entries1.sort();
    entries2.sort();

    /* Compare all entries */
    acl_permset_t permset1, permset2;
    acl_tag_t tag1, tag2;
    uid_t uid1, uid2;
    acl_entry_t *e1, *e2;

    for ( e1 = entries1.first(), e2 = entries2.first(); e1; e1 = entries1.next(), e2 = entries2.next() ) {
        /* Compare tag */
        if ( acl_get_tag_type( *e1, &tag1 ) != 0 ) return 1;
        if ( acl_get_tag_type( *e2, &tag2 ) != 0 ) return 1;
        if ( tag1 != tag2 ) return 1;

        /* Compare permissions */
        if ( acl_get_permset( *e1, &permset1 ) != 0 ) return 1;
        if ( acl_get_permset( *e2, &permset2 ) != 0 ) return 1;
        if ( *permset1 != *permset2) return 1;

        /* Compare uid */
        switch( tag1 ) {
            case ACL_USER:
            case ACL_GROUP:
                uid1 = *( (uid_t*) acl_get_qualifier( *e1 ) );
                uid2 = *( (uid_t*) acl_get_qualifier( *e2 ) );
                if ( uid1 != uid2 ) return 1;
        }
    }

    return 0;
}

acl_t acl_from_mode(mode_t mode)
{
    acl_t newACL = acl_init( 3 );
    acl_entry_t entry;
    acl_permset_t permset;
    int error = 0;

    /* Add owner entry */
    if ( ( error = acl_create_entry( &newACL, &entry ) ) == 0 ) {
        /* Set owner permissions */
        acl_set_tag_type( entry, ACL_USER_OBJ );
        acl_get_permset( entry, &permset );
        acl_clear_perms( permset );
        if ( mode & S_IRUSR ) acl_add_perm( permset, ACL_READ );
        if ( mode & S_IWUSR ) acl_add_perm( permset, ACL_WRITE );
        if ( mode & S_IXUSR ) acl_add_perm( permset, ACL_EXECUTE );
        acl_set_permset( entry, permset );

        /* Add group entry */
        if ( ( error = acl_create_entry( &newACL, &entry ) ) == 0 ) {
            /* Set group permissions */
            acl_set_tag_type( entry, ACL_GROUP_OBJ );
            acl_get_permset( entry, &permset );
            acl_clear_perms( permset );
            if ( mode & S_IRGRP ) acl_add_perm( permset, ACL_READ );
            if ( mode & S_IWGRP ) acl_add_perm( permset, ACL_WRITE );
            if ( mode & S_IXGRP ) acl_add_perm( permset, ACL_EXECUTE );
            acl_set_permset( entry, permset );

            /* Add other entry */
            if ( ( error = acl_create_entry( &newACL, &entry ) ) == 0) {
                /* Set other permissions */
                acl_set_tag_type( entry, ACL_OTHER );
                acl_get_permset( entry, &permset );
                acl_clear_perms( permset );
                if ( mode & S_IROTH ) acl_add_perm( permset, ACL_READ );
                if ( mode & S_IWOTH ) acl_add_perm( permset, ACL_WRITE );
                if ( mode & S_IXOTH ) acl_add_perm( permset, ACL_EXECUTE );
                acl_set_permset( entry, permset );
            }
        }
    }

    if ( error ) {
        acl_free ( &newACL );
        return NULL;
    }

    return newACL;
}

int acl_equiv_mode(acl_t acl, mode_t *mode_p)
{
    acl_entry_t entry;
    acl_tag_t tag;
    acl_permset_t permset;
    mode_t mode = 0;
    int notEquiv = 0;

    if ( !acl )
        return -1;

    int ret = acl_get_entry( acl, ACL_FIRST_ENTRY, &entry );
    while ( ret == 1 ) {
        acl_get_tag_type( entry, &tag );
        acl_get_permset( entry, &permset );

        switch( tag ) {
            case ACL_USER_OBJ:
                if ( acl_get_perm( permset, ACL_READ ) ) mode |= S_IRUSR;
                if ( acl_get_perm( permset, ACL_WRITE ) ) mode |= S_IWUSR;
                if ( acl_get_perm( permset, ACL_EXECUTE ) ) mode |= S_IXUSR;
                break;

            case ACL_GROUP_OBJ:
                if ( acl_get_perm( permset, ACL_READ ) ) mode |= S_IRGRP;
                if ( acl_get_perm( permset, ACL_WRITE ) ) mode |= S_IWGRP;
                if ( acl_get_perm( permset, ACL_EXECUTE ) ) mode |= S_IXGRP;
                break;

            case ACL_OTHER:
                if ( acl_get_perm( permset, ACL_READ ) ) mode |= S_IROTH;
                if ( acl_get_perm( permset, ACL_WRITE ) ) mode |= S_IWOTH;
                if ( acl_get_perm( permset, ACL_EXECUTE ) ) mode |= S_IXOTH;
                break;

            case ACL_USER:
            case ACL_GROUP:
            case ACL_MASK:
                notEquiv = 1;
                break;

            default:
                errno = EINVAL;
                return -1;
        }

        ret = acl_get_entry( acl, ACL_NEXT_ENTRY, &entry );
    }

    if (mode_p)
        *mode_p = mode;

    return notEquiv;
}

#endif // USE_POSIX_ACL