]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/m_dline.c
irc_string.h -> match.h, irc_string.h; includes changed
[irc/rqf/shadowircd.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: m_dline.c 3225 2007-03-04 23:42:55Z jilles $
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
46 static int mo_dline(struct Client *, struct Client *, int, const char **);
47 static int mo_undline(struct Client *, struct Client *, int, const char **);
48
49 struct 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 };
53 struct 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
58 mapi_clist_av1 dline_clist[] = { &dline_msgtab, &undline_msgtab, NULL };
59 DECLARE_MODULE_AV1(dline, NULL, NULL, dline_clist, NULL, NULL, "$Revision: 3225 $");
60
61 static int valid_comment(char *comment);
62 static int flush_write(struct Client *, FILE *, char *, char *);
63 static int remove_temp_dline(struct ConfItem *);
64
65 /* mo_dline()
66 *
67 * parv[1] - dline to add
68 * parv[2] - reason
69 */
70 static int
71 mo_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;
78 struct rb_sockaddr_storage daddr;
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];
105 rb_strlcpy(cidr_form_host, dlhost, sizeof(cidr_form_host));
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
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);
156 #ifdef RB_IPV6
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 }
181 }
182
183 rb_set_time();
184 current_date = smalldate();
185
186 aconf = make_conf();
187 aconf->status = CONF_DLINE;
188 aconf->host = rb_strdup(dlhost);
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))
197 aconf->spasswd = rb_strdup(oper_reason);
198 }
199
200 if(tdline_time > 0)
201 {
202 rb_snprintf(dlbuffer, sizeof(dlbuffer),
203 "Temporary D-line %d min. - %s (%s)",
204 (int) (tdline_time / 60), reason, current_date);
205 aconf->passwd = rb_strdup(dlbuffer);
206 aconf->hold = rb_current_time() + tdline_time;
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 {
235 rb_snprintf(dlbuffer, sizeof(dlbuffer), "%s (%s)", reason, current_date);
236 aconf->passwd = rb_strdup(dlbuffer);
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 */
250 static int
251 mo_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;
258 struct ConfItem *aconf;
259 int pairme = NO, error_on_write = NO;
260 mode_t oldumask;
261
262 rb_snprintf(temppath, sizeof(temppath), "%s.tmp", ConfigFileEntry.dlinefile);
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 {
275 sendto_one_notice(source_p, ":Invalid D-Line");
276 return 0;
277 }
278
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
286 rb_strlcpy(buf, aconf->host, sizeof buf);
287 if(remove_temp_dline(aconf))
288 {
289 sendto_one(source_p,
290 ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines",
291 me.name, parv[0], buf);
292 sendto_realops_snomask(SNO_GENERAL, L_ALL,
293 "%s has removed the temporary D-Line for: [%s]",
294 get_oper_name(source_p), buf);
295 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), buf);
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 {
320 rb_strlcpy(buff, buf, sizeof(buff));
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
339 if(irccmp(found_cidr, aconf->host) == 0)
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 {
364 sendto_one_notice(source_p, ":Cannot find D-Line for %s in file",
365 aconf->host);
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 }
378
379 sendto_one(source_p, ":%s NOTICE %s :D-Line for [%s] is removed", me.name, parv[0], aconf->host);
380 sendto_realops_snomask(SNO_GENERAL, L_ALL,
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);
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 */
396 static int
397 valid_comment(char *comment)
398 {
399 if(strchr(comment, '"'))
400 return 0;
401
402 if(strlen(comment) > BANREASONLEN)
403 comment[BANREASONLEN] = '\0';
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 */
427 static int
428 flush_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 {
434 sendto_one_notice(source_p, ":Unable to write to %s", temppath);
435 fclose(out);
436 if(temppath != NULL)
437 (void) unlink(temppath);
438 }
439 return (error_on_write);
440 }
441
442 /* remove_temp_dline()
443 *
444 * inputs - confitem to undline
445 * outputs -
446 * side effects - tries to undline anything that matches
447 */
448 static int
449 remove_temp_dline(struct ConfItem *aconf)
450 {
451 rb_dlink_node *ptr;
452 int i;
453
454 for (i = 0; i < LAST_TEMP_TYPE; i++)
455 {
456 RB_DLINK_FOREACH(ptr, temp_dlines[i].head)
457 {
458 if (aconf == ptr->data)
459 {
460 rb_dlinkDestroy(ptr, &temp_dlines[i]);
461 delete_one_address_conf(aconf->host, aconf);
462 return YES;
463 }
464 }
465 }
466
467 return NO;
468 }