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 $
32 #include "irc_string.h"
33 #include "sprintf_irc.h"
38 #include "s_newconf.h"
48 static int mo_dline(struct Client
*, struct Client
*, int, const char **);
49 static int mo_undline(struct Client
*, struct Client
*, int, const char **);
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}}
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}}
60 mapi_clist_av1 dline_clist
[] = { &dline_msgtab
, &undline_msgtab
, NULL
};
61 DECLARE_MODULE_AV1(dline
, NULL
, NULL
, dline_clist
, NULL
, NULL
, "$Revision: 3225 $");
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
*);
69 * parv[1] - dline to add
73 mo_dline(struct Client
*client_p
, struct Client
*source_p
,
74 int parc
, const char *parv
[])
76 char def
[] = "No Reason";
80 char cidr_form_host
[HOSTLEN
+ 1];
81 struct ConfItem
*aconf
;
83 char dlbuffer
[IRCD_BUFSIZE
];
84 const char *current_date
;
88 if(!IsOperK(source_p
))
90 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
91 me
.name
, source_p
->name
, "kline");
95 if((tdline_time
= valid_temp_time(parv
[loc
])) >= 0)
100 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
101 me
.name
, source_p
->name
, "DLINE");
106 strlcpy(cidr_form_host
, dlhost
, sizeof(cidr_form_host
));
108 if(!parse_netmask(dlhost
, NULL
, &bits
))
110 sendto_one(source_p
, ":%s NOTICE %s :Invalid D-Line",
111 me
.name
, source_p
->name
);
117 if(parc
>= loc
+ 1) /* host :reason */
119 if(!EmptyString(parv
[loc
]))
120 reason
= LOCAL_COPY(parv
[loc
]);
122 if(!valid_comment(reason
))
125 ":%s NOTICE %s :Invalid character '\"' in comment",
126 me
.name
, source_p
->name
);
131 if(IsOperAdmin(source_p
))
136 ":%s NOTICE %s :For safety, bitmasks less than 8 require conf access.",
146 ":%s NOTICE %s :Dline bitmasks less than 16 are for admins only.",
152 if(ConfigFileEntry
.non_redundant_klines
)
154 struct rb_sockaddr_storage daddr
;
156 int t
= AF_INET
, ty
, b
;
157 ty
= parse_netmask(dlhost
, (struct sockaddr
*)&daddr
, &b
);
165 if((aconf
= find_dline((struct sockaddr
*)&daddr
)) != NULL
)
168 parse_netmask(aconf
->host
, NULL
, &bx
);
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
);
177 sendto_one_notice(source_p
,
178 ":[%s] already D-lined by [%s] - %s",
179 dlhost
, aconf
->host
, creason
);
186 current_date
= smalldate();
189 aconf
->status
= CONF_DLINE
;
190 aconf
->host
= rb_strdup(dlhost
);
192 /* Look for an oper reason */
193 if((oper_reason
= strchr(reason
, '|')) != NULL
)
198 if(!EmptyString(oper_reason
))
199 aconf
->spasswd
= rb_strdup(oper_reason
);
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
);
211 if(EmptyString(oper_reason
))
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
);
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
);
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
);
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);
250 * parv[1] = dline to remove
253 mo_undline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
257 char buf
[BUFSIZE
], buff
[BUFSIZE
], temppath
[BUFSIZE
], *p
;
258 const char *filename
, *found_cidr
;
260 struct ConfItem
*aconf
;
261 int pairme
= NO
, error_on_write
= NO
;
264 rb_snprintf(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");
275 if(parse_netmask(cidr
, NULL
, NULL
) == HM_HOST
)
277 sendto_one_notice(source_p
, ":Invalid D-Line");
281 aconf
= find_exact_conf_by_address(cidr
, CONF_DLINE
, NULL
);
284 sendto_one_notice(source_p
, ":No D-Line for %s", cidr
);
288 strlcpy(buf
, aconf
->host
, sizeof buf
);
289 if(remove_temp_dline(aconf
))
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
);
301 filename
= get_conf_name(DLINE_TYPE
);
303 if((in
= fopen(filename
, "r")) == 0)
305 sendto_one(source_p
, ":%s NOTICE %s :Cannot open %s", me
.name
, parv
[0], filename
);
310 if((out
= fopen(temppath
, "w")) == 0)
312 sendto_one(source_p
, ":%s NOTICE %s :Cannot open %s", me
.name
, parv
[0], temppath
);
320 while (fgets(buf
, sizeof(buf
), in
))
322 strlcpy(buff
, buf
, sizeof(buff
));
324 if((p
= strchr(buff
, '\n')) != NULL
)
327 if((*buff
== '\0') || (*buff
== '#'))
330 flush_write(source_p
, out
, buf
, temppath
);
334 if((found_cidr
= getfield(buff
)) == NULL
)
337 flush_write(source_p
, out
, buf
, temppath
);
341 if(irccmp(found_cidr
, aconf
->host
) == 0)
348 flush_write(source_p
, out
, buf
, temppath
);
355 error_on_write
= YES
;
360 ":%s NOTICE %s :Couldn't write D-line file, aborted",
366 sendto_one_notice(source_p
, ":Cannot find D-Line for %s in file",
370 (void) unlink(temppath
);
375 if (rename(temppath
, filename
))
377 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
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
);
386 delete_one_address_conf(aconf
->host
, aconf
);
393 * inputs - pointer to client
394 * - pointer to comment
395 * output - 0 if no valid comment, 1 if valid
396 * side effects - NONE
399 valid_comment(char *comment
)
401 if(strchr(comment
, '"'))
404 if(strlen(comment
) > BANREASONLEN
)
405 comment
[BANREASONLEN
] = '\0';
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
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.
424 * The idea here is, to be as robust as possible when writing to the
430 flush_write(struct Client
*source_p
, FILE * out
, char *buf
, char *temppath
)
432 int error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
436 sendto_one_notice(source_p
, ":Unable to write to %s", temppath
);
439 (void) unlink(temppath
);
441 return (error_on_write
);
444 /* remove_temp_dline()
446 * inputs - confitem to undline
448 * side effects - tries to undline anything that matches
451 remove_temp_dline(struct ConfItem
*aconf
)
456 for (i
= 0; i
< LAST_TEMP_TYPE
; i
++)
458 RB_DLINK_FOREACH(ptr
, temp_dlines
[i
].head
)
460 if (aconf
== ptr
->data
)
462 rb_dlinkDestroy(ptr
, &temp_dlines
[i
]);
463 delete_one_address_conf(aconf
->host
, aconf
);