]> jfr.im git - solanum.git/blob - modules/m_dline.c
Fix -Wformat errors found in ircd-ratbox.
[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 * $Id$
25 */
26
27 #include "stdinc.h"
28 #include "channel.h"
29 #include "class.h"
30 #include "client.h"
31 #include "common.h"
32 #include "match.h"
33 #include "ircd.h"
34 #include "hostmask.h"
35 #include "numeric.h"
36 #include "s_conf.h"
37 #include "s_newconf.h"
38 #include "logger.h"
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"
45 #include "bandbi.h"
46 #include "operhash.h"
47
48 static int mo_dline(struct Client *, struct Client *, int, const char **);
49 static int me_dline(struct Client *, struct Client *, int, const char **);
50 static int mo_undline(struct Client *, struct Client *, int, const char **);
51 static int me_undline(struct Client *, struct Client *, int, const char **);
52
53 struct Message dline_msgtab = {
54 "DLINE", 0, 0, 0, MFLG_SLOW,
55 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_dline, 3}, {mo_dline, 2}}
56 };
57
58 struct Message undline_msgtab = {
59 "UNDLINE", 0, 0, 0, MFLG_SLOW,
60 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_undline, 1}, {mo_undline, 2}}
61 };
62
63 mapi_clist_av1 dline_clist[] = { &dline_msgtab, &undline_msgtab, NULL };
64
65 DECLARE_MODULE_AV1(dline, NULL, NULL, dline_clist, NULL, NULL, "$Revision$");
66
67 static int valid_comment(char *comment);
68 static int remove_temp_dline(struct ConfItem *);
69 static int apply_dline(struct Client *, const char *, int, char *);
70 static int apply_undline(struct Client *, const char *);
71
72 /* mo_dline()
73 *
74 * parv[1] - dline to add
75 * parv[2] - reason
76 */
77 static int
78 mo_dline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
79 {
80 char def[] = "No Reason";
81 const char *dlhost;
82 char *reason = def;
83 char cidr_form_host[HOSTLEN + 1];
84 int tdline_time = 0;
85 const char *target_server = NULL;
86 int loc = 1;
87
88 if(!IsOperK(source_p))
89 {
90 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "kline");
91 return 0;
92 }
93
94 if((tdline_time = valid_temp_time(parv[loc])) >= 0)
95 loc++;
96
97 dlhost = parv[loc];
98 rb_strlcpy(cidr_form_host, dlhost, sizeof(cidr_form_host));
99 loc++;
100
101 /* would break the protocol */
102 if (*dlhost == ':')
103 {
104 sendto_one_notice(source_p, ":Invalid D-Line");
105 return 0;
106 }
107
108 if(parc >= loc + 2 && !irccmp(parv[loc], "ON"))
109 {
110 if(!IsOperRemoteBan(source_p))
111 {
112 sendto_one(source_p, form_str(ERR_NOPRIVS),
113 me.name, source_p->name, "remoteban");
114 return 0;
115 }
116
117 target_server = parv[loc + 1];
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)
125 {
126 sendto_match_servs(source_p, target_server,
127 CAP_ENCAP, NOCAPS,
128 "ENCAP %s DLINE %d %s :%s",
129 target_server, tdline_time, dlhost, reason);
130
131 if(!match(target_server, me.name))
132 return 0;
133 }
134
135 apply_dline(source_p, dlhost, tdline_time, reason);
136
137 check_dlines();
138 return 0;
139 }
140
141 /* mo_undline()
142 *
143 * parv[1] = dline to remove
144 */
145 static int
146 mo_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 {
153 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "unkline");
154 return 0;
155 }
156
157 cidr = parv[1];
158
159 if(parc >= 4 && !irccmp(parv[2], "ON"))
160 {
161 if(!IsOperRemoteBan(source_p))
162 {
163 sendto_one(source_p, form_str(ERR_NOPRIVS),
164 me.name, source_p->name, "remoteban");
165 return 0;
166 }
167
168 target_server = parv[3];
169 sendto_match_servs(source_p, target_server,
170 CAP_ENCAP, NOCAPS, "ENCAP %s UNDLINE %s", target_server, cidr);
171
172 if(!match(target_server, me.name))
173 return 0;
174 }
175
176 apply_undline(source_p, cidr);
177
178 return 0;
179 }
180
181 static int
182 me_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,
193 source_p->servptr->name,
194 tdline_time > 0 ? SHARED_TDLINE : SHARED_PDLINE))
195 return 0;
196
197 apply_dline(source_p, parv[2], tdline_time, LOCAL_COPY(parv[3]));
198
199 check_dlines();
200 return 0;
201 }
202
203 static int
204 me_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,
210 source_p->servptr->name, SHARED_UNDLINE))
211 return 0;
212
213 apply_undline(source_p, parv[1]);
214
215 return 0;
216 }
217
218 static int
219 apply_dline(struct Client *source_p, const char *dlhost, int tdline_time, char *reason)
220 {
221 struct ConfItem *aconf;
222 char *oper_reason;
223 struct rb_sockaddr_storage daddr;
224 int t = AF_INET, ty, b;
225 const char *creason;
226
227 ty = parse_netmask(dlhost, (struct sockaddr *) &daddr, &b);
228 if(ty == HM_HOST)
229 {
230 sendto_one(source_p, ":%s NOTICE %s :Invalid D-Line", me.name, source_p->name);
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 */
241 if(IsOperAdmin(source_p))
242 {
243 if(b < 8)
244 {
245 sendto_one_notice(source_p,
246 ":For safety, bitmasks less than 8 require conf access.");
247 return 0;
248 }
249 }
250 else
251 {
252 if(b < 16)
253 {
254 sendto_one_notice(source_p,
255 ":Dline bitmasks less than 16 are for admins only.");
256 return 0;
257 }
258 }
259
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
268 if(ConfigFileEntry.non_redundant_klines)
269 {
270 if((aconf = find_dline((struct sockaddr *) &daddr, t)) != NULL)
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",
280 me.name, source_p->name, dlhost, aconf->host,
281 creason);
282 else
283 sendto_one(source_p,
284 ":%s NOTICE %s :[%s] already D-lined by [%s] - %s",
285 me.name, source_p->name, dlhost, aconf->host,
286 creason);
287 return 0;
288 }
289 }
290 }
291
292 rb_set_time();
293
294 aconf = make_conf();
295 aconf->status = CONF_DLINE;
296 aconf->created = rb_current_time();
297 aconf->host = rb_strdup(dlhost);
298 aconf->passwd = rb_strdup(reason);
299 aconf->info.oper = operhash_add(get_oper_name(source_p));
300
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
311 if(tdline_time > 0)
312 {
313 aconf->hold = rb_current_time() + tdline_time;
314 add_temp_dline(aconf);
315
316 if(EmptyString(oper_reason))
317 {
318 sendto_realops_snomask(SNO_GENERAL, L_ALL,
319 "%s added temporary %d min. D-Line for [%s] [%s]",
320 get_oper_name(source_p), tdline_time / 60,
321 aconf->host, reason);
322 ilog(L_KLINE, "D %s %d %s %s",
323 get_oper_name(source_p), tdline_time / 60, aconf->host, reason);
324 }
325 else
326 {
327 sendto_realops_snomask(SNO_GENERAL, L_ALL,
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);
331 ilog(L_KLINE, "D %s %d %s %s|%s",
332 get_oper_name(source_p), tdline_time / 60,
333 aconf->host, reason, oper_reason);
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 {
341 add_conf_by_address(aconf->host, CONF_DLINE, NULL, NULL, aconf);
342
343 bandb_add(BANDB_DLINE, source_p, aconf->host, NULL,
344 reason, EmptyString(aconf->spasswd) ? NULL : aconf->spasswd, 0);
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 }
363 }
364
365 return 0;
366 }
367
368 static int
369 apply_undline(struct Client *source_p, const char *cidr)
370 {
371 char buf[BUFSIZE];
372 struct ConfItem *aconf;
373
374 if(parse_netmask(cidr, NULL, NULL) == HM_HOST)
375 {
376 sendto_one_notice(source_p, ":Invalid D-Line");
377 return 0;
378 }
379
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
387 rb_strlcpy(buf, aconf->host, sizeof buf);
388 if(remove_temp_dline(aconf))
389 {
390 sendto_one(source_p,
391 ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines",
392 me.name, source_p->name, buf);
393 sendto_realops_snomask(SNO_GENERAL, L_ALL,
394 "%s has removed the temporary D-Line for: [%s]",
395 get_oper_name(source_p), buf);
396 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), buf);
397 return 0;
398 }
399
400 bandb_del(BANDB_DLINE, aconf->host, NULL);
401
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);
406 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), aconf->host);
407 delete_one_address_conf(aconf->host, aconf);
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 */
419 static int
420 valid_comment(char *comment)
421 {
422 if(strchr(comment, '"'))
423 return 0;
424
425 if(strlen(comment) > BANREASONLEN)
426 comment[BANREASONLEN] = '\0';
427
428 return 1;
429 }
430
431 /* remove_temp_dline()
432 *
433 * inputs - confitem to undline
434 * outputs -
435 * side effects - tries to undline anything that matches
436 */
437 static int
438 remove_temp_dline(struct ConfItem *aconf)
439 {
440 rb_dlink_node *ptr;
441 int i;
442
443 for(i = 0; i < LAST_TEMP_TYPE; i++)
444 {
445 RB_DLINK_FOREACH(ptr, temp_dlines[i].head)
446 {
447 if(aconf == ptr->data)
448 {
449 rb_dlinkDestroy(ptr, &temp_dlines[i]);
450 delete_one_address_conf(aconf->host, aconf);
451 return YES;
452 }
453 }
454 }
455
456 return NO;
457 }