]> jfr.im git - irc/quakenet/newserv.git/blame - glines/glines.c
Add missing "Done." reply.
[irc/quakenet/newserv.git] / glines / glines.c
CommitLineData
8f128e0d 1#include <stdio.h>
a44fc5f7
GB
2#include <string.h>
3#include <assert.h>
a473a1be 4#include "../lib/irc_string.h"
c4610da5 5#include "../lib/version.h"
580103bc 6#include "../core/schedule.h"
813c5b73 7#include "../irc/irc.h"
813c5b73 8#include "../trusts/trusts.h"
a473a1be 9#include "../control/control.h"
813c5b73
CP
10#include "glines.h"
11
c4610da5
GB
12MODULE_VERSION("");
13
580103bc
GB
14static void glines_sched_save(void *arg);
15
a44fc5f7
GB
16void _init() {
17 /* If we're connected to IRC, force a disconnect. */
18 if (connected) {
c684f472
GB
19 irc_send("%s SQ %s 0 :Resync [adding gline support]", mynumeric->content, myserver->content);
20 irc_disconnected();
a44fc5f7
GB
21 }
22
23 registerserverhandler("GL", handleglinemsg, 6);
24 registerhook(HOOK_CORE_STATSREQUEST, handleglinestats);
580103bc
GB
25
26 schedulerecurring(time(NULL), 0, GLSTORE_SAVE_INTERVAL, &glines_sched_save, NULL);
27
28 glstore_load();
a44fc5f7
GB
29}
30
31void _fini() {
32 deregisterserverhandler("GL", handleglinemsg);
33 deregisterhook(HOOK_CORE_STATSREQUEST, handleglinestats);
580103bc
GB
34
35 deleteschedule(NULL, glines_sched_save, NULL);
36}
37
38static void glines_sched_save(void *arg) {
39 glstore_save();
a44fc5f7
GB
40}
41
c684f472
GB
42int gline_match_nick(gline *gl, nick *np) {
43 if (gl->flags & GLINE_BADCHAN)
44 return 0;
45
46 if (gl->flags & GLINE_REALNAME) {
96662a86 47 if (gl->user && match(gl->user->content, np->realname->name->content) != 0)
c684f472
GB
48 return 0;
49
50 return 1;
51 }
52
53 if (gl->nick && match(gl->nick->content, np->nick) != 0)
54 return 0;
55
56 if (gl->user && match(gl->user->content, np->ident) != 0)
57 return 0;
58
59 if (gl->flags & GLINE_IPMASK) {
60 if (!ipmask_check(&gl->ip, &np->p_ipaddr, gl->bits))
61 return 0;
62 } else {
63 if (gl->host && match(gl->host->content, np->host->name->content) != 0)
64 return 0;
65 }
66
67 return 1;
68}
69
70int gline_match_channel(gline *gl, channel *cp) {
71 if (!(gl->flags & GLINE_BADCHAN))
72 return 0;
73
74 if (match(gl->user->content, cp->index->name->content) != 0)
75 return 0;
76
77 return 1;
78}
79
96662a86 80gline *findgline(const char *mask) {
c684f472 81 gline *gl, *next;
a44fc5f7
GB
82 gline *globalgline;
83 time_t curtime = time(0);
84
c684f472
GB
85 globalgline = makegline(mask);
86
87 if (!globalgline)
88 return NULL; /* gline mask couldn't be processed */
a44fc5f7 89
c684f472
GB
90 for (gl = glinelist; gl; gl = next) {
91 next = gl->next;
a44fc5f7
GB
92
93 if (gl->lifetime <= curtime) {
94 removegline(gl);
95 continue;
96 } else if (gl->expire <= curtime) {
97 gl->flags &= ~GLINE_ACTIVE;
98 }
99
100 if (glineequal(globalgline, gl)) {
101 freegline(globalgline);
102 return gl;
103 }
104 }
105
106 freegline(globalgline);
107 return 0;
108}
109
331ecd41 110void gline_activate(gline *agline, time_t lastmod, int propagate) {
a44fc5f7 111 time_t now = getnettime();
20447d72 112
a44fc5f7
GB
113 agline->flags |= GLINE_ACTIVE;
114
331ecd41 115 if (lastmod)
a44fc5f7 116 agline->lastmod = lastmod;
331ecd41
GB
117 else if (now <= agline->lastmod)
118 agline->lastmod++;
119 else
120 agline->lastmod = now;
a44fc5f7
GB
121
122 if (propagate)
123 gline_propagate(agline);
a44fc5f7
GB
124}
125
331ecd41 126void gline_deactivate(gline *agline, time_t lastmod, int propagate) {
a44fc5f7 127 time_t now = getnettime();
20447d72
GB
128
129 agline->flags &= ~GLINE_ACTIVE;
130
131 if (lastmod)
132 agline->lastmod = lastmod;
133 else if (now <= agline->lastmod)
134 agline->lastmod++;
135 else
136 agline->lastmod = now;
137
138 if (propagate)
139 gline_propagate(agline);
140}
141
142void gline_destroy(gline *agline, time_t lastmod, int propagate) {
143 time_t now = getnettime();
144
a44fc5f7 145 agline->flags &= ~GLINE_ACTIVE;
20447d72 146 agline->flags |= GLINE_DESTROYED;
a44fc5f7 147
331ecd41 148 if (lastmod)
a44fc5f7 149 agline->lastmod = lastmod;
331ecd41
GB
150 else if (now <= agline->lastmod)
151 agline->lastmod++;
152 else
153 agline->lastmod = now;
a44fc5f7
GB
154
155 if (propagate)
156 gline_propagate(agline);
20447d72
GB
157
158 removegline(agline);
a44fc5f7
GB
159}
160
161void gline_propagate(gline *agline) {
20447d72
GB
162 if (agline->flags & GLINE_DESTROYED) {
163 controlwall(NO_OPER, NL_GLINES, "Destroying G-Line on '%s' lasting %s with reason '%s', created by: %s",
164 glinetostring(agline), longtoduration(agline->expire-getnettime(), 0),
165 agline->reason->content, agline->creator->content);
166
167#if 1
168 irc_send("%s GL * %%-%s %lu %lu :%s\r\n", mynumeric->content,
169 glinetostring(agline), agline->expire - getnettime(),
170 agline->lastmod, agline->reason->content);
171#else
172 controlwall(NO_OPER, NL_GLINES, "%s GL * %%-%s %lu %lu :%s\r\n", mynumeric->content,
173 glinetostring(agline), agline->expire - getnettime(),
174 agline->lastmod, agline->reason->content);
175#endif
176 } else if (agline->flags & GLINE_ACTIVE) {
5649ec17 177 controlwall(NO_OPER, NL_GLINES, "Activating G-Line on '%s' lasting %s with reason '%s', created by: %s",
c684f472
GB
178 glinetostring(agline), longtoduration(agline->expire-getnettime(), 0),
179 agline->reason->content, agline->creator->content);
5649ec17 180
cb581522 181#if 1
c684f472
GB
182 irc_send("%s GL * +%s %lu %lu :%s\r\n", mynumeric->content,
183 glinetostring(agline), agline->expire - getnettime(),
184 agline->lastmod, agline->reason->content);
cb581522 185#else
5649ec17
GB
186 controlwall(NO_OPER, NL_GLINES, "%s GL * +%s %lu %lu :%s\r\n", mynumeric->content,
187 glinetostring(agline), agline->expire - getnettime(),
188 agline->lastmod, agline->reason->content);
cb581522 189#endif
a44fc5f7 190 } else {
5649ec17
GB
191 controlwall(NO_OPER, NL_GLINES, "Deactivating G-Line on '%s'",
192 glinetostring(agline));
193
cb581522 194#if 1
c684f472
GB
195 irc_send("%s GL * -%s %lu %lu :%s\r\n", mynumeric->content,
196 glinetostring(agline), agline->expire - getnettime(),
197 agline->lastmod, agline->reason->content);
cb581522 198#else
5649ec17
GB
199 controlwall(NO_OPER, NL_GLINES, "%s GL * -%s %lu %lu :%s\r\n", mynumeric->content,
200 glinetostring(agline), agline->expire - getnettime(),
201 agline->lastmod, agline->reason->content);
cb581522 202#endif
a44fc5f7
GB
203 }
204}
205
206/* returns non-zero if the glines are exactly the same */
207int glineequal(gline *gla, gline *glb) {
c684f472 208 if ((gla->flags & GLINE_BADCHAN) != (glb->flags & GLINE_BADCHAN))
a44fc5f7
GB
209 return 0;
210
c684f472 211 if ((gla->flags & GLINE_REALNAME) != (glb->flags & GLINE_REALNAME))
a44fc5f7
GB
212 return 0;
213
c684f472
GB
214 if ((gla->flags & GLINE_IPMASK) != (glb->flags & GLINE_IPMASK))
215 return 0;
216
217 if ((!gla->nick && glb->nick) || (gla->nick && !glb->nick))
a44fc5f7
GB
218 return 0;
219
c684f472 220 if (gla->nick && ircd_strcmp(gla->nick->content, glb->nick->content) != 0)
a44fc5f7
GB
221 return 0;
222
c684f472 223 if ((!gla->user && glb->user) || (gla->user && !glb->user))
a44fc5f7
GB
224 return 0;
225
c684f472 226 if (gla->user && ircd_strcmp(gla->user->content, glb->user->content) != 0)
a44fc5f7
GB
227 return 0;
228
c684f472
GB
229 if (gla->flags & GLINE_IPMASK) {
230 if (gla->bits != glb->bits)
231 return 0;
232
233 if (!ipmask_check(&gla->ip, &glb->ip, gla->bits))
234 return 0;
235 } else {
236 if ((!gla->host && glb->host) || (gla->host && !glb->host))
237 return 0;
238
239 if (gla->host && ircd_strcmp(gla->host->content, glb->host->content) != 0)
240 return 0;
241 }
242
a44fc5f7
GB
243 return 1;
244}
245
246/* returns non-zero on match */
c684f472
GB
247int gline_match_mask(gline *gla, gline *glb) {
248 if ((gla->flags & GLINE_BADCHAN) != (glb->flags & GLINE_BADCHAN))
a44fc5f7
GB
249 return 0;
250
c684f472 251 if ((gla->flags & GLINE_REALNAME) != (glb->flags & GLINE_REALNAME))
a44fc5f7
GB
252 return 0;
253
c684f472 254 if ((gla->flags & GLINE_IPMASK) != (glb->flags & GLINE_IPMASK))
a44fc5f7
GB
255 return 0;
256
c684f472 257 if (gla->nick && !glb->nick)
a44fc5f7
GB
258 return 0;
259
c684f472 260 if (gla->nick && glb->nick && match(gla->nick->content, glb->nick->content) != 0)
a44fc5f7
GB
261 return 0;
262
c684f472 263 if (gla->user && !glb->user)
a44fc5f7
GB
264 return 0;
265
c684f472
GB
266 if (gla->user && glb->user && match(gla->user->content, glb->user->content) != 0)
267 return 0;
a44fc5f7 268
c684f472
GB
269 if (gla->flags & GLINE_IPMASK) {
270 if (gla->bits > glb->bits)
271 return 0;
a44fc5f7 272
c684f472
GB
273 if (!ipmask_check(&gla->ip, &glb->ip, gla->bits))
274 return 0;
275 } else {
276 if (gla->host && !glb->host)
277 return 0;
278
279 if (gla->host && glb->host && match(gla->host->content, glb->host->content) != 0)
280 return 0;
281 }
282
283 return 1;
a44fc5f7
GB
284}
285
a86fc0c4
GB
286int isglinesane(gline *gl, const char **hint) {
287 int wildcard, nowildcardcount;
288 char *pos;
289 trusthost *th;
290
291 /* Hits all realnames. */
292 if ((gl->flags & GLINE_REALNAME) && !gl->user) {
293 *hint = "Matches all realnames.";
294 return 0;
295 }
296
297 /* Hits all local/non-local channels. */
298 if ((gl->flags & GLINE_BADCHAN) && (strcmp(gl->user->content, "&*") == 0 || strcmp(gl->user->content, "#*") == 0)) {
299 *hint = "Matches all local/non-local channels.";
300 return 0;
301 }
302
521d4392
GB
303 /* Skip the other checks for nickname glines. */
304 if (gl->nick)
305 return 1;
306
a86fc0c4
GB
307 /* Mask wider than /16 for IPv4 or /32 for IPv6. */
308 if ((gl->flags & GLINE_IPMASK) && gl->bits < (irc_in_addr_is_ipv4(&gl->ip) ? (96 + 16) : 32)) {
309 *hint = "CIDR mask too wide.";
310 return 0;
311 }
312
313 /* Doesn't have at least two non-wildcarded host components. */
314 if (gl->flags & GLINE_HOSTMASK) {
315 nowildcardcount = 0;
316
317 wildcard = 0;
318 pos = gl->host->content;
319
320 for (;;) {
321 switch (*pos) {
322 case '*':
323 /* fall through */
324 case '?':
325 wildcard = 1;
326 break;
327
328 case '.':
329 case '\0':
330 if (!wildcard)
331 nowildcardcount++;
332 else
333 wildcard = 0;
334
335 if (*pos == '\0')
336 pos = NULL; /* Leave the loop. */
337
338 break;
339 }
340
341 if (pos)
342 pos++;
343 else
344 break;
345 }
346
347 if (nowildcardcount < 2) {
348 *hint = "Needs at least 2 non-wildcarded host components.";
349 return 0;
350 }
351 }
352
353 /* Wildcard username match for trusted host with reliable usernames. */
354 if (gl->flags & GLINE_IPMASK && (!gl->user || strchr(gl->user->content, '*') || strchr(gl->user->content, '?'))) {
355 th = th_getbyhost(&gl->ip);
356
357 if (th && (th->group->flags & TRUST_RELIABLE_USERNAME)) {
358 *hint = "Wildcard username match for a trusted host that has reliable usernames.";
359 return 0;
360 }
361 }
362
363 return 1;
364}