]> jfr.im git - irc/quakenet/newserv.git/blame - glines/glines.c
Reject invalid IPv6 gline masks.
[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
a44fc5f7
GB
110gline *gline_activate(gline *agline, time_t lastmod, int propagate) {
111 time_t now = getnettime();
112 agline->flags |= GLINE_ACTIVE;
113
114 if (lastmod) {
115 agline->lastmod = lastmod;
116 } else {
c684f472 117 if (now <= agline->lastmod)
a44fc5f7
GB
118 agline->lastmod++;
119 else
c684f472 120 agline->lastmod = now;
a44fc5f7
GB
121 }
122
123 if (propagate)
124 gline_propagate(agline);
125
126 return agline;
127}
128
129gline *gline_deactivate(gline *agline, time_t lastmod, int propagate) {
130 time_t now = getnettime();
131 agline->flags &= ~GLINE_ACTIVE;
132
133 if (lastmod) {
134 agline->lastmod = lastmod;
135 } else {
c684f472 136 if (now <= agline->lastmod)
a44fc5f7
GB
137 agline->lastmod++;
138 else
c684f472 139 agline->lastmod = now;
a44fc5f7
GB
140 }
141
142 if (propagate)
143 gline_propagate(agline);
144
145 return agline;
146}
147
148void gline_propagate(gline *agline) {
149 if (agline->flags & GLINE_ACTIVE) {
5649ec17 150 controlwall(NO_OPER, NL_GLINES, "Activating G-Line on '%s' lasting %s with reason '%s', created by: %s",
c684f472
GB
151 glinetostring(agline), longtoduration(agline->expire-getnettime(), 0),
152 agline->reason->content, agline->creator->content);
5649ec17 153
cb581522 154#if 1
c684f472
GB
155 irc_send("%s GL * +%s %lu %lu :%s\r\n", mynumeric->content,
156 glinetostring(agline), agline->expire - getnettime(),
157 agline->lastmod, agline->reason->content);
cb581522 158#else
5649ec17
GB
159 controlwall(NO_OPER, NL_GLINES, "%s GL * +%s %lu %lu :%s\r\n", mynumeric->content,
160 glinetostring(agline), agline->expire - getnettime(),
161 agline->lastmod, agline->reason->content);
cb581522 162#endif
a44fc5f7 163 } else {
5649ec17
GB
164 controlwall(NO_OPER, NL_GLINES, "Deactivating G-Line on '%s'",
165 glinetostring(agline));
166
cb581522 167#if 1
c684f472
GB
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);
cb581522 171#else
5649ec17
GB
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);
cb581522 175#endif
a44fc5f7
GB
176 }
177}
178
179/* returns non-zero if the glines are exactly the same */
180int glineequal(gline *gla, gline *glb) {
c684f472 181 if ((gla->flags & GLINE_BADCHAN) != (glb->flags & GLINE_BADCHAN))
a44fc5f7
GB
182 return 0;
183
c684f472 184 if ((gla->flags & GLINE_REALNAME) != (glb->flags & GLINE_REALNAME))
a44fc5f7
GB
185 return 0;
186
c684f472
GB
187 if ((gla->flags & GLINE_IPMASK) != (glb->flags & GLINE_IPMASK))
188 return 0;
189
190 if ((!gla->nick && glb->nick) || (gla->nick && !glb->nick))
a44fc5f7
GB
191 return 0;
192
c684f472 193 if (gla->nick && ircd_strcmp(gla->nick->content, glb->nick->content) != 0)
a44fc5f7
GB
194 return 0;
195
c684f472 196 if ((!gla->user && glb->user) || (gla->user && !glb->user))
a44fc5f7
GB
197 return 0;
198
c684f472 199 if (gla->user && ircd_strcmp(gla->user->content, glb->user->content) != 0)
a44fc5f7
GB
200 return 0;
201
c684f472
GB
202 if (gla->flags & GLINE_IPMASK) {
203 if (gla->bits != glb->bits)
204 return 0;
205
206 if (!ipmask_check(&gla->ip, &glb->ip, gla->bits))
207 return 0;
208 } else {
209 if ((!gla->host && glb->host) || (gla->host && !glb->host))
210 return 0;
211
212 if (gla->host && ircd_strcmp(gla->host->content, glb->host->content) != 0)
213 return 0;
214 }
215
a44fc5f7
GB
216 return 1;
217}
218
219/* returns non-zero on match */
c684f472
GB
220int gline_match_mask(gline *gla, gline *glb) {
221 if ((gla->flags & GLINE_BADCHAN) != (glb->flags & GLINE_BADCHAN))
a44fc5f7
GB
222 return 0;
223
c684f472 224 if ((gla->flags & GLINE_REALNAME) != (glb->flags & GLINE_REALNAME))
a44fc5f7
GB
225 return 0;
226
c684f472 227 if ((gla->flags & GLINE_IPMASK) != (glb->flags & GLINE_IPMASK))
a44fc5f7
GB
228 return 0;
229
c684f472 230 if (gla->nick && !glb->nick)
a44fc5f7
GB
231 return 0;
232
c684f472 233 if (gla->nick && glb->nick && match(gla->nick->content, glb->nick->content) != 0)
a44fc5f7
GB
234 return 0;
235
c684f472 236 if (gla->user && !glb->user)
a44fc5f7
GB
237 return 0;
238
c684f472
GB
239 if (gla->user && glb->user && match(gla->user->content, glb->user->content) != 0)
240 return 0;
a44fc5f7 241
c684f472
GB
242 if (gla->flags & GLINE_IPMASK) {
243 if (gla->bits > glb->bits)
244 return 0;
a44fc5f7 245
c684f472
GB
246 if (!ipmask_check(&gla->ip, &glb->ip, gla->bits))
247 return 0;
248 } else {
249 if (gla->host && !glb->host)
250 return 0;
251
252 if (gla->host && glb->host && match(gla->host->content, glb->host->content) != 0)
253 return 0;
254 }
255
256 return 1;
a44fc5f7
GB
257}
258
a86fc0c4
GB
259int isglinesane(gline *gl, const char **hint) {
260 int wildcard, nowildcardcount;
261 char *pos;
262 trusthost *th;
263
264 /* Hits all realnames. */
265 if ((gl->flags & GLINE_REALNAME) && !gl->user) {
266 *hint = "Matches all realnames.";
267 return 0;
268 }
269
270 /* Hits all local/non-local channels. */
271 if ((gl->flags & GLINE_BADCHAN) && (strcmp(gl->user->content, "&*") == 0 || strcmp(gl->user->content, "#*") == 0)) {
272 *hint = "Matches all local/non-local channels.";
273 return 0;
274 }
275
276 /* Mask wider than /16 for IPv4 or /32 for IPv6. */
277 if ((gl->flags & GLINE_IPMASK) && gl->bits < (irc_in_addr_is_ipv4(&gl->ip) ? (96 + 16) : 32)) {
278 *hint = "CIDR mask too wide.";
279 return 0;
280 }
281
282 /* Doesn't have at least two non-wildcarded host components. */
283 if (gl->flags & GLINE_HOSTMASK) {
284 nowildcardcount = 0;
285
286 wildcard = 0;
287 pos = gl->host->content;
288
289 for (;;) {
290 switch (*pos) {
291 case '*':
292 /* fall through */
293 case '?':
294 wildcard = 1;
295 break;
296
297 case '.':
298 case '\0':
299 if (!wildcard)
300 nowildcardcount++;
301 else
302 wildcard = 0;
303
304 if (*pos == '\0')
305 pos = NULL; /* Leave the loop. */
306
307 break;
308 }
309
310 if (pos)
311 pos++;
312 else
313 break;
314 }
315
316 if (nowildcardcount < 2) {
317 *hint = "Needs at least 2 non-wildcarded host components.";
318 return 0;
319 }
320 }
321
322 /* Wildcard username match for trusted host with reliable usernames. */
323 if (gl->flags & GLINE_IPMASK && (!gl->user || strchr(gl->user->content, '*') || strchr(gl->user->content, '?'))) {
324 th = th_getbyhost(&gl->ip);
325
326 if (th && (th->group->flags & TRUST_RELIABLE_USERNAME)) {
327 *hint = "Wildcard username match for a trusted host that has reliable usernames.";
328 return 0;
329 }
330 }
331
332 return 1;
333}