]>
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 | ||
580103bc GB |
14 | static void glines_sched_save(void *arg); |
15 | ||
a44fc5f7 GB |
16 | void _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 | ||
31 | void _fini() { | |
32 | deregisterserverhandler("GL", handleglinemsg); | |
33 | deregisterhook(HOOK_CORE_STATSREQUEST, handleglinestats); | |
580103bc GB |
34 | |
35 | deleteschedule(NULL, glines_sched_save, NULL); | |
36 | } | |
37 | ||
38 | static void glines_sched_save(void *arg) { | |
39 | glstore_save(); | |
a44fc5f7 GB |
40 | } |
41 | ||
c684f472 GB |
42 | int 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 | ||
70 | int 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 | 80 | gline *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 |
110 | gline *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 | ||
129 | gline *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 | ||
148 | void 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 */ | |
180 | int 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 |
220 | int 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 |
259 | int 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 | ||
521d4392 GB |
276 | /* Skip the other checks for nickname glines. */ |
277 | if (gl->nick) | |
278 | return 1; | |
279 | ||
a86fc0c4 GB |
280 | /* Mask wider than /16 for IPv4 or /32 for IPv6. */ |
281 | if ((gl->flags & GLINE_IPMASK) && gl->bits < (irc_in_addr_is_ipv4(&gl->ip) ? (96 + 16) : 32)) { | |
282 | *hint = "CIDR mask too wide."; | |
283 | return 0; | |
284 | } | |
285 | ||
286 | /* Doesn't have at least two non-wildcarded host components. */ | |
287 | if (gl->flags & GLINE_HOSTMASK) { | |
288 | nowildcardcount = 0; | |
289 | ||
290 | wildcard = 0; | |
291 | pos = gl->host->content; | |
292 | ||
293 | for (;;) { | |
294 | switch (*pos) { | |
295 | case '*': | |
296 | /* fall through */ | |
297 | case '?': | |
298 | wildcard = 1; | |
299 | break; | |
300 | ||
301 | case '.': | |
302 | case '\0': | |
303 | if (!wildcard) | |
304 | nowildcardcount++; | |
305 | else | |
306 | wildcard = 0; | |
307 | ||
308 | if (*pos == '\0') | |
309 | pos = NULL; /* Leave the loop. */ | |
310 | ||
311 | break; | |
312 | } | |
313 | ||
314 | if (pos) | |
315 | pos++; | |
316 | else | |
317 | break; | |
318 | } | |
319 | ||
320 | if (nowildcardcount < 2) { | |
321 | *hint = "Needs at least 2 non-wildcarded host components."; | |
322 | return 0; | |
323 | } | |
324 | } | |
325 | ||
326 | /* Wildcard username match for trusted host with reliable usernames. */ | |
327 | if (gl->flags & GLINE_IPMASK && (!gl->user || strchr(gl->user->content, '*') || strchr(gl->user->content, '?'))) { | |
328 | th = th_getbyhost(&gl->ip); | |
329 | ||
330 | if (th && (th->group->flags & TRUST_RELIABLE_USERNAME)) { | |
331 | *hint = "Wildcard username match for a trusted host that has reliable usernames."; | |
332 | return 0; | |
333 | } | |
334 | } | |
335 | ||
336 | return 1; | |
337 | } |