File: privs.c

package info (click to toggle)
atheme-services 7.2.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,992 kB
  • sloc: ansic: 93,230; sh: 7,440; php: 5,032; perl: 3,327; makefile: 1,261; sed: 16; ruby: 15; python: 3
file content (463 lines) | stat: -rw-r--r-- 9,838 bytes parent folder | download | duplicates (2)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
 * atheme-services: A collection of minimalist IRC services
 * privs.c: Fine grained services operator privileges
 *
 * Copyright (c) 2005-2007 Atheme Project (http://www.atheme.org)
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "atheme.h"
#include "privs.h"

mowgli_list_t operclasslist;
mowgli_list_t soperlist;

mowgli_heap_t *operclass_heap;
mowgli_heap_t *soper_heap;

static operclass_t *user_r = NULL;
static operclass_t *authenticated_r = NULL;
static operclass_t *ircop_r = NULL;

void init_privs(void)
{
	operclass_heap = sharedheap_get(sizeof(operclass_t));
	soper_heap = sharedheap_get(sizeof(soper_t));

	if (!operclass_heap || !soper_heap)
	{
		slog(LG_INFO, "init_privs(): block allocator failed.");
		exit(EXIT_FAILURE);
	}

	/* create built-in operclasses. */
	user_r = operclass_add("user", "", OPERCLASS_BUILTIN);
	authenticated_r = operclass_add("authenticated", AC_AUTHENTICATED, OPERCLASS_BUILTIN);
	ircop_r = operclass_add("ircop", "", OPERCLASS_BUILTIN);
}

/*************************
 * O P E R C L A S S E S *
 *************************/
/*
 * operclass_add(const char *name, const char *privs)
 *
 * Add or override an operclass.
 */
operclass_t *operclass_add(const char *name, const char *privs, int flags)
{
	operclass_t *operclass;
	mowgli_node_t *n;

	operclass = operclass_find(name);

	if (operclass != NULL)
	{
		bool builtin = operclass->flags & OPERCLASS_BUILTIN;

		slog(LG_DEBUG, "operclass_add(): update %s [%s]", name, privs);

		free(operclass->privs);
		operclass->privs = sstrdup(privs);
		operclass->flags = flags | (builtin ? OPERCLASS_BUILTIN : 0);

		return operclass;
	}

	slog(LG_DEBUG, "operclass_add(): create %s [%s]", name, privs);

	operclass = mowgli_heap_alloc(operclass_heap);
	operclass->name = sstrdup(name);
	operclass->privs = sstrdup(privs);
	operclass->flags = flags;

	mowgli_node_add(operclass, &operclass->node, &operclasslist);

	cnt.operclass++;

	MOWGLI_ITER_FOREACH(n, soperlist.head)
	{
		soper_t *soper = n->data;
		if (soper->operclass == NULL &&
				!strcasecmp(name, soper->classname))
			soper->operclass = operclass;
	}

	return operclass;
}

void operclass_delete(operclass_t *operclass)
{
	mowgli_node_t *n;

	if (operclass == NULL)
		return;

	if (operclass->flags & OPERCLASS_BUILTIN)
		return;

	slog(LG_DEBUG, "operclass_delete(): %s", operclass->name);
	mowgli_node_delete(&operclass->node, &operclasslist);

	MOWGLI_ITER_FOREACH(n, soperlist.head)
	{
		soper_t *soper = n->data;
		if (soper->operclass == operclass)
			soper->operclass = NULL;
	}

	free(operclass->name);
	free(operclass->privs);

	mowgli_heap_free(operclass_heap, operclass);
	cnt.operclass--;
}

operclass_t *operclass_find(const char *name)
{
	operclass_t *operclass;
	mowgli_node_t *n;

	MOWGLI_ITER_FOREACH(n, operclasslist.head)
	{
		operclass = (operclass_t *)n->data;

		if (!strcasecmp(operclass->name, name))
			return operclass;
	}

	return NULL;
}

/***************
 * S O P E R S *
 ***************/

soper_t *soper_add(const char *name, const char *classname, int flags, const char *password)
{
	soper_t *soper;
	myuser_t *mu = myuser_find(name);
	mowgli_node_t *n;
	operclass_t *operclass = operclass_find(classname);

	soper = mu ? soper_find(mu) : soper_find_named(name);

	if (soper != NULL)
	{
		if (flags & SOPER_CONF && !(soper->flags & SOPER_CONF))
		{
			slog(LG_INFO, "soper_add(): conf soper %s (%s) is replacing DB soper with class %s", name, classname, soper->classname);
			soper_delete(soper);
		}
		else if (!(flags & SOPER_CONF) && soper->flags & SOPER_CONF)
		{
			slog(LG_INFO, "soper_add(): ignoring DB soper %s (%s) because of conf soper with class %s", name, classname, soper->classname);
			return NULL;
		}
		else
		{
			slog(LG_INFO, "soper_add(): duplicate soper %s", name);
			return NULL;
		}
	}

	slog(LG_DEBUG, "soper_add(): %s -> %s", (mu) ? entity(mu)->name : name, operclass ? operclass->name : "<null>");

	soper = mowgli_heap_alloc(soper_heap);
	n = mowgli_node_create();

	mowgli_node_add(soper, n, &soperlist);

	if (mu)
	{
		soper->myuser = mu;
		mu->soper = soper;
		soper->name = NULL;
	}
	else
	{
		soper->name = sstrdup(name);
		soper->myuser = NULL;
	}

	soper->operclass = operclass;
	soper->classname = sstrdup(classname);
	soper->flags = flags;
	soper->password = sstrdup(password);

	cnt.soper++;

	return soper;
}

void soper_delete(soper_t *soper)
{
	mowgli_node_t *n;

	if (!soper)
	{
		slog(LG_DEBUG, "soper_delete(): called for null soper");

		return;
	}

	slog(LG_DEBUG, "soper_delete(): %s", (soper->myuser) ? entity(soper->myuser)->name : soper->name);

	n = mowgli_node_find(soper, &soperlist);
	mowgli_node_delete(n, &soperlist);
	mowgli_node_free(n);

	if (soper->myuser)
		soper->myuser->soper = NULL;

	if (soper->name)
		free(soper->name);

	free(soper->classname);
	free(soper->password);

	mowgli_heap_free(soper_heap, soper);

	cnt.soper--;
}

soper_t *soper_find(myuser_t *myuser)
{
	soper_t *soper;
	mowgli_node_t *n;

	MOWGLI_ITER_FOREACH(n, soperlist.head)
	{
		soper = (soper_t *)n->data;

		if (soper->myuser && soper->myuser == myuser)
			return soper;
	}

	return NULL;
}

soper_t *soper_find_named(const char *name)
{
	soper_t *soper;
	mowgli_node_t *n;

	MOWGLI_ITER_FOREACH(n, soperlist.head)
	{
		soper = (soper_t *)n->data;

		if (soper->name && !irccasecmp(soper->name, name))
			return soper;
	}

	return NULL;
}

bool is_soper(myuser_t *myuser)
{
	if (!myuser)
		return false;

	if (myuser->soper)
		return true;

	return false;
}

bool is_conf_soper(myuser_t *myuser)
{
	if (!myuser)
		return false;

	if (myuser->soper && myuser->soper->flags & SOPER_CONF)
		return true;

	return false;
}

/* name1 name2 name3... */
static bool string_in_list(const char *str, const char *name)
{
	char *p;
	int l;

	if (str == NULL)
		return false;
	l = strlen(name);
	while (*str != '\0')
	{
		p = strchr(str, ' ');
		if (p != NULL ? p - str == l && !strncasecmp(str, name, p - str) : !strcasecmp(str, name))
			return true;
		if (p == NULL)
			return false;
		str = p;
		while (*str == ' ')
			str++;
	}
	return false;
}

bool has_priv_operclass(operclass_t *operclass, const char *priv)
{
	if (operclass == NULL)
		return false;
	if (string_in_list(operclass->privs, priv))
		return true;
	return false;
}

bool has_any_privs(sourceinfo_t *si)
{
	if (si->su != NULL && is_ircop(si->su))
		return true;
	if (si->smu && is_soper(si->smu))
		return true;
	return false;
}

bool has_any_privs_user(user_t *u)
{
	if (u == NULL)
		return false;
	if (is_ircop(u))
		return true;
	if (u->myuser && is_soper(u->myuser))
		return true;
	return false;
}

bool has_priv(sourceinfo_t *si, const char *priv)
{
	return si->su != NULL ? has_priv_user(si->su, priv) :
		has_priv_myuser(si->smu, priv);
}

bool has_priv_user(user_t *u, const char *priv)
{
	operclass_t *operclass;

	if (priv == NULL)
		return true;

	if (u == NULL)
		return false;

	if (has_priv_operclass(user_r, priv))
		return true;

	if (is_ircop(u) && has_priv_operclass(ircop_r, priv))
		return true;

	if (u->myuser != NULL && has_priv_operclass(authenticated_r, priv))
		return true;

	if (u->myuser && is_soper(u->myuser))
	{
		operclass = u->myuser->soper->operclass;
		if (operclass == NULL)
			return false;
		if (operclass->flags & OPERCLASS_NEEDOPER && !is_ircop(u))
			return false;
		if (u->myuser->soper->password != NULL && !(u->flags & UF_SOPER_PASS))
			return false;
		if (has_priv_operclass(operclass, priv))
			return true;
	}

	return false;
}

bool has_priv_myuser(myuser_t *mu, const char *priv)
{
	operclass_t *operclass;

	if (priv == NULL)
		return true;
	if (mu == NULL)
		return false;

	if (has_priv_operclass(authenticated_r, priv))
		return true;

	if (!is_soper(mu))
		return false;
	operclass = mu->soper->operclass;
	if (operclass == NULL)
		return false;
	if (has_priv_operclass(operclass, priv))
		return true;

	return false;
}

bool has_all_operclass(sourceinfo_t *si, operclass_t *operclass)
{
	char *privs2;
	char *priv;

	privs2 = sstrdup(operclass->privs);
	priv = strtok(privs2, " ");
	while (priv != NULL)
	{
		if (!has_priv(si, priv))
		{
			free(privs2);
			return false;
		}
		priv = strtok(NULL, " ");
	}
	free(privs2);
	return true;
}

/**********************************************************************************/

const soper_t *get_sourceinfo_soper(sourceinfo_t *si)
{
	if (si->smu != NULL && is_soper(si->smu))
		return si->smu->soper;

	return NULL;
}

const operclass_t *get_sourceinfo_operclass(sourceinfo_t *si)
{
	operclass_t *out = NULL;
	const soper_t *soper;

	if ((soper = get_sourceinfo_soper(si)) != NULL)
		return soper->operclass;

	if (si->su != NULL && is_ircop(si->su))
	{
		if ((out = operclass_find("ircop")) != NULL)
			return out;
	}

	if ((out = operclass_find("user")) != NULL)
		return out;

	return NULL;
}

/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
 * vim:ts=8
 * vim:sw=8
 * vim:noexpandtab
 */