ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/motd.c
Revision: 8279
Committed: Tue Feb 20 19:30:13 2018 UTC (7 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 12688 byte(s)
Log Message:
- Update copyright years

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

Properties

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