Add support for CIDR-based MOTD masks.
[ircu2.10.12-pk.git] / ircd / motd.c
1 /*
2  * IRC - Internet Relay Chat, ircd/motd.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
6  *
7  * See file AUTHORS in IRC package for additional names of
8  * the programmers.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 1, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 /** @file
25  * @brief Message-of-the-day manipulation implementation.
26  * @version $Id$
27  */
28 #include "config.h"
29
30 #include "motd.h"
31 #include "class.h"
32 #include "client.h"
33 #include "fileio.h"
34 #include "ircd.h"
35 #include "ircd_alloc.h"
36 #include "ircd_features.h"
37 #include "ircd_reply.h"
38 #include "ircd_string.h"
39 #include "match.h"
40 #include "msg.h"
41 #include "numeric.h"
42 #include "numnicks.h"
43 #include "s_conf.h"
44 #include "s_debug.h"
45 #include "s_user.h"
46 #include "s_stats.h"
47 #include "send.h"
48
49 #include <assert.h>
50 #include <errno.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <sys/stat.h>
54
55 /** Global list of messages of the day. */
56 static struct {
57   struct Motd*      local;     /**< Local MOTD. */
58   struct Motd*      remote;    /**< Remote MOTD. */
59   struct Motd*      other;     /**< MOTDs specified in configuration file. */
60   struct Motd*      freelist;  /**< Currently unused Motd structs. */
61   struct MotdCache* cachelist; /**< List of MotdCache entries. */
62 } MotdList = { 0, 0, 0, 0, 0 };
63
64 /** Create a struct Motd and initialize it.
65  * @param[in] hostmask Hostmask (or connection class name) to filter on.
66  * @param[in] path Path to MOTD file.
67  * @param[in] maxcount Maximum number of lines permitted for MOTD.
68  */
69 static struct Motd *
70 motd_create(const char *hostmask, const char *path, int maxcount)
71 {
72   struct Motd* tmp;
73
74   assert(0 != path);
75
76   /* allocate memory and initialize the structure */
77   if (MotdList.freelist)
78   {
79     tmp = MotdList.freelist;
80     MotdList.freelist = tmp->next;
81   } else
82     tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
83   tmp->next = 0;
84
85   if (hostmask == NULL)
86     tmp->type = MOTD_UNIVERSAL;
87   else if (find_class(hostmask))
88     tmp->type = MOTD_CLASS;
89   else if (ipmask_parse(hostmask, &tmp->address, &tmp->addrbits))
90     tmp->type = MOTD_IPMASK;
91   else
92     tmp->type = MOTD_HOSTMASK;
93
94   if (hostmask != NULL)
95     DupString(tmp->hostmask, hostmask);
96
97   DupString(tmp->path, path);
98   tmp->maxcount = maxcount;
99   tmp->cache = 0;
100
101   return tmp;
102 }
103
104 /** This function reads a motd out of a file (if needed) and caches it.
105  * If a matching cache entry already exists, reuse it.  Otherwise,
106  * allocate and populate a new MotdCache for it.
107  * @param[in] motd Specification for MOTD file.
108  * @return Matching MotdCache entry.
109  */
110 static struct MotdCache *
111 motd_cache(struct Motd *motd)
112 {
113   FBFILE*               file;
114   struct MotdCache*     cache;
115   struct stat           sb;
116   char                  line[MOTD_LINESIZE + 2]; /* \r\n */
117   char*                 tmp;
118   int                   i;
119
120   assert(0 != motd);
121   assert(0 != motd->path);
122
123   if (motd->cache)
124     return motd->cache;
125
126   /* try to find it in the list of cached files... */
127   for (cache = MotdList.cachelist; cache; cache = cache->next) {
128     if (!strcmp(cache->path, motd->path) &&
129         cache->maxcount == motd->maxcount) { /* found one... */
130       cache->ref++; /* increase reference count... */
131       motd->cache = cache; /* remember cache... */
132       return motd->cache; /* return it */
133     }
134   }
135
136   /* gotta read in the file, now */
137   if (!(file = fbopen(motd->path, "r"))) {
138     Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motd->path,
139            strerror(errno)));
140     return 0;
141   }
142
143   /* need the file's modification time */
144   if (-1 == fbstat(&sb, file)) {
145     fbclose(file);
146     return 0;
147   }
148
149   /* Ok, allocate a structure; we'll realloc later to trim memory */
150   cache = (struct MotdCache *)MyMalloc(sizeof(struct MotdCache) +
151                                        (MOTD_LINESIZE * (MOTD_MAXLINES - 1)));
152
153   cache->ref = 1;
154   DupString(cache->path, motd->path);
155   cache->maxcount = motd->maxcount;
156
157   cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
158
159   cache->count = 0;
160   while (cache->count < cache->maxcount && fbgets(line, sizeof(line), file)) {
161     /* copy over line, stopping when we overflow or hit line end */
162     for (tmp = line, i = 0;
163          i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n';
164          tmp++, i++)
165       cache->motd[cache->count][i] = *tmp;
166     cache->motd[cache->count][i] = '\0';
167
168     cache->count++;
169   }
170
171   fbclose(file); /* close the file */
172
173   /* trim memory usage a little */
174   motd->cache = (struct MotdCache*)MyMalloc(sizeof(struct MotdCache) +
175                                             (MOTD_LINESIZE * (cache->count - 1)));
176   memcpy(motd->cache, cache, sizeof(struct MotdCache) +
177          (MOTD_LINESIZE * (cache->count - 1)));
178   MyFree(cache);
179
180   /* now link it in... */
181   motd->cache->next = MotdList.cachelist;
182   motd->cache->prev_p = &MotdList.cachelist;
183   if (MotdList.cachelist)
184     MotdList.cachelist->prev_p = &motd->cache->next;
185   MotdList.cachelist = motd->cache;
186
187   return motd->cache;
188 }
189
190 /** Clear and dereference the Motd::cache element of \a motd.
191  * If the MotdCache::ref count goes to zero, free it.
192  * @param[in] motd MOTD to uncache.
193  */
194 static void
195 motd_decache(struct Motd *motd)
196 {
197   struct MotdCache* cache;
198
199   assert(0 != motd);
200
201   if (!(cache = motd->cache)) /* we can be called for records with no cache */
202     return;
203
204   motd->cache = 0; /* zero the cache */
205
206   if (!--cache->ref) { /* reduce reference count... */
207     if (cache->next) /* ref is 0, delink from list and free */
208       cache->next->prev_p = cache->prev_p;
209     *cache->prev_p = cache->next;
210
211     MyFree(cache->path); /* free path info... */
212
213     MyFree(cache); /* very simple for a reason... */
214   }
215 }
216
217 /** Deallocate a MOTD structure.
218  * If it has cached content, uncache it.
219  * @param[in] motd MOTD to destroy.
220  */
221 static void
222 motd_destroy(struct Motd *motd)
223 {
224   assert(0 != motd);
225
226   MyFree(motd->path); /* we always must have a path */
227   MyFree(motd->hostmask);
228   if (motd->cache) /* drop the cache */
229     motd_decache(motd);
230
231   motd->next = MotdList.freelist;
232   MotdList.freelist = motd;
233 }
234
235 /** Find the first matching MOTD block for a user.
236  * If the user is remote, always use remote MOTD.
237  * Otherwise, if there is a hostmask- or class-based MOTD that matches
238  * the user, use it.
239  * Otherwise, use the local MOTD.
240  * @param[in] cptr Client to find MOTD for.
241  * @return Pointer to first matching MOTD for the client.
242  */
243 static struct Motd *
244 motd_lookup(struct Client *cptr)
245 {
246   struct Motd *ptr;
247   char *c_class = NULL;
248
249   assert(0 != cptr);
250
251   if (!MyUser(cptr)) /* not my user, always return remote motd */
252     return MotdList.remote;
253
254   c_class = get_client_class(cptr);
255   assert(c_class != NULL);
256
257   /* check the motd blocks first */
258   for (ptr = MotdList.other; ptr; ptr = ptr->next)
259   {
260     if (ptr->type == MOTD_CLASS
261         && !match(ptr->hostmask, c_class))
262       return ptr;
263     else if (ptr->type == MOTD_HOSTMASK
264              && !match(ptr->hostmask, cli_sockhost(cptr)))
265       return ptr;
266     else if (ptr->type == MOTD_IPMASK
267              && ipmask_check(&cli_ip(cptr), &ptr->address, ptr->addrbits))
268       return ptr;
269   }
270
271   return MotdList.local; /* Ok, return the default motd */
272 }
273
274 /** Send the content of a MotdCache to a user.
275  * If \a cache is NULL, simply send ERR_NOMOTD to the client.
276  * @param[in] cptr Client to send MOTD to.
277  * @param[in] cache MOTD body to send to client.
278  */
279 static int
280 motd_forward(struct Client *cptr, struct MotdCache *cache)
281 {
282   int i;
283
284   assert(0 != cptr);
285
286   if (!cache) /* no motd to send */
287     return send_reply(cptr, ERR_NOMOTD);
288
289   /* send the motd */
290   send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
291   send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
292              cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
293              cache->modtime.tm_mday, cache->modtime.tm_hour,
294              cache->modtime.tm_min);
295
296   for (i = 0; i < cache->count; i++)
297     send_reply(cptr, RPL_MOTD, cache->motd[i]);
298
299   return send_reply(cptr, RPL_ENDOFMOTD); /* end */
300 }
301
302 /** Find the MOTD for a client and send it.
303  * @param[in] cptr Client being greeted.
304  */
305 int
306 motd_send(struct Client* cptr)
307 {
308   assert(0 != cptr);
309
310   return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
311 }
312
313 /** Send the signon MOTD to a user.
314  * If FEAT_NODEFAULTMOTD is true and a matching MOTD exists for the
315  * user, direct the client to type /MOTD to read it.  Otherwise, call
316  * motd_forward() for the user.
317  * @param[in] cptr Client that has just connected.
318  */
319 void
320 motd_signon(struct Client* cptr)
321 {
322   struct MotdCache *cache;
323   const char *banner = NULL;
324
325   cache = motd_cache(motd_lookup(cptr));
326
327   if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
328     motd_forward(cptr, cache);
329   else {
330     send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
331     if ((banner = feature_str(FEAT_MOTD_BANNER)))
332       send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":%s", banner);
333     send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
334                "AUP before continuing using this service.\002");
335     send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
336                "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
337                cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
338                cache->modtime.tm_hour, cache->modtime.tm_min);
339     send_reply(cptr, RPL_ENDOFMOTD);
340   }
341 }
342
343 /** Clear all cached MOTD bodies.
344  * The local and remote MOTDs are re-cached immediately.
345  */
346 void
347 motd_recache(void)
348 {
349   struct Motd* tmp;
350
351   motd_decache(MotdList.local); /* decache local and remote MOTDs */
352   motd_decache(MotdList.remote);
353
354   for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
355     motd_decache(tmp);
356
357   /* now recache local and remote MOTDs */
358   motd_cache(MotdList.local);
359   motd_cache(MotdList.remote);
360 }
361
362 /** Re-cache the local and remote MOTDs.
363  * If they already exist, they are deallocated first.
364  */
365 void
366 motd_init(void)
367 {
368   if (MotdList.local) /* destroy old local... */
369     motd_destroy(MotdList.local);
370
371   MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
372   motd_cache(MotdList.local); /* init local and cache it */
373
374   if (MotdList.remote) /* destroy old remote... */
375     motd_destroy(MotdList.remote);
376
377   MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
378   motd_cache(MotdList.remote); /* init remote and cache it */
379 }
380
381 /** Add a new MOTD.
382  * @param[in] hostmask Hostmask (or connection class name) to send this to.
383  * @param[in] path Pathname of file to send.
384  */
385 void
386 motd_add(const char *hostmask, const char *path)
387 {
388   struct Motd *tmp;
389
390   tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
391
392   tmp->next = MotdList.other; /* link it into the list */
393   MotdList.other = tmp;
394 }
395
396 /** Clear out all MOTDs.
397  * Compared to motd_recache(), this destroys all hostmask- or
398  * class-based MOTDs rather than simply uncaching them.
399  * Re-cache the local and remote MOTDs.
400  */
401 void
402 motd_clear(void)
403 {
404   struct Motd *ptr, *next;
405
406   motd_decache(MotdList.local); /* decache local and remote MOTDs */
407   motd_decache(MotdList.remote);
408
409   if (MotdList.other) /* destroy other MOTDs */
410     for (ptr = MotdList.other; ptr; ptr = next)
411     {
412       next = ptr->next;
413       motd_destroy(ptr);
414     }
415
416   MotdList.other = 0;
417
418   /* now recache local and remote MOTDs */
419   motd_cache(MotdList.local);
420   motd_cache(MotdList.remote);
421 }
422
423 /** Report list of non-default MOTDs.
424  * @param[in] to Client requesting statistics.
425  * @param[in] sd Stats descriptor for request (ignored).
426  * @param[in] param Extra parameter from user (ignored).
427  */
428 void
429 motd_report(struct Client *to, const struct StatDesc *sd, char *param)
430 {
431   struct Motd *ptr;
432
433   for (ptr = MotdList.other; ptr; ptr = ptr->next)
434     send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %s %s",
435                ptr->hostmask, ptr->path);
436 }
437
438 /** Report MOTD memory usage to a client.
439  * @param[in] cptr Client requesting memory usage.
440  */
441 void
442 motd_memory_count(struct Client *cptr)
443 {
444   struct Motd *ptr;
445   struct MotdCache *cache;
446   unsigned int mt = 0,   /* motd count */
447                mtm = 0,  /* memory consumed by motd */
448                mtc = 0,  /* motd cache count */
449                mtcm = 0, /* memory consumed by motd cache */
450                mtf = 0;  /* motd free list count */
451   if (MotdList.local)
452   {
453     mt++;
454     mtm += sizeof(struct Motd);
455     mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0;
456   }
457
458   if (MotdList.remote)
459   {
460     mt++;
461     mtm += sizeof(struct Motd);
462     mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0;
463   }
464
465   for (ptr = MotdList.other; ptr; ptr = ptr->next)
466   {
467     mt++;
468     mtm += sizeof(struct Motd);
469     mtm += ptr->path ? (strlen(ptr->path) + 1) : 0;
470   }
471
472   for (cache = MotdList.cachelist; cache; cache = cache->next)
473   {
474     mtc++;
475     mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1));
476   }
477
478   if (MotdList.freelist)
479     for (ptr = MotdList.freelist; ptr; ptr = ptr->next)
480       mtf++;
481
482   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
483              ":Motds %d(%zu) Cache %d(%zu) Free %d(%zu)",
484              mt, mtm, mtc, mtcm, mtf, (mtf * sizeof(struct Motd)));
485 }