ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/motd.c
Revision: 3292
Committed: Thu Apr 10 13:53:16 2014 UTC (11 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 13031 byte(s)
Log Message:
- motd.c:motd_lookup(): replaced MyClient() test with MyConnect()

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 2916 * Copyright (c) 2000 Kevin L. Mitchell <klmitch@mit.edu>
5     * Copyright (c) 2013-2014 ircd-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 3254 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 michael 2150 } 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 michael 3235 if (hostmask)
85 michael 2150 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 michael 3254 char line[MOTD_LINESIZE + 2]; /* +2 for \r\n */
106 michael 2194 char *tmp = NULL;
107 michael 2164 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 3254 /* Try to find it in the list of cached files... */
117 michael 2150 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 michael 3254 cache->ref++; /* Increase reference count... */
124     motd->cache = cache; /* Remember cache... */
125     return motd->cache; /* Return it */
126 michael 2150 }
127     }
128    
129 michael 3254 /* Need the file's modification time */
130 michael 2190 if (stat(motd->path, &sb) == -1)
131 adx 30 {
132 michael 2190 ilog(LOG_TYPE_IRCD, "Couldn't stat \"%s\": %s", motd->path,
133 michael 2150 strerror(errno));
134     return 0;
135     }
136 adx 30
137 michael 3254 /* Gotta read in the file, now */
138 michael 2190 if ((file = fopen(motd->path, "r")) == NULL)
139 michael 2150 {
140 michael 2190 ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", motd->path,
141     strerror(errno));
142 michael 2150 return 0;
143     }
144 adx 30
145 michael 2150 /* Ok, allocate a structure; we'll realloc later to trim memory */
146 michael 2410 cache = MyMalloc(sizeof(struct MotdCache) + (MOTD_LINESIZE * MOTD_MAXLINES));
147 michael 2150 cache->ref = 1;
148     cache->path = xstrdup(motd->path);
149     cache->maxcount = motd->maxcount;
150     cache->modtime = *localtime((time_t *)&sb.st_mtime); /* store modtime */
151 adx 30
152 michael 2150 while (cache->count < cache->maxcount && fgets(line, sizeof(line), file))
153     {
154 michael 3254 /* Copy over line, stopping when we overflow or hit line end */
155 michael 2194 for (tmp = line, i = 0; i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n'; ++tmp, ++i)
156 michael 2150 cache->motd[cache->count][i] = *tmp;
157     cache->motd[cache->count][i] = '\0';
158 adx 30
159 michael 2150 cache->count++;
160 adx 30 }
161    
162 michael 3254 fclose(file); /* Close the file */
163 michael 2150
164 michael 3254 /* Trim memory usage a little */
165 michael 2150 motd->cache = MyMalloc(sizeof(struct MotdCache) +
166 michael 2410 (MOTD_LINESIZE * cache->count));
167 michael 2150 memcpy(motd->cache, cache, sizeof(struct MotdCache) +
168 michael 2410 (MOTD_LINESIZE * cache->count));
169 michael 2150 MyFree(cache);
170    
171 michael 3254 /* Now link it in... */
172 michael 2150 dlinkAdd(motd->cache, &motd->cache->node, &MotdList.cachelist);
173    
174     return motd->cache;
175 adx 30 }
176    
177 michael 2150 /*! \brief Clear and dereference the Motd::cache element of \a motd.
178     * If the MotdCache::ref count goes to zero, free it.
179     * \param motd MOTD to uncache.
180 adx 30 */
181 michael 2150 static void
182     motd_decache(struct Motd *motd)
183 adx 30 {
184 michael 2150 struct MotdCache *cache = NULL;
185 adx 30
186 michael 2150 assert(motd);
187 adx 30
188 michael 3254 if ((cache = motd->cache) == NULL) /* We can be called for records with no cache */
189 michael 2150 return;
190 adx 30
191 michael 3254 motd->cache = NULL; /* Zero the cache */
192 adx 30
193 michael 3254 if (--cache->ref == 0) /* Reduce reference count... */
194 adx 30 {
195 michael 2150 dlinkDelete(&cache->node, &MotdList.cachelist);
196 michael 3254 MyFree(cache->path); /* Free path info... */
197     MyFree(cache); /* Very simple for a reason... */
198 adx 30 }
199 michael 2150 }
200 adx 30
201 michael 2150 /*! \brief Deallocate a MOTD structure.
202     * If it has cached content, uncache it.
203     * \param motd MOTD to destroy.
204     */
205     static void
206     motd_destroy(struct Motd *motd)
207     {
208     assert(motd);
209 adx 30
210 michael 3254 if (motd->cache) /* Drop the cache */
211 michael 2150 motd_decache(motd);
212 adx 30
213 michael 3254 MyFree(motd->path); /* We always must have a path */
214 michael 2150 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 michael 2874 motd_lookup(const struct Client *client_p)
228 michael 2150 {
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 3292 if (!MyConnect(client_p)) /* Not my user, always return remote motd */
235 michael 2150 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 3254 /* Check the motd blocks first */
241 michael 2150 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 michael 2168 #ifdef IPV6
261 michael 2167 case MOTD_IPMASKV6:
262     if (client_p->localClient->aftype == AF_INET6)
263     if (match_ipv6(&client_p->localClient->ip, &motd->address, motd->addrbits))
264     return motd;
265     break;
266 michael 2168 #endif
267 michael 2150 default: break;
268 adx 30 }
269     }
270    
271 michael 3254 return MotdList.local; /* Ok, return the default motd */
272 adx 30 }
273    
274 michael 2150 /*! \brief Send the content of a MotdCache to a user.
275     * If \a cache is NULL, simply send ERR_NOMOTD to the client.
276     * \param source_p Client to send MOTD to.
277     * \param cache MOTD body to send to client.
278 adx 30 */
279 michael 2150 static void
280     motd_forward(struct Client *source_p, const struct MotdCache *cache)
281     {
282 michael 3254 if (!cache) /* No motd to send */
283 michael 2150 {
284 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOMOTD);
285 michael 2150 return;
286     }
287    
288 michael 3254 /* Send the motd */
289 michael 3109 sendto_one_numeric(source_p, &me, RPL_MOTDSTART, me.name);
290 michael 2150
291 michael 3235 for (unsigned int i = 0; i < cache->count; ++i)
292 michael 3109 sendto_one_numeric(source_p, &me, RPL_MOTD, cache->motd[i]);
293     sendto_one_numeric(source_p, &me, RPL_ENDOFMOTD);
294 michael 2150 }
295    
296     /*! \brief Find the MOTD for a client and send it.
297     * \param client_p Client being greeted.
298     */
299     void
300     motd_send(struct Client *client_p)
301 adx 30 {
302 michael 2150 assert(client_p);
303 adx 30
304 michael 2150 motd_forward(client_p, motd_cache(motd_lookup(client_p)));
305 adx 30 }
306    
307 michael 2150 /*! \brief Send the signon MOTD to a user.
308 michael 3254 * If general::short_motd is true and a matching MOTD exists for the
309     * user, direct the client to type /MOTD to read it. Otherwise, call
310 michael 2150 * motd_forward() for the user.
311     * \param source_p Client that has just connected.
312 adx 30 */
313     void
314 michael 2150 motd_signon(struct Client *source_p)
315 adx 30 {
316 michael 2150 const struct MotdCache *cache = motd_cache(motd_lookup(source_p));
317 adx 30
318 michael 2150 if (!ConfigFileEntry.short_motd || !cache)
319     motd_forward(source_p, cache);
320     else
321 adx 30 {
322 michael 3110 sendto_one_notice(source_p, &me, ":*** Notice -- motd was last changed at %d/%d/%d %d:%02d",
323     cache->modtime.tm_year + 1900,
324     cache->modtime.tm_mon + 1,
325     cache->modtime.tm_mday,
326     cache->modtime.tm_hour,
327     cache->modtime.tm_min);
328     sendto_one_notice(source_p, &me, ":*** Notice -- Please read the motd if you haven't read it");
329 michael 3109 sendto_one_numeric(source_p, &me, RPL_MOTDSTART, me.name);
330     sendto_one_numeric(source_p, &me, RPL_MOTD,
331     "*** This is the short motd ***");
332     sendto_one_numeric(source_p, &me, RPL_ENDOFMOTD);
333 adx 30 }
334 michael 2150 }
335    
336     /*! \brief Clear all cached MOTD bodies.
337     * The local and remote MOTDs are re-cached immediately.
338     */
339     void
340     motd_recache(void)
341     {
342     dlink_node *ptr = NULL;
343    
344 michael 3254 motd_decache(MotdList.local); /* Decache local and remote MOTDs */
345 michael 2150 motd_decache(MotdList.remote);
346    
347 michael 3254 DLINK_FOREACH(ptr, MotdList.other.head) /* Now all the others */
348 michael 2150 motd_decache(ptr->data);
349    
350     /* now recache local and remote MOTDs */
351     motd_cache(MotdList.local);
352     motd_cache(MotdList.remote);
353     }
354    
355     /*! \brief Re-cache the local and remote MOTDs.
356     * If they already exist, they are deallocated first.
357     */
358     void
359     motd_init(void)
360     {
361 michael 3254 if (MotdList.local) /* Destroy old local... */
362 michael 2150 motd_destroy(MotdList.local);
363    
364 michael 2203 MotdList.local = motd_create(NULL, MPATH);
365 michael 3254 motd_cache(MotdList.local); /* Init local and cache it */
366 michael 2150
367 michael 3254 if (MotdList.remote) /* Destroy old remote... */
368 michael 2150 motd_destroy(MotdList.remote);
369    
370 michael 2203 MotdList.remote = motd_create(NULL, MPATH);
371 michael 3254 motd_cache(MotdList.remote); /* Init remote and cache it */
372 michael 2150 }
373    
374     /* \brief Add a new MOTD.
375     * \param hostmask Hostmask (or connection class name) to send this to.
376     * \param path Pathname of file to send.
377     */
378     void
379     motd_add(const char *hostmask, const char *path)
380     {
381     struct Motd *motd = motd_create(hostmask, path); /* create the motd */
382    
383     dlinkAdd(motd, &motd->node, &MotdList.other);
384     }
385    
386     /*! \brief Clear out all MOTDs.
387     * Compared to motd_recache(), this destroys all hostmask- or
388     * class-based MOTDs rather than simply uncaching them.
389     * Re-cache the local and remote MOTDs.
390     */
391     void
392     motd_clear(void)
393     {
394     dlink_node *ptr = NULL, *ptr_next = NULL;
395    
396 michael 3254 motd_decache(MotdList.local); /* Decache local and remote MOTDs */
397 michael 2150 motd_decache(MotdList.remote);
398    
399 michael 3254 DLINK_FOREACH_SAFE(ptr, ptr_next, MotdList.other.head) /* Destroy other MOTDs */
400 michael 2193 {
401     dlinkDelete(ptr, &MotdList.other);
402 michael 2150 motd_destroy(ptr->data);
403 michael 2193 }
404 michael 2150
405     /* now recache local and remote MOTDs */
406     motd_cache(MotdList.local);
407     motd_cache(MotdList.remote);
408     }
409    
410     /*! \brief Report list of non-default MOTDs.
411     * \param source_p Client requesting statistics.
412     */
413     void
414     motd_report(struct Client *source_p)
415     {
416     const dlink_node *ptr = NULL;
417    
418     DLINK_FOREACH(ptr, MotdList.other.head)
419 adx 30 {
420 michael 2150 const struct Motd *motd = ptr->data;
421    
422 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSTLINE,
423     motd->hostmask, motd->path);
424 adx 30 }
425     }
426    
427 michael 2150 /*! \brief Report MOTD memory usage to a client.
428     * \param source_p Client requesting memory usage.
429 adx 30 */
430     void
431 michael 2150 motd_memory_count(struct Client *source_p)
432 adx 30 {
433 michael 2150 const dlink_node *ptr = NULL;
434     unsigned int mt = 0; /* motd count */
435     unsigned int mtc = 0; /* motd cache count */
436     size_t mtm = 0; /* memory consumed by motd */
437     size_t mtcm = 0; /* memory consumed by motd cache */
438 adx 30
439 michael 2150 if (MotdList.local)
440     {
441     mt++;
442     mtm += sizeof(struct Motd);
443     mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0;
444     }
445 adx 30
446 michael 2150 if (MotdList.remote)
447 adx 30 {
448 michael 2150 mt++;
449     mtm += sizeof(struct Motd);
450     mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0;
451     }
452    
453     DLINK_FOREACH(ptr, MotdList.other.head)
454     {
455 michael 2320 const struct Motd *motd = ptr->data;
456 michael 2150
457     mt++;
458     mtm += sizeof(struct Motd);
459     mtm += motd->path ? (strlen(motd->path) + 1) : 0;
460     }
461    
462     DLINK_FOREACH(ptr, MotdList.cachelist.head)
463     {
464     const struct MotdCache *cache = ptr->data;
465    
466     mtc++;
467     mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1));
468     }
469    
470     sendto_one(source_p, ":%s %d %s z :Motds %u(%u) Cache %u(%u)",
471     me.name, RPL_STATSDEBUG, source_p->name,
472     mt, mtm, mtc, mtcm);
473 adx 30 }

Properties

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