]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * charybdis: An advanced ircd. | |
3 | * m_ban.c: Propagates network bans across servers. | |
4 | * | |
5 | * Copyright (C) 2010 Jilles Tjoelker | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions are | |
9 | * met: | |
10 | * | |
11 | * 1.Redistributions of source code must retain the above copyright notice, | |
12 | * this list of conditions and the following disclaimer. | |
13 | * 2.Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
20 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
25 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
27 | * POSSIBILITY OF SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | #include "stdinc.h" | |
31 | #include "send.h" | |
32 | #include "channel.h" | |
33 | #include "client.h" | |
34 | #include "common.h" | |
35 | #include "config.h" | |
36 | #include "ircd.h" | |
37 | #include "match.h" | |
38 | #include "s_conf.h" | |
39 | #include "s_newconf.h" | |
40 | #include "msg.h" | |
41 | #include "modules.h" | |
42 | #include "hash.h" | |
43 | #include "s_serv.h" | |
44 | #include "operhash.h" | |
45 | #include "reject.h" | |
46 | #include "hostmask.h" | |
47 | ||
48 | static int ms_ban(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]); | |
49 | ||
50 | struct Message ban_msgtab = { | |
51 | "BAN", 0, 0, 0, MFLG_SLOW, | |
52 | {mg_unreg, mg_ignore, {ms_ban, 9}, {ms_ban, 9}, mg_ignore, mg_ignore} | |
53 | }; | |
54 | ||
55 | mapi_clist_av1 ban_clist[] = { &ban_msgtab, NULL }; | |
56 | DECLARE_MODULE_AV1(ban, NULL, NULL, ban_clist, NULL, NULL, "$Revision: 1349 $"); | |
57 | ||
58 | /* ms_ban() | |
59 | * | |
60 | * parv[1] - type | |
61 | * parv[2] - username mask or * | |
62 | * parv[3] - hostname mask | |
63 | * parv[4] - creation TS | |
64 | * parv[5] - duration (relative to creation) | |
65 | * parv[6] - lifetime (relative to creation) | |
66 | * parv[7] - oper or * | |
67 | * parv[8] - reason (possibly with |operreason) | |
68 | */ | |
69 | static int | |
70 | ms_ban(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
71 | { | |
72 | rb_dlink_node *ptr; | |
73 | struct ConfItem *aconf; | |
74 | unsigned int ntype; | |
75 | const char *oper, *stype; | |
76 | time_t created, hold, lifetime; | |
77 | char *p; | |
78 | int act; | |
79 | int valid; | |
80 | ||
81 | if (strlen(parv[1]) != 1) | |
82 | { | |
83 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
84 | "Unknown BAN type %s from %s", | |
85 | parv[1], source_p->name); | |
86 | return 0; | |
87 | } | |
88 | switch (parv[1][0]) | |
89 | { | |
90 | case 'K': | |
91 | ntype = CONF_KILL; | |
92 | stype = "K-Line"; | |
93 | break; | |
94 | case 'X': | |
95 | ntype = CONF_XLINE; | |
96 | stype = "X-Line"; | |
97 | break; | |
98 | case 'R': | |
99 | ntype = IsChannelName(parv[3]) ? CONF_RESV_CHANNEL : | |
100 | CONF_RESV_NICK; | |
101 | stype = "RESV"; | |
102 | break; | |
103 | default: | |
104 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
105 | "Unknown BAN type %s from %s", | |
106 | parv[1], source_p->name); | |
107 | return 0; | |
108 | } | |
109 | created = atol(parv[4]); | |
110 | hold = created + atoi(parv[5]); | |
111 | lifetime = created + atoi(parv[6]); | |
112 | if (!strcmp(parv[7], "*")) | |
113 | oper = IsServer(source_p) ? source_p->name : get_oper_name(source_p); | |
114 | else | |
115 | oper = parv[7]; | |
116 | ptr = find_prop_ban(ntype, parv[2], parv[3]); | |
117 | if (ptr != NULL) | |
118 | { | |
119 | /* We already know about this ban mask. */ | |
120 | aconf = ptr->data; | |
121 | if (aconf->created > created || | |
122 | (aconf->created == created && | |
123 | aconf->lifetime >= lifetime)) | |
124 | { | |
125 | if (IsPerson(source_p)) | |
126 | sendto_one_notice(source_p, | |
127 | ":Your %s [%s%s%s] has been superseded", | |
128 | stype, | |
129 | aconf->user ? aconf->user : "", | |
130 | aconf->user ? "@" : "", | |
131 | aconf->host); | |
132 | return 0; | |
133 | } | |
134 | /* act indicates if something happened (from the oper's | |
135 | * point of view). This is the case if the ban was | |
136 | * previously active (not deleted) or if the new ban | |
137 | * is not a removal and not already expired. | |
138 | */ | |
139 | act = !(aconf->status & CONF_ILLEGAL) || (hold != created && | |
140 | hold > rb_current_time()); | |
141 | if (lifetime > aconf->lifetime) | |
142 | aconf->lifetime = lifetime; | |
143 | /* already expired, hmm */ | |
144 | if (aconf->lifetime <= rb_current_time()) | |
145 | return 0; | |
146 | /* Deactivate, it will be reactivated later if appropriate. */ | |
147 | deactivate_conf(aconf, ptr); | |
148 | rb_free(aconf->user); | |
149 | aconf->user = NULL; | |
150 | rb_free(aconf->host); | |
151 | aconf->host = NULL; | |
152 | operhash_delete(aconf->info.oper); | |
153 | aconf->info.oper = NULL; | |
154 | rb_free(aconf->passwd); | |
155 | aconf->passwd = NULL; | |
156 | rb_free(aconf->spasswd); | |
157 | aconf->spasswd = NULL; | |
158 | } | |
159 | else | |
160 | { | |
161 | /* New ban mask. */ | |
162 | aconf = make_conf(); | |
163 | aconf->status = CONF_ILLEGAL | ntype; | |
164 | aconf->lifetime = lifetime; | |
165 | rb_dlinkAddAlloc(aconf, &prop_bans); | |
166 | act = hold != created && hold > rb_current_time(); | |
167 | } | |
168 | aconf->flags &= ~CONF_FLAGS_MYOPER; | |
169 | aconf->flags |= CONF_FLAGS_TEMPORARY; | |
170 | aconf->user = ntype == CONF_KILL ? rb_strdup(parv[2]) : NULL; | |
171 | aconf->host = rb_strdup(parv[3]); | |
172 | aconf->info.oper = operhash_add(oper); | |
173 | aconf->created = created; | |
174 | aconf->hold = hold; | |
175 | if (ntype != CONF_KILL || (p = strchr(parv[parc - 1], '|')) == NULL) | |
176 | aconf->passwd = rb_strdup(parv[parc - 1]); | |
177 | else | |
178 | { | |
179 | aconf->passwd = rb_strndup(parv[parc - 1], p - parv[parc - 1] + 1); | |
180 | aconf->spasswd = rb_strdup(p + 1); | |
181 | } | |
182 | /* The ban is fully filled in and in the prop_bans list | |
183 | * but still deactivated. Now determine if it should be activated | |
184 | * and send the server notices. | |
185 | */ | |
186 | /* We only reject *@* and the like here. | |
187 | * Otherwise malformed bans are fairly harmless and can be removed. | |
188 | */ | |
189 | switch (ntype) | |
190 | { | |
191 | case CONF_KILL: | |
192 | valid = valid_wild_card(aconf->user, aconf->host); | |
193 | break; | |
194 | case CONF_RESV_CHANNEL: | |
195 | valid = 1; | |
196 | break; | |
197 | default: | |
198 | valid = valid_wild_card_simple(aconf->host); | |
199 | break; | |
200 | } | |
201 | if (act && hold != created && !valid) | |
202 | { | |
203 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
204 | "Ignoring global %d min. %s from %s%s%s for [%s%s%s]: too few non-wildcard characters", | |
205 | (int)((hold - rb_current_time()) / 60), | |
206 | stype, | |
207 | IsServer(source_p) ? source_p->name : get_oper_name(source_p), | |
208 | strcmp(parv[7], "*") ? " on behalf of " : "", | |
209 | strcmp(parv[7], "*") ? parv[7] : "", | |
210 | aconf->user ? aconf->user : "", | |
211 | aconf->user ? "@" : "", | |
212 | aconf->host); | |
213 | if(IsPerson(source_p)) | |
214 | sendto_one_notice(source_p, | |
215 | ":Your %s [%s%s%s] has too few non-wildcard characters", | |
216 | stype, | |
217 | aconf->user ? aconf->user : "", | |
218 | aconf->user ? "@" : "", | |
219 | aconf->host); | |
220 | /* Propagate it, but do not apply it locally. */ | |
221 | } | |
222 | else if (act && hold != created) | |
223 | { | |
224 | /* Keep the notices in sync with modules/m_kline.c etc. */ | |
225 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
226 | "%s added global %d min. %s%s%s for [%s%s%s] [%s]", | |
227 | IsServer(source_p) ? source_p->name : get_oper_name(source_p), | |
228 | (int)((hold - rb_current_time()) / 60), | |
229 | stype, | |
230 | strcmp(parv[7], "*") ? " from " : "", | |
231 | strcmp(parv[7], "*") ? parv[7] : "", | |
232 | aconf->user ? aconf->user : "", | |
233 | aconf->user ? "@" : "", | |
234 | aconf->host, | |
235 | parv[parc - 1]); | |
236 | ilog(L_KLINE, "%s %s %d %s%s%s %s", parv[1], | |
237 | IsServer(source_p) ? source_p->name : get_oper_name(source_p), | |
238 | (int)((hold - rb_current_time()) / 60), | |
239 | aconf->user ? aconf->user : "", | |
240 | aconf->user ? " " : "", | |
241 | aconf->host, | |
242 | parv[parc - 1]); | |
243 | aconf->status &= ~CONF_ILLEGAL; | |
244 | } | |
245 | else if (act) | |
246 | { | |
247 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
248 | "%s has removed the global %s for: [%s%s%s]%s%s", | |
249 | IsServer(source_p) ? source_p->name : get_oper_name(source_p), | |
250 | stype, | |
251 | aconf->user ? aconf->user : "", | |
252 | aconf->user ? "@" : "", | |
253 | aconf->host, | |
254 | strcmp(parv[7], "*") ? " on behalf of " : "", | |
255 | strcmp(parv[7], "*") ? parv[7] : ""); | |
256 | ilog(L_KLINE, "U%s %s %s%s %s", parv[1], | |
257 | IsServer(source_p) ? source_p->name : get_oper_name(source_p), | |
258 | aconf->user ? aconf->user : "", | |
259 | aconf->user ? " " : "", | |
260 | aconf->host); | |
261 | } | |
262 | /* If CONF_ILLEGAL is still set at this point, remove entries from the | |
263 | * reject cache (for klines and xlines). | |
264 | * If CONF_ILLEGAL is not set, add the ban to the type-specific data | |
265 | * structure and take action on matched clients/channels. | |
266 | */ | |
267 | switch (ntype) | |
268 | { | |
269 | case CONF_KILL: | |
270 | if (aconf->status & CONF_ILLEGAL) | |
271 | remove_reject_mask(aconf->user, aconf->host); | |
272 | else | |
273 | { | |
274 | add_conf_by_address(aconf->host, CONF_KILL, aconf->user, NULL, aconf); | |
275 | if(ConfigFileEntry.kline_delay || | |
276 | (IsServer(source_p) && | |
277 | !HasSentEob(source_p))) | |
278 | { | |
279 | if(kline_queued == 0) | |
280 | { | |
281 | rb_event_addonce("check_klines", check_klines_event, NULL, | |
282 | ConfigFileEntry.kline_delay); | |
283 | kline_queued = 1; | |
284 | } | |
285 | } | |
286 | else | |
287 | check_klines(); | |
288 | } | |
289 | break; | |
290 | case CONF_XLINE: | |
291 | if (aconf->status & CONF_ILLEGAL) | |
292 | remove_reject_mask(aconf->host, NULL); | |
293 | else | |
294 | { | |
295 | rb_dlinkAddAlloc(aconf, &xline_conf_list); | |
296 | check_xlines(); | |
297 | } | |
298 | break; | |
299 | case CONF_RESV_CHANNEL: | |
300 | if (!(aconf->status & CONF_ILLEGAL)) | |
301 | { | |
302 | add_to_resv_hash(aconf->host, aconf); | |
303 | resv_chan_forcepart(aconf->host, aconf->passwd, hold - rb_current_time()); | |
304 | } | |
305 | break; | |
306 | case CONF_RESV_NICK: | |
307 | if (!(aconf->status & CONF_ILLEGAL)) | |
308 | rb_dlinkAddAlloc(aconf, &resv_conf_list); | |
309 | break; | |
310 | } | |
311 | sendto_server(client_p, NULL, CAP_BAN|CAP_TS6, NOCAPS, | |
312 | ":%s BAN %s %s %s %s %s %s %s :%s", | |
313 | source_p->id, | |
314 | parv[1], | |
315 | parv[2], | |
316 | parv[3], | |
317 | parv[4], | |
318 | parv[5], | |
319 | parv[6], | |
320 | parv[7], | |
321 | parv[parc - 1]); | |
322 | return 0; | |
323 | } |