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