]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_dline.c
Patch for remote dlines support - test needed
[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 *
61569b65 24 * $Id: m_dline.c 3225 2007-03-04 23:42:55Z jilles $
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"
45
46static int mo_dline(struct Client *, struct Client *, int, const char **);
47static int mo_undline(struct Client *, struct Client *, int, const char **);
48
49struct Message dline_msgtab = {
50 "DLINE", 0, 0, 0, MFLG_SLOW,
51 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_dline, 2}}
52};
53struct Message undline_msgtab = {
54 "UNDLINE", 0, 0, 0, MFLG_SLOW,
55 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_undline, 2}}
56};
57
58mapi_clist_av1 dline_clist[] = { &dline_msgtab, &undline_msgtab, NULL };
61569b65 59DECLARE_MODULE_AV1(dline, NULL, NULL, dline_clist, NULL, NULL, "$Revision: 3225 $");
212380e3 60
61static int valid_comment(char *comment);
62static int flush_write(struct Client *, FILE *, char *, char *);
6f3a09ff 63static int remove_temp_dline(struct ConfItem *);
212380e3 64
65/* mo_dline()
66 *
67 * parv[1] - dline to add
68 * parv[2] - reason
69 */
70static int
71mo_dline(struct Client *client_p, struct Client *source_p,
72 int parc, const char *parv[])
73{
74 char def[] = "No Reason";
75 const char *dlhost;
76 char *oper_reason;
77 char *reason = def;
3ea5fee7 78 struct rb_sockaddr_storage daddr;
212380e3 79 char cidr_form_host[HOSTLEN + 1];
80 struct ConfItem *aconf;
81 int bits;
82 char dlbuffer[IRCD_BUFSIZE];
83 const char *current_date;
84 int tdline_time = 0;
85 int loc = 1;
86
87 if(!IsOperK(source_p))
88 {
89 sendto_one(source_p, form_str(ERR_NOPRIVS),
90 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 if(parc < loc + 1)
98 {
99 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
100 me.name, source_p->name, "DLINE");
101 return 0;
102 }
103
104 dlhost = parv[loc];
907468c4 105 rb_strlcpy(cidr_form_host, dlhost, sizeof(cidr_form_host));
212380e3 106
107 if(!parse_netmask(dlhost, NULL, &bits))
108 {
109 sendto_one(source_p, ":%s NOTICE %s :Invalid D-Line",
110 me.name, source_p->name);
111 return 0;
112 }
113
114 loc++;
115
116 if(parc >= loc + 1) /* host :reason */
117 {
118 if(!EmptyString(parv[loc]))
119 reason = LOCAL_COPY(parv[loc]);
120
121 if(!valid_comment(reason))
122 {
123 sendto_one(source_p,
124 ":%s NOTICE %s :Invalid character '\"' in comment",
125 me.name, source_p->name);
126 return 0;
127 }
128 }
129
130 if(IsOperAdmin(source_p))
131 {
132 if(bits < 8)
133 {
134 sendto_one(source_p,
135 ":%s NOTICE %s :For safety, bitmasks less than 8 require conf access.",
136 me.name, parv[0]);
137 return 0;
138 }
139 }
140 else
141 {
142 if(bits < 16)
143 {
144 sendto_one(source_p,
145 ":%s NOTICE %s :Dline bitmasks less than 16 are for admins only.",
146 me.name, parv[0]);
147 return 0;
148 }
149 }
150
21c9d815
VY
151 if(ConfigFileEntry.non_redundant_klines)
152 {
153 const char *creason;
154 int t = AF_INET, ty, b;
155 ty = parse_netmask(dlhost, (struct sockaddr *)&daddr, &b);
2f5fa921 156#ifdef RB_IPV6
21c9d815
VY
157 if(ty == HM_IPV6)
158 t = AF_INET6;
159 else
160#endif
161 t = AF_INET;
162
163 if((aconf = find_dline((struct sockaddr *)&daddr, t)) != NULL)
164 {
165 int bx;
166 parse_netmask(aconf->host, NULL, &bx);
167 if(b >= bx)
168 {
169 creason = aconf->passwd ? aconf->passwd : "<No Reason>";
170 if(IsConfExemptKline(aconf))
171 sendto_one(source_p,
172 ":%s NOTICE %s :[%s] is (E)d-lined by [%s] - %s",
173 me.name, parv[0], dlhost, aconf->host, creason);
174 else
175 sendto_one(source_p,
176 ":%s NOTICE %s :[%s] already D-lined by [%s] - %s",
177 me.name, parv[0], dlhost, aconf->host, creason);
178 return 0;
179 }
180 }
212380e3 181 }
182
4362b282 183 rb_set_time();
212380e3 184 current_date = smalldate();
185
186 aconf = make_conf();
187 aconf->status = CONF_DLINE;
62d28946 188 aconf->host = rb_strdup(dlhost);
212380e3 189
190 /* Look for an oper reason */
191 if((oper_reason = strchr(reason, '|')) != NULL)
192 {
193 *oper_reason = '\0';
194 oper_reason++;
195
196 if(!EmptyString(oper_reason))
62d28946 197 aconf->spasswd = rb_strdup(oper_reason);
212380e3 198 }
199
200 if(tdline_time > 0)
201 {
581fa5c4 202 rb_snprintf(dlbuffer, sizeof(dlbuffer),
212380e3 203 "Temporary D-line %d min. - %s (%s)",
204 (int) (tdline_time / 60), reason, current_date);
62d28946 205 aconf->passwd = rb_strdup(dlbuffer);
9f6bbe3c 206 aconf->hold = rb_current_time() + tdline_time;
212380e3 207 add_temp_dline(aconf);
208
209 if(EmptyString(oper_reason))
210 {
211 sendto_realops_snomask(SNO_GENERAL, L_ALL,
212 "%s added temporary %d min. D-Line for [%s] [%s]",
213 get_oper_name(source_p), tdline_time / 60,
214 aconf->host, reason);
215 ilog(L_KLINE, "D %s %d %s %s",
216 get_oper_name(source_p), tdline_time / 60,
217 aconf->host, reason);
218 }
219 else
220 {
221 sendto_realops_snomask(SNO_GENERAL, L_ALL,
222 "%s added temporary %d min. D-Line for [%s] [%s|%s]",
223 get_oper_name(source_p), tdline_time / 60,
224 aconf->host, reason, oper_reason);
225 ilog(L_KLINE, "D %s %d %s %s|%s",
226 get_oper_name(source_p), tdline_time / 60,
227 aconf->host, reason, oper_reason);
228 }
229
230 sendto_one(source_p, ":%s NOTICE %s :Added temporary %d min. D-Line for [%s]",
231 me.name, source_p->name, tdline_time / 60, aconf->host);
232 }
233 else
234 {
581fa5c4 235 rb_snprintf(dlbuffer, sizeof(dlbuffer), "%s (%s)", reason, current_date);
62d28946 236 aconf->passwd = rb_strdup(dlbuffer);
212380e3 237 add_conf_by_address(aconf->host, CONF_DLINE, NULL, aconf);
238 write_confitem(DLINE_TYPE, source_p, NULL, aconf->host, reason,
239 oper_reason, current_date, 0);
240 }
241
242 check_dlines();
243 return 0;
244}
245
246/* mo_undline()
247 *
248 * parv[1] = dline to remove
249 */
250static int
251mo_undline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
252{
253 FILE *in;
254 FILE *out;
255 char buf[BUFSIZE], buff[BUFSIZE], temppath[BUFSIZE], *p;
256 const char *filename, *found_cidr;
257 const char *cidr;
6f3a09ff 258 struct ConfItem *aconf;
212380e3 259 int pairme = NO, error_on_write = NO;
260 mode_t oldumask;
261
581fa5c4 262 rb_snprintf(temppath, sizeof(temppath), "%s.tmp", ConfigFileEntry.dlinefile);
212380e3 263
264 if(!IsOperUnkline(source_p))
265 {
266 sendto_one(source_p, form_str(ERR_NOPRIVS),
267 me.name, source_p->name, "unkline");
268 return 0;
269 }
270
271 cidr = parv[1];
272
273 if(parse_netmask(cidr, NULL, NULL) == HM_HOST)
274 {
5366977b 275 sendto_one_notice(source_p, ":Invalid D-Line");
212380e3 276 return 0;
277 }
278
6f3a09ff
JT
279 aconf = find_exact_conf_by_address(cidr, CONF_DLINE, NULL);
280 if(aconf == NULL)
281 {
282 sendto_one_notice(source_p, ":No D-Line for %s", cidr);
283 return 0;
284 }
285
907468c4 286 rb_strlcpy(buf, aconf->host, sizeof buf);
6f3a09ff 287 if(remove_temp_dline(aconf))
212380e3 288 {
289 sendto_one(source_p,
290 ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines",
6f3a09ff 291 me.name, parv[0], buf);
212380e3 292 sendto_realops_snomask(SNO_GENERAL, L_ALL,
293 "%s has removed the temporary D-Line for: [%s]",
6f3a09ff
JT
294 get_oper_name(source_p), buf);
295 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), buf);
212380e3 296 return 0;
297 }
298
299 filename = get_conf_name(DLINE_TYPE);
300
301 if((in = fopen(filename, "r")) == 0)
302 {
303 sendto_one(source_p, ":%s NOTICE %s :Cannot open %s", me.name, parv[0], filename);
304 return 0;
305 }
306
307 oldumask = umask(0);
308 if((out = fopen(temppath, "w")) == 0)
309 {
310 sendto_one(source_p, ":%s NOTICE %s :Cannot open %s", me.name, parv[0], temppath);
311 fclose(in);
312 umask(oldumask);
313 return 0;
314 }
315
316 umask(oldumask);
317
318 while (fgets(buf, sizeof(buf), in))
319 {
907468c4 320 rb_strlcpy(buff, buf, sizeof(buff));
212380e3 321
322 if((p = strchr(buff, '\n')) != NULL)
323 *p = '\0';
324
325 if((*buff == '\0') || (*buff == '#'))
326 {
327 if(!error_on_write)
328 flush_write(source_p, out, buf, temppath);
329 continue;
330 }
331
332 if((found_cidr = getfield(buff)) == NULL)
333 {
334 if(!error_on_write)
335 flush_write(source_p, out, buf, temppath);
336 continue;
337 }
338
6f3a09ff 339 if(irccmp(found_cidr, aconf->host) == 0)
212380e3 340 {
341 pairme++;
342 }
343 else
344 {
345 if(!error_on_write)
346 flush_write(source_p, out, buf, temppath);
347 continue;
348 }
349 }
350
351 fclose(in);
352 if (fclose(out))
353 error_on_write = YES;
354
355 if(error_on_write)
356 {
357 sendto_one(source_p,
358 ":%s NOTICE %s :Couldn't write D-line file, aborted",
359 me.name, parv[0]);
360 return 0;
361 }
362 else if(!pairme)
363 {
6f3a09ff
JT
364 sendto_one_notice(source_p, ":Cannot find D-Line for %s in file",
365 aconf->host);
212380e3 366
367 if(temppath != NULL)
368 (void) unlink(temppath);
369
370 return 0;
371 }
372
373 if (rename(temppath, filename))
374 {
375 sendto_one_notice(source_p, ":Couldn't rename temp file, aborted");
376 return 0;
377 }
212380e3 378
6f3a09ff 379 sendto_one(source_p, ":%s NOTICE %s :D-Line for [%s] is removed", me.name, parv[0], aconf->host);
212380e3 380 sendto_realops_snomask(SNO_GENERAL, L_ALL,
6f3a09ff
JT
381 "%s has removed the D-Line for: [%s]", get_oper_name(source_p), aconf->host);
382 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), aconf->host);
383
384 delete_one_address_conf(aconf->host, aconf);
212380e3 385
386 return 0;
387}
388
389/*
390 * valid_comment
391 * inputs - pointer to client
392 * - pointer to comment
393 * output - 0 if no valid comment, 1 if valid
394 * side effects - NONE
395 */
396static int
397valid_comment(char *comment)
398{
399 if(strchr(comment, '"'))
400 return 0;
401
61569b65 402 if(strlen(comment) > BANREASONLEN)
403 comment[BANREASONLEN] = '\0';
212380e3 404
405 return 1;
406}
407
408/*
409 * flush_write()
410 *
411 * inputs - pointer to client structure of oper requesting unkline
412 * - out is the file descriptor
413 * - buf is the buffer to write
414 * - ntowrite is the expected number of character to be written
415 * - temppath is the temporary file name to be written
416 * output - YES for error on write
417 * - NO for success
418 * side effects - if successful, the buf is written to output file
419 * if a write failure happesn, and the file pointed to
420 * by temppath, if its non NULL, is removed.
421 *
422 * The idea here is, to be as robust as possible when writing to the
423 * kline file.
424 *
425 * -Dianora
426 */
427static int
428flush_write(struct Client *source_p, FILE * out, char *buf, char *temppath)
429{
430 int error_on_write = (fputs(buf, out) < 0) ? YES : NO;
431
432 if(error_on_write)
433 {
5366977b 434 sendto_one_notice(source_p, ":Unable to write to %s", temppath);
212380e3 435 fclose(out);
436 if(temppath != NULL)
437 (void) unlink(temppath);
438 }
439 return (error_on_write);
440}
441
442/* remove_temp_dline()
443 *
6f3a09ff 444 * inputs - confitem to undline
212380e3 445 * outputs -
446 * side effects - tries to undline anything that matches
447 */
448static int
6f3a09ff 449remove_temp_dline(struct ConfItem *aconf)
212380e3 450{
08d11e34 451 rb_dlink_node *ptr;
212380e3 452 int i;
453
212380e3 454 for (i = 0; i < LAST_TEMP_TYPE; i++)
455 {
08d11e34 456 RB_DLINK_FOREACH(ptr, temp_dlines[i].head)
212380e3 457 {
6f3a09ff 458 if (aconf == ptr->data)
212380e3 459 {
9f6c3353 460 rb_dlinkDestroy(ptr, &temp_dlines[i]);
212380e3 461 delete_one_address_conf(aconf->host, aconf);
462 return YES;
463 }
464 }
465 }
466
467 return NO;
468}