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
24 * $Id: m_dline.c 3225 2007-03-04 23:42:55Z jilles $
33 #include "irc_string.h"
34 #include "sprintf_irc.h"
40 #include "s_newconf.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: 3225 $");
64 static int valid_comment(char *comment
);
65 static int flush_write(struct Client
*, FILE *, char *, char *);
66 static int remove_temp_dline(struct ConfItem
*);
70 * parv[1] - dline to add
74 mo_dline(struct Client
*client_p
, struct Client
*source_p
,
75 int parc
, const char *parv
[])
77 char def
[] = "No Reason";
81 struct irc_sockaddr_storage daddr
;
82 char cidr_form_host
[HOSTLEN
+ 1];
83 struct ConfItem
*aconf
;
85 char dlbuffer
[IRCD_BUFSIZE
];
86 const char *current_date
;
90 if(!IsOperK(source_p
))
92 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
93 me
.name
, source_p
->name
, "kline");
97 if((tdline_time
= valid_temp_time(parv
[loc
])) >= 0)
102 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
103 me
.name
, source_p
->name
, "DLINE");
108 strlcpy(cidr_form_host
, dlhost
, sizeof(cidr_form_host
));
110 if(!parse_netmask(dlhost
, NULL
, &bits
))
112 sendto_one(source_p
, ":%s NOTICE %s :Invalid D-Line",
113 me
.name
, source_p
->name
);
119 if(parc
>= loc
+ 1) /* host :reason */
121 if(!EmptyString(parv
[loc
]))
122 reason
= LOCAL_COPY(parv
[loc
]);
124 if(!valid_comment(reason
))
127 ":%s NOTICE %s :Invalid character '\"' in comment",
128 me
.name
, source_p
->name
);
133 if(IsOperAdmin(source_p
))
138 ":%s NOTICE %s :For safety, bitmasks less than 8 require conf access.",
148 ":%s NOTICE %s :Dline bitmasks less than 16 are for admins only.",
154 if(ConfigFileEntry
.non_redundant_klines
)
157 int t
= AF_INET
, ty
, b
;
158 ty
= parse_netmask(dlhost
, (struct sockaddr
*)&daddr
, &b
);
166 if((aconf
= find_dline((struct sockaddr
*)&daddr
, t
)) != NULL
)
169 parse_netmask(aconf
->host
, NULL
, &bx
);
172 creason
= aconf
->passwd
? aconf
->passwd
: "<No Reason>";
173 if(IsConfExemptKline(aconf
))
175 ":%s NOTICE %s :[%s] is (E)d-lined by [%s] - %s",
176 me
.name
, parv
[0], dlhost
, aconf
->host
, creason
);
179 ":%s NOTICE %s :[%s] already D-lined by [%s] - %s",
180 me
.name
, parv
[0], dlhost
, aconf
->host
, creason
);
187 current_date
= smalldate();
190 aconf
->status
= CONF_DLINE
;
191 DupString(aconf
->host
, dlhost
);
193 /* Look for an oper reason */
194 if((oper_reason
= strchr(reason
, '|')) != NULL
)
199 if(!EmptyString(oper_reason
))
200 DupString(aconf
->spasswd
, oper_reason
);
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_snomask(SNO_GENERAL
, 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
);
224 sendto_realops_snomask(SNO_GENERAL
, 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
);
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);
251 * parv[1] = dline to remove
254 mo_undline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
258 char buf
[BUFSIZE
], buff
[BUFSIZE
], temppath
[BUFSIZE
], *p
;
259 const char *filename
, *found_cidr
;
261 struct ConfItem
*aconf
;
262 int pairme
= NO
, error_on_write
= NO
;
265 ircsnprintf(temppath
, sizeof(temppath
), "%s.tmp", ConfigFileEntry
.dlinefile
);
267 if(!IsOperUnkline(source_p
))
269 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
270 me
.name
, source_p
->name
, "unkline");
276 if(parse_netmask(cidr
, NULL
, NULL
) == HM_HOST
)
278 sendto_one_notice(source_p
, ":Invalid D-Line");
282 aconf
= find_exact_conf_by_address(cidr
, CONF_DLINE
, NULL
);
285 sendto_one_notice(source_p
, ":No D-Line for %s", cidr
);
289 strlcpy(buf
, aconf
->host
, sizeof buf
);
290 if(remove_temp_dline(aconf
))
293 ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines",
294 me
.name
, parv
[0], buf
);
295 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
296 "%s has removed the temporary D-Line for: [%s]",
297 get_oper_name(source_p
), buf
);
298 ilog(L_KLINE
, "UD %s %s", get_oper_name(source_p
), buf
);
302 filename
= get_conf_name(DLINE_TYPE
);
304 if((in
= fopen(filename
, "r")) == 0)
306 sendto_one(source_p
, ":%s NOTICE %s :Cannot open %s", me
.name
, parv
[0], filename
);
311 if((out
= fopen(temppath
, "w")) == 0)
313 sendto_one(source_p
, ":%s NOTICE %s :Cannot open %s", me
.name
, parv
[0], temppath
);
321 while (fgets(buf
, sizeof(buf
), in
))
323 strlcpy(buff
, buf
, sizeof(buff
));
325 if((p
= strchr(buff
, '\n')) != NULL
)
328 if((*buff
== '\0') || (*buff
== '#'))
331 flush_write(source_p
, out
, buf
, temppath
);
335 if((found_cidr
= getfield(buff
)) == NULL
)
338 flush_write(source_p
, out
, buf
, temppath
);
342 if(irccmp(found_cidr
, aconf
->host
) == 0)
349 flush_write(source_p
, out
, buf
, temppath
);
356 error_on_write
= YES
;
361 ":%s NOTICE %s :Couldn't write D-line file, aborted",
367 sendto_one_notice(source_p
, ":Cannot find D-Line for %s in file",
371 (void) unlink(temppath
);
376 if (rename(temppath
, filename
))
378 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
382 sendto_one(source_p
, ":%s NOTICE %s :D-Line for [%s] is removed", me
.name
, parv
[0], aconf
->host
);
383 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
384 "%s has removed the D-Line for: [%s]", get_oper_name(source_p
), aconf
->host
);
385 ilog(L_KLINE
, "UD %s %s", get_oper_name(source_p
), aconf
->host
);
387 delete_one_address_conf(aconf
->host
, aconf
);
394 * inputs - pointer to client
395 * - pointer to comment
396 * output - 0 if no valid comment, 1 if valid
397 * side effects - NONE
400 valid_comment(char *comment
)
402 if(strchr(comment
, '"'))
405 if(strlen(comment
) > BANREASONLEN
)
406 comment
[BANREASONLEN
] = '\0';
414 * inputs - pointer to client structure of oper requesting unkline
415 * - out is the file descriptor
416 * - buf is the buffer to write
417 * - ntowrite is the expected number of character to be written
418 * - temppath is the temporary file name to be written
419 * output - YES for error on write
421 * side effects - if successful, the buf is written to output file
422 * if a write failure happesn, and the file pointed to
423 * by temppath, if its non NULL, is removed.
425 * The idea here is, to be as robust as possible when writing to the
431 flush_write(struct Client
*source_p
, FILE * out
, char *buf
, char *temppath
)
433 int error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
437 sendto_one_notice(source_p
, ":Unable to write to %s", temppath
);
440 (void) unlink(temppath
);
442 return (error_on_write
);
445 /* remove_temp_dline()
447 * inputs - confitem to undline
449 * side effects - tries to undline anything that matches
452 remove_temp_dline(struct ConfItem
*aconf
)
457 for (i
= 0; i
< LAST_TEMP_TYPE
; i
++)
459 DLINK_FOREACH(ptr
, temp_dlines
[i
].head
)
461 if (aconf
== ptr
->data
)
463 dlinkDestroy(ptr
, &temp_dlines
[i
]);
464 delete_one_address_conf(aconf
->host
, aconf
);