]> jfr.im git - solanum.git/blame - modules/m_dline.c
Combine stats A output parameters (#35)
[solanum.git] / modules / m_dline.c
CommitLineData
212380e3
AC
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
212380e3
AC
23 */
24
25#include "stdinc.h"
212380e3
AC
26#include "channel.h"
27#include "class.h"
28#include "client.h"
4562c604 29#include "match.h"
212380e3
AC
30#include "ircd.h"
31#include "hostmask.h"
32#include "numeric.h"
212380e3
AC
33#include "s_conf.h"
34#include "s_newconf.h"
4016731b 35#include "logger.h"
212380e3
AC
36#include "send.h"
37#include "hash.h"
38#include "s_serv.h"
39#include "msg.h"
40#include "parse.h"
41#include "modules.h"
8bbeb278 42#include "bandbi.h"
27f616dd 43#include "operhash.h"
212380e3 44
eeabf33a
EM
45static const char dline_desc[] = "Provides the DLINE facility to ban users via IP address";
46
3c7d6fcc
EM
47static void mo_dline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
48static void me_dline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
49static void mo_undline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
50static void me_undline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
212380e3
AC
51
52struct Message dline_msgtab = {
7baa37a9 53 "DLINE", 0, 0, 0, 0,
b647efa0 54 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_dline, 4}, {mo_dline, 2}}
212380e3 55};
8bbeb278 56
212380e3 57struct Message undline_msgtab = {
7baa37a9 58 "UNDLINE", 0, 0, 0, 0,
b647efa0 59 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_undline, 2}, {mo_undline, 2}}
212380e3
AC
60};
61
62mapi_clist_av1 dline_clist[] = { &dline_msgtab, &undline_msgtab, NULL };
8bbeb278 63
9fd8e7cb 64DECLARE_MODULE_AV2(dline, NULL, NULL, dline_clist, NULL, NULL, NULL, NULL, dline_desc);
212380e3 65
3c7d6fcc
EM
66static bool remove_temp_dline(struct ConfItem *);
67static void apply_dline(struct Client *, const char *, int, char *);
68static void apply_undline(struct Client *, const char *);
212380e3
AC
69
70/* mo_dline()
55abcbb2 71 *
212380e3
AC
72 * parv[1] - dline to add
73 * parv[2] - reason
74 */
3c7d6fcc 75static void
428ca87b 76mo_dline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
77{
78 char def[] = "No Reason";
79 const char *dlhost;
212380e3 80 char *reason = def;
212380e3 81 int tdline_time = 0;
f5cb68d5 82 const char *target_server = NULL;
212380e3
AC
83 int loc = 1;
84
85 if(!IsOperK(source_p))
86 {
8bbeb278 87 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "kline");
3c7d6fcc 88 return;
212380e3
AC
89 }
90
91 if((tdline_time = valid_temp_time(parv[loc])) >= 0)
92 loc++;
93
a77ebe1e
EK
94 if (loc >= parc)
95 {
96 sendto_one_notice(source_p, ":Need an IP to D-Line");
97 return;
98 }
99
212380e3 100 dlhost = parv[loc];
f5cb68d5
VY
101 loc++;
102
2b843a5b
JT
103 /* would break the protocol */
104 if (*dlhost == ':')
105 {
72464c6a 106 sendto_one_notice(source_p, ":Invalid D-Line [%s] - IP cannot start with :", dlhost);
3c7d6fcc 107 return;
2b843a5b
JT
108 }
109
bf493a34
EK
110 int ty = parse_netmask_strict(dlhost, NULL, NULL);
111 if (ty != HM_IPV4 && ty != HM_IPV6)
112 {
72464c6a 113 sendto_one_notice(source_p, ":Invalid D-Line [%s] - doesn't look like IP[/cidr]", dlhost);
bf493a34
EK
114 return;
115 }
116
8bbeb278
AC
117 if(parc >= loc + 2 && !irccmp(parv[loc], "ON"))
118 {
2cfb7214
JT
119 if(!IsOperRemoteBan(source_p))
120 {
121 sendto_one(source_p, form_str(ERR_NOPRIVS),
8bbeb278 122 me.name, source_p->name, "remoteban");
3c7d6fcc 123 return;
2cfb7214
JT
124 }
125
8bbeb278 126 target_server = parv[loc + 1];
f5cb68d5
VY
127 loc += 2;
128 }
129
130 if(parc >= loc + 1 && !EmptyString(parv[loc]))
131 reason = LOCAL_COPY(parv[loc]);
132
133 if(target_server != NULL)
212380e3 134 {
f5cb68d5 135 sendto_match_servs(source_p, target_server,
8bbeb278
AC
136 CAP_ENCAP, NOCAPS,
137 "ENCAP %s DLINE %d %s :%s",
138 target_server, tdline_time, dlhost, reason);
f5cb68d5
VY
139
140 if(!match(target_server, me.name))
3c7d6fcc 141 return;
212380e3
AC
142 }
143
f5cb68d5
VY
144 apply_dline(source_p, dlhost, tdline_time, reason);
145
146 check_dlines();
f5cb68d5 147}
212380e3 148
f5cb68d5
VY
149/* mo_undline()
150 *
151 * parv[1] = dline to remove
152 */
3c7d6fcc 153static void
428ca87b 154mo_undline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
f5cb68d5
VY
155{
156 const char *cidr;
157 const char *target_server = NULL;
158
159 if(!IsOperK(source_p))
160 {
8bbeb278 161 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "unkline");
3c7d6fcc 162 return;
8bbeb278
AC
163 }
164
f5cb68d5
VY
165 cidr = parv[1];
166
167 if(parc >= 4 && !irccmp(parv[2], "ON"))
212380e3 168 {
2cfb7214
JT
169 if(!IsOperRemoteBan(source_p))
170 {
171 sendto_one(source_p, form_str(ERR_NOPRIVS),
8bbeb278 172 me.name, source_p->name, "remoteban");
3c7d6fcc 173 return;
2cfb7214
JT
174 }
175
f5cb68d5
VY
176 target_server = parv[3];
177 sendto_match_servs(source_p, target_server,
8bbeb278 178 CAP_ENCAP, NOCAPS, "ENCAP %s UNDLINE %s", target_server, cidr);
212380e3 179
f5cb68d5 180 if(!match(target_server, me.name))
3c7d6fcc 181 return;
212380e3
AC
182 }
183
f5cb68d5 184 apply_undline(source_p, cidr);
f5cb68d5
VY
185}
186
3c7d6fcc 187static void
428ca87b 188me_dline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
5f7a6a3d
JT
189{
190 int tdline_time = atoi(parv[1]);
191 /* Since this is coming over a server link, assume that the originating
192 * server did the relevant permission/sanity checks...
193 */
194
195 if(!IsPerson(source_p))
3c7d6fcc 196 return;
5f7a6a3d
JT
197
198 if(!find_shared_conf(source_p->username, source_p->host,
8bbeb278
AC
199 source_p->servptr->name,
200 tdline_time > 0 ? SHARED_TDLINE : SHARED_PDLINE))
3c7d6fcc 201 return;
5f7a6a3d
JT
202
203 apply_dline(source_p, parv[2], tdline_time, LOCAL_COPY(parv[3]));
204
205 check_dlines();
5f7a6a3d
JT
206}
207
3c7d6fcc 208static void
428ca87b 209me_undline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
5f7a6a3d
JT
210{
211 if(!IsPerson(source_p))
3c7d6fcc 212 return;
5f7a6a3d
JT
213
214 if(!find_shared_conf(source_p->username, source_p->host,
8bbeb278 215 source_p->servptr->name, SHARED_UNDLINE))
3c7d6fcc 216 return;
5f7a6a3d
JT
217
218 apply_undline(source_p, parv[1]);
f5cb68d5
VY
219}
220
3c7d6fcc 221static void
f5cb68d5
VY
222apply_dline(struct Client *source_p, const char *dlhost, int tdline_time, char *reason)
223{
224 struct ConfItem *aconf;
225 char *oper_reason;
f5cb68d5
VY
226 struct rb_sockaddr_storage daddr;
227 int t = AF_INET, ty, b;
228 const char *creason;
229
7d9e8e9d
EK
230 ty = parse_netmask_strict(dlhost, &daddr, &b);
231 if(ty != HM_IPV4 && ty != HM_IPV6)
f5cb68d5 232 {
8bbeb278 233 sendto_one(source_p, ":%s NOTICE %s :Invalid D-Line", me.name, source_p->name);
3c7d6fcc 234 return;
f5cb68d5 235 }
f5cb68d5
VY
236 if(ty == HM_IPV6)
237 t = AF_INET6;
238 else
f5cb68d5
VY
239 t = AF_INET;
240
241 /* This means dlines wider than /16 cannot be set remotely */
212380e3
AC
242 if(IsOperAdmin(source_p))
243 {
f5cb68d5 244 if(b < 8)
212380e3 245 {
f5cb68d5 246 sendto_one_notice(source_p,
8bbeb278 247 ":For safety, bitmasks less than 8 require conf access.");
3c7d6fcc 248 return;
212380e3
AC
249 }
250 }
251 else
252 {
f5cb68d5 253 if(b < 16)
212380e3 254 {
f5cb68d5 255 sendto_one_notice(source_p,
8bbeb278 256 ":Dline bitmasks less than 16 are for admins only.");
3c7d6fcc 257 return;
212380e3
AC
258 }
259 }
260
54ac8b60
VY
261 if(ConfigFileEntry.non_redundant_klines)
262 {
8bbeb278 263 if((aconf = find_dline((struct sockaddr *) &daddr, t)) != NULL)
54ac8b60
VY
264 {
265 int bx;
7d9e8e9d
EK
266 int masktype = parse_netmask_strict(aconf->host, NULL, &bx);
267
268 if (masktype != HM_ERROR && b >= bx)
54ac8b60
VY
269 {
270 creason = aconf->passwd ? aconf->passwd : "<No Reason>";
271 if(IsConfExemptKline(aconf))
272 sendto_one(source_p,
273 ":%s NOTICE %s :[%s] is (E)d-lined by [%s] - %s",
8bbeb278
AC
274 me.name, source_p->name, dlhost, aconf->host,
275 creason);
54ac8b60
VY
276 else
277 sendto_one(source_p,
278 ":%s NOTICE %s :[%s] already D-lined by [%s] - %s",
8bbeb278
AC
279 me.name, source_p->name, dlhost, aconf->host,
280 creason);
3c7d6fcc 281 return;
54ac8b60
VY
282 }
283 }
212380e3
AC
284 }
285
7df5fcfb 286 rb_set_time();
212380e3
AC
287
288 aconf = make_conf();
289 aconf->status = CONF_DLINE;
b52c2949 290 aconf->created = rb_current_time();
47a03750 291 aconf->host = rb_strdup(dlhost);
a12ad044 292 aconf->passwd = rb_strdup(reason);
27f616dd 293 aconf->info.oper = operhash_add(get_oper_name(source_p));
212380e3 294
9319a2e2
KB
295 if(strlen(reason) > BANREASONLEN)
296 reason[BANREASONLEN] = '\0';
297
08e8aa7a
VY
298 /* Look for an oper reason */
299 if((oper_reason = strchr(reason, '|')) != NULL)
300 {
301 *oper_reason = '\0';
302 oper_reason++;
303
304 if(!EmptyString(oper_reason))
305 aconf->spasswd = rb_strdup(oper_reason);
306 }
307
212380e3
AC
308 if(tdline_time > 0)
309 {
e3354945 310 aconf->hold = rb_current_time() + tdline_time;
212380e3
AC
311 add_temp_dline(aconf);
312
313 if(EmptyString(oper_reason))
314 {
61c096aa 315 sendto_realops_snomask(SNO_GENERAL, L_ALL,
8bbeb278
AC
316 "%s added temporary %d min. D-Line for [%s] [%s]",
317 get_oper_name(source_p), tdline_time / 60,
318 aconf->host, reason);
212380e3 319 ilog(L_KLINE, "D %s %d %s %s",
8bbeb278 320 get_oper_name(source_p), tdline_time / 60, aconf->host, reason);
212380e3
AC
321 }
322 else
323 {
61c096aa 324 sendto_realops_snomask(SNO_GENERAL, L_ALL,
8bbeb278
AC
325 "%s added temporary %d min. D-Line for [%s] [%s|%s]",
326 get_oper_name(source_p), tdline_time / 60,
327 aconf->host, reason, oper_reason);
212380e3 328 ilog(L_KLINE, "D %s %d %s %s|%s",
8bbeb278
AC
329 get_oper_name(source_p), tdline_time / 60,
330 aconf->host, reason, oper_reason);
212380e3
AC
331 }
332
333 sendto_one(source_p, ":%s NOTICE %s :Added temporary %d min. D-Line for [%s]",
334 me.name, source_p->name, tdline_time / 60, aconf->host);
335 }
336 else
337 {
40c1fd47 338 add_conf_by_address(aconf->host, CONF_DLINE, NULL, NULL, aconf);
8bbeb278
AC
339
340 bandb_add(BANDB_DLINE, source_p, aconf->host, NULL,
341 reason, EmptyString(aconf->spasswd) ? NULL : aconf->spasswd, 0);
9964e935
AC
342
343 if(EmptyString(oper_reason))
344 {
345 sendto_realops_snomask(SNO_GENERAL, L_ALL,
346 "%s added D-Line for [%s] [%s]",
347 get_oper_name(source_p), aconf->host, reason);
348 ilog(L_KLINE, "D %s 0 %s %s",
349 get_oper_name(source_p), aconf->host, reason);
350 }
351 else
352 {
353 sendto_realops_snomask(SNO_GENERAL, L_ALL,
354 "%s added D-Line for [%s] [%s|%s]",
355 get_oper_name(source_p), aconf->host, reason, oper_reason);
356 ilog(L_KLINE, "D %s 0 %s %s|%s",
357 get_oper_name(source_p),
358 aconf->host, reason, oper_reason);
359 }
212380e3 360 }
212380e3
AC
361}
362
3c7d6fcc 363static void
f5cb68d5 364apply_undline(struct Client *source_p, const char *cidr)
212380e3 365{
8bbeb278 366 char buf[BUFSIZE];
f5cb68d5 367 struct ConfItem *aconf;
212380e3 368
cf0aa421 369 int masktype = parse_netmask(cidr, NULL, NULL);
7d9e8e9d
EK
370
371 if(masktype != HM_IPV4 && masktype != HM_IPV6)
212380e3 372 {
72464c6a 373 sendto_one_notice(source_p, ":Invalid D-Line [%s] - doesn't look like IP[/cidr]", cidr);
3c7d6fcc 374 return;
212380e3
AC
375 }
376
6f3a09ff
JT
377 aconf = find_exact_conf_by_address(cidr, CONF_DLINE, NULL);
378 if(aconf == NULL)
379 {
380 sendto_one_notice(source_p, ":No D-Line for %s", cidr);
3c7d6fcc 381 return;
6f3a09ff
JT
382 }
383
f427c8b0 384 rb_strlcpy(buf, aconf->host, sizeof buf);
6f3a09ff 385 if(remove_temp_dline(aconf))
212380e3
AC
386 {
387 sendto_one(source_p,
388 ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines",
f5cb68d5 389 me.name, source_p->name, buf);
212380e3 390 sendto_realops_snomask(SNO_GENERAL, L_ALL,
8bbeb278
AC
391 "%s has removed the temporary D-Line for: [%s]",
392 get_oper_name(source_p), buf);
6f3a09ff 393 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), buf);
3c7d6fcc 394 return;
212380e3
AC
395 }
396
8bbeb278 397 bandb_del(BANDB_DLINE, aconf->host, NULL);
212380e3 398
8bbeb278
AC
399 sendto_one(source_p, ":%s NOTICE %s :D-Line for [%s] is removed", me.name, source_p->name,
400 aconf->host);
401 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s has removed the D-Line for: [%s]",
402 get_oper_name(source_p), aconf->host);
37f6cc05
JT
403 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), aconf->host);
404 delete_one_address_conf(aconf->host, aconf);
212380e3
AC
405}
406
212380e3
AC
407/* remove_temp_dline()
408 *
6f3a09ff 409 * inputs - confitem to undline
3c7d6fcc 410 * outputs - true if removed, false otherwise.
212380e3
AC
411 * side effects - tries to undline anything that matches
412 */
3c7d6fcc 413static bool
6f3a09ff 414remove_temp_dline(struct ConfItem *aconf)
212380e3 415{
5b96d9a6 416 rb_dlink_node *ptr;
212380e3
AC
417 int i;
418
8bbeb278 419 for(i = 0; i < LAST_TEMP_TYPE; i++)
212380e3 420 {
5b96d9a6 421 RB_DLINK_FOREACH(ptr, temp_dlines[i].head)
212380e3 422 {
8bbeb278 423 if(aconf == ptr->data)
212380e3 424 {
555ac41f 425 rb_dlinkDestroy(ptr, &temp_dlines[i]);
212380e3 426 delete_one_address_conf(aconf->host, aconf);
3c7d6fcc 427 return true;
212380e3
AC
428 }
429 }
430 }
431
3c7d6fcc 432 return false;
212380e3 433}