summaryrefslogtreecommitdiff
path: root/libacl/__acl_reorder_obj_p.c
blob: 9cbbf4ca631b38858ff9871fbe86bb9c96e6c927 (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
/*
  File: __acl_reorder_obj_p.c
  (Linux Access Control List Management, Posix Library Functions)

  Copyright (C) 1999, 2000
  Andreas Gruenbacher, <andreas.gruenbacher@gmail.com>

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "config.h"
#include <alloca.h>
#include "libacl.h"


static inline int
__acl_entry_p_compare(const acl_entry_obj *a_p, const acl_entry_obj *b_p)
{
	if (a_p->etag < b_p->etag)
		return -1;
	else if (a_p->etag > b_p->etag)
		return 1;

	if (a_p->eid.qid < b_p->eid.qid)
		return -1;
	else if (a_p->eid.qid > b_p->eid.qid)
		return 1;
	else
		return 0;
}


static int
__acl_entry_pp_compare(const void *a, const void *b)
{
	return __acl_entry_p_compare(*(const acl_entry_obj **)a,
				   *(const acl_entry_obj **)b);
}


/*
  Take an ACL entry form its current place in the entry ring,
  and insert it at its proper place. Entries that are not valid
  (yet) are not reordered.
*/
int
__acl_reorder_entry_obj_p(acl_entry_obj *entry_obj_p)
{
	acl_obj *acl_obj_p = entry_obj_p->econtainer;
	acl_entry_obj *here_obj_p;

	if (acl_obj_p->aused <= 1)
		return 0;
	switch(entry_obj_p->etag) {
		case ACL_UNDEFINED_TAG:
			return 1;
		case ACL_USER:
		case ACL_GROUP:
			if (qualifier_obj_id(entry_obj_p->eid) ==
			    ACL_UNDEFINED_ID)
				return 1;
	}

	/* Remove entry from ring */
	entry_obj_p->eprev->enext = entry_obj_p->enext;
	entry_obj_p->enext->eprev = entry_obj_p->eprev;

	/* Search for next greater entry */
	FOREACH_ACL_ENTRY(here_obj_p, acl_obj_p) {
		if (__acl_entry_p_compare(here_obj_p, entry_obj_p) > 0)
			break;
	}
	
	/* Re-insert entry into ring */
	entry_obj_p->eprev = here_obj_p->eprev;
	entry_obj_p->enext = here_obj_p;
	entry_obj_p->eprev->enext = entry_obj_p;
	entry_obj_p->enext->eprev = entry_obj_p;

	return 0;
}


/*
  Sort all ACL entries at once, after initializing them. This function is
  only used when converting complete ACLs from external formats to ACLs;
  the ACL entries are always kept in canonical order while an ACL is
  manipulated.
*/
int
__acl_reorder_obj_p(acl_obj *acl_obj_p)
{
	acl_entry_obj **vector = alloca(sizeof(acl_entry_obj *) *
					acl_obj_p->aused), **v, *x;
	acl_entry_obj *entry_obj_p;

	if (acl_obj_p->aused <= 1)
		return 0;

	v = vector;
	FOREACH_ACL_ENTRY(entry_obj_p, acl_obj_p) {
		*v++ = entry_obj_p;
	}

	qsort(vector, acl_obj_p->aused, sizeof(acl_entry_obj *),
	       __acl_entry_pp_compare);

	x = (acl_entry_obj *)acl_obj_p;
	for (v = vector; v != vector + acl_obj_p->aused; v++) {
		(*v)->eprev = x;
		x = *v;
	}
	acl_obj_p->aprev = *(vector + acl_obj_p->aused - 1);

	x = (acl_entry_obj *)acl_obj_p;
	for (v = vector + acl_obj_p->aused - 1; v != vector - 1; v--) {
		(*v)->enext = x;
		x = *v;
	}
	acl_obj_p->anext = *vector;
	return 0;
}