ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/motd.c
Revision: 6537
Committed: Sat Sep 12 16:30:37 2015 UTC (9 years, 11 months ago) by michael
Content type: text/x-csrc
File size: 12805 byte(s)
Log Message:
- motd.c, send.c: use %zu conversion specifier for size_t types

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 michael 5347 * Copyright (c) 2013-2015 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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 adx 30 * 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     #include "numeric.h"
34     #include "client.h"
35     #include "irc_string.h"
36     #include "memory.h"
37 michael 2150 #include "log.h"
38     #include "motd.h"
39 michael 2167 #include "hostmask.h"
40 michael 6514 #include "misc.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 michael 4351 * \param mask Hostmask (or connection class name) to filter on.
55 michael 2150 * \param path Path to MOTD file.
56     */
57     static struct Motd *
58 michael 4351 motd_create(const char *mask, const char *path)
59 michael 2150 {
60 michael 3504 struct Motd *tmp = MyCalloc(sizeof(struct Motd));
61 michael 2150
62 michael 4351 if (EmptyString(mask))
63 michael 2150 tmp->type = MOTD_UNIVERSAL;
64 michael 4351 else if (class_find(mask, 1))
65 michael 2150 tmp->type = MOTD_CLASS;
66     else
67 michael 2167 {
68 michael 4351 switch (parse_netmask(mask, &tmp->address, &tmp->addrbits))
69 michael 2167 {
70     case HM_IPV4:
71 michael 5011 tmp->type = MOTD_IPMASKV4;
72     break;
73     case HM_IPV6:
74     tmp->type = MOTD_IPMASKV6;
75     break;
76     default: /* HM_HOST */
77     tmp->type = MOTD_HOSTMASK;
78     break;
79 michael 2167 }
80     }
81 michael 2150
82 michael 4351 if (mask)
83     tmp->mask = xstrdup(mask);
84 michael 2150
85     tmp->path = xstrdup(path);
86     tmp->maxcount = MOTD_MAXLINES;
87    
88     return tmp;
89 adx 30 }
90    
91 michael 2150 /*! brief\ This function reads a motd out of a file (if needed) and caches it.
92     * If a matching cache entry already exists, reuse it. Otherwise,
93     * allocate and populate a new MotdCache for it.
94     * \param motd Specification for MOTD file.
95     * \return Matching MotdCache entry.
96     */
97     static struct MotdCache *
98     motd_cache(struct Motd *motd)
99 adx 30 {
100 michael 2150 FILE *file = NULL;
101     struct MotdCache *cache = NULL;
102     struct stat sb;
103 michael 3254 char line[MOTD_LINESIZE + 2]; /* +2 for \r\n */
104 michael 2194 char *tmp = NULL;
105 michael 2164 unsigned int i = 0;
106 michael 4815 dlink_node *node = NULL;
107 adx 30
108 michael 2150 assert(motd);
109     assert(motd->path);
110 adx 30
111 michael 2150 if (motd->cache)
112     return motd->cache;
113 adx 30
114 michael 4348 /* Try to find it in the list of cached files */
115 michael 4815 DLINK_FOREACH(node, MotdList.cachelist.head)
116 michael 2150 {
117 michael 4815 cache = node->data;
118 adx 30
119 michael 2150 if (!strcmp(cache->path, motd->path) && cache->maxcount == motd->maxcount)
120     {
121 michael 4348 cache->ref++; /* Increase reference count */
122     motd->cache = cache; /* Remember cache */
123 michael 3254 return motd->cache; /* Return it */
124 michael 2150 }
125     }
126    
127 michael 3254 /* Need the file's modification time */
128 michael 2190 if (stat(motd->path, &sb) == -1)
129 adx 30 {
130 michael 2190 ilog(LOG_TYPE_IRCD, "Couldn't stat \"%s\": %s", motd->path,
131 michael 2150 strerror(errno));
132     return 0;
133     }
134 adx 30
135 michael 3254 /* Gotta read in the file, now */
136 michael 2190 if ((file = fopen(motd->path, "r")) == NULL)
137 michael 2150 {
138 michael 2190 ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", motd->path,
139     strerror(errno));
140 michael 2150 return 0;
141     }
142 adx 30
143 michael 2150 /* Ok, allocate a structure; we'll realloc later to trim memory */
144 michael 3504 cache = MyCalloc(sizeof(struct MotdCache) + (MOTD_LINESIZE * MOTD_MAXLINES));
145 michael 2150 cache->ref = 1;
146     cache->path = xstrdup(motd->path);
147     cache->maxcount = motd->maxcount;
148 michael 6514 cache->modtime = sb.st_mtime; /* Store modtime */
149 adx 30
150 michael 2150 while (cache->count < cache->maxcount && fgets(line, sizeof(line), file))
151     {
152 michael 3254 /* Copy over line, stopping when we overflow or hit line end */
153 michael 2194 for (tmp = line, i = 0; i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n'; ++tmp, ++i)
154 michael 2150 cache->motd[cache->count][i] = *tmp;
155     cache->motd[cache->count][i] = '\0';
156 adx 30
157 michael 2150 cache->count++;
158 adx 30 }
159    
160 michael 3254 fclose(file); /* Close the file */
161 michael 2150
162 michael 3254 /* Trim memory usage a little */
163 michael 3504 motd->cache = MyCalloc(sizeof(struct MotdCache) +
164 michael 2410 (MOTD_LINESIZE * cache->count));
165 michael 2150 memcpy(motd->cache, cache, sizeof(struct MotdCache) +
166 michael 2410 (MOTD_LINESIZE * cache->count));
167 michael 2150 MyFree(cache);
168    
169 michael 4348 /* Now link it in */
170 michael 2150 dlinkAdd(motd->cache, &motd->cache->node, &MotdList.cachelist);
171    
172     return motd->cache;
173 adx 30 }
174    
175 michael 2150 /*! \brief Clear and dereference the Motd::cache element of \a motd.
176     * If the MotdCache::ref count goes to zero, free it.
177     * \param motd MOTD to uncache.
178 adx 30 */
179 michael 2150 static void
180     motd_decache(struct Motd *motd)
181 adx 30 {
182 michael 2150 struct MotdCache *cache = NULL;
183 adx 30
184 michael 2150 assert(motd);
185 adx 30
186 michael 3254 if ((cache = motd->cache) == NULL) /* We can be called for records with no cache */
187 michael 2150 return;
188 adx 30
189 michael 3254 motd->cache = NULL; /* Zero the cache */
190 adx 30
191 michael 4348 if (--cache->ref == 0) /* Reduce reference count */
192 adx 30 {
193 michael 2150 dlinkDelete(&cache->node, &MotdList.cachelist);
194 michael 4348 MyFree(cache->path); /* Free path info */
195     MyFree(cache); /* Very simple for a reason */
196 adx 30 }
197 michael 2150 }
198 adx 30
199 michael 2150 /*! \brief Deallocate a MOTD structure.
200     * If it has cached content, uncache it.
201     * \param motd MOTD to destroy.
202     */
203     static void
204     motd_destroy(struct Motd *motd)
205     {
206     assert(motd);
207 adx 30
208 michael 3254 if (motd->cache) /* Drop the cache */
209 michael 2150 motd_decache(motd);
210 adx 30
211 michael 3254 MyFree(motd->path); /* We always must have a path */
212 michael 4351 MyFree(motd->mask);
213 michael 2150 MyFree(motd);
214     }
215 adx 30
216 michael 2150 /*! \brief Find the first matching MOTD block for a user.
217     * If the user is remote, always use remote MOTD.
218     * Otherwise, if there is a hostmask- or class-based MOTD that matches
219     * the user, use it.
220     * Otherwise, use the local MOTD.
221     * \param client_p Client to find MOTD for.
222     * \return Pointer to first matching MOTD for the client.
223     */
224     static struct Motd *
225 michael 2874 motd_lookup(const struct Client *client_p)
226 michael 2150 {
227 michael 4815 dlink_node *node = NULL;
228 michael 2150 const struct ClassItem *class = NULL;
229 adx 30
230 michael 2150 assert(client_p);
231 adx 30
232 michael 3292 if (!MyConnect(client_p)) /* Not my user, always return remote motd */
233 michael 2150 return MotdList.remote;
234 adx 30
235 michael 4588 class = get_class_ptr(&client_p->connection->confs);
236 michael 2150 assert(class);
237 adx 30
238 michael 3254 /* Check the motd blocks first */
239 michael 4815 DLINK_FOREACH(node, MotdList.other.head)
240 michael 2150 {
241 michael 4815 struct Motd *motd = node->data;
242 adx 30
243 michael 2150 switch (motd->type)
244 adx 30 {
245 michael 2150 case MOTD_CLASS:
246 michael 4351 if (!match(motd->mask, class->name))
247 michael 2150 return motd;
248 michael 2167 break;
249 michael 2150 case MOTD_HOSTMASK:
250 michael 5387 if (!match(motd->mask, client_p->host) || !match(motd->mask, client_p->sockhost))
251 michael 2150 return motd;
252 michael 2167 break;
253     case MOTD_IPMASKV4:
254 michael 4588 if (client_p->connection->aftype == AF_INET)
255     if (match_ipv4(&client_p->connection->ip, &motd->address, motd->addrbits))
256 michael 2167 return motd;
257     break;
258     case MOTD_IPMASKV6:
259 michael 4588 if (client_p->connection->aftype == AF_INET6)
260     if (match_ipv6(&client_p->connection->ip, &motd->address, motd->addrbits))
261 michael 2167 return motd;
262     break;
263 michael 2150 default: break;
264 adx 30 }
265     }
266    
267 michael 3254 return MotdList.local; /* Ok, return the default motd */
268 adx 30 }
269    
270 michael 2150 /*! \brief Send the content of a MotdCache to a user.
271     * If \a cache is NULL, simply send ERR_NOMOTD to the client.
272     * \param source_p Client to send MOTD to.
273     * \param cache MOTD body to send to client.
274 adx 30 */
275 michael 2150 static void
276     motd_forward(struct Client *source_p, const struct MotdCache *cache)
277     {
278 michael 3254 if (!cache) /* No motd to send */
279 michael 2150 {
280 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOMOTD);
281 michael 2150 return;
282     }
283    
284 michael 3254 /* Send the motd */
285 michael 3109 sendto_one_numeric(source_p, &me, RPL_MOTDSTART, me.name);
286 michael 2150
287 michael 3235 for (unsigned int i = 0; i < cache->count; ++i)
288 michael 3109 sendto_one_numeric(source_p, &me, RPL_MOTD, cache->motd[i]);
289 michael 4890
290 michael 3109 sendto_one_numeric(source_p, &me, RPL_ENDOFMOTD);
291 michael 2150 }
292    
293     /*! \brief Find the MOTD for a client and send it.
294     * \param client_p Client being greeted.
295     */
296     void
297     motd_send(struct Client *client_p)
298 adx 30 {
299 michael 2150 assert(client_p);
300 adx 30
301 michael 2150 motd_forward(client_p, motd_cache(motd_lookup(client_p)));
302 adx 30 }
303    
304 michael 2150 /*! \brief Send the signon MOTD to a user.
305 michael 3254 * If general::short_motd is true and a matching MOTD exists for the
306     * user, direct the client to type /MOTD to read it. Otherwise, call
307 michael 2150 * motd_forward() for the user.
308     * \param source_p Client that has just connected.
309 adx 30 */
310     void
311 michael 2150 motd_signon(struct Client *source_p)
312 adx 30 {
313 michael 2150 const struct MotdCache *cache = motd_cache(motd_lookup(source_p));
314 adx 30
315 michael 4340 if (!ConfigGeneral.short_motd || !cache)
316 michael 2150 motd_forward(source_p, cache);
317     else
318 adx 30 {
319 michael 6514 sendto_one_notice(source_p, &me, ":*** Notice -- motd was last changed at %s",
320     date_iso8601(cache->modtime));
321 michael 3110 sendto_one_notice(source_p, &me, ":*** Notice -- Please read the motd if you haven't read it");
322 michael 3109 sendto_one_numeric(source_p, &me, RPL_MOTDSTART, me.name);
323 michael 4890 sendto_one_numeric(source_p, &me, RPL_MOTD, "*** This is the short motd ***");
324 michael 3109 sendto_one_numeric(source_p, &me, RPL_ENDOFMOTD);
325 adx 30 }
326 michael 2150 }
327    
328     /*! \brief Clear all cached MOTD bodies.
329     * The local and remote MOTDs are re-cached immediately.
330     */
331     void
332     motd_recache(void)
333     {
334 michael 4815 dlink_node *node = NULL;
335 michael 2150
336 michael 3254 motd_decache(MotdList.local); /* Decache local and remote MOTDs */
337 michael 2150 motd_decache(MotdList.remote);
338    
339 michael 4815 DLINK_FOREACH(node, MotdList.other.head) /* Now all the others */
340     motd_decache(node->data);
341 michael 2150
342 michael 4348 /* Now recache local and remote MOTDs */
343 michael 2150 motd_cache(MotdList.local);
344     motd_cache(MotdList.remote);
345     }
346    
347     /*! \brief Re-cache the local and remote MOTDs.
348     * If they already exist, they are deallocated first.
349     */
350     void
351     motd_init(void)
352     {
353 michael 4348 if (MotdList.local) /* Destroy old local MOTD */
354 michael 2150 motd_destroy(MotdList.local);
355    
356 michael 2203 MotdList.local = motd_create(NULL, MPATH);
357 michael 4348 motd_cache(MotdList.local); /* Initialize local MOTD and cache it */
358 michael 2150
359 michael 4348 if (MotdList.remote) /* Destroy old remote MOTD */
360 michael 2150 motd_destroy(MotdList.remote);
361    
362 michael 2203 MotdList.remote = motd_create(NULL, MPATH);
363 michael 4348 motd_cache(MotdList.remote); /* Initialize remote MOTD and cache it */
364 michael 2150 }
365    
366     /* \brief Add a new MOTD.
367 michael 4351 * \param mask Hostmask (or connection class name) to send this to.
368 michael 2150 * \param path Pathname of file to send.
369     */
370     void
371 michael 4351 motd_add(const char *mask, const char *path)
372 michael 2150 {
373 michael 4351 struct Motd *motd = motd_create(mask, path); /* Create the motd */
374 michael 2150
375     dlinkAdd(motd, &motd->node, &MotdList.other);
376     }
377    
378     /*! \brief Clear out all MOTDs.
379     * Compared to motd_recache(), this destroys all hostmask- or
380     * class-based MOTDs rather than simply uncaching them.
381     * Re-cache the local and remote MOTDs.
382     */
383     void
384     motd_clear(void)
385     {
386 michael 4815 dlink_node *node = NULL, *node_next = NULL;
387 michael 2150
388 michael 3254 motd_decache(MotdList.local); /* Decache local and remote MOTDs */
389 michael 2150 motd_decache(MotdList.remote);
390    
391 michael 4815 DLINK_FOREACH_SAFE(node, node_next, MotdList.other.head) /* Destroy other MOTDs */
392 michael 2193 {
393 michael 4815 dlinkDelete(node, &MotdList.other);
394     motd_destroy(node->data);
395 michael 2193 }
396 michael 2150
397 michael 4348 /* Now recache local and remote MOTDs */
398 michael 2150 motd_cache(MotdList.local);
399     motd_cache(MotdList.remote);
400     }
401    
402     /*! \brief Report list of non-default MOTDs.
403     * \param source_p Client requesting statistics.
404     */
405     void
406 michael 4479 motd_report(struct Client *source_p, int parc, char *parv[])
407 michael 2150 {
408 michael 4815 const dlink_node *node = NULL;
409 michael 2150
410 michael 4815 DLINK_FOREACH(node, MotdList.other.head)
411 adx 30 {
412 michael 4815 const struct Motd *motd = node->data;
413 michael 2150
414 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSTLINE,
415 michael 4351 motd->mask, motd->path);
416 adx 30 }
417     }
418    
419 michael 2150 /*! \brief Report MOTD memory usage to a client.
420     * \param source_p Client requesting memory usage.
421 adx 30 */
422     void
423 michael 2150 motd_memory_count(struct Client *source_p)
424 adx 30 {
425 michael 4815 const dlink_node *node = NULL;
426 michael 4348 unsigned int mt = 0; /* Motd count */
427     unsigned int mtc = 0; /* Motd cache count */
428     size_t mtm = 0; /* Memory consumed by motd */
429     size_t mtcm = 0; /* Memory consumed by motd cache */
430 adx 30
431 michael 2150 if (MotdList.local)
432     {
433 michael 5545 ++mt;
434 michael 2150 mtm += sizeof(struct Motd);
435     mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0;
436     }
437 adx 30
438 michael 2150 if (MotdList.remote)
439 adx 30 {
440 michael 5545 ++mt;
441 michael 2150 mtm += sizeof(struct Motd);
442     mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0;
443     }
444    
445 michael 4815 DLINK_FOREACH(node, MotdList.other.head)
446 michael 2150 {
447 michael 4815 const struct Motd *motd = node->data;
448 michael 2150
449 michael 5545 ++mt;
450 michael 2150 mtm += sizeof(struct Motd);
451     mtm += motd->path ? (strlen(motd->path) + 1) : 0;
452     }
453    
454 michael 4815 DLINK_FOREACH(node, MotdList.cachelist.head)
455 michael 2150 {
456 michael 4815 const struct MotdCache *cache = node->data;
457 michael 2150
458 michael 5545 ++mtc;
459 michael 2150 mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1));
460     }
461    
462 michael 5545 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
463 michael 6537 "z :Motds %u(%zu) Cache %u(%zu)",
464 michael 3589 mt, mtm, mtc, mtcm);
465 adx 30 }

Properties

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