ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/motd.c
Revision: 2167
Committed: Sun Jun 2 20:02:51 2013 UTC (12 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 13284 byte(s)
Log Message:
- motd.c, motd.h: add support for ip masks

File Contents

# User Rev Content
1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     *
4 michael 2150 * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
5     * Copyright (C) 2013 by the Hybrid Development Team.
6 adx 30 *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     */
22    
23 michael 2150 /*! \file motd.c
24     * \brief Message-of-the-day manipulation implementation.
25     * \version $Id$
26     */
27    
28 adx 30 #include "stdinc.h"
29 michael 1011 #include "list.h"
30 adx 30 #include "ircd.h"
31 michael 1309 #include "conf.h"
32 adx 30 #include "send.h"
33 michael 2150 #include "s_serv.h"
34 adx 30 #include "numeric.h"
35     #include "client.h"
36     #include "irc_string.h"
37     #include "memory.h"
38 michael 2150 #include "log.h"
39     #include "motd.h"
40 michael 2167 #include "hostmask.h"
41 adx 30
42 michael 2150
43     /** Global list of messages of the day. */
44     static struct
45 adx 30 {
46 michael 2150 struct Motd *local; /**< Local MOTD. */
47     struct Motd *remote; /**< Remote MOTD. */
48     dlink_list other; /**< MOTDs specified in configuration file. */
49     dlink_list cachelist; /**< List of MotdCache entries. */
50     } MotdList;
51    
52    
53     /*! \brief Create a struct Motd and initialize it.
54     * \param hostmask Hostmask (or connection class name) to filter on.
55     * \param path Path to MOTD file.
56     */
57     static struct Motd *
58     motd_create(const char *hostmask, const char *path)
59     {
60     struct Motd *tmp = MyMalloc(sizeof(struct Motd));
61    
62     if (EmptyString(hostmask))
63     tmp->type = MOTD_UNIVERSAL;
64     else if (class_find(hostmask, 1))
65     tmp->type = MOTD_CLASS;
66     else
67 michael 2167 {
68     switch (parse_netmask(hostmask, &tmp->address, &tmp->addrbits))
69     {
70     case HM_IPV4:
71     tmp->type = MOTD_IPMASKV4;
72     break;
73     #ifdef IPV6
74     case HM_IPV6:
75     tmp->type = MOTD_IPMASKV6;
76     break;
77     #endif
78     default: /* HM_HOST */
79     tmp->type = MOTD_HOSTMASK;
80     break;
81     }
82     }
83 michael 2150
84     if (hostmask != NULL)
85     tmp->hostmask = xstrdup(hostmask);
86    
87     tmp->path = xstrdup(path);
88     tmp->maxcount = MOTD_MAXLINES;
89    
90     return tmp;
91 adx 30 }
92    
93 michael 2150 /*! brief\ This function reads a motd out of a file (if needed) and caches it.
94     * If a matching cache entry already exists, reuse it. Otherwise,
95     * allocate and populate a new MotdCache for it.
96     * \param motd Specification for MOTD file.
97     * \return Matching MotdCache entry.
98     */
99     static struct MotdCache *
100     motd_cache(struct Motd *motd)
101 adx 30 {
102 michael 2150 FILE *file = NULL;
103     struct MotdCache *cache = NULL;
104     struct stat sb;
105     char line[MOTD_LINESIZE + 2]; /* \r\n */
106 michael 2164 char *tmp = line;
107     unsigned int i = 0;
108 michael 2150 dlink_node *ptr = NULL;
109 adx 30
110 michael 2150 assert(motd);
111     assert(motd->path);
112 adx 30
113 michael 2150 if (motd->cache)
114     return motd->cache;
115 adx 30
116 michael 2150 /* try to find it in the list of cached files... */
117     DLINK_FOREACH(ptr, MotdList.cachelist.head)
118     {
119     cache = ptr->data;
120 adx 30
121 michael 2150 if (!strcmp(cache->path, motd->path) && cache->maxcount == motd->maxcount)
122     {
123     cache->ref++; /* increase reference count... */
124     motd->cache = cache; /* remember cache... */
125     return motd->cache; /* return it */
126     }
127     }
128    
129     /* gotta read in the file, now */
130     if ((file = fopen(motd->path, "r")) == NULL)
131 adx 30 {
132 michael 2150 ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", motd->path,
133     strerror(errno));
134     return 0;
135     }
136 adx 30
137 michael 2150 /* need the file's modification time */
138     if (stat(motd->path, &sb) == -1)
139     {
140     fclose(file);
141     return 0;
142     }
143 adx 30
144 michael 2150 /* Ok, allocate a structure; we'll realloc later to trim memory */
145     cache = MyMalloc(sizeof(struct MotdCache) + (MOTD_LINESIZE * (MOTD_MAXLINES - 1)));
146     cache->ref = 1;
147     cache->path = xstrdup(motd->path);
148     cache->maxcount = motd->maxcount;
149     cache->modtime = *localtime((time_t *)&sb.st_mtime); /* store modtime */
150 adx 30
151 michael 2150 while (cache->count < cache->maxcount && fgets(line, sizeof(line), file))
152     {
153     /* copy over line, stopping when we overflow or hit line end */
154 michael 2164 for (; i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n'; ++tmp, ++i)
155 michael 2150 cache->motd[cache->count][i] = *tmp;
156     cache->motd[cache->count][i] = '\0';
157 adx 30
158 michael 2150 cache->count++;
159 adx 30 }
160    
161 michael 2150 fclose(file); /* close the file */
162    
163     /* trim memory usage a little */
164     motd->cache = MyMalloc(sizeof(struct MotdCache) +
165     (MOTD_LINESIZE * (cache->count - 1)));
166     memcpy(motd->cache, cache, sizeof(struct MotdCache) +
167     (MOTD_LINESIZE * (cache->count - 1)));
168     MyFree(cache);
169    
170     /* now link it in... */
171     dlinkAdd(motd->cache, &motd->cache->node, &MotdList.cachelist);
172    
173     return motd->cache;
174 adx 30 }
175    
176 michael 2150 /*! \brief Clear and dereference the Motd::cache element of \a motd.
177     * If the MotdCache::ref count goes to zero, free it.
178     * \param motd MOTD to uncache.
179 adx 30 */
180 michael 2150 static void
181     motd_decache(struct Motd *motd)
182 adx 30 {
183 michael 2150 struct MotdCache *cache = NULL;
184 adx 30
185 michael 2150 assert(motd);
186 adx 30
187 michael 2150 if ((cache = motd->cache) == NULL) /* we can be called for records with no cache */
188     return;
189 adx 30
190 michael 2150 motd->cache = NULL; /* zero the cache */
191 adx 30
192 michael 2164 if (--cache->ref == 0) /* reduce reference count... */
193 adx 30 {
194 michael 2150 dlinkDelete(&cache->node, &MotdList.cachelist);
195     MyFree(cache->path); /* free path info... */
196     MyFree(cache); /* very simple for a reason... */
197 adx 30 }
198 michael 2150 }
199 adx 30
200 michael 2150 /*! \brief Deallocate a MOTD structure.
201     * If it has cached content, uncache it.
202     * \param motd MOTD to destroy.
203     */
204     static void
205     motd_destroy(struct Motd *motd)
206     {
207     assert(motd);
208 adx 30
209 michael 2150 if (motd->cache) /* drop the cache */
210     motd_decache(motd);
211 adx 30
212 michael 2150 dlinkDelete(&motd->node, &MotdList.other);
213     MyFree(motd->path); /* we always must have a path */
214     MyFree(motd->hostmask);
215     MyFree(motd);
216     }
217 adx 30
218 michael 2150 /*! \brief Find the first matching MOTD block for a user.
219     * If the user is remote, always use remote MOTD.
220     * Otherwise, if there is a hostmask- or class-based MOTD that matches
221     * the user, use it.
222     * Otherwise, use the local MOTD.
223     * \param client_p Client to find MOTD for.
224     * \return Pointer to first matching MOTD for the client.
225     */
226     static struct Motd *
227     motd_lookup(struct Client *client_p)
228     {
229     dlink_node *ptr = NULL;
230     const struct ClassItem *class = NULL;
231 adx 30
232 michael 2150 assert(client_p);
233 adx 30
234 michael 2150 if (!MyClient(client_p)) /* not my user, always return remote motd */
235     return MotdList.remote;
236 adx 30
237 michael 2150 class = get_class_ptr(&client_p->localClient->confs);
238     assert(class);
239 adx 30
240 michael 2150 /* check the motd blocks first */
241     DLINK_FOREACH(ptr, MotdList.other.head)
242     {
243     struct Motd *motd = ptr->data;
244 adx 30
245 michael 2150 switch (motd->type)
246 adx 30 {
247 michael 2150 case MOTD_CLASS:
248     if (!match(motd->hostmask, class->name))
249     return motd;
250 michael 2167 break;
251 michael 2150 case MOTD_HOSTMASK:
252     if (!match(motd->hostmask, client_p->host))
253     return motd;
254 michael 2167 break;
255     case MOTD_IPMASKV4:
256     if (client_p->localClient->aftype == AF_INET)
257     if (match_ipv4(&client_p->localClient->ip, &motd->address, motd->addrbits))
258     return motd;
259     break;
260     case MOTD_IPMASKV6:
261     if (client_p->localClient->aftype == AF_INET6)
262     if (match_ipv6(&client_p->localClient->ip, &motd->address, motd->addrbits))
263     return motd;
264     break;
265 michael 2150 default: break;
266 adx 30 }
267     }
268    
269 michael 2150 return MotdList.local; /* Ok, return the default motd */
270 adx 30 }
271    
272 michael 2150 /*! \brief Send the content of a MotdCache to a user.
273     * If \a cache is NULL, simply send ERR_NOMOTD to the client.
274     * \param source_p Client to send MOTD to.
275     * \param cache MOTD body to send to client.
276 adx 30 */
277 michael 2150 static void
278     motd_forward(struct Client *source_p, const struct MotdCache *cache)
279     {
280 michael 2164 unsigned int i = 0;
281 michael 2150 const char *from = ID_or_name(&me, source_p->from);
282     const char *to = ID_or_name(source_p, source_p->from);
283 adx 30
284 michael 2150 assert(source_p);
285    
286     if (!cache) /* no motd to send */
287     {
288     sendto_one(source_p, form_str(ERR_NOMOTD), from, to);
289     return;
290     }
291    
292     /* send the motd */
293     sendto_one(source_p, form_str(RPL_MOTDSTART),
294     from, to, me.name);
295    
296 michael 2164 for (; i < cache->count; ++i)
297 michael 2150 sendto_one(source_p, form_str(RPL_MOTD),
298     from, to, cache->motd[i]);
299     sendto_one(source_p, form_str(RPL_ENDOFMOTD), from, to);
300     }
301    
302     /*! \brief Find the MOTD for a client and send it.
303     * \param client_p Client being greeted.
304     */
305     void
306     motd_send(struct Client *client_p)
307 adx 30 {
308 michael 2150 assert(client_p);
309 adx 30
310 michael 2150 motd_forward(client_p, motd_cache(motd_lookup(client_p)));
311 adx 30 }
312    
313 michael 2150 /*! \brief 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 source_p Client that has just connected.
318 adx 30 */
319     void
320 michael 2150 motd_signon(struct Client *source_p)
321 adx 30 {
322 michael 2150 const struct MotdCache *cache = motd_cache(motd_lookup(source_p));
323 adx 30
324 michael 2150 if (!ConfigFileEntry.short_motd || !cache)
325     motd_forward(source_p, cache);
326     else
327 adx 30 {
328 michael 2150 sendto_one(source_p, ":%s NOTICE %s :*** Notice -- motd was last changed at %d/%d/%d %d:%02d",
329     me.name, source_p->name, cache->modtime.tm_year + 1900,
330     cache->modtime.tm_mon + 1,
331     cache->modtime.tm_mday,
332     cache->modtime.tm_hour,
333     cache->modtime.tm_min);
334     sendto_one(source_p,
335     ":%s NOTICE %s :*** Notice -- Please read the motd if you haven't "
336     "read it", me.name, source_p->name);
337     sendto_one(source_p, form_str(RPL_MOTDSTART),
338     me.name, source_p->name, me.name);
339     sendto_one(source_p, form_str(RPL_MOTD),
340     me.name, source_p->name,
341     "*** This is the short motd ***");
342     sendto_one(source_p, form_str(RPL_ENDOFMOTD),
343     me.name, source_p->name);
344 adx 30 }
345 michael 2150 }
346    
347     /*! \brief Clear all cached MOTD bodies.
348     * The local and remote MOTDs are re-cached immediately.
349     */
350     void
351     motd_recache(void)
352     {
353     dlink_node *ptr = NULL;
354    
355     motd_decache(MotdList.local); /* decache local and remote MOTDs */
356     motd_decache(MotdList.remote);
357    
358     DLINK_FOREACH(ptr, MotdList.other.head) /* now all the others */
359     motd_decache(ptr->data);
360    
361     /* now recache local and remote MOTDs */
362     motd_cache(MotdList.local);
363     motd_cache(MotdList.remote);
364     }
365    
366     /*! \brief Re-cache the local and remote MOTDs.
367     * If they already exist, they are deallocated first.
368     */
369     void
370     motd_init(void)
371     {
372     if (MotdList.local) /* destroy old local... */
373     motd_destroy(MotdList.local);
374    
375     MotdList.local = motd_create(0, MPATH);
376     motd_cache(MotdList.local); /* init local and cache it */
377    
378     if (MotdList.remote) /* destroy old remote... */
379     motd_destroy(MotdList.remote);
380    
381     MotdList.remote = motd_create(0, MPATH);
382     motd_cache(MotdList.remote); /* init remote and cache it */
383     }
384    
385     /* \brief Add a new MOTD.
386     * \param hostmask Hostmask (or connection class name) to send this to.
387     * \param path Pathname of file to send.
388     */
389     void
390     motd_add(const char *hostmask, const char *path)
391     {
392     struct Motd *motd = motd_create(hostmask, path); /* create the motd */
393    
394     dlinkAdd(motd, &motd->node, &MotdList.other);
395     }
396    
397     /*! \brief 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     dlink_node *ptr = NULL, *ptr_next = NULL;
406    
407     motd_decache(MotdList.local); /* decache local and remote MOTDs */
408     motd_decache(MotdList.remote);
409    
410     DLINK_FOREACH_SAFE(ptr, ptr_next, MotdList.other.head) /* destroy other MOTDs */
411     motd_destroy(ptr->data);
412    
413     /* now recache local and remote MOTDs */
414     motd_cache(MotdList.local);
415     motd_cache(MotdList.remote);
416     }
417    
418     /*! \brief Report list of non-default MOTDs.
419     * \param source_p Client requesting statistics.
420     */
421     void
422     motd_report(struct Client *source_p)
423     {
424     const dlink_node *ptr = NULL;
425    
426     DLINK_FOREACH(ptr, MotdList.other.head)
427 adx 30 {
428 michael 2150 const struct Motd *motd = ptr->data;
429    
430     sendto_one(source_p, form_str(RPL_STATSTLINE),
431     me.name, source_p->name,
432     motd->hostmask, motd->path);
433 adx 30 }
434     }
435    
436 michael 2150 /*! \brief Report MOTD memory usage to a client.
437     * \param source_p Client requesting memory usage.
438 adx 30 */
439     void
440 michael 2150 motd_memory_count(struct Client *source_p)
441 adx 30 {
442 michael 2150 const dlink_node *ptr = NULL;
443     unsigned int mt = 0; /* motd count */
444     unsigned int mtc = 0; /* motd cache count */
445     size_t mtm = 0; /* memory consumed by motd */
446     size_t mtcm = 0; /* memory consumed by motd cache */
447 adx 30
448 michael 2150 if (MotdList.local)
449     {
450     mt++;
451     mtm += sizeof(struct Motd);
452     mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0;
453     }
454 adx 30
455 michael 2150 if (MotdList.remote)
456 adx 30 {
457 michael 2150 mt++;
458     mtm += sizeof(struct Motd);
459     mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0;
460     }
461    
462     DLINK_FOREACH(ptr, MotdList.other.head)
463     {
464     const struct MotdCache *motd = ptr->data;
465    
466     mt++;
467     mtm += sizeof(struct Motd);
468     mtm += motd->path ? (strlen(motd->path) + 1) : 0;
469     }
470    
471     DLINK_FOREACH(ptr, MotdList.cachelist.head)
472     {
473     const struct MotdCache *cache = ptr->data;
474    
475     mtc++;
476     mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1));
477     }
478    
479     sendto_one(source_p, ":%s %d %s z :Motds %u(%u) Cache %u(%u)",
480     me.name, RPL_STATSDEBUG, source_p->name,
481     mt, mtm, mtc, mtcm);
482 adx 30 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision