Initial import
[ratbox-ambernet.git] / modules / m_dline.c
blobab4c88435cd736813cfbe9f14bcf18fdf8187953
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_dline.c: Bans/unbans a user.
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
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.
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.
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
24 * $Id: m_dline.c 22639 2006-05-24 15:27:55Z androsyn $
27 #include "stdinc.h"
28 #include "tools.h"
29 #include "channel.h"
30 #include "class.h"
31 #include "client.h"
32 #include "common.h"
33 #include "irc_string.h"
34 #include "sprintf_irc.h"
35 #include "ircd.h"
36 #include "hostmask.h"
37 #include "numeric.h"
38 #include "commio.h"
39 #include "s_conf.h"
40 #include "s_newconf.h"
41 #include "s_log.h"
42 #include "send.h"
43 #include "hash.h"
44 #include "s_serv.h"
45 #include "msg.h"
46 #include "parse.h"
47 #include "modules.h"
49 static int mo_dline(struct Client *, struct Client *, int, const char **);
50 static int mo_undline(struct Client *, struct Client *, int, const char **);
52 struct Message dline_msgtab = {
53 "DLINE", 0, 0, 0, MFLG_SLOW,
54 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_dline, 2}}
56 struct Message undline_msgtab = {
57 "UNDLINE", 0, 0, 0, MFLG_SLOW,
58 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_undline, 2}}
61 mapi_clist_av1 dline_clist[] = { &dline_msgtab, &undline_msgtab, NULL };
62 DECLARE_MODULE_AV1(dline, NULL, NULL, dline_clist, NULL, NULL, "$Revision: 22639 $");
64 static int valid_comment(char *comment);
65 static int flush_write(struct Client *, FILE *, char *, char *);
66 static int remove_temp_dline(const char *);
68 /* mo_dline()
70 * parv[1] - dline to add
71 * parv[2] - reason
73 static int
74 mo_dline(struct Client *client_p, struct Client *source_p,
75 int parc, const char *parv[])
77 char def[] = "No Reason";
78 const char *dlhost;
79 char *oper_reason;
80 char *reason = def;
81 char cidr_form_host[HOSTLEN + 1];
82 struct ConfItem *aconf;
83 int bits;
84 char dlbuffer[IRCD_BUFSIZE];
85 const char *current_date;
86 int tdline_time = 0;
87 int loc = 1;
89 if(!IsOperK(source_p))
91 sendto_one(source_p, form_str(ERR_NOPRIVS),
92 me.name, source_p->name, "kline");
93 return 0;
96 if((tdline_time = valid_temp_time(parv[loc])) >= 0)
97 loc++;
99 if(parc < loc + 1)
101 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
102 me.name, source_p->name, "DLINE");
103 return 0;
106 dlhost = parv[loc];
107 strlcpy(cidr_form_host, dlhost, sizeof(cidr_form_host));
109 if(!parse_netmask(dlhost, NULL, &bits))
111 sendto_one(source_p, ":%s NOTICE %s :Invalid D-Line",
112 me.name, source_p->name);
113 return 0;
116 loc++;
118 if(parc >= loc + 1) /* host :reason */
120 if(!EmptyString(parv[loc]))
121 reason = LOCAL_COPY(parv[loc]);
123 if(!valid_comment(reason))
125 sendto_one(source_p,
126 ":%s NOTICE %s :Invalid character '\"' in comment",
127 me.name, source_p->name);
128 return 0;
132 if(IsOperAdmin(source_p))
134 if(bits < 8)
136 sendto_one(source_p,
137 ":%s NOTICE %s :For safety, bitmasks less than 8 require conf access.",
138 me.name, parv[0]);
139 return 0;
142 else
144 if(bits < 16)
146 sendto_one(source_p,
147 ":%s NOTICE %s :Dline bitmasks less than 16 are for admins only.",
148 me.name, parv[0]);
149 return 0;
153 if(ConfigFileEntry.non_redundant_klines)
155 struct irc_sockaddr_storage daddr;
156 const char *creason;
157 int t = AF_INET, ty, b;
158 ty = parse_netmask(dlhost, (struct sockaddr *)&daddr, &b);
159 #ifdef IPV6
160 if(ty == HM_IPV6)
161 t = AF_INET6;
162 else
163 #endif
164 t = AF_INET;
166 if((aconf = find_dline((struct sockaddr *)&daddr, t)) != NULL)
168 int bx;
169 parse_netmask(aconf->host, NULL, &bx);
170 if(b >= bx)
172 creason = aconf->passwd ? aconf->passwd : "<No Reason>";
173 if(IsConfExemptKline(aconf))
174 sendto_one(source_p,
175 ":%s NOTICE %s :[%s] is (E)d-lined by [%s] - %s",
176 me.name, parv[0], dlhost, aconf->host, creason);
177 else
178 sendto_one(source_p,
179 ":%s NOTICE %s :[%s] already D-lined by [%s] - %s",
180 me.name, parv[0], dlhost, aconf->host, creason);
181 return 0;
186 set_time();
187 current_date = smalldate();
189 aconf = make_conf();
190 aconf->status = CONF_DLINE;
191 DupString(aconf->host, dlhost);
193 /* Look for an oper reason */
194 if((oper_reason = strchr(reason, '|')) != NULL)
196 *oper_reason = '\0';
197 oper_reason++;
199 if(!EmptyString(oper_reason))
200 DupString(aconf->spasswd, oper_reason);
203 if(tdline_time > 0)
205 ircsnprintf(dlbuffer, sizeof(dlbuffer),
206 "Temporary D-line %d min. - %s (%s)",
207 (int) (tdline_time / 60), reason, current_date);
208 DupString(aconf->passwd, dlbuffer);
209 aconf->hold = CurrentTime + tdline_time;
210 add_temp_dline(aconf);
212 if(EmptyString(oper_reason))
214 sendto_realops_flags(UMODE_ALL, L_ALL,
215 "%s added temporary %d min. D-Line for [%s] [%s]",
216 get_oper_name(source_p), tdline_time / 60,
217 aconf->host, reason);
218 ilog(L_KLINE, "D %s %d %s %s",
219 get_oper_name(source_p), tdline_time / 60,
220 aconf->host, reason);
222 else
224 sendto_realops_flags(UMODE_ALL, L_ALL,
225 "%s added temporary %d min. D-Line for [%s] [%s|%s]",
226 get_oper_name(source_p), tdline_time / 60,
227 aconf->host, reason, oper_reason);
228 ilog(L_KLINE, "D %s %d %s %s|%s",
229 get_oper_name(source_p), tdline_time / 60,
230 aconf->host, reason, oper_reason);
233 sendto_one(source_p, ":%s NOTICE %s :Added temporary %d min. D-Line for [%s]",
234 me.name, source_p->name, tdline_time / 60, aconf->host);
236 else
238 ircsnprintf(dlbuffer, sizeof(dlbuffer), "%s (%s)", reason, current_date);
239 DupString(aconf->passwd, dlbuffer);
240 add_conf_by_address(aconf->host, CONF_DLINE, NULL, aconf);
241 write_confitem(DLINE_TYPE, source_p, NULL, aconf->host, reason,
242 oper_reason, current_date, 0);
245 check_dlines();
246 return 0;
249 /* mo_undline()
251 * parv[1] = dline to remove
253 static int
254 mo_undline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
256 FILE *in;
257 FILE *out;
258 char buf[BUFSIZE], buff[BUFSIZE], temppath[BUFSIZE], *p;
259 const char *filename, *found_cidr;
260 const char *cidr;
261 int pairme = NO, error_on_write = NO;
262 mode_t oldumask;
264 ircsnprintf(temppath, sizeof(temppath), "%s.tmp", ConfigFileEntry.dlinefile);
266 if(!IsOperUnkline(source_p))
268 sendto_one(source_p, form_str(ERR_NOPRIVS),
269 me.name, source_p->name, "unkline");
270 return 0;
273 cidr = parv[1];
275 if(parse_netmask(cidr, NULL, NULL) == HM_HOST)
277 sendto_one(source_p, ":%s NOTICE %s :Invalid D-Line",
278 me.name, source_p->name);
279 return 0;
282 if(remove_temp_dline(cidr))
284 sendto_one(source_p,
285 ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines",
286 me.name, parv[0], cidr);
287 sendto_realops_flags(UMODE_ALL, L_ALL,
288 "%s has removed the temporary D-Line for: [%s]",
289 get_oper_name(source_p), cidr);
290 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), cidr);
291 return 0;
294 filename = get_conf_name(DLINE_TYPE);
296 if((in = fopen(filename, "r")) == 0)
298 sendto_one(source_p, ":%s NOTICE %s :Cannot open %s", me.name, parv[0], filename);
299 return 0;
302 oldumask = umask(0);
303 if((out = fopen(temppath, "w")) == 0)
305 sendto_one(source_p, ":%s NOTICE %s :Cannot open %s", me.name, parv[0], temppath);
306 fclose(in);
307 umask(oldumask);
308 return 0;
311 umask(oldumask);
313 while (fgets(buf, sizeof(buf), in))
315 strlcpy(buff, buf, sizeof(buff));
317 if((p = strchr(buff, '\n')) != NULL)
318 *p = '\0';
320 if((*buff == '\0') || (*buff == '#'))
322 if(!error_on_write)
323 flush_write(source_p, out, buf, temppath);
324 continue;
327 if((found_cidr = getfield(buff)) == NULL)
329 if(!error_on_write)
330 flush_write(source_p, out, buf, temppath);
331 continue;
334 if(irccmp(found_cidr, cidr) == 0)
336 pairme++;
338 else
340 if(!error_on_write)
341 flush_write(source_p, out, buf, temppath);
342 continue;
346 fclose(in);
347 fclose(out);
349 if(error_on_write)
351 sendto_one(source_p,
352 ":%s NOTICE %s :Couldn't write D-line file, aborted",
353 me.name, parv[0]);
354 return 0;
356 else if(!pairme)
358 sendto_one(source_p, ":%s NOTICE %s :No D-Line for %s",
359 me.name, parv[0], cidr);
361 if(temppath != NULL)
362 (void) unlink(temppath);
364 return 0;
367 (void) rename(temppath, filename);
368 rehash_bans(0);
371 sendto_one(source_p, ":%s NOTICE %s :D-Line for [%s] is removed", me.name, parv[0], cidr);
372 sendto_realops_flags(UMODE_ALL, L_ALL,
373 "%s has removed the D-Line for: [%s]", get_oper_name(source_p), cidr);
374 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), cidr);
376 return 0;
380 * valid_comment
381 * inputs - pointer to client
382 * - pointer to comment
383 * output - 0 if no valid comment, 1 if valid
384 * side effects - NONE
386 static int
387 valid_comment(char *comment)
389 if(strchr(comment, '"'))
390 return 0;
392 if(strlen(comment) > REASONLEN)
393 comment[REASONLEN] = '\0';
395 return 1;
399 * flush_write()
401 * inputs - pointer to client structure of oper requesting unkline
402 * - out is the file descriptor
403 * - buf is the buffer to write
404 * - ntowrite is the expected number of character to be written
405 * - temppath is the temporary file name to be written
406 * output - YES for error on write
407 * - NO for success
408 * side effects - if successful, the buf is written to output file
409 * if a write failure happesn, and the file pointed to
410 * by temppath, if its non NULL, is removed.
412 * The idea here is, to be as robust as possible when writing to the
413 * kline file.
415 * -Dianora
417 static int
418 flush_write(struct Client *source_p, FILE * out, char *buf, char *temppath)
420 int error_on_write = (fputs(buf, out) < 0) ? YES : NO;
422 if(error_on_write)
424 sendto_one(source_p, ":%s NOTICE %s :Unable to write to %s",
425 me.name, source_p->name, temppath);
426 fclose(out);
427 if(temppath != NULL)
428 (void) unlink(temppath);
430 return (error_on_write);
433 /* remove_temp_dline()
435 * inputs - hostname to undline
436 * outputs -
437 * side effects - tries to undline anything that matches
439 static int
440 remove_temp_dline(const char *host)
442 struct ConfItem *aconf;
443 dlink_node *ptr;
444 struct irc_sockaddr_storage addr, caddr;
445 int bits, cbits;
446 int i;
448 parse_netmask(host, (struct sockaddr *)&addr, &bits);
450 for (i = 0; i < LAST_TEMP_TYPE; i++)
452 DLINK_FOREACH(ptr, temp_dlines[i].head)
454 aconf = ptr->data;
456 parse_netmask(aconf->host, (struct sockaddr *)&caddr, &cbits);
458 if(comp_with_mask_sock((struct sockaddr *)&addr, (struct sockaddr *)&caddr, bits) && bits == cbits)
460 dlinkDestroy(ptr, &temp_dlines[i]);
461 delete_one_address_conf(aconf->host, aconf);
462 return YES;
467 return NO;