]> jfr.im git - solanum.git/blame - modules/m_rehash.c
ircd: hash: use an irc_radixtree for storing resv's
[solanum.git] / modules / m_rehash.c
CommitLineData
212380e3
AC
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_rehash.c: Re-reads the configuration file.
4 *
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
8 *
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.
13 *
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.
18 *
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
22 * USA
23 *
5366977b 24 * $Id: m_rehash.c 3161 2007-01-25 07:23:01Z nenolod $
212380e3
AC
25 */
26
27#include "stdinc.h"
28#include "client.h"
29#include "channel.h"
30#include "common.h"
4562c604 31#include "match.h"
212380e3 32#include "ircd.h"
212380e3
AC
33#include "s_serv.h"
34#include "numeric.h"
212380e3
AC
35#include "s_conf.h"
36#include "s_newconf.h"
4016731b 37#include "logger.h"
212380e3
AC
38#include "send.h"
39#include "msg.h"
40#include "parse.h"
41#include "modules.h"
42#include "hostmask.h"
43#include "reject.h"
44#include "hash.h"
45#include "cache.h"
46be39fa 46#include "irc_radixtree.h"
212380e3
AC
47
48static int mo_rehash(struct Client *, struct Client *, int, const char **);
49static int me_rehash(struct Client *, struct Client *, int, const char **);
50
51struct Message rehash_msgtab = {
52 "REHASH", 0, 0, 0, MFLG_SLOW,
53 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_rehash, 0}, {mo_rehash, 0}}
54};
55
56mapi_clist_av1 rehash_clist[] = { &rehash_msgtab, NULL };
5366977b 57DECLARE_MODULE_AV1(rehash, NULL, NULL, rehash_clist, NULL, NULL, "$Revision: 3161 $");
212380e3
AC
58
59struct hash_commands
60{
61 const char *cmd;
62 void (*handler) (struct Client * source_p);
63};
64
65static void
66rehash_bans_loc(struct Client *source_p)
67{
68 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s is rehashing bans",
69 get_oper_name(source_p));
61b7e0d9
JT
70 if (!MyConnect(source_p))
71 remote_rehash_oper_p = source_p;
212380e3
AC
72
73 rehash_bans(0);
74}
75
76static void
77rehash_dns(struct Client *source_p)
78{
55abcbb2 79 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s is rehashing DNS",
212380e3 80 get_oper_name(source_p));
61b7e0d9
JT
81 if (!MyConnect(source_p))
82 remote_rehash_oper_p = source_p;
212380e3
AC
83
84 /* reread /etc/resolv.conf and reopen res socket */
1eeb0469 85 restart_authd();
212380e3
AC
86}
87
88static void
89rehash_motd(struct Client *source_p)
90{
91 sendto_realops_snomask(SNO_GENERAL, L_ALL,
92 "%s is forcing re-reading of MOTD file",
93 get_oper_name(source_p));
61b7e0d9
JT
94 if (!MyConnect(source_p))
95 remote_rehash_oper_p = source_p;
212380e3 96
041d54ff 97 cache_user_motd();
212380e3
AC
98}
99
100static void
101rehash_omotd(struct Client *source_p)
102{
103 sendto_realops_snomask(SNO_GENERAL, L_ALL,
104 "%s is forcing re-reading of OPER MOTD file",
105 get_oper_name(source_p));
61b7e0d9
JT
106 if (!MyConnect(source_p))
107 remote_rehash_oper_p = source_p;
212380e3
AC
108
109 free_cachefile(oper_motd);
110 oper_motd = cache_file(OPATH, "opers.motd", 0);
111}
112
212380e3
AC
113static void
114rehash_tklines(struct Client *source_p)
115{
116 struct ConfItem *aconf;
637c4932 117 rb_dlink_node *ptr, *next_ptr;
212380e3
AC
118 int i;
119
120 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s is clearing temp klines",
121 get_oper_name(source_p));
61b7e0d9
JT
122 if (!MyConnect(source_p))
123 remote_rehash_oper_p = source_p;
212380e3
AC
124
125 for(i = 0; i < LAST_TEMP_TYPE; i++)
126 {
637c4932 127 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, temp_klines[i].head)
212380e3
AC
128 {
129 aconf = ptr->data;
130
131 delete_one_address_conf(aconf->host, aconf);
555ac41f 132 rb_dlinkDestroy(ptr, &temp_klines[i]);
212380e3
AC
133 }
134 }
135}
136
137static void
138rehash_tdlines(struct Client *source_p)
139{
140 struct ConfItem *aconf;
637c4932 141 rb_dlink_node *ptr, *next_ptr;
212380e3
AC
142 int i;
143
144 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s is clearing temp dlines",
145 get_oper_name(source_p));
61b7e0d9
JT
146 if (!MyConnect(source_p))
147 remote_rehash_oper_p = source_p;
212380e3
AC
148
149 for(i = 0; i < LAST_TEMP_TYPE; i++)
150 {
637c4932 151 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, temp_dlines[i].head)
212380e3
AC
152 {
153 aconf = ptr->data;
154
155 delete_one_address_conf(aconf->host, aconf);
555ac41f 156 rb_dlinkDestroy(ptr, &temp_dlines[i]);
212380e3
AC
157 }
158 }
159}
160
161static void
162rehash_txlines(struct Client *source_p)
163{
164 struct ConfItem *aconf;
5b96d9a6 165 rb_dlink_node *ptr;
637c4932 166 rb_dlink_node *next_ptr;
212380e3
AC
167
168 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s is clearing temp xlines",
169 get_oper_name(source_p));
61b7e0d9
JT
170 if (!MyConnect(source_p))
171 remote_rehash_oper_p = source_p;
212380e3 172
637c4932 173 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, xline_conf_list.head)
212380e3
AC
174 {
175 aconf = ptr->data;
176
97f44d34 177 if(!aconf->hold || aconf->lifetime)
212380e3
AC
178 continue;
179
180 free_conf(aconf);
555ac41f 181 rb_dlinkDestroy(ptr, &xline_conf_list);
212380e3
AC
182 }
183}
184
185static void
186rehash_tresvs(struct Client *source_p)
187{
188 struct ConfItem *aconf;
46be39fa 189 struct irc_radixtree_iteration_state iter;
5b96d9a6 190 rb_dlink_node *ptr;
637c4932 191 rb_dlink_node *next_ptr;
212380e3
AC
192 int i;
193
194 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s is clearing temp resvs",
195 get_oper_name(source_p));
61b7e0d9
JT
196 if (!MyConnect(source_p))
197 remote_rehash_oper_p = source_p;
212380e3 198
46be39fa 199 IRC_RADIXTREE_FOREACH(aconf, &iter, resv_tree)
212380e3 200 {
97f44d34 201 if(!aconf->hold || aconf->lifetime)
212380e3
AC
202 continue;
203
46be39fa 204 irc_radixtree_delete(resv_tree, aconf->host);
212380e3 205 free_conf(aconf);
212380e3 206 }
212380e3 207
637c4932 208 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, resv_conf_list.head)
212380e3
AC
209 {
210 aconf = ptr->data;
211
97f44d34 212 if(!aconf->hold || aconf->lifetime)
212380e3
AC
213 continue;
214
215 free_conf(aconf);
555ac41f 216 rb_dlinkDestroy(ptr, &resv_conf_list);
212380e3
AC
217 }
218}
219
220static void
221rehash_rejectcache(struct Client *source_p)
222{
223 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s is clearing reject cache",
224 get_oper_name(source_p));
61b7e0d9
JT
225 if (!MyConnect(source_p))
226 remote_rehash_oper_p = source_p;
212380e3
AC
227 flush_reject();
228
229}
230
995f300e
JT
231static void
232rehash_throttles(struct Client *source_p)
233{
234 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s is clearing throttles",
235 get_oper_name(source_p));
61b7e0d9
JT
236 if (!MyConnect(source_p))
237 remote_rehash_oper_p = source_p;
995f300e
JT
238 flush_throttle();
239
240}
241
212380e3
AC
242static void
243rehash_help(struct Client *source_p)
244{
245 sendto_realops_snomask(SNO_GENERAL, L_ALL,
55abcbb2 246 "%s is forcing re-reading of HELP files",
212380e3 247 get_oper_name(source_p));
61b7e0d9
JT
248 if (!MyConnect(source_p))
249 remote_rehash_oper_p = source_p;
212380e3
AC
250 load_help();
251}
252
253static void
254rehash_nickdelay(struct Client *source_p)
255{
256 struct nd_entry *nd;
5b96d9a6
AC
257 rb_dlink_node *ptr;
258 rb_dlink_node *safe_ptr;
212380e3
AC
259
260 sendto_realops_snomask(SNO_GENERAL, L_ALL,
261 "%s is clearing the nick delay table",
262 get_oper_name(source_p));
61b7e0d9
JT
263 if (!MyConnect(source_p))
264 remote_rehash_oper_p = source_p;
212380e3 265
5b96d9a6 266 RB_DLINK_FOREACH_SAFE(ptr, safe_ptr, nd_list.head)
212380e3
AC
267 {
268 nd = ptr->data;
55abcbb2 269
212380e3
AC
270 free_nd_entry(nd);
271 }
272}
273
274/* *INDENT-OFF* */
275static struct hash_commands rehash_commands[] =
276{
277 {"BANS", rehash_bans_loc },
278 {"DNS", rehash_dns },
279 {"MOTD", rehash_motd },
280 {"OMOTD", rehash_omotd },
212380e3
AC
281 {"TKLINES", rehash_tklines },
282 {"TDLINES", rehash_tdlines },
283 {"TXLINES", rehash_txlines },
284 {"TRESVS", rehash_tresvs },
285 {"REJECTCACHE", rehash_rejectcache },
995f300e 286 {"THROTTLES", rehash_throttles },
212380e3
AC
287 {"HELP", rehash_help },
288 {"NICKDELAY", rehash_nickdelay },
289 {NULL, NULL }
290};
291/* *INDENT-ON* */
292
293static void
294do_rehash(struct Client *source_p, const char *type)
295{
296 if (type != NULL)
297 {
298 int x;
299 char cmdbuf[100];
300
301 for (x = 0; rehash_commands[x].cmd != NULL && rehash_commands[x].handler != NULL;
302 x++)
303 {
304 if(irccmp(type, rehash_commands[x].cmd) == 0)
305 {
306 sendto_one(source_p, form_str(RPL_REHASHING), me.name,
307 source_p->name, rehash_commands[x].cmd);
212380e3
AC
308 ilog(L_MAIN, "REHASH %s From %s[%s]", type,
309 get_oper_name(source_p), source_p->sockhost);
61b7e0d9
JT
310 rehash_commands[x].handler(source_p);
311 remote_rehash_oper_p = NULL;
212380e3
AC
312 return;
313 }
314 }
315
316 /* We are still here..we didn't match */
317 cmdbuf[0] = '\0';
318 for (x = 0; rehash_commands[x].cmd != NULL && rehash_commands[x].handler != NULL;
319 x++)
320 {
1f9de103
VY
321 rb_strlcat(cmdbuf, " ", sizeof(cmdbuf));
322 rb_strlcat(cmdbuf, rehash_commands[x].cmd, sizeof(cmdbuf));
212380e3 323 }
5366977b 324 sendto_one_notice(source_p, ":rehash one of:%s", cmdbuf);
212380e3
AC
325 }
326 else
327 {
328 sendto_one(source_p, form_str(RPL_REHASHING), me.name, source_p->name,
329 ConfigFileEntry.configfile);
330 sendto_realops_snomask(SNO_GENERAL, L_ALL,
331 "%s is rehashing server config file", get_oper_name(source_p));
61b7e0d9
JT
332 if (!MyConnect(source_p))
333 remote_rehash_oper_p = source_p;
212380e3
AC
334 ilog(L_MAIN, "REHASH From %s[%s]", get_oper_name(source_p),
335 source_p->sockhost);
336 rehash(0);
61b7e0d9 337 remote_rehash_oper_p = NULL;
212380e3
AC
338 }
339}
340
341/*
342 * mo_rehash - REHASH message handler
343 *
344 * parv[1] = rehash type or destination
345 * parv[2] = destination
346 */
347static int
348mo_rehash(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
349{
350 const char *type = NULL, *target_server = NULL;
351
352 if(!IsOperRehash(source_p))
353 {
354 sendto_one(source_p, form_str(ERR_NOPRIVS),
355 me.name, source_p->name, "rehash");
356 return 0;
357 }
358
359 if (parc > 2)
360 type = parv[1], target_server = parv[2];
361 else if (parc > 1 && (strchr(parv[1], '.') || strchr(parv[1], '?') || strchr(parv[1], '*')))
362 type = NULL, target_server = parv[1];
363 else if (parc > 1)
364 type = parv[1], target_server = NULL;
365 else
366 type = NULL, target_server = NULL;
367
368 if (target_server != NULL)
369 {
370 if(!IsOperRemoteBan(source_p))
371 {
372 sendto_one(source_p, form_str(ERR_NOPRIVS),
373 me.name, source_p->name, "remoteban");
374 return 0;
375 }
376 sendto_match_servs(source_p, target_server,
377 CAP_ENCAP, NOCAPS,
378 "ENCAP %s REHASH %s",
379 target_server, type != NULL ? type : "");
380 if (match(target_server, me.name) == 0)
381 return 0;
382 }
383
384 do_rehash(source_p, type);
385
386 return 0;
387}
388
389static int
390me_rehash(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
391{
392
393 if (!IsPerson(source_p))
394 return 0;
395
396 if (!find_shared_conf(source_p->username, source_p->host,
c88cdb00 397 source_p->servptr->name, SHARED_REHASH))
212380e3
AC
398 return 0;
399
400 do_rehash(source_p, parc > 1 ? parv[1] : NULL);
401
402 return 0;
403}