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