]>
jfr.im git - irc/evilnet/x3.git/blob - src/gline.c
1 /* gline.c - Gline database
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of x3.
6 * x3 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 #ifdef HAVE_SYS_SOCKET_H
29 #include <sys/socket.h>
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
34 #ifdef HAVE_ARPA_INET_H
35 #include <arpa/inet.h>
41 #define KEY_REASON "reason"
42 #define KEY_EXPIRES "expires"
43 #define KEY_ISSUER "issuer"
44 #define KEY_ISSUED "issued"
46 static heap_t gline_heap
; /* key: expiry time, data: struct gline_entry* */
47 static dict_t gline_dict
; /* key: target, data: struct gline_entry* */
50 gline_comparator(const void *a
, const void *b
)
52 const struct gline
*ga
=a
, *gb
=b
;
53 return ga
->expires
- gb
->expires
;
57 free_gline_from_dict(void *data
)
59 struct gline
*ent
= data
;
67 free_gline(struct gline
*ent
)
69 dict_remove(gline_dict
, ent
->target
);
73 gline_for_p(UNUSED_ARG(void *key
), void *data
, void *extra
)
75 struct gline
*ge
= data
;
76 return !irccasecmp(ge
->target
, extra
);
80 delete_gline_for_p(UNUSED_ARG(void *key
), void *data
, void *extra
)
82 struct gline
*ge
= data
;
84 if (!irccasecmp(ge
->target
, extra
)) {
93 gline_expire(UNUSED_ARG(void *data
))
99 while (heap_size(gline_heap
)) {
100 heap_peek(gline_heap
, 0, &wraa
);
101 stopped
= ((struct gline
*)wraa
)->expires
;
104 heap_pop(gline_heap
);
107 if (heap_size(gline_heap
))
108 timeq_add(stopped
, gline_expire
, NULL
);
112 gline_remove(const char *target
, int announce
)
114 int res
= dict_find(gline_dict
, target
, NULL
) ? 1 : 0;
115 if (heap_remove_pred(gline_heap
, delete_gline_for_p
, (char*)target
)) {
117 struct gline
*new_first
;
118 heap_peek(gline_heap
, 0, &argh
);
121 timeq_del(0, gline_expire
, 0, TIMEQ_IGNORE_WHEN
|TIMEQ_IGNORE_DATA
);
122 timeq_add(new_first
->expires
, gline_expire
, 0);
131 gline_add(const char *issuer
, const char *target
, unsigned long duration
, const char *reason
, time_t issued
, int announce
, int silent
)
134 struct gline
*prev_first
;
137 heap_peek(gline_heap
, 0, &argh
);
139 ent
= dict_find(gline_dict
, target
, NULL
);
141 heap_remove_pred(gline_heap
, gline_for_p
, (char*)target
);
142 if (ent
->expires
< (time_t)(now
+ duration
))
143 ent
->expires
= now
+ duration
;
145 ent
= malloc(sizeof(*ent
));
146 ent
->issued
= issued
;
147 ent
->issuer
= strdup(issuer
);
148 ent
->target
= strdup(target
);
149 ent
->expires
= now
+ duration
;
150 ent
->reason
= strdup(reason
);
151 dict_insert(gline_dict
, ent
->target
, ent
);
153 heap_insert(gline_heap
, ent
, ent
);
154 if (!prev_first
|| (ent
->expires
< prev_first
->expires
)) {
155 timeq_del(0, gline_expire
, 0, TIMEQ_IGNORE_WHEN
|TIMEQ_IGNORE_DATA
);
156 timeq_add(ent
->expires
, gline_expire
, 0);
159 irc_gline(NULL
, ent
, silent
);
164 gline_alternate_target(const char *target
)
166 const char *hostname
;
168 /* If no host part, bail. */
169 if (!(hostname
= strchr(target
, '@')))
171 /* If host part contains wildcards, bail. */
172 if (hostname
[strcspn(hostname
, "*?/")])
175 irc_in_addr_t in
; /* move this to the right place */
176 if (irc_pton(&in
, NULL
, hostname
+1)) {
177 if (getnameinfo(/*TODO*/))
179 } else if (!getaddrinfo(/*TODO*/)) {
187 gline_find(const char *target
)
193 res
= dict_find(gline_dict
, target
, NULL
);
196 /* Stock ircu requires BADCHANs to match exactly. */
197 if ((target
[0] == '#') || (target
[0] == '&'))
199 else if (target
[strcspn(target
, "*?")]) {
200 /* Wildcard: do an obnoxiously long search. */
201 for (it
= dict_first(gline_dict
); it
; it
= iter_next(it
)) {
203 if (match_ircglob(target
, res
->target
))
207 /* See if we can resolve the hostname part of the mask. */
208 if ((alt_target
= gline_alternate_target(target
))) {
209 res
= gline_find(alt_target
);
217 gline_refresh_helper(UNUSED_ARG(void *key
), void *data
, void *extra
)
219 struct gline
*ge
= data
;
220 irc_gline(extra
, ge
, 0);
225 gline_refresh_server(struct server
*srv
)
227 heap_remove_pred(gline_heap
, gline_refresh_helper
, srv
);
231 gline_refresh_all(void)
233 heap_remove_pred(gline_heap
, gline_refresh_helper
, 0);
239 return dict_size(gline_dict
);
243 gline_add_record(const char *key
, void *data
, UNUSED_ARG(void *extra
))
245 struct record_data
*rd
= data
;
246 const char *issuer
, *reason
, *dstr
;
247 time_t issued
, expiration
;
249 if (!(reason
= database_get_data(rd
->d
.object
, KEY_REASON
, RECDB_QSTRING
))) {
250 log_module(MAIN_LOG
, LOG_ERROR
, "Missing reason for gline %s", key
);
253 if (!(dstr
= database_get_data(rd
->d
.object
, KEY_EXPIRES
, RECDB_QSTRING
))) {
254 log_module(MAIN_LOG
, LOG_ERROR
, "Missing expiration for gline %s", key
);
257 expiration
= strtoul(dstr
, NULL
, 0);
258 if ((dstr
= database_get_data(rd
->d
.object
, KEY_ISSUED
, RECDB_QSTRING
))) {
259 issued
= strtoul(dstr
, NULL
, 0);
263 if (!(issuer
= database_get_data(rd
->d
.object
, KEY_ISSUER
, RECDB_QSTRING
))) {
264 issuer
= "<unknown>";
266 if (expiration
> now
)
267 gline_add(issuer
, key
, expiration
- now
, reason
, issued
, 0, 0);
272 gline_saxdb_read(struct dict
*db
)
274 return dict_foreach(db
, gline_add_record
, 0) != NULL
;
278 gline_write_entry(UNUSED_ARG(void *key
), void *data
, void *extra
)
280 struct gline
*ent
= data
;
281 struct saxdb_context
*ctx
= extra
;
283 saxdb_start_record(ctx
, ent
->target
, 0);
284 saxdb_write_int(ctx
, KEY_EXPIRES
, ent
->expires
);
285 saxdb_write_int(ctx
, KEY_ISSUED
, ent
->issued
);
286 saxdb_write_string(ctx
, KEY_REASON
, ent
->reason
);
287 saxdb_write_string(ctx
, KEY_ISSUER
, ent
->issuer
);
288 saxdb_end_record(ctx
);
293 gline_saxdb_write(struct saxdb_context
*ctx
)
295 heap_remove_pred(gline_heap
, gline_write_entry
, ctx
);
300 gline_db_cleanup(void)
302 heap_delete(gline_heap
);
303 dict_delete(gline_dict
);
309 gline_heap
= heap_new(gline_comparator
);
310 gline_dict
= dict_new();
311 dict_set_free_data(gline_dict
, free_gline_from_dict
);
312 saxdb_register("gline", gline_saxdb_read
, gline_saxdb_write
);
313 reg_exit_func(gline_db_cleanup
);
316 struct gline_discrim
*
317 gline_discrim_create(struct userNode
*user
, struct userNode
*src
, unsigned int argc
, char *argv
[])
320 struct gline_discrim
*discrim
;
322 discrim
= calloc(1, sizeof(*discrim
));
323 discrim
->max_issued
= now
;
326 for (i
=0; i
<argc
; i
++) {
328 send_message(user
, src
, "MSG_MISSING_PARAMS", argv
[i
]);
330 } else if (!irccasecmp(argv
[i
], "mask") || !irccasecmp(argv
[i
], "host")) {
331 if (!irccasecmp(argv
[++i
], "exact"))
332 discrim
->target_mask_type
= EXACT
;
333 else if (!irccasecmp(argv
[i
], "subset"))
334 discrim
->target_mask_type
= SUBSET
;
335 else if (!irccasecmp(argv
[i
], "superset"))
336 discrim
->target_mask_type
= SUPERSET
;
338 discrim
->target_mask_type
= SUBSET
, i
--;
340 send_message(user
, src
, "MSG_MISSING_PARAMS", argv
[i
-1]);
343 if (!is_gline(argv
[i
]) && !IsChannelName(argv
[i
])) {
344 send_message(user
, src
, "MSG_INVALID_GLINE", argv
[i
]);
347 discrim
->target_mask
= argv
[i
];
348 discrim
->alt_target_mask
= gline_alternate_target(discrim
->target_mask
);
349 } else if (!irccasecmp(argv
[i
], "limit"))
350 discrim
->limit
= strtoul(argv
[++i
], NULL
, 0);
351 else if (!irccasecmp(argv
[i
], "reason"))
352 discrim
->reason_mask
= argv
[++i
];
353 else if (!irccasecmp(argv
[i
], "issuer"))
354 discrim
->issuer_mask
= argv
[++i
];
355 else if (!irccasecmp(argv
[i
], "after"))
356 discrim
->min_expire
= now
+ ParseInterval(argv
[++i
]);
357 else if (!irccasecmp(argv
[i
], "before"))
358 discrim
->max_issued
= now
- ParseInterval(argv
[++i
]);
360 send_message(user
, src
, "MSG_INVALID_CRITERIA", argv
[i
]);
366 free(discrim
->alt_target_mask
);
371 struct gline_search
{
372 struct gline_discrim
*discrim
;
373 gline_search_func func
;
379 gline_discrim_match(struct gline
*gline
, struct gline_discrim
*discrim
)
381 if ((discrim
->issuer_mask
&& !match_ircglob(gline
->issuer
, discrim
->issuer_mask
))
382 || (discrim
->reason_mask
&& !match_ircglob(gline
->reason
, discrim
->reason_mask
))
383 || (discrim
->target_mask
384 && (((discrim
->target_mask_type
== SUBSET
)
385 && !match_ircglobs(discrim
->target_mask
, gline
->target
)
386 && (!discrim
->alt_target_mask
387 || !match_ircglobs(discrim
->alt_target_mask
, gline
->target
)))
388 || ((discrim
->target_mask_type
== EXACT
)
389 && irccasecmp(discrim
->target_mask
, gline
->target
)
390 && (!discrim
->alt_target_mask
391 || !irccasecmp(discrim
->alt_target_mask
, gline
->target
)))
392 || ((discrim
->target_mask_type
== SUPERSET
)
393 && !match_ircglobs(gline
->target
, discrim
->target_mask
)
394 && (!discrim
->alt_target_mask
395 || !match_ircglobs(discrim
->alt_target_mask
, gline
->target
)))))
396 || (discrim
->max_issued
< gline
->issued
)
397 || (discrim
->min_expire
> gline
->expires
)) {
404 gline_search_helper(UNUSED_ARG(void *key
), void *data
, void *extra
)
406 struct gline
*gline
= data
;
407 struct gline_search
*search
= extra
;
409 if (gline_discrim_match(gline
, search
->discrim
)
410 && (search
->hits
++ < search
->discrim
->limit
)) {
411 search
->func(gline
, search
->data
);
417 gline_discrim_search(struct gline_discrim
*discrim
, gline_search_func gsf
, void *data
)
419 struct gline_search search
;
420 search
.discrim
= discrim
;
424 heap_remove_pred(gline_heap
, gline_search_helper
, &search
);