]>
Commit | Line | Data |
---|---|---|
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 |
12 | MODULE_VERSION(""); |
13 | ||
a44fc5f7 GB |
14 | void _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 | ||
25 | void _fini() { | |
26 | deregisterserverhandler("GL", handleglinemsg); | |
27 | deregisterhook(HOOK_CORE_STATSREQUEST, handleglinestats); | |
28 | } | |
29 | ||
c684f472 GB |
30 | int 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 | ||
58 | int 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 | 68 | gline *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 | 98 | void 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 | 114 | void 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 | ||
135 | void 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 | ||
159 | void gline_propagate(gline *agline) { | |
20447d72 GB |
160 | if (agline->flags & GLINE_DESTROYED) { |
161 | controlwall(NO_OPER, NL_GLINES, "Destroying G-Line on '%s' lasting %s with reason '%s', created by: %s", | |
162 | glinetostring(agline), longtoduration(agline->expire-getnettime(), 0), | |
163 | agline->reason->content, agline->creator->content); | |
164 | ||
18845894 | 165 | #if 1 |
20447d72 GB |
166 | irc_send("%s GL * %%-%s %lu %lu :%s\r\n", mynumeric->content, |
167 | glinetostring(agline), agline->expire - getnettime(), | |
168 | agline->lastmod, agline->reason->content); | |
169 | #else | |
170 | controlwall(NO_OPER, NL_GLINES, "%s GL * %%-%s %lu %lu :%s\r\n", mynumeric->content, | |
171 | glinetostring(agline), agline->expire - getnettime(), | |
172 | agline->lastmod, agline->reason->content); | |
173 | #endif | |
174 | } else if (agline->flags & GLINE_ACTIVE) { | |
d892838f | 175 | controlwall(NO_OPER, NL_GLINES, "Activating G-Line on '%s' lasting %s created by %s with reason '%s'", |
c684f472 | 176 | glinetostring(agline), longtoduration(agline->expire-getnettime(), 0), |
d892838f | 177 | agline->creator->content, agline->reason->content); |
5649ec17 | 178 | |
18845894 | 179 | #if 1 |
c684f472 GB |
180 | irc_send("%s GL * +%s %lu %lu :%s\r\n", mynumeric->content, |
181 | glinetostring(agline), agline->expire - getnettime(), | |
182 | agline->lastmod, agline->reason->content); | |
cb581522 | 183 | #else |
5649ec17 GB |
184 | controlwall(NO_OPER, NL_GLINES, "%s GL * +%s %lu %lu :%s\r\n", mynumeric->content, |
185 | glinetostring(agline), agline->expire - getnettime(), | |
186 | agline->lastmod, agline->reason->content); | |
cb581522 | 187 | #endif |
a44fc5f7 | 188 | } else { |
d892838f GB |
189 | controlwall(NO_OPER, NL_GLINES, "Deactivating G-Line on '%s' lasting %s created by %s with reason '%s'", |
190 | glinetostring(agline), longtoduration(agline->expire-getnettime(), 0), | |
191 | agline->creator->content, agline->reason->content); | |
5649ec17 | 192 | |
18845894 | 193 | #if 1 |
c684f472 GB |
194 | irc_send("%s GL * -%s %lu %lu :%s\r\n", mynumeric->content, |
195 | glinetostring(agline), agline->expire - getnettime(), | |
196 | agline->lastmod, agline->reason->content); | |
cb581522 | 197 | #else |
5649ec17 GB |
198 | controlwall(NO_OPER, NL_GLINES, "%s GL * -%s %lu %lu :%s\r\n", mynumeric->content, |
199 | glinetostring(agline), agline->expire - getnettime(), | |
200 | agline->lastmod, agline->reason->content); | |
cb581522 | 201 | #endif |
a44fc5f7 GB |
202 | } |
203 | } | |
204 | ||
205 | /* returns non-zero if the glines are exactly the same */ | |
206 | int glineequal(gline *gla, gline *glb) { | |
c684f472 | 207 | if ((gla->flags & GLINE_BADCHAN) != (glb->flags & GLINE_BADCHAN)) |
a44fc5f7 GB |
208 | return 0; |
209 | ||
c684f472 | 210 | if ((gla->flags & GLINE_REALNAME) != (glb->flags & GLINE_REALNAME)) |
a44fc5f7 GB |
211 | return 0; |
212 | ||
c684f472 GB |
213 | if ((gla->flags & GLINE_IPMASK) != (glb->flags & GLINE_IPMASK)) |
214 | return 0; | |
215 | ||
216 | if ((!gla->nick && glb->nick) || (gla->nick && !glb->nick)) | |
a44fc5f7 GB |
217 | return 0; |
218 | ||
c684f472 | 219 | if (gla->nick && ircd_strcmp(gla->nick->content, glb->nick->content) != 0) |
a44fc5f7 GB |
220 | return 0; |
221 | ||
c684f472 | 222 | if ((!gla->user && glb->user) || (gla->user && !glb->user)) |
a44fc5f7 GB |
223 | return 0; |
224 | ||
c684f472 | 225 | if (gla->user && ircd_strcmp(gla->user->content, glb->user->content) != 0) |
a44fc5f7 GB |
226 | return 0; |
227 | ||
c684f472 GB |
228 | if (gla->flags & GLINE_IPMASK) { |
229 | if (gla->bits != glb->bits) | |
230 | return 0; | |
231 | ||
232 | if (!ipmask_check(&gla->ip, &glb->ip, gla->bits)) | |
233 | return 0; | |
234 | } else { | |
235 | if ((!gla->host && glb->host) || (gla->host && !glb->host)) | |
236 | return 0; | |
237 | ||
238 | if (gla->host && ircd_strcmp(gla->host->content, glb->host->content) != 0) | |
239 | return 0; | |
240 | } | |
241 | ||
a44fc5f7 GB |
242 | return 1; |
243 | } | |
244 | ||
245 | /* returns non-zero on match */ | |
c684f472 GB |
246 | int gline_match_mask(gline *gla, gline *glb) { |
247 | if ((gla->flags & GLINE_BADCHAN) != (glb->flags & GLINE_BADCHAN)) | |
a44fc5f7 GB |
248 | return 0; |
249 | ||
c684f472 | 250 | if ((gla->flags & GLINE_REALNAME) != (glb->flags & GLINE_REALNAME)) |
a44fc5f7 GB |
251 | return 0; |
252 | ||
c684f472 | 253 | if ((gla->flags & GLINE_IPMASK) != (glb->flags & GLINE_IPMASK)) |
a44fc5f7 GB |
254 | return 0; |
255 | ||
c684f472 | 256 | if (gla->nick && !glb->nick) |
a44fc5f7 GB |
257 | return 0; |
258 | ||
c684f472 | 259 | if (gla->nick && glb->nick && match(gla->nick->content, glb->nick->content) != 0) |
a44fc5f7 GB |
260 | return 0; |
261 | ||
c684f472 | 262 | if (gla->user && !glb->user) |
a44fc5f7 GB |
263 | return 0; |
264 | ||
c684f472 GB |
265 | if (gla->user && glb->user && match(gla->user->content, glb->user->content) != 0) |
266 | return 0; | |
a44fc5f7 | 267 | |
c684f472 GB |
268 | if (gla->flags & GLINE_IPMASK) { |
269 | if (gla->bits > glb->bits) | |
270 | return 0; | |
a44fc5f7 | 271 | |
c684f472 GB |
272 | if (!ipmask_check(&gla->ip, &glb->ip, gla->bits)) |
273 | return 0; | |
274 | } else { | |
275 | if (gla->host && !glb->host) | |
276 | return 0; | |
277 | ||
278 | if (gla->host && glb->host && match(gla->host->content, glb->host->content) != 0) | |
279 | return 0; | |
280 | } | |
281 | ||
282 | return 1; | |
a44fc5f7 GB |
283 | } |
284 | ||
a86fc0c4 GB |
285 | int isglinesane(gline *gl, const char **hint) { |
286 | int wildcard, nowildcardcount; | |
287 | char *pos; | |
288 | trusthost *th; | |
289 | ||
18845894 GB |
290 | /* Reason is too short */ |
291 | if (strlen(gl->reason->content) < MINGLINEREASONLEN) { | |
292 | *hint = "G-Line reason is too short."; | |
293 | return 0; | |
294 | } | |
295 | ||
296 | /* Duration is too long */ | |
297 | if (gl->expire - getnettime() > MAXGLINEDURATION) { | |
298 | *hint = "G-Line duration is too long."; | |
299 | return 0; | |
300 | } | |
301 | ||
a86fc0c4 GB |
302 | /* Hits all realnames. */ |
303 | if ((gl->flags & GLINE_REALNAME) && !gl->user) { | |
304 | *hint = "Matches all realnames."; | |
305 | return 0; | |
306 | } | |
307 | ||
308 | /* Hits all local/non-local channels. */ | |
309 | if ((gl->flags & GLINE_BADCHAN) && (strcmp(gl->user->content, "&*") == 0 || strcmp(gl->user->content, "#*") == 0)) { | |
310 | *hint = "Matches all local/non-local channels."; | |
311 | return 0; | |
312 | } | |
313 | ||
fb89902c GB |
314 | /* Hostmask is too long. */ |
315 | if (!(gl->flags & (GLINE_BADCHAN | GLINE_REALNAME)) && | |
316 | ((gl->nick && strlen(gl->nick->content) > NICKLEN) || | |
317 | (gl->user && strlen(gl->user->content) > USERLEN) || | |
318 | (gl->host && strlen(gl->host->content) > HOSTLEN))) { | |
319 | *hint = "Hostmask components are too long."; | |
320 | return 0; | |
321 | } | |
322 | ||
521d4392 GB |
323 | /* Skip the other checks for nickname glines. */ |
324 | if (gl->nick) | |
325 | return 1; | |
326 | ||
a86fc0c4 GB |
327 | /* Mask wider than /16 for IPv4 or /32 for IPv6. */ |
328 | if ((gl->flags & GLINE_IPMASK) && gl->bits < (irc_in_addr_is_ipv4(&gl->ip) ? (96 + 16) : 32)) { | |
329 | *hint = "CIDR mask too wide."; | |
330 | return 0; | |
331 | } | |
332 | ||
333 | /* Doesn't have at least two non-wildcarded host components. */ | |
334 | if (gl->flags & GLINE_HOSTMASK) { | |
335 | nowildcardcount = 0; | |
336 | ||
337 | wildcard = 0; | |
338 | pos = gl->host->content; | |
339 | ||
340 | for (;;) { | |
341 | switch (*pos) { | |
342 | case '*': | |
343 | /* fall through */ | |
344 | case '?': | |
345 | wildcard = 1; | |
346 | break; | |
347 | ||
348 | case '.': | |
349 | case '\0': | |
350 | if (!wildcard) | |
351 | nowildcardcount++; | |
352 | else | |
353 | wildcard = 0; | |
354 | ||
355 | if (*pos == '\0') | |
356 | pos = NULL; /* Leave the loop. */ | |
357 | ||
358 | break; | |
359 | } | |
360 | ||
361 | if (pos) | |
362 | pos++; | |
363 | else | |
364 | break; | |
365 | } | |
366 | ||
367 | if (nowildcardcount < 2) { | |
368 | *hint = "Needs at least 2 non-wildcarded host components."; | |
369 | return 0; | |
370 | } | |
371 | } | |
372 | ||
373 | /* Wildcard username match for trusted host with reliable usernames. */ | |
61702c86 | 374 | if ((gl->flags & GLINE_IPMASK) && (!gl->user || strchr(gl->user->content, '*') || strchr(gl->user->content, '?'))) { |
a86fc0c4 GB |
375 | th = th_getbyhost(&gl->ip); |
376 | ||
377 | if (th && (th->group->flags & TRUST_RELIABLE_USERNAME)) { | |
378 | *hint = "Wildcard username match for a trusted host that has reliable usernames."; | |
379 | return 0; | |
380 | } | |
381 | } | |
382 | ||
383 | return 1; | |
384 | } |