]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_dline.c
Generate the "Temporary K-line %d min" part from aconf->hold - aconf->created.
[irc/rqf/shadowircd.git] / modules / m_dline.c
CommitLineData
212380e3 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 *
d8a4c5f6 24 * $Id$
212380e3 25 */
26
27#include "stdinc.h"
212380e3 28#include "channel.h"
29#include "class.h"
30#include "client.h"
31#include "common.h"
13ae2f4b 32#include "match.h"
212380e3 33#include "ircd.h"
34#include "hostmask.h"
35#include "numeric.h"
212380e3 36#include "s_conf.h"
37#include "s_newconf.h"
d3455e2c 38#include "logger.h"
212380e3 39#include "send.h"
40#include "hash.h"
41#include "s_serv.h"
42#include "msg.h"
43#include "parse.h"
44#include "modules.h"
d8a4c5f6 45#include "bandbi.h"
212380e3 46
47static int mo_dline(struct Client *, struct Client *, int, const char **);
45be325c 48static int me_dline(struct Client *, struct Client *, int, const char **);
212380e3 49static int mo_undline(struct Client *, struct Client *, int, const char **);
45be325c 50static int me_undline(struct Client *, struct Client *, int, const char **);
212380e3 51
52struct Message dline_msgtab = {
53 "DLINE", 0, 0, 0, MFLG_SLOW,
45be325c 54 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_dline, 3}, {mo_dline, 2}}
212380e3 55};
d8a4c5f6 56
212380e3 57struct Message undline_msgtab = {
58 "UNDLINE", 0, 0, 0, MFLG_SLOW,
45be325c 59 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_undline, 1}, {mo_undline, 2}}
212380e3 60};
61
62mapi_clist_av1 dline_clist[] = { &dline_msgtab, &undline_msgtab, NULL };
d8a4c5f6
WP
63
64DECLARE_MODULE_AV1(dline, NULL, NULL, dline_clist, NULL, NULL, "$Revision$");
212380e3 65
66static int valid_comment(char *comment);
6f3a09ff 67static int remove_temp_dline(struct ConfItem *);
e26306d3 68static int apply_dline(struct Client *, const char *, int, char *);
45be325c 69static int apply_undline(struct Client *, const char *);
212380e3 70
71/* mo_dline()
72 *
73 * parv[1] - dline to add
74 * parv[2] - reason
75 */
76static int
d8a4c5f6 77mo_dline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3 78{
79 char def[] = "No Reason";
80 const char *dlhost;
212380e3 81 char *reason = def;
212380e3 82 char cidr_form_host[HOSTLEN + 1];
212380e3 83 int tdline_time = 0;
45be325c 84 const char *target_server = NULL;
212380e3 85 int loc = 1;
86
87 if(!IsOperK(source_p))
88 {
d8a4c5f6 89 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "kline");
212380e3 90 return 0;
91 }
92
93 if((tdline_time = valid_temp_time(parv[loc])) >= 0)
94 loc++;
95
212380e3 96 dlhost = parv[loc];
907468c4 97 rb_strlcpy(cidr_form_host, dlhost, sizeof(cidr_form_host));
212380e3 98
45be325c
VY
99 loc++;
100
d8a4c5f6
WP
101 if(parc >= loc + 2 && !irccmp(parv[loc], "ON"))
102 {
a66dbced
JT
103 if(!IsOperRemoteBan(source_p))
104 {
105 sendto_one(source_p, form_str(ERR_NOPRIVS),
d8a4c5f6 106 me.name, source_p->name, "remoteban");
a66dbced
JT
107 return 0;
108 }
109
d8a4c5f6 110 target_server = parv[loc + 1];
45be325c
VY
111 loc += 2;
112 }
113
114 if(parc >= loc + 1 && !EmptyString(parv[loc]))
115 reason = LOCAL_COPY(parv[loc]);
116
117 if(target_server != NULL)
212380e3 118 {
45be325c 119 sendto_match_servs(source_p, target_server,
d8a4c5f6
WP
120 CAP_ENCAP, NOCAPS,
121 "ENCAP %s DLINE %d %s :%s",
122 target_server, tdline_time, dlhost, reason);
45be325c
VY
123
124 if(!match(target_server, me.name))
125 return 0;
212380e3 126 }
127
45be325c
VY
128 apply_dline(source_p, dlhost, tdline_time, reason);
129
130 check_dlines();
131 return 0;
132}
212380e3 133
45be325c
VY
134/* mo_undline()
135 *
136 * parv[1] = dline to remove
137 */
138static int
139mo_undline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
140{
141 const char *cidr;
142 const char *target_server = NULL;
143
144 if(!IsOperK(source_p))
145 {
d8a4c5f6
WP
146 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "unkline");
147 return 0;
148 }
149
45be325c
VY
150 cidr = parv[1];
151
152 if(parc >= 4 && !irccmp(parv[2], "ON"))
212380e3 153 {
a66dbced
JT
154 if(!IsOperRemoteBan(source_p))
155 {
156 sendto_one(source_p, form_str(ERR_NOPRIVS),
d8a4c5f6 157 me.name, source_p->name, "remoteban");
a66dbced
JT
158 return 0;
159 }
160
45be325c
VY
161 target_server = parv[3];
162 sendto_match_servs(source_p, target_server,
d8a4c5f6 163 CAP_ENCAP, NOCAPS, "ENCAP %s UNDLINE %s", target_server, cidr);
212380e3 164
45be325c 165 if(!match(target_server, me.name))
212380e3 166 return 0;
212380e3 167 }
168
45be325c
VY
169 apply_undline(source_p, cidr);
170
171 return 0;
172}
173
e26306d3
JT
174static int
175me_dline(struct Client *client_p, struct Client *source_p, int parc, const char **parv)
176{
177 int tdline_time = atoi(parv[1]);
178 /* Since this is coming over a server link, assume that the originating
179 * server did the relevant permission/sanity checks...
180 */
181
182 if(!IsPerson(source_p))
183 return 0;
184
185 if(!find_shared_conf(source_p->username, source_p->host,
d8a4c5f6
WP
186 source_p->servptr->name,
187 tdline_time > 0 ? SHARED_TDLINE : SHARED_PDLINE))
e26306d3 188 return 0;
e26306d3
JT
189
190 apply_dline(source_p, parv[2], tdline_time, LOCAL_COPY(parv[3]));
191
192 check_dlines();
193 return 0;
194}
195
196static int
197me_undline(struct Client *client_p, struct Client *source_p, int parc, const char **parv)
198{
199 if(!IsPerson(source_p))
200 return 0;
201
202 if(!find_shared_conf(source_p->username, source_p->host,
d8a4c5f6 203 source_p->servptr->name, SHARED_UNDLINE))
e26306d3
JT
204 return 0;
205
206 apply_undline(source_p, parv[1]);
207
208 return 0;
45be325c
VY
209}
210
211static int
212apply_dline(struct Client *source_p, const char *dlhost, int tdline_time, char *reason)
213{
214 struct ConfItem *aconf;
215 char *oper_reason;
45be325c
VY
216 struct rb_sockaddr_storage daddr;
217 int t = AF_INET, ty, b;
218 const char *creason;
219
d8a4c5f6 220 ty = parse_netmask(dlhost, (struct sockaddr *) &daddr, &b);
45be325c
VY
221 if(ty == HM_HOST)
222 {
d8a4c5f6 223 sendto_one(source_p, ":%s NOTICE %s :Invalid D-Line", me.name, source_p->name);
45be325c
VY
224 return 0;
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 */
212380e3 234 if(IsOperAdmin(source_p))
235 {
45be325c 236 if(b < 8)
212380e3 237 {
45be325c 238 sendto_one_notice(source_p,
d8a4c5f6 239 ":For safety, bitmasks less than 8 require conf access.");
212380e3 240 return 0;
241 }
242 }
243 else
244 {
45be325c 245 if(b < 16)
212380e3 246 {
45be325c 247 sendto_one_notice(source_p,
d8a4c5f6 248 ":Dline bitmasks less than 16 are for admins only.");
212380e3 249 return 0;
250 }
251 }
252
45be325c
VY
253 if(!valid_comment(reason))
254 {
255 sendto_one(source_p,
256 ":%s NOTICE %s :Invalid character '\"' in comment",
257 me.name, source_p->name);
258 return 0;
259 }
260
21c9d815
VY
261 if(ConfigFileEntry.non_redundant_klines)
262 {
d8a4c5f6 263 if((aconf = find_dline((struct sockaddr *) &daddr, t)) != NULL)
21c9d815
VY
264 {
265 int bx;
266 parse_netmask(aconf->host, NULL, &bx);
267 if(b >= bx)
268 {
269 creason = aconf->passwd ? aconf->passwd : "<No Reason>";
270 if(IsConfExemptKline(aconf))
271 sendto_one(source_p,
272 ":%s NOTICE %s :[%s] is (E)d-lined by [%s] - %s",
d8a4c5f6
WP
273 me.name, source_p->name, dlhost, aconf->host,
274 creason);
21c9d815
VY
275 else
276 sendto_one(source_p,
277 ":%s NOTICE %s :[%s] already D-lined by [%s] - %s",
d8a4c5f6
WP
278 me.name, source_p->name, dlhost, aconf->host,
279 creason);
21c9d815
VY
280 return 0;
281 }
282 }
212380e3 283 }
284
4362b282 285 rb_set_time();
212380e3 286
287 aconf = make_conf();
288 aconf->status = CONF_DLINE;
ce60772d 289 aconf->created = rb_current_time();
62d28946 290 aconf->host = rb_strdup(dlhost);
157c1f04 291 aconf->passwd = rb_strdup(reason);
212380e3 292
1bf48553
VY
293 /* Look for an oper reason */
294 if((oper_reason = strchr(reason, '|')) != NULL)
295 {
296 *oper_reason = '\0';
297 oper_reason++;
298
299 if(!EmptyString(oper_reason))
300 aconf->spasswd = rb_strdup(oper_reason);
301 }
302
212380e3 303 if(tdline_time > 0)
304 {
9f6bbe3c 305 aconf->hold = rb_current_time() + tdline_time;
212380e3 306 add_temp_dline(aconf);
307
308 if(EmptyString(oper_reason))
309 {
931e8bc4 310 sendto_realops_snomask(SNO_GENERAL, L_ALL,
d8a4c5f6
WP
311 "%s added temporary %d min. D-Line for [%s] [%s]",
312 get_oper_name(source_p), tdline_time / 60,
313 aconf->host, reason);
212380e3 314 ilog(L_KLINE, "D %s %d %s %s",
d8a4c5f6 315 get_oper_name(source_p), tdline_time / 60, aconf->host, reason);
212380e3 316 }
317 else
318 {
931e8bc4 319 sendto_realops_snomask(SNO_GENERAL, L_ALL,
d8a4c5f6
WP
320 "%s added temporary %d min. D-Line for [%s] [%s|%s]",
321 get_oper_name(source_p), tdline_time / 60,
322 aconf->host, reason, oper_reason);
212380e3 323 ilog(L_KLINE, "D %s %d %s %s|%s",
d8a4c5f6
WP
324 get_oper_name(source_p), tdline_time / 60,
325 aconf->host, reason, oper_reason);
212380e3 326 }
327
328 sendto_one(source_p, ":%s NOTICE %s :Added temporary %d min. D-Line for [%s]",
329 me.name, source_p->name, tdline_time / 60, aconf->host);
330 }
331 else
332 {
969a1ae6 333 add_conf_by_address(aconf->host, CONF_DLINE, NULL, NULL, aconf);
d8a4c5f6
WP
334
335 bandb_add(BANDB_DLINE, source_p, aconf->host, NULL,
336 reason, EmptyString(aconf->spasswd) ? NULL : aconf->spasswd, 0);
d63447bf
WP
337
338 if(EmptyString(oper_reason))
339 {
340 sendto_realops_snomask(SNO_GENERAL, L_ALL,
341 "%s added D-Line for [%s] [%s]",
342 get_oper_name(source_p), aconf->host, reason);
343 ilog(L_KLINE, "D %s 0 %s %s",
344 get_oper_name(source_p), aconf->host, reason);
345 }
346 else
347 {
348 sendto_realops_snomask(SNO_GENERAL, L_ALL,
349 "%s added D-Line for [%s] [%s|%s]",
350 get_oper_name(source_p), aconf->host, reason, oper_reason);
351 ilog(L_KLINE, "D %s 0 %s %s|%s",
352 get_oper_name(source_p),
353 aconf->host, reason, oper_reason);
354 }
212380e3 355 }
356
212380e3 357 return 0;
358}
359
212380e3 360static int
45be325c 361apply_undline(struct Client *source_p, const char *cidr)
212380e3 362{
d8a4c5f6 363 char buf[BUFSIZE];
45be325c 364 struct ConfItem *aconf;
212380e3 365
366 if(parse_netmask(cidr, NULL, NULL) == HM_HOST)
367 {
5366977b 368 sendto_one_notice(source_p, ":Invalid D-Line");
212380e3 369 return 0;
370 }
371
6f3a09ff
JT
372 aconf = find_exact_conf_by_address(cidr, CONF_DLINE, NULL);
373 if(aconf == NULL)
374 {
375 sendto_one_notice(source_p, ":No D-Line for %s", cidr);
376 return 0;
377 }
378
907468c4 379 rb_strlcpy(buf, aconf->host, sizeof buf);
6f3a09ff 380 if(remove_temp_dline(aconf))
212380e3 381 {
382 sendto_one(source_p,
383 ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines",
45be325c 384 me.name, source_p->name, buf);
212380e3 385 sendto_realops_snomask(SNO_GENERAL, L_ALL,
d8a4c5f6
WP
386 "%s has removed the temporary D-Line for: [%s]",
387 get_oper_name(source_p), buf);
6f3a09ff 388 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), buf);
212380e3 389 return 0;
390 }
391
d8a4c5f6 392 bandb_del(BANDB_DLINE, aconf->host, NULL);
212380e3 393
d8a4c5f6
WP
394 sendto_one(source_p, ":%s NOTICE %s :D-Line for [%s] is removed", me.name, source_p->name,
395 aconf->host);
396 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s has removed the D-Line for: [%s]",
397 get_oper_name(source_p), aconf->host);
47819d98
JT
398 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), aconf->host);
399 delete_one_address_conf(aconf->host, aconf);
212380e3 400
401 return 0;
402}
403
404/*
405 * valid_comment
406 * inputs - pointer to client
407 * - pointer to comment
408 * output - 0 if no valid comment, 1 if valid
409 * side effects - NONE
410 */
411static int
412valid_comment(char *comment)
413{
414 if(strchr(comment, '"'))
415 return 0;
416
61569b65 417 if(strlen(comment) > BANREASONLEN)
418 comment[BANREASONLEN] = '\0';
212380e3 419
420 return 1;
421}
422
212380e3 423/* remove_temp_dline()
424 *
6f3a09ff 425 * inputs - confitem to undline
212380e3 426 * outputs -
427 * side effects - tries to undline anything that matches
428 */
429static int
6f3a09ff 430remove_temp_dline(struct ConfItem *aconf)
212380e3 431{
08d11e34 432 rb_dlink_node *ptr;
212380e3 433 int i;
434
d8a4c5f6 435 for(i = 0; i < LAST_TEMP_TYPE; i++)
212380e3 436 {
08d11e34 437 RB_DLINK_FOREACH(ptr, temp_dlines[i].head)
212380e3 438 {
d8a4c5f6 439 if(aconf == ptr->data)
212380e3 440 {
9f6c3353 441 rb_dlinkDestroy(ptr, &temp_dlines[i]);
212380e3 442 delete_one_address_conf(aconf->host, aconf);
443 return YES;
444 }
445 }
446 }
447
448 return NO;
449}