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