2 * An interface to the ban db.
4 * Copyright (C) 2006 Lee Hardy <lee -at- leeh.co.uk>
5 * Copyright (C) 2006 ircd-ratbox development team
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * 1.Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2.Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3.The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
42 #include "s_newconf.h"
46 #include "msg.h" /* XXX: MAXPARA */
49 static char bandb_add_letter
[LAST_BANDB_TYPE
] = {
53 rb_dlink_list bandb_pending
;
55 static rb_helper
*bandb_helper
;
56 static int start_bandb(void);
58 static void bandb_parse(rb_helper
*);
59 static void bandb_restart_cb(rb_helper
*);
60 static char *bandb_path
;
67 ilog(L_MAIN
, "Unable to start bandb helper: %s", strerror(errno
));
76 char fullpath
[PATH_MAX
+ 1];
78 const char *suffix
= ".exe";
80 const char *suffix
= "";
83 rb_setenv("BANDB_DBPATH", PKGLOCALSTATEDIR
"/ban.db", 1);
84 if(bandb_path
== NULL
)
86 snprintf(fullpath
, sizeof(fullpath
), "%s/bandb%s", PKGLIBEXECDIR
, suffix
);
88 if(access(fullpath
, X_OK
) == -1)
90 snprintf(fullpath
, sizeof(fullpath
), "%s/bin/bandb%s",
91 ConfigFileEntry
.dpath
, suffix
);
93 if(access(fullpath
, X_OK
) == -1)
96 "Unable to execute bandb%s in %s or %s/bin",
97 suffix
, PKGLIBEXECDIR
, ConfigFileEntry
.dpath
);
101 bandb_path
= rb_strdup(fullpath
);
105 bandb_helper
= rb_helper_start("bandb", bandb_path
, bandb_parse
, bandb_restart_cb
);
107 if(bandb_helper
== NULL
)
109 ilog(L_MAIN
, "Unable to start bandb: %s", strerror(errno
));
110 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Unable to start bandb: %s",
115 rb_helper_run(bandb_helper
);
120 bandb_add(bandb_type type
, struct Client
*source_p
, const char *mask1
,
121 const char *mask2
, const char *reason
, const char *oper_reason
, int perm
)
123 if(bandb_helper
== NULL
)
126 static char buf
[BUFSIZE
];
128 snprintf(buf
, sizeof(buf
), "%c %s ", bandb_add_letter
[type
], mask1
);
130 if(!EmptyString(mask2
))
131 rb_snprintf_append(buf
, sizeof(buf
), "%s ", mask2
);
133 rb_snprintf_append(buf
, sizeof(buf
), "%s %ld %d :%s",
134 get_oper_name(source_p
), (long int)rb_current_time(), perm
, reason
);
136 if(!EmptyString(oper_reason
))
137 rb_snprintf_append(buf
, sizeof(buf
), "|%s", oper_reason
);
139 rb_helper_write(bandb_helper
, "%s", buf
);
142 static char bandb_del_letter
[LAST_BANDB_TYPE
] = {
147 bandb_del(bandb_type type
, const char *mask1
, const char *mask2
)
149 if(bandb_helper
== NULL
)
152 static char buf
[BUFSIZE
];
156 rb_snprintf_append(buf
, sizeof(buf
), "%c %s", bandb_del_letter
[type
], mask1
);
158 if(!EmptyString(mask2
))
159 rb_snprintf_append(buf
, sizeof(buf
), " %s", mask2
);
161 rb_helper_write(bandb_helper
, "%s", buf
);
165 bandb_handle_ban(char *parv
[], int parc
)
167 struct ConfItem
*aconf
;
174 if(parv
[0][0] == 'K')
175 aconf
->user
= rb_strdup(parv
[para
++]);
177 aconf
->host
= rb_strdup(parv
[para
++]);
178 aconf
->info
.oper
= operhash_add(parv
[para
++]);
183 aconf
->status
= CONF_KILL
;
187 aconf
->status
= CONF_DLINE
;
191 aconf
->status
= CONF_XLINE
;
195 if(IsChannelName(aconf
->host
))
196 aconf
->status
= CONF_RESV_CHANNEL
;
198 aconf
->status
= CONF_RESV_NICK
;
203 if((p
= strchr(parv
[para
], '|')))
206 aconf
->spasswd
= rb_strdup(p
);
209 aconf
->passwd
= rb_strdup(parv
[para
]);
211 rb_dlinkAddAlloc(aconf
, &bandb_pending
);
215 bandb_check_kline(struct ConfItem
*aconf
)
217 struct rb_sockaddr_storage daddr
;
218 struct ConfItem
*kconf
= NULL
;
222 aftype
= parse_netmask(aconf
->host
, &daddr
, NULL
);
224 if(aftype
!= HM_HOST
)
227 if(aftype
== HM_IPV6
)
233 kconf
= find_conf_by_address(aconf
->host
, NULL
, NULL
, (struct sockaddr
*)&daddr
,
234 CONF_KILL
, aftype
, aconf
->user
, NULL
);
237 kconf
= find_conf_by_address(aconf
->host
, NULL
, NULL
, NULL
, CONF_KILL
, 0, aconf
->user
, NULL
);
239 if(kconf
&& ((kconf
->flags
& CONF_FLAGS_TEMPORARY
) == 0))
242 for(p
= aconf
->user
; *p
; p
++)
244 if(!IsUserChar(*p
) && !IsKWildChar(*p
))
248 for(p
= aconf
->host
; *p
; p
++)
250 if(!IsHostChar(*p
) && !IsKWildChar(*p
))
258 bandb_check_dline(struct ConfItem
*aconf
)
260 struct rb_sockaddr_storage daddr
;
263 if(!parse_netmask(aconf
->host
, &daddr
, &bits
))
270 bandb_check_xline(struct ConfItem
*aconf
)
272 struct ConfItem
*xconf
;
273 /* XXX perhaps convert spaces to \s? -- jilles */
275 xconf
= find_xline_mask(aconf
->host
);
276 if(xconf
!= NULL
&& !(xconf
->flags
& CONF_FLAGS_TEMPORARY
))
283 bandb_check_resv_channel(struct ConfItem
*aconf
)
287 if(hash_find_resv(aconf
->host
) || strlen(aconf
->host
) > CHANNELLEN
)
290 for(p
= aconf
->host
; *p
; p
++)
300 bandb_check_resv_nick(struct ConfItem
*aconf
)
302 if(!clean_resv_nick(aconf
->host
))
305 if(find_nick_resv(aconf
->host
))
312 bandb_handle_clear(void)
314 rb_dlink_node
*ptr
, *next_ptr
;
316 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, bandb_pending
.head
)
318 free_conf(ptr
->data
);
319 rb_dlinkDestroy(ptr
, &bandb_pending
);
324 bandb_handle_finish(void)
326 struct ConfItem
*aconf
;
327 rb_dlink_node
*ptr
, *next_ptr
;
329 clear_out_address_conf_bans();
330 clear_s_newconf_bans();
332 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, bandb_pending
.head
)
336 rb_dlinkDestroy(ptr
, &bandb_pending
);
338 switch (aconf
->status
)
341 if(bandb_check_kline(aconf
))
342 add_conf_by_address(aconf
->host
, CONF_KILL
, aconf
->user
, NULL
, aconf
);
349 if(bandb_check_dline(aconf
))
350 add_conf_by_address(aconf
->host
, CONF_DLINE
, aconf
->user
, NULL
, aconf
);
357 if(bandb_check_xline(aconf
))
358 rb_dlinkAddAlloc(aconf
, &xline_conf_list
);
364 case CONF_RESV_CHANNEL
:
365 if(bandb_check_resv_channel(aconf
))
366 add_to_resv_hash(aconf
->host
, aconf
);
373 if(bandb_check_resv_nick(aconf
))
374 rb_dlinkAddAlloc(aconf
, &resv_conf_list
);
382 check_banned_lines();
386 bandb_handle_failure(rb_helper
*helper
, char **parv
, int parc
)
388 if(server_state_foreground
)
389 fprintf(stderr
, "bandb - bandb failure: %s\n", parv
[1]);
391 ilog(L_MAIN
, "bandb - bandb failure: %s", parv
[1]);
392 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "bandb - bandb failure: %s", parv
[1]);
397 bandb_parse(rb_helper
*helper
)
399 static char buf
[READBUF_SIZE
];
400 char *parv
[MAXPARA
+ 1];
403 while((len
= rb_helper_read(helper
, buf
, sizeof(buf
))))
405 parc
= rb_string_to_array(buf
, parv
, MAXPARA
);
413 bandb_handle_failure(helper
, parv
, parc
);
419 bandb_handle_ban(parv
, parc
);
423 bandb_handle_clear();
426 bandb_handle_finish();
433 bandb_rehash_bans(void)
435 if(bandb_helper
!= NULL
)
436 rb_helper_write(bandb_helper
, "L");
440 bandb_restart_cb(rb_helper
*helper
)
442 ilog(L_MAIN
, "bandb - bandb_restart_cb called, bandb helper died?");
443 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
444 "bandb - bandb_restart_cb called, bandb helper died?");
447 rb_helper_close(helper
);