]> jfr.im git - solanum.git/blob - modules/m_dline.c
m_close: remove dangling return [ci skip]
[solanum.git] / modules / m_dline.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_dline.c: Bans/unbans a user.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 */
24
25 #include "stdinc.h"
26 #include "channel.h"
27 #include "class.h"
28 #include "client.h"
29 #include "common.h"
30 #include "match.h"
31 #include "ircd.h"
32 #include "hostmask.h"
33 #include "numeric.h"
34 #include "s_conf.h"
35 #include "s_newconf.h"
36 #include "logger.h"
37 #include "send.h"
38 #include "hash.h"
39 #include "s_serv.h"
40 #include "msg.h"
41 #include "parse.h"
42 #include "modules.h"
43 #include "bandbi.h"
44 #include "operhash.h"
45
46 static const char dline_desc[] = "Provides the DLINE facility to ban users via IP address";
47
48 static void mo_dline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
49 static void me_dline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
50 static void mo_undline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
51 static void me_undline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
52
53 struct Message dline_msgtab = {
54 "DLINE", 0, 0, 0, 0,
55 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_dline, 4}, {mo_dline, 2}}
56 };
57
58 struct Message undline_msgtab = {
59 "UNDLINE", 0, 0, 0, 0,
60 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_undline, 2}, {mo_undline, 2}}
61 };
62
63 mapi_clist_av1 dline_clist[] = { &dline_msgtab, &undline_msgtab, NULL };
64
65 DECLARE_MODULE_AV2(dline, NULL, NULL, dline_clist, NULL, NULL, NULL, NULL, dline_desc);
66
67 static bool remove_temp_dline(struct ConfItem *);
68 static void apply_dline(struct Client *, const char *, int, char *);
69 static void apply_undline(struct Client *, const char *);
70
71 /* mo_dline()
72 *
73 * parv[1] - dline to add
74 * parv[2] - reason
75 */
76 static void
77 mo_dline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
78 {
79 char def[] = "No Reason";
80 const char *dlhost;
81 char *reason = def;
82 char cidr_form_host[HOSTLEN + 1];
83 int tdline_time = 0;
84 const char *target_server = NULL;
85 int loc = 1;
86
87 if(!IsOperK(source_p))
88 {
89 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "kline");
90 return;
91 }
92
93 if((tdline_time = valid_temp_time(parv[loc])) >= 0)
94 loc++;
95
96 dlhost = parv[loc];
97 rb_strlcpy(cidr_form_host, dlhost, sizeof(cidr_form_host));
98 loc++;
99
100 /* would break the protocol */
101 if (*dlhost == ':')
102 {
103 sendto_one_notice(source_p, ":Invalid D-Line");
104 return;
105 }
106
107 if(parc >= loc + 2 && !irccmp(parv[loc], "ON"))
108 {
109 if(!IsOperRemoteBan(source_p))
110 {
111 sendto_one(source_p, form_str(ERR_NOPRIVS),
112 me.name, source_p->name, "remoteban");
113 return;
114 }
115
116 target_server = parv[loc + 1];
117 loc += 2;
118 }
119
120 if(parc >= loc + 1 && !EmptyString(parv[loc]))
121 reason = LOCAL_COPY(parv[loc]);
122
123 if(target_server != NULL)
124 {
125 sendto_match_servs(source_p, target_server,
126 CAP_ENCAP, NOCAPS,
127 "ENCAP %s DLINE %d %s :%s",
128 target_server, tdline_time, dlhost, reason);
129
130 if(!match(target_server, me.name))
131 return;
132 }
133
134 apply_dline(source_p, dlhost, tdline_time, reason);
135
136 check_dlines();
137 }
138
139 /* mo_undline()
140 *
141 * parv[1] = dline to remove
142 */
143 static void
144 mo_undline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
145 {
146 const char *cidr;
147 const char *target_server = NULL;
148
149 if(!IsOperK(source_p))
150 {
151 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "unkline");
152 return;
153 }
154
155 cidr = parv[1];
156
157 if(parc >= 4 && !irccmp(parv[2], "ON"))
158 {
159 if(!IsOperRemoteBan(source_p))
160 {
161 sendto_one(source_p, form_str(ERR_NOPRIVS),
162 me.name, source_p->name, "remoteban");
163 return;
164 }
165
166 target_server = parv[3];
167 sendto_match_servs(source_p, target_server,
168 CAP_ENCAP, NOCAPS, "ENCAP %s UNDLINE %s", target_server, cidr);
169
170 if(!match(target_server, me.name))
171 return;
172 }
173
174 apply_undline(source_p, cidr);
175 }
176
177 static void
178 me_dline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
179 {
180 int tdline_time = atoi(parv[1]);
181 /* Since this is coming over a server link, assume that the originating
182 * server did the relevant permission/sanity checks...
183 */
184
185 if(!IsPerson(source_p))
186 return;
187
188 if(!find_shared_conf(source_p->username, source_p->host,
189 source_p->servptr->name,
190 tdline_time > 0 ? SHARED_TDLINE : SHARED_PDLINE))
191 return;
192
193 apply_dline(source_p, parv[2], tdline_time, LOCAL_COPY(parv[3]));
194
195 check_dlines();
196 }
197
198 static void
199 me_undline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
200 {
201 if(!IsPerson(source_p))
202 return;
203
204 if(!find_shared_conf(source_p->username, source_p->host,
205 source_p->servptr->name, SHARED_UNDLINE))
206 return;
207
208 apply_undline(source_p, parv[1]);
209 }
210
211 static void
212 apply_dline(struct Client *source_p, const char *dlhost, int tdline_time, char *reason)
213 {
214 struct ConfItem *aconf;
215 char *oper_reason;
216 struct rb_sockaddr_storage daddr;
217 int t = AF_INET, ty, b;
218 const char *creason;
219
220 ty = parse_netmask(dlhost, &daddr, &b);
221 if(ty == HM_HOST)
222 {
223 sendto_one(source_p, ":%s NOTICE %s :Invalid D-Line", me.name, source_p->name);
224 return;
225 }
226 #ifdef RB_IPV6
227 if(ty == HM_IPV6)
228 t = AF_INET6;
229 else
230 #endif
231 t = AF_INET;
232
233 /* This means dlines wider than /16 cannot be set remotely */
234 if(IsOperAdmin(source_p))
235 {
236 if(b < 8)
237 {
238 sendto_one_notice(source_p,
239 ":For safety, bitmasks less than 8 require conf access.");
240 return;
241 }
242 }
243 else
244 {
245 if(b < 16)
246 {
247 sendto_one_notice(source_p,
248 ":Dline bitmasks less than 16 are for admins only.");
249 return;
250 }
251 }
252
253 if(ConfigFileEntry.non_redundant_klines)
254 {
255 if((aconf = find_dline((struct sockaddr *) &daddr, t)) != NULL)
256 {
257 int bx;
258 parse_netmask(aconf->host, NULL, &bx);
259 if(b >= bx)
260 {
261 creason = aconf->passwd ? aconf->passwd : "<No Reason>";
262 if(IsConfExemptKline(aconf))
263 sendto_one(source_p,
264 ":%s NOTICE %s :[%s] is (E)d-lined by [%s] - %s",
265 me.name, source_p->name, dlhost, aconf->host,
266 creason);
267 else
268 sendto_one(source_p,
269 ":%s NOTICE %s :[%s] already D-lined by [%s] - %s",
270 me.name, source_p->name, dlhost, aconf->host,
271 creason);
272 return;
273 }
274 }
275 }
276
277 rb_set_time();
278
279 aconf = make_conf();
280 aconf->status = CONF_DLINE;
281 aconf->created = rb_current_time();
282 aconf->host = rb_strdup(dlhost);
283 aconf->passwd = rb_strdup(reason);
284 aconf->info.oper = operhash_add(get_oper_name(source_p));
285
286 if(strlen(reason) > BANREASONLEN)
287 reason[BANREASONLEN] = '\0';
288
289 /* Look for an oper reason */
290 if((oper_reason = strchr(reason, '|')) != NULL)
291 {
292 *oper_reason = '\0';
293 oper_reason++;
294
295 if(!EmptyString(oper_reason))
296 aconf->spasswd = rb_strdup(oper_reason);
297 }
298
299 if(tdline_time > 0)
300 {
301 aconf->hold = rb_current_time() + tdline_time;
302 add_temp_dline(aconf);
303
304 if(EmptyString(oper_reason))
305 {
306 sendto_realops_snomask(SNO_GENERAL, L_ALL,
307 "%s added temporary %d min. D-Line for [%s] [%s]",
308 get_oper_name(source_p), tdline_time / 60,
309 aconf->host, reason);
310 ilog(L_KLINE, "D %s %d %s %s",
311 get_oper_name(source_p), tdline_time / 60, aconf->host, reason);
312 }
313 else
314 {
315 sendto_realops_snomask(SNO_GENERAL, L_ALL,
316 "%s added temporary %d min. D-Line for [%s] [%s|%s]",
317 get_oper_name(source_p), tdline_time / 60,
318 aconf->host, reason, oper_reason);
319 ilog(L_KLINE, "D %s %d %s %s|%s",
320 get_oper_name(source_p), tdline_time / 60,
321 aconf->host, reason, oper_reason);
322 }
323
324 sendto_one(source_p, ":%s NOTICE %s :Added temporary %d min. D-Line for [%s]",
325 me.name, source_p->name, tdline_time / 60, aconf->host);
326 }
327 else
328 {
329 add_conf_by_address(aconf->host, CONF_DLINE, NULL, NULL, aconf);
330
331 bandb_add(BANDB_DLINE, source_p, aconf->host, NULL,
332 reason, EmptyString(aconf->spasswd) ? NULL : aconf->spasswd, 0);
333
334 if(EmptyString(oper_reason))
335 {
336 sendto_realops_snomask(SNO_GENERAL, L_ALL,
337 "%s added D-Line for [%s] [%s]",
338 get_oper_name(source_p), aconf->host, reason);
339 ilog(L_KLINE, "D %s 0 %s %s",
340 get_oper_name(source_p), aconf->host, reason);
341 }
342 else
343 {
344 sendto_realops_snomask(SNO_GENERAL, L_ALL,
345 "%s added D-Line for [%s] [%s|%s]",
346 get_oper_name(source_p), aconf->host, reason, oper_reason);
347 ilog(L_KLINE, "D %s 0 %s %s|%s",
348 get_oper_name(source_p),
349 aconf->host, reason, oper_reason);
350 }
351 }
352 }
353
354 static void
355 apply_undline(struct Client *source_p, const char *cidr)
356 {
357 char buf[BUFSIZE];
358 struct ConfItem *aconf;
359
360 if(parse_netmask(cidr, NULL, NULL) == HM_HOST)
361 {
362 sendto_one_notice(source_p, ":Invalid D-Line");
363 return;
364 }
365
366 aconf = find_exact_conf_by_address(cidr, CONF_DLINE, NULL);
367 if(aconf == NULL)
368 {
369 sendto_one_notice(source_p, ":No D-Line for %s", cidr);
370 return;
371 }
372
373 rb_strlcpy(buf, aconf->host, sizeof buf);
374 if(remove_temp_dline(aconf))
375 {
376 sendto_one(source_p,
377 ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines",
378 me.name, source_p->name, buf);
379 sendto_realops_snomask(SNO_GENERAL, L_ALL,
380 "%s has removed the temporary D-Line for: [%s]",
381 get_oper_name(source_p), buf);
382 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), buf);
383 return;
384 }
385
386 bandb_del(BANDB_DLINE, aconf->host, NULL);
387
388 sendto_one(source_p, ":%s NOTICE %s :D-Line for [%s] is removed", me.name, source_p->name,
389 aconf->host);
390 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s has removed the D-Line for: [%s]",
391 get_oper_name(source_p), aconf->host);
392 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), aconf->host);
393 delete_one_address_conf(aconf->host, aconf);
394 }
395
396 /* remove_temp_dline()
397 *
398 * inputs - confitem to undline
399 * outputs - true if removed, false otherwise.
400 * side effects - tries to undline anything that matches
401 */
402 static bool
403 remove_temp_dline(struct ConfItem *aconf)
404 {
405 rb_dlink_node *ptr;
406 int i;
407
408 for(i = 0; i < LAST_TEMP_TYPE; i++)
409 {
410 RB_DLINK_FOREACH(ptr, temp_dlines[i].head)
411 {
412 if(aconf == ptr->data)
413 {
414 rb_dlinkDestroy(ptr, &temp_dlines[i]);
415 delete_one_address_conf(aconf->host, aconf);
416 return true;
417 }
418 }
419 }
420
421 return false;
422 }