]> jfr.im git - solanum.git/blob - ircd/privilege.c
Use bsearch to check privset membership
[solanum.git] / ircd / privilege.c
1 /*
2 * Solanum: a slightly advanced ircd
3 * privilege.c: Dynamic privileges API.
4 *
5 * Copyright (c) 2021 Ed Kellett <e@kellett.im>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * Copyright (c) 2008 William Pitcock <nenolod@dereferenced.org>
23 *
24 * Permission to use, copy, modify, and/or distribute this software for any
25 * purpose with or without fee is hereby granted, provided that the above
26 * copyright notice and this permission notice is present in all copies.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
32 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include <stdinc.h>
42 #include "s_conf.h"
43 #include "privilege.h"
44 #include "numeric.h"
45 #include "s_assert.h"
46 #include "logger.h"
47 #include "send.h"
48
49 static rb_dlink_list privilegeset_list = {NULL, NULL, 0};
50
51 static struct PrivilegeSet *
52 privilegeset_get_any(const char *name)
53 {
54 rb_dlink_node *iter;
55
56 s_assert(name != NULL);
57
58 RB_DLINK_FOREACH(iter, privilegeset_list.head)
59 {
60 struct PrivilegeSet *set = (struct PrivilegeSet *) iter->data;
61
62 if (!rb_strcasecmp(set->name, name))
63 return set;
64 }
65
66 return NULL;
67 }
68
69 static int
70 privilegeset_cmp_priv(const void *a_, const void *b_)
71 {
72 const char *const *a = a_, *const *b = b_;
73 return strcmp(*a, *b);
74 }
75
76 static void
77 privilegeset_index(struct PrivilegeSet *set)
78 {
79 size_t n;
80 const char *s;
81 const char **p;
82
83 rb_free(set->privs);
84
85 set->privs = rb_malloc(sizeof *set->privs * (set->size + 1));
86 p = set->privs;
87
88 for (n = 0, s = set->priv_storage; n < set->size; n++, s += strlen(s) + 1)
89 *p++ = s;
90 qsort(set->privs, set->size, sizeof *set->privs, privilegeset_cmp_priv);
91 set->privs[set->size] = NULL;
92 }
93
94 static void
95 privilegeset_add_privs(struct PrivilegeSet *dst, const char *privs)
96 {
97 size_t alloc_size;
98 size_t n;
99
100 if (dst->priv_storage == NULL)
101 {
102 dst->stored_size = dst->allocated_size = 0;
103 alloc_size = 256;
104 }
105 else
106 {
107 alloc_size = dst->allocated_size;
108 }
109
110 dst->stored_size += strlen(privs) + 1;
111
112 while (alloc_size < dst->stored_size)
113 alloc_size *= 2;
114
115 if (alloc_size > dst->allocated_size)
116 dst->priv_storage = rb_realloc(dst->priv_storage, alloc_size);
117
118 dst->allocated_size = alloc_size;
119
120 const char *s;
121 char *d;
122 for (s = privs, d = dst->priv_storage; s < privs + strlen(privs); s += n , d += n)
123 {
124 const char *e = strchr(s, ' ');
125 /* up to space if there is one, else up to end of string */
126 n = 1 + (e != NULL ? e - s : strlen(s));
127 rb_strlcpy(d, s, n);
128
129 dst->size += 1;
130 }
131
132 privilegeset_index(dst);
133 }
134
135 static void
136 privilegeset_add_privilegeset(struct PrivilegeSet *dst, const struct PrivilegeSet *src)
137 {
138 size_t cur_size, alloc_size;
139
140 if (dst->priv_storage == NULL)
141 {
142 dst->stored_size = dst->allocated_size = 0;
143 cur_size = 0;
144 alloc_size = 256;
145 }
146 else
147 {
148 cur_size = dst->stored_size;
149 alloc_size = dst->allocated_size;
150 }
151
152 dst->stored_size = cur_size + src->stored_size;
153
154 while (alloc_size < dst->stored_size)
155 alloc_size *= 2;
156
157 if (alloc_size > dst->allocated_size)
158 dst->priv_storage = rb_realloc(dst->priv_storage, alloc_size);
159
160 dst->allocated_size = alloc_size;
161
162 memcpy(dst->priv_storage + cur_size, src->priv_storage, src->stored_size);
163 dst->size += src->size;
164
165 privilegeset_index(dst);
166 }
167
168 static struct PrivilegeSet *
169 privilegeset_new_orphan(const char *name)
170 {
171 struct PrivilegeSet *set;
172 set = rb_malloc(sizeof *set);
173 *set = (struct PrivilegeSet) {
174 .size = 0,
175 .privs = NULL,
176 .priv_storage = NULL,
177 .shadow = NULL,
178 .status = 0,
179 .refs = 0,
180 .name = rb_strdup(name),
181 };
182 return set;
183 }
184
185 static void
186 privilegeset_free(struct PrivilegeSet *set)
187 {
188 if (set == NULL)
189 return;
190
191 privilegeset_free(set->shadow);
192 rb_free(set->name);
193 rb_free(set->privs);
194 rb_free(set->priv_storage);
195 rb_free(set);
196 }
197
198 static void
199 privilegeset_shade(struct PrivilegeSet *set)
200 {
201 privilegeset_free(set->shadow);
202
203 set->shadow = privilegeset_new_orphan(set->name);
204 set->shadow->privs = set->privs;
205 set->shadow->size = set->size;
206 set->shadow->priv_storage = set->priv_storage;
207 set->shadow->stored_size = set->stored_size;
208 set->shadow->allocated_size = set->allocated_size;
209
210 set->privs = NULL;
211 set->size = 0;
212 set->priv_storage = NULL;
213 set->stored_size = 0;
214 set->allocated_size = 0;
215 }
216
217 static void
218 privilegeset_clear(struct PrivilegeSet *set)
219 {
220 rb_free(set->privs);
221 set->privs = NULL;
222 set->size = 0;
223 set->stored_size = 0;
224 }
225
226 bool
227 privilegeset_in_set(const struct PrivilegeSet *set, const char *priv)
228 {
229 s_assert(set != NULL);
230 s_assert(priv != NULL);
231
232 const char **found = bsearch(&priv, set->privs, set->size, sizeof *set->privs, privilegeset_cmp_priv);
233 return found != NULL;
234 }
235
236 struct PrivilegeSet *
237 privilegeset_set_new(const char *name, const char *privs, PrivilegeFlags flags)
238 {
239 struct PrivilegeSet *set;
240
241 set = privilegeset_get_any(name);
242 if (set != NULL)
243 {
244 if (!(set->status & CONF_ILLEGAL))
245 ilog(L_MAIN, "Duplicate privset %s", name);
246 set->status &= ~CONF_ILLEGAL;
247 privilegeset_clear(set);
248 }
249 else
250 {
251 set = privilegeset_new_orphan(name);
252 rb_dlinkAdd(set, &set->node, &privilegeset_list);
253 }
254 privilegeset_add_privs(set, privs);
255 set->flags = flags;
256
257 return set;
258 }
259
260 struct PrivilegeSet *
261 privilegeset_extend(const struct PrivilegeSet *parent, const char *name, const char *privs, PrivilegeFlags flags)
262 {
263 struct PrivilegeSet *set;
264
265 s_assert(parent != NULL);
266 s_assert(name != NULL);
267 s_assert(privs != NULL);
268
269 set = privilegeset_set_new(name, privs, flags);
270 privilegeset_add_privilegeset(set, parent);
271 set->flags = flags;
272
273 return set;
274 }
275
276 struct PrivilegeSet *
277 privilegeset_get(const char *name)
278 {
279 struct PrivilegeSet *set;
280
281 set = privilegeset_get_any(name);
282 if (set != NULL && set->status & CONF_ILLEGAL)
283 set = NULL;
284 return set;
285 }
286
287 struct PrivilegeSet *
288 privilegeset_ref(struct PrivilegeSet *set)
289 {
290 s_assert(set != NULL);
291
292 set->refs++;
293
294 return set;
295 }
296
297 void
298 privilegeset_unref(struct PrivilegeSet *set)
299 {
300 s_assert(set != NULL);
301
302 if (set->refs > 0)
303 set->refs--;
304 else
305 ilog(L_MAIN, "refs on privset %s is already 0",
306 set->name);
307 if (set->refs == 0 && set->status & CONF_ILLEGAL)
308 {
309 rb_dlinkDelete(&set->node, &privilegeset_list);
310
311 privilegeset_free(set);
312 }
313 }
314
315 const struct PrivilegeSet **
316 privilegeset_diff(const struct PrivilegeSet *old, const struct PrivilegeSet *new)
317 {
318 static const char *no_privs[] = { NULL };
319 static const struct PrivilegeSet empty = { .size = 0, .privs = no_privs };
320 static struct PrivilegeSet *set_unchanged = NULL,
321 *set_added = NULL,
322 *set_removed = NULL;
323 static const struct PrivilegeSet *result_sets[3];
324 static size_t n_privs = 0;
325 size_t new_size = n_privs ? n_privs : 32;
326 size_t i = 0, j = 0;
327
328 if (result_sets[0] == NULL)
329 {
330 result_sets[0] = set_unchanged = privilegeset_new_orphan("<unchanged>");
331 result_sets[1] = set_added = privilegeset_new_orphan("<added>");
332 result_sets[2] = set_removed = privilegeset_new_orphan("<removed>");
333 }
334
335 if (old == NULL)
336 old = &empty;
337 if (new == NULL)
338 new = &empty;
339
340 while (new_size < MAX(old->size, new->size) + 1)
341 new_size *= 2;
342
343 if (new_size > n_privs)
344 {
345 set_unchanged->privs = rb_realloc(set_unchanged->privs, sizeof *set_unchanged->privs * new_size);
346 set_added->privs = rb_realloc(set_added->privs, sizeof *set_added->privs * new_size);
347 set_removed->privs = rb_realloc(set_removed->privs, sizeof *set_removed->privs * new_size);
348 }
349
350 const char **res_unchanged = set_unchanged->privs;
351 const char **res_added = set_added->privs;
352 const char **res_removed = set_removed->privs;
353
354 while (i < old->size || j < new->size)
355 {
356 const char *oldpriv = NULL, *newpriv = NULL;
357 int ord = 0;
358 if (i < old->size)
359 oldpriv = old->privs[i];
360 if (j < new->size)
361 newpriv = new->privs[j];
362
363 if (oldpriv && newpriv)
364 ord = strcmp(oldpriv, newpriv);
365
366 if (newpriv == NULL || ord < 0)
367 {
368 *res_removed++ = oldpriv;
369 i++;
370 }
371 else if (oldpriv == NULL || ord > 0)
372 {
373 *res_added++ = newpriv;
374 j++;
375 }
376 else
377 {
378 *res_unchanged++ = oldpriv;
379 i++; j++;
380 }
381 }
382
383 *res_removed = *res_added = *res_unchanged = NULL;
384 set_unchanged->size = res_unchanged - set_unchanged->privs;
385 set_added->size = res_added - set_added->privs;
386 set_removed->size = res_removed - set_removed->privs;
387
388 return result_sets;
389 }
390
391 void
392 privilegeset_prepare_rehash()
393 {
394 rb_dlink_node *iter;
395
396 RB_DLINK_FOREACH(iter, privilegeset_list.head)
397 {
398 struct PrivilegeSet *set = iter->data;
399
400 /* the "default" privset is special and must remain available */
401 if (!strcmp(set->name, "default"))
402 continue;
403
404 set->status |= CONF_ILLEGAL;
405 privilegeset_shade(set);
406 }
407 }
408
409 void
410 privilegeset_cleanup_rehash()
411 {
412 rb_dlink_node *iter, *next;
413
414 RB_DLINK_FOREACH_SAFE(iter, next, privilegeset_list.head)
415 {
416 struct PrivilegeSet *set = iter->data;
417
418 if (set->shadow)
419 {
420 privilegeset_free(set->shadow);
421 set->shadow = NULL;
422 }
423
424 privilegeset_ref(set);
425 privilegeset_unref(set);
426 }
427 }
428
429 void
430 privilegeset_report(struct Client *source_p)
431 {
432 rb_dlink_node *ptr;
433
434 RB_DLINK_FOREACH(ptr, privilegeset_list.head)
435 {
436 struct PrivilegeSet *set = ptr->data;
437
438 /* use RPL_STATSDEBUG for now -- jilles */
439 send_multiline_init(source_p, " ", ":%s %03d %s O :%s ",
440 get_id(&me, source_p),
441 RPL_STATSDEBUG,
442 get_id(source_p, source_p),
443 set->name);
444 send_multiline_remote_pad(source_p, &me);
445 send_multiline_remote_pad(source_p, source_p);
446 for (const char **s = set->privs; s && *s; s++)
447 send_multiline_item(source_p, "%s", *s);
448 send_multiline_fini(source_p, NULL);
449 }
450 }