]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8530dc44 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2014 Lennart Poettering | |
8530dc44 LP |
6 | ***/ |
7 | ||
a8fbdf54 TA |
8 | #include <errno.h> |
9 | #include <stdlib.h> | |
10 | #include <string.h> | |
11 | ||
62d74c78 | 12 | #include "alloc-util.h" |
a8fbdf54 | 13 | #include "macro.h" |
8530dc44 | 14 | #include "uid-range.h" |
b1d4f8e1 | 15 | #include "user-util.h" |
8530dc44 LP |
16 | |
17 | static bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) { | |
18 | assert(range); | |
19 | ||
20 | return range->start <= start + nr && | |
21 | range->start + range->nr >= start; | |
22 | } | |
23 | ||
24 | static void uid_range_coalesce(UidRange **p, unsigned *n) { | |
25 | unsigned i, j; | |
26 | ||
27 | assert(p); | |
28 | assert(n); | |
29 | ||
30 | for (i = 0; i < *n; i++) { | |
31 | for (j = i + 1; j < *n; j++) { | |
32 | UidRange *x = (*p)+i, *y = (*p)+j; | |
33 | ||
34 | if (uid_range_intersect(x, y->start, y->nr)) { | |
35 | uid_t begin, end; | |
36 | ||
37 | begin = MIN(x->start, y->start); | |
38 | end = MAX(x->start + x->nr, y->start + y->nr); | |
39 | ||
40 | x->start = begin; | |
41 | x->nr = end - begin; | |
42 | ||
43 | if (*n > j+1) | |
44 | memmove(y, y+1, sizeof(UidRange) * (*n - j -1)); | |
45 | ||
313cefa1 | 46 | (*n)--; |
8530dc44 LP |
47 | j--; |
48 | } | |
49 | } | |
50 | } | |
8530dc44 LP |
51 | } |
52 | ||
53 | static int uid_range_compare(const void *a, const void *b) { | |
54 | const UidRange *x = a, *y = b; | |
55 | ||
56 | if (x->start < y->start) | |
57 | return -1; | |
58 | if (x->start > y->start) | |
59 | return 1; | |
60 | ||
61 | if (x->nr < y->nr) | |
62 | return -1; | |
63 | if (x->nr > y->nr) | |
64 | return 1; | |
65 | ||
66 | return 0; | |
67 | } | |
68 | ||
69 | int uid_range_add(UidRange **p, unsigned *n, uid_t start, uid_t nr) { | |
70 | bool found = false; | |
71 | UidRange *x; | |
72 | unsigned i; | |
73 | ||
74 | assert(p); | |
75 | assert(n); | |
76 | ||
77 | if (nr <= 0) | |
78 | return 0; | |
79 | ||
80 | for (i = 0; i < *n; i++) { | |
81 | x = (*p) + i; | |
82 | if (uid_range_intersect(x, start, nr)) { | |
83 | found = true; | |
84 | break; | |
85 | } | |
86 | } | |
87 | ||
88 | if (found) { | |
89 | uid_t begin, end; | |
90 | ||
91 | begin = MIN(x->start, start); | |
92 | end = MAX(x->start + x->nr, start + nr); | |
93 | ||
94 | x->start = begin; | |
95 | x->nr = end - begin; | |
96 | } else { | |
97 | UidRange *t; | |
98 | ||
62d74c78 | 99 | t = reallocarray(*p, *n + 1, sizeof(UidRange)); |
8530dc44 LP |
100 | if (!t) |
101 | return -ENOMEM; | |
102 | ||
103 | *p = t; | |
104 | x = t + ((*n) ++); | |
105 | ||
106 | x->start = start; | |
107 | x->nr = nr; | |
108 | } | |
109 | ||
110 | qsort(*p, *n, sizeof(UidRange), uid_range_compare); | |
111 | uid_range_coalesce(p, n); | |
112 | ||
113 | return *n; | |
114 | } | |
115 | ||
116 | int uid_range_add_str(UidRange **p, unsigned *n, const char *s) { | |
117 | uid_t start, nr; | |
118 | const char *t; | |
119 | int r; | |
120 | ||
121 | assert(p); | |
122 | assert(n); | |
123 | assert(s); | |
124 | ||
125 | t = strchr(s, '-'); | |
126 | if (t) { | |
127 | char *b; | |
128 | uid_t end; | |
129 | ||
130 | b = strndupa(s, t - s); | |
131 | r = parse_uid(b, &start); | |
132 | if (r < 0) | |
133 | return r; | |
134 | ||
135 | r = parse_uid(t+1, &end); | |
136 | if (r < 0) | |
137 | return r; | |
138 | ||
139 | if (end < start) | |
140 | return -EINVAL; | |
141 | ||
142 | nr = end - start + 1; | |
143 | } else { | |
144 | r = parse_uid(s, &start); | |
145 | if (r < 0) | |
146 | return r; | |
147 | ||
148 | nr = 1; | |
149 | } | |
150 | ||
151 | return uid_range_add(p, n, start, nr); | |
152 | } | |
153 | ||
154 | int uid_range_next_lower(const UidRange *p, unsigned n, uid_t *uid) { | |
fed1e721 | 155 | uid_t closest = UID_INVALID, candidate; |
8530dc44 LP |
156 | unsigned i; |
157 | ||
158 | assert(p); | |
159 | assert(uid); | |
160 | ||
161 | candidate = *uid - 1; | |
162 | ||
163 | for (i = 0; i < n; i++) { | |
164 | uid_t begin, end; | |
165 | ||
166 | begin = p[i].start; | |
167 | end = p[i].start + p[i].nr - 1; | |
168 | ||
169 | if (candidate >= begin && candidate <= end) { | |
170 | *uid = candidate; | |
171 | return 1; | |
172 | } | |
173 | ||
174 | if (end < candidate) | |
175 | closest = end; | |
176 | } | |
177 | ||
fed1e721 | 178 | if (closest == UID_INVALID) |
8530dc44 LP |
179 | return -EBUSY; |
180 | ||
181 | *uid = closest; | |
182 | return 1; | |
183 | } | |
184 | ||
185 | bool uid_range_contains(const UidRange *p, unsigned n, uid_t uid) { | |
186 | unsigned i; | |
187 | ||
188 | assert(p); | |
189 | assert(uid); | |
190 | ||
191 | for (i = 0; i < n; i++) | |
192 | if (uid >= p[i].start && uid < p[i].start + p[i].nr) | |
193 | return true; | |
194 | ||
195 | return false; | |
196 | } |