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