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 $
37 #include "s_newconf.h"
46 static int mo_dline(struct Client
*, struct Client
*, int, const char **);
47 static int me_dline(struct Client
*, struct Client
*, int, const char **);
48 static int mo_undline(struct Client
*, struct Client
*, int, const char **);
49 static int me_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
, {me_dline
, 3}, {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
, {me_undline
, 1}, {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
*);
66 static int apply_dline(struct Client
*, const char *, int, char *);
67 static int apply_undline(struct Client
*, const char *);
71 * parv[1] - dline to add
75 mo_dline(struct Client
*client_p
, struct Client
*source_p
,
76 int parc
, const char *parv
[])
78 char def
[] = "No Reason";
81 char cidr_form_host
[HOSTLEN
+ 1];
83 const char *target_server
= NULL
;
86 if(!IsOperK(source_p
))
88 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
89 me
.name
, source_p
->name
, "kline");
93 if((tdline_time
= valid_temp_time(parv
[loc
])) >= 0)
97 rb_strlcpy(cidr_form_host
, dlhost
, sizeof(cidr_form_host
));
101 if(parc
>= loc
+2 && !irccmp(parv
[loc
], "ON"))
103 target_server
= parv
[loc
+1];
107 if(parc
>= loc
+ 1 && !EmptyString(parv
[loc
]))
108 reason
= LOCAL_COPY(parv
[loc
]);
110 if(target_server
!= NULL
)
112 sendto_match_servs(source_p
, target_server
,
114 "ENCAP %s DLINE %d %s :%s",
115 target_server
, tdline_time
, dlhost
, reason
);
117 if(!match(target_server
, me
.name
))
121 apply_dline(source_p
, dlhost
, tdline_time
, reason
);
129 * parv[1] = dline to remove
132 mo_undline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
135 const char *target_server
= NULL
;
137 if(!IsOperK(source_p
))
139 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
140 me
.name
, source_p
->name
, "unkline");
146 if(parc
>= 4 && !irccmp(parv
[2], "ON"))
148 target_server
= parv
[3];
149 sendto_match_servs(source_p
, target_server
,
151 "ENCAP %s UNDLINE %s",
152 target_server
, cidr
);
154 if(!match(target_server
, me
.name
))
158 apply_undline(source_p
, cidr
);
164 me_dline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
166 int tdline_time
= atoi(parv
[1]);
167 /* Since this is coming over a server link, assume that the originating
168 * server did the relevant permission/sanity checks...
171 if(!IsPerson(source_p
))
174 if(!find_shared_conf(source_p
->username
, source_p
->host
,
175 source_p
->servptr
->name
, tdline_time
> 0 ? SHARED_TDLINE
: SHARED_PDLINE
))
177 sendto_realops_snomask(SNO_DEBUG
, L_NETWIDE
, "undline failed %s %s %s",
178 source_p
->name
, parv
[1], parv
[2]);
182 apply_dline(source_p
, parv
[2], tdline_time
, LOCAL_COPY(parv
[3]));
189 me_undline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
191 if(!IsPerson(source_p
))
194 if(!find_shared_conf(source_p
->username
, source_p
->host
,
195 source_p
->servptr
->name
, SHARED_UNDLINE
))
198 apply_undline(source_p
, parv
[1]);
204 apply_dline(struct Client
*source_p
, const char *dlhost
, int tdline_time
, char *reason
)
206 struct ConfItem
*aconf
;
208 char dlbuffer
[IRCD_BUFSIZE
];
209 const char *current_date
;
210 struct rb_sockaddr_storage daddr
;
211 int t
= AF_INET
, ty
, b
;
214 ty
= parse_netmask(dlhost
, (struct sockaddr
*)&daddr
, &b
);
217 sendto_one(source_p
, ":%s NOTICE %s :Invalid D-Line",
218 me
.name
, source_p
->name
);
228 /* This means dlines wider than /16 cannot be set remotely */
229 if(IsOperAdmin(source_p
))
233 sendto_one_notice(source_p
,
234 ":For safety, bitmasks less than 8 require conf access.");
242 sendto_one_notice(source_p
,
243 ":Dline bitmasks less than 16 are for admins only.");
248 if(!valid_comment(reason
))
251 ":%s NOTICE %s :Invalid character '\"' in comment",
252 me
.name
, source_p
->name
);
256 /* Look for an oper reason */
257 if((oper_reason
= strchr(reason
, '|')) != NULL
)
262 if(!EmptyString(oper_reason
))
263 aconf
->spasswd
= rb_strdup(oper_reason
);
266 if(ConfigFileEntry
.non_redundant_klines
)
268 if((aconf
= find_dline((struct sockaddr
*)&daddr
, t
)) != NULL
)
271 parse_netmask(aconf
->host
, NULL
, &bx
);
274 creason
= aconf
->passwd
? aconf
->passwd
: "<No Reason>";
275 if(IsConfExemptKline(aconf
))
277 ":%s NOTICE %s :[%s] is (E)d-lined by [%s] - %s",
278 me
.name
, source_p
->name
, dlhost
, aconf
->host
, creason
);
281 ":%s NOTICE %s :[%s] already D-lined by [%s] - %s",
282 me
.name
, source_p
->name
, dlhost
, aconf
->host
, creason
);
289 current_date
= smalldate();
292 aconf
->status
= CONF_DLINE
;
293 aconf
->host
= rb_strdup(dlhost
);
297 rb_snprintf(dlbuffer
, sizeof(dlbuffer
),
298 "Temporary D-line %d min. - %s (%s)",
299 (int) (tdline_time
/ 60), reason
, current_date
);
300 aconf
->passwd
= rb_strdup(dlbuffer
);
301 aconf
->hold
= rb_current_time() + tdline_time
;
302 add_temp_dline(aconf
);
304 if(EmptyString(oper_reason
))
306 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
307 "%s added temporary %d min. D-Line for [%s] [%s]",
308 get_oper_name(source_p
), tdline_time
/ 60,
309 aconf
->host
, reason
);
310 ilog(L_KLINE
, "D %s %d %s %s",
311 get_oper_name(source_p
), tdline_time
/ 60,
312 aconf
->host
, reason
);
316 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
317 "%s added temporary %d min. D-Line for [%s] [%s|%s]",
318 get_oper_name(source_p
), tdline_time
/ 60,
319 aconf
->host
, reason
, oper_reason
);
320 ilog(L_KLINE
, "D %s %d %s %s|%s",
321 get_oper_name(source_p
), tdline_time
/ 60,
322 aconf
->host
, reason
, oper_reason
);
325 sendto_one(source_p
, ":%s NOTICE %s :Added temporary %d min. D-Line for [%s]",
326 me
.name
, source_p
->name
, tdline_time
/ 60, aconf
->host
);
330 rb_snprintf(dlbuffer
, sizeof(dlbuffer
), "%s (%s)", reason
, current_date
);
331 aconf
->passwd
= rb_strdup(dlbuffer
);
332 add_conf_by_address(aconf
->host
, CONF_DLINE
, NULL
, aconf
);
333 write_confitem(DLINE_TYPE
, source_p
, NULL
, aconf
->host
, reason
,
334 oper_reason
, current_date
, 0);
341 apply_undline(struct Client
*source_p
, const char *cidr
)
345 char buf
[BUFSIZE
], buff
[BUFSIZE
], temppath
[BUFSIZE
], *p
;
346 const char *filename
, *found_cidr
;
347 int pairme
= NO
, error_on_write
= NO
;
349 struct ConfItem
*aconf
;
351 if(parse_netmask(cidr
, NULL
, NULL
) == HM_HOST
)
353 sendto_one_notice(source_p
, ":Invalid D-Line");
357 rb_snprintf(temppath
, sizeof(temppath
), "%s.tmp", ConfigFileEntry
.dlinefile
);
359 aconf
= find_exact_conf_by_address(cidr
, CONF_DLINE
, NULL
);
362 sendto_one_notice(source_p
, ":No D-Line for %s", cidr
);
366 rb_strlcpy(buf
, aconf
->host
, sizeof buf
);
367 if(remove_temp_dline(aconf
))
370 ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines",
371 me
.name
, source_p
->name
, buf
);
372 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
373 "%s has removed the temporary D-Line for: [%s]",
374 get_oper_name(source_p
), buf
);
375 ilog(L_KLINE
, "UD %s %s", get_oper_name(source_p
), buf
);
379 filename
= get_conf_name(DLINE_TYPE
);
381 if((in
= fopen(filename
, "r")) == 0)
383 sendto_one(source_p
, ":%s NOTICE %s :Cannot open %s", me
.name
, source_p
->name
, filename
);
388 if((out
= fopen(temppath
, "w")) == 0)
390 sendto_one(source_p
, ":%s NOTICE %s :Cannot open %s", me
.name
, source_p
->name
, temppath
);
398 while (fgets(buf
, sizeof(buf
), in
))
400 rb_strlcpy(buff
, buf
, sizeof(buff
));
402 if((p
= strchr(buff
, '\n')) != NULL
)
405 if((*buff
== '\0') || (*buff
== '#'))
408 flush_write(source_p
, out
, buf
, temppath
);
412 if((found_cidr
= getfield(buff
)) == NULL
)
415 flush_write(source_p
, out
, buf
, temppath
);
419 if(irccmp(found_cidr
, aconf
->host
) == 0)
426 flush_write(source_p
, out
, buf
, temppath
);
433 error_on_write
= YES
;
438 ":%s NOTICE %s :Couldn't write D-line file, aborted",
439 me
.name
, source_p
->name
);
444 sendto_one_notice(source_p
, ":Cannot find D-Line for %s in file",
448 (void) unlink(temppath
);
453 if (rename(temppath
, filename
))
455 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
459 sendto_one(source_p
, ":%s NOTICE %s :D-Line for [%s] is removed", me
.name
, source_p
->name
, aconf
->host
);
460 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
461 "%s has removed the D-Line for: [%s]", get_oper_name(source_p
), aconf
->host
);
462 ilog(L_KLINE
, "UD %s %s", get_oper_name(source_p
), aconf
->host
);
463 delete_one_address_conf(aconf
->host
, aconf
);
470 * inputs - pointer to client
471 * - pointer to comment
472 * output - 0 if no valid comment, 1 if valid
473 * side effects - NONE
476 valid_comment(char *comment
)
478 if(strchr(comment
, '"'))
481 if(strlen(comment
) > BANREASONLEN
)
482 comment
[BANREASONLEN
] = '\0';
490 * inputs - pointer to client structure of oper requesting unkline
491 * - out is the file descriptor
492 * - buf is the buffer to write
493 * - ntowrite is the expected number of character to be written
494 * - temppath is the temporary file name to be written
495 * output - YES for error on write
497 * side effects - if successful, the buf is written to output file
498 * if a write failure happesn, and the file pointed to
499 * by temppath, if its non NULL, is removed.
501 * The idea here is, to be as robust as possible when writing to the
507 flush_write(struct Client
*source_p
, FILE * out
, char *buf
, char *temppath
)
509 int error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
513 sendto_one_notice(source_p
, ":Unable to write to %s", temppath
);
516 (void) unlink(temppath
);
518 return (error_on_write
);
521 /* remove_temp_dline()
523 * inputs - confitem to undline
525 * side effects - tries to undline anything that matches
528 remove_temp_dline(struct ConfItem
*aconf
)
533 for (i
= 0; i
< LAST_TEMP_TYPE
; i
++)
535 RB_DLINK_FOREACH(ptr
, temp_dlines
[i
].head
)
537 if (aconf
== ptr
->data
)
539 rb_dlinkDestroy(ptr
, &temp_dlines
[i
]);
540 delete_one_address_conf(aconf
->host
, aconf
);