]> jfr.im git - solanum.git/blame - ircd/cache.c
ircd/authproc.c: avoid crash on lack of any configured DNSBLs
[solanum.git] / ircd / cache.c
CommitLineData
1087485c
JT
1/*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * cache.c - code for caching files
4 *
5 * Copyright (C) 2003 Lee Hardy <lee@leeh.co.uk>
6 * Copyright (C) 2003-2005 ircd-ratbox development team
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1.Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2.Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3.The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
1087485c
JT
31 */
32
91e2f81c
VY
33#include "stdinc.h"
34#include "ircd_defs.h"
91e2f81c
VY
35#include "s_conf.h"
36#include "client.h"
37#include "hash.h"
38#include "cache.h"
a4bf26dd 39#include "rb_dictionary.h"
1087485c 40#include "numeric.h"
77d3d2db 41#include "send.h"
1087485c
JT
42
43struct cachefile *user_motd = NULL;
44struct cachefile *oper_motd = NULL;
45struct cacheline *emptyline = NULL;
46rb_dlink_list links_cache_list;
47char user_motd_changed[MAX_DATE_STRING];
48
4177311e
EM
49rb_dictionary *help_dict_oper = NULL;
50rb_dictionary *help_dict_user = NULL;
1087485c
JT
51
52/* init_cache()
53 *
54 * inputs -
55 * outputs -
56 * side effects - inits the file/line cache blockheaps, loads motds
57 */
58void
59init_cache(void)
60{
61 /* allocate the emptyline */
62 emptyline = rb_malloc(sizeof(struct cacheline));
3dae60ef
AC
63 emptyline->data = rb_strdup(" ");
64
1087485c
JT
65 user_motd_changed[0] = '\0';
66
4d8cfacd
AC
67 user_motd = cache_file(ircd_paths[IRCD_PATH_IRCD_MOTD], "ircd.motd", 0);
68 oper_motd = cache_file(ircd_paths[IRCD_PATH_IRCD_OMOTD], "opers.motd", 0);
1087485c
JT
69 memset(&links_cache_list, 0, sizeof(links_cache_list));
70
f956cb0f
EM
71 help_dict_oper = rb_dictionary_create("oper help", rb_strcasecmp);
72 help_dict_user = rb_dictionary_create("user help", rb_strcasecmp);
1087485c
JT
73}
74
55abcbb2 75/*
22b98b1e
VY
76 * removes tabs from src, replaces with 8 spaces, and returns the length
77 * of the new string. if the new string would be greater than destlen,
78 * it is truncated to destlen - 1
79 */
80static size_t
81untabify(char *dest, const char *src, size_t destlen)
82{
83 size_t x = 0, i;
84 const char *s = src;
85 char *d = dest;
86
87 while(*s != '\0' && x < destlen - 1)
88 {
89 if(*s == '\t')
90 {
91 for(i = 0; i < 8 && x < destlen - 1; i++, x++, d++)
92 *d = ' ';
93 s++;
55abcbb2 94 } else
22b98b1e
VY
95 {
96 *d++ = *s++;
97 x++;
98 }
99 }
100 *d = '\0';
101 return x;
102}
103
1087485c
JT
104/* cache_file()
105 *
106 * inputs - file to cache, files "shortname", flags to set
107 * outputs - pointer to file cached, else NULL
108 * side effects -
109 */
110struct cachefile *
111cache_file(const char *filename, const char *shortname, int flags)
112{
113 FILE *in;
114 struct cachefile *cacheptr;
115 struct cacheline *lineptr;
116 char line[BUFSIZE];
117 char *p;
118
119 if((in = fopen(filename, "r")) == NULL)
120 return NULL;
121
122
123 cacheptr = rb_malloc(sizeof(struct cachefile));
124
125 rb_strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name));
126 cacheptr->flags = flags;
127
128 /* cache the file... */
129 while(fgets(line, sizeof(line), in) != NULL)
130 {
131 if((p = strpbrk(line, "\r\n")) != NULL)
132 *p = '\0';
133
134 if(!EmptyString(line))
135 {
3dae60ef
AC
136 char untabline[BUFSIZE];
137
1087485c 138 lineptr = rb_malloc(sizeof(struct cacheline));
3dae60ef
AC
139
140 untabify(untabline, line, sizeof(untabline));
141 lineptr->data = rb_strdup(untabline);
142
1087485c
JT
143 rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents);
144 }
145 else
146 rb_dlinkAddTailAlloc(emptyline, &cacheptr->contents);
147 }
148
d06f3da9
SB
149 if (0 == rb_dlink_list_length(&cacheptr->contents))
150 {
151 /* No contents. Don't cache it after all. */
152 rb_free(cacheptr);
153 cacheptr = NULL;
154 }
155
1087485c
JT
156 fclose(in);
157 return cacheptr;
158}
159
160void
161cache_links(void *unused)
162{
163 struct Client *target_p;
164 rb_dlink_node *ptr;
165 rb_dlink_node *next_ptr;
166 char *links_line;
167
168 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head)
169 {
170 rb_free(ptr->data);
171 rb_free_rb_dlink_node(ptr);
172 }
173
174 links_cache_list.head = links_cache_list.tail = NULL;
175 links_cache_list.length = 0;
176
177 RB_DLINK_FOREACH(ptr, global_serv_list.head)
178 {
179 target_p = ptr->data;
180
181 /* skip ourselves (done in /links) and hidden servers */
182 if(IsMe(target_p) ||
183 (IsHidden(target_p) && !ConfigServerHide.disable_hidden))
184 continue;
185
186 /* if the below is ever modified, change LINKSLINELEN */
187 links_line = rb_malloc(LINKSLINELEN);
5203cba5 188 snprintf(links_line, LINKSLINELEN, "%s %s :1 %s",
55abcbb2
KB
189 target_p->name, me.name,
190 target_p->info[0] ? target_p->info :
1087485c
JT
191 "(Unknown Location)");
192
193 rb_dlinkAddTailAlloc(links_line, &links_cache_list);
194 }
195}
196
197/* free_cachefile()
198 *
199 * inputs - cachefile to free
200 * outputs -
201 * side effects - cachefile and its data is free'd
202 */
203void
204free_cachefile(struct cachefile *cacheptr)
205{
206 rb_dlink_node *ptr;
207 rb_dlink_node *next_ptr;
208
209 if(cacheptr == NULL)
210 return;
211
212 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head)
213 {
214 if(ptr->data != emptyline)
3dae60ef
AC
215 {
216 struct cacheline *line = ptr->data;
217 rb_free(line->data);
218 rb_free(line);
219 }
4ce7eaef
SA
220 else
221 {
222 rb_free_rb_dlink_node(ptr);
223 }
1087485c
JT
224 }
225
226 rb_free(cacheptr);
227}
228
212380e3
AC
229/* load_help()
230 *
231 * inputs -
232 * outputs -
e8149a2c
JT
233 * side effects - old help cache deleted
234 * - contents of help directories are loaded.
212380e3
AC
235 */
236void
237load_help(void)
238{
239 DIR *helpfile_dir = NULL;
240 struct dirent *ldirent= NULL;
d74fa5b5 241 char filename[PATH_MAX];
212380e3 242 struct cachefile *cacheptr;
4177311e 243 rb_dictionary_iter iter;
212380e3 244
4231cedc
VY
245#if defined(S_ISLNK) && defined(HAVE_LSTAT)
246 struct stat sb;
247#endif
248
56f84ded 249 RB_DICTIONARY_FOREACH(cacheptr, &iter, help_dict_oper)
e8149a2c 250 {
a4bf26dd 251 rb_dictionary_delete(help_dict_oper, cacheptr->name);
85550587
JT
252 free_cachefile(cacheptr);
253 }
56f84ded 254 RB_DICTIONARY_FOREACH(cacheptr, &iter, help_dict_user)
85550587 255 {
a4bf26dd 256 rb_dictionary_delete(help_dict_user, cacheptr->name);
e8149a2c
JT
257 free_cachefile(cacheptr);
258 }
259
4d8cfacd 260 helpfile_dir = opendir(ircd_paths[IRCD_PATH_OPERHELP]);
212380e3
AC
261
262 if(helpfile_dir == NULL)
263 return;
264
265 while((ldirent = readdir(helpfile_dir)) != NULL)
266 {
cd5d9abf
JT
267 if(ldirent->d_name[0] == '.')
268 continue;
8f0c3422 269 snprintf(filename, sizeof(filename), "%s/%s", ircd_paths[IRCD_PATH_OPERHELP], ldirent->d_name);
212380e3 270 cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER);
a4bf26dd 271 rb_dictionary_add(help_dict_oper, cacheptr->name, cacheptr);
212380e3
AC
272 }
273
274 closedir(helpfile_dir);
0d180487 275 helpfile_dir = opendir(ircd_paths[IRCD_PATH_USERHELP]);
212380e3
AC
276
277 if(helpfile_dir == NULL)
278 return;
279
280 while((ldirent = readdir(helpfile_dir)) != NULL)
281 {
cd5d9abf
JT
282 if(ldirent->d_name[0] == '.')
283 continue;
8f0c3422 284 snprintf(filename, sizeof(filename), "%s/%s", ircd_paths[IRCD_PATH_USERHELP], ldirent->d_name);
212380e3 285
4231cedc
VY
286#if defined(S_ISLNK) && defined(HAVE_LSTAT)
287 if(lstat(filename, &sb) < 0)
288 continue;
289
290 /* ok, if its a symlink, we work on the presumption if an
291 * oper help exists of that name, its a symlink to that --fl
292 */
293 if(S_ISLNK(sb.st_mode))
294 {
a4bf26dd 295 cacheptr = rb_dictionary_retrieve(help_dict_oper, ldirent->d_name);
4231cedc
VY
296
297 if(cacheptr != NULL)
298 {
299 cacheptr->flags |= HELP_USER;
300 continue;
301 }
302 }
303#endif
304
212380e3 305 cacheptr = cache_file(filename, ldirent->d_name, HELP_USER);
a4bf26dd 306 rb_dictionary_add(help_dict_user, cacheptr->name, cacheptr);
212380e3
AC
307 }
308
309 closedir(helpfile_dir);
1087485c
JT
310}
311
312/* send_user_motd()
313 *
314 * inputs - client to send motd to
315 * outputs - client is sent motd if exists, else ERR_NOMOTD
316 * side effects -
317 */
318void
319send_user_motd(struct Client *source_p)
320{
321 struct cacheline *lineptr;
322 rb_dlink_node *ptr;
323 const char *myname = get_id(&me, source_p);
324 const char *nick = get_id(source_p, source_p);
325 if(user_motd == NULL || rb_dlink_list_length(&user_motd->contents) == 0)
326 {
327 sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick);
328 return;
329 }
330
331 sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name);
332
333 RB_DLINK_FOREACH(ptr, user_motd->contents.head)
334 {
335 lineptr = ptr->data;
336 sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data);
337 }
338
339 sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick);
340}
341
342void
343cache_user_motd(void)
344{
345 struct stat sb;
346 struct tm *local_tm;
55abcbb2 347
4d8cfacd 348 if(stat(ircd_paths[IRCD_PATH_IRCD_MOTD], &sb) == 0)
1087485c
JT
349 {
350 local_tm = localtime(&sb.st_mtime);
351
55abcbb2 352 if(local_tm != NULL)
1087485c 353 {
5203cba5 354 snprintf(user_motd_changed, sizeof(user_motd_changed),
1087485c
JT
355 "%d/%d/%d %d:%d",
356 local_tm->tm_mday, local_tm->tm_mon + 1,
357 1900 + local_tm->tm_year, local_tm->tm_hour,
358 local_tm->tm_min);
359 }
55abcbb2 360 }
1087485c 361 free_cachefile(user_motd);
4d8cfacd 362 user_motd = cache_file(ircd_paths[IRCD_PATH_IRCD_MOTD], "ircd.motd", 0);
1087485c 363}
fe18f4bc
JT
364
365
366/* send_oper_motd()
367 *
368 * inputs - client to send motd to
369 * outputs - client is sent oper motd if exists
370 * side effects -
371 */
372void
373send_oper_motd(struct Client *source_p)
374{
375 struct cacheline *lineptr;
376 rb_dlink_node *ptr;
377
378 if(oper_motd == NULL || rb_dlink_list_length(&oper_motd->contents) == 0)
379 return;
380
55abcbb2 381 sendto_one(source_p, form_str(RPL_OMOTDSTART),
fe18f4bc
JT
382 me.name, source_p->name);
383
384 RB_DLINK_FOREACH(ptr, oper_motd->contents.head)
385 {
386 lineptr = ptr->data;
387 sendto_one(source_p, form_str(RPL_OMOTD),
388 me.name, source_p->name, lineptr->data);
389 }
390
55abcbb2 391 sendto_one(source_p, form_str(RPL_ENDOFOMOTD),
fe18f4bc
JT
392 me.name, source_p->name);
393}