ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/motd.c
Revision: 9101
Committed: Wed Jan 1 09:58:45 2020 UTC (5 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 12657 byte(s)
Log Message:
- Bump copyright years everywhere

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 2000 Kevin L. Mitchell <klmitch@mit.edu>
5 * Copyright (c) 2013-2020 ircd-hybrid development team
6 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 * USA
21 */
22
23 /*! \file motd.c
24 * \brief Message-of-the-day manipulation implementation.
25 * \version $Id$
26 */
27
28 #include "stdinc.h"
29 #include "list.h"
30 #include "ircd.h"
31 #include "conf.h"
32 #include "send.h"
33 #include "numeric.h"
34 #include "client.h"
35 #include "irc_string.h"
36 #include "memory.h"
37 #include "log.h"
38 #include "motd.h"
39 #include "hostmask.h"
40 #include "misc.h"
41
42
43 /** Global list of messages of the day. */
44 static struct
45 {
46 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 mask 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 *mask, const char *path)
59 {
60 struct Motd *motd = xcalloc(sizeof(*motd));
61
62 if (EmptyString(mask))
63 motd->type = MOTD_UNIVERSAL;
64 else if (class_find(mask, true))
65 motd->type = MOTD_CLASS;
66 else
67 {
68 switch (parse_netmask(mask, &motd->address, &motd->addrbits))
69 {
70 case HM_IPV4:
71 motd->type = MOTD_IPMASKV4;
72 break;
73 case HM_IPV6:
74 motd->type = MOTD_IPMASKV6;
75 break;
76 default: /* HM_HOST */
77 motd->type = MOTD_HOSTMASK;
78 break;
79 }
80 }
81
82 if (mask)
83 motd->mask = xstrdup(mask);
84
85 motd->path = xstrdup(path);
86 motd->maxcount = MOTD_MAXLINES;
87
88 return motd;
89 }
90
91 /*! 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 {
100 FILE *file = NULL;
101 struct stat sb;
102 char line[MOTD_LINESIZE + 2]; /* +2 for \r\n */
103 char *tmp = NULL;
104 unsigned int i = 0;
105 dlink_node *node;
106
107 assert(motd);
108 assert(motd->path);
109
110 if (motd->cache)
111 return motd->cache;
112
113 /* Try to find it in the list of cached files */
114 DLINK_FOREACH(node, MotdList.cachelist.head)
115 {
116 struct MotdCache *cache = node->data;
117
118 if (strcmp(cache->path, motd->path) == 0 && cache->maxcount == motd->maxcount)
119 {
120 cache->ref++; /* Increase reference count */
121 motd->cache = cache; /* Remember cache */
122 return motd->cache; /* Return it */
123 }
124 }
125
126 /* Need the file's modification time */
127 if (stat(motd->path, &sb) == -1)
128 {
129 ilog(LOG_TYPE_IRCD, "Couldn't stat \"%s\": %s", motd->path,
130 strerror(errno));
131 return 0;
132 }
133
134 /* Gotta read in the file, now */
135 if ((file = fopen(motd->path, "r")) == NULL)
136 {
137 ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", motd->path,
138 strerror(errno));
139 return 0;
140 }
141
142 /* Ok, allocate a structure; we'll realloc later to trim memory */
143 struct MotdCache *cache = xcalloc(sizeof(*cache) + (MOTD_LINESIZE * MOTD_MAXLINES));
144 cache->ref = 1;
145 cache->path = xstrdup(motd->path);
146 cache->maxcount = motd->maxcount;
147 cache->modtime = sb.st_mtime; /* Store modtime */
148
149 while (cache->count < cache->maxcount && fgets(line, sizeof(line), file))
150 {
151 /* Copy over line, stopping when we overflow or hit line end */
152 for (tmp = line, i = 0; i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n'; ++tmp, ++i)
153 cache->motd[cache->count][i] = *tmp;
154 cache->motd[cache->count][i] = '\0';
155
156 cache->count++;
157 }
158
159 fclose(file); /* Close the file */
160
161 /* Trim memory usage a little */
162 motd->cache = xcalloc(sizeof(*motd->cache) + (MOTD_LINESIZE * cache->count));
163 memcpy(motd->cache, cache, sizeof(*motd->cache) + (MOTD_LINESIZE * cache->count));
164 xfree(cache);
165
166 /* Now link it in */
167 dlinkAdd(motd->cache, &motd->cache->node, &MotdList.cachelist);
168
169 return motd->cache;
170 }
171
172 /*! \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 */
176 static void
177 motd_decache(struct Motd *motd)
178 {
179 struct MotdCache *cache;
180
181 assert(motd);
182
183 if ((cache = motd->cache) == NULL) /* We can be called for records with no cache */
184 return;
185
186 motd->cache = NULL; /* Zero the cache */
187
188 if (--cache->ref == 0) /* Reduce reference count */
189 {
190 dlinkDelete(&cache->node, &MotdList.cachelist);
191 xfree(cache->path); /* Free path info */
192 xfree(cache); /* Very simple for a reason */
193 }
194 }
195
196 /*! \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
205 if (motd->cache) /* Drop the cache */
206 motd_decache(motd);
207
208 xfree(motd->path); /* We always must have a path */
209 xfree(motd->mask);
210 xfree(motd);
211 }
212
213 /*! \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 motd_lookup(const struct Client *client_p)
223 {
224 dlink_node *node;
225
226 assert(client_p);
227
228 if (!MyConnect(client_p)) /* Not my user, always return remote motd */
229 return MotdList.remote;
230
231 /* Check the motd blocks first */
232 DLINK_FOREACH(node, MotdList.other.head)
233 {
234 struct Motd *motd = node->data;
235
236 switch (motd->type)
237 {
238 case MOTD_CLASS:
239 {
240 const struct ClassItem *class = class_get_ptr(&client_p->connection->confs);
241 if (match(motd->mask, class->name) == 0)
242 return motd;
243 break;
244 }
245 case MOTD_HOSTMASK:
246 if (match(motd->mask, client_p->host) == 0 || match(motd->mask, client_p->sockhost) == 0)
247 return motd;
248 break;
249 case MOTD_IPMASKV4:
250 if (client_p->ip.ss.ss_family == AF_INET)
251 if (match_ipv4(&client_p->ip, &motd->address, motd->addrbits))
252 return motd;
253 break;
254 case MOTD_IPMASKV6:
255 if (client_p->ip.ss.ss_family == AF_INET6)
256 if (match_ipv6(&client_p->ip, &motd->address, motd->addrbits))
257 return motd;
258 break;
259 default: break;
260 }
261 }
262
263 return MotdList.local; /* Ok, return the default motd */
264 }
265
266 /*! \brief Send the content of a MotdCache to a user.
267 * If \a cache is NULL, simply send ERR_NOMOTD to the client.
268 * \param client_p Client to send MOTD to.
269 * \param cache MOTD body to send to client.
270 */
271 static void
272 motd_forward(struct Client *client_p, const struct MotdCache *cache)
273 {
274 if (cache == NULL) /* No motd to send */
275 {
276 sendto_one_numeric(client_p, &me, ERR_NOMOTD);
277 return;
278 }
279
280 /* Send the motd */
281 sendto_one_numeric(client_p, &me, RPL_MOTDSTART, me.name);
282
283 for (unsigned int i = 0; i < cache->count; ++i)
284 sendto_one_numeric(client_p, &me, RPL_MOTD, cache->motd[i]);
285
286 sendto_one_numeric(client_p, &me, RPL_ENDOFMOTD);
287 }
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 {
295 assert(client_p);
296
297 motd_forward(client_p, motd_cache(motd_lookup(client_p)));
298 }
299
300 /*! \brief Send the signon MOTD to a user.
301 * 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 * motd_forward() for the user.
304 * \param client_p Client that has just connected.
305 */
306 void
307 motd_signon(struct Client *client_p)
308 {
309 const struct MotdCache *cache = motd_cache(motd_lookup(client_p));
310
311 if (ConfigGeneral.short_motd == 0 || cache == NULL)
312 motd_forward(client_p, cache);
313 else
314 {
315 sendto_one_notice(client_p, &me, ":*** Notice -- motd was last changed at %s",
316 date_iso8601(cache->modtime));
317 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 }
322 }
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 dlink_node *node;
331
332 motd_decache(MotdList.local); /* Decache local and remote MOTDs */
333 motd_decache(MotdList.remote);
334
335 DLINK_FOREACH(node, MotdList.other.head) /* Now all the others */
336 motd_decache(node->data);
337
338 /* Now recache local and remote MOTDs */
339 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 if (MotdList.local) /* Destroy old local MOTD */
350 motd_destroy(MotdList.local);
351
352 MotdList.local = motd_create(NULL, MPATH);
353 motd_cache(MotdList.local); /* Initialize local MOTD and cache it */
354
355 if (MotdList.remote) /* Destroy old remote MOTD */
356 motd_destroy(MotdList.remote);
357
358 MotdList.remote = motd_create(NULL, MPATH);
359 motd_cache(MotdList.remote); /* Initialize remote MOTD and cache it */
360 }
361
362 /* \brief Add a new MOTD.
363 * \param mask Hostmask (or connection class name) to send this to.
364 * \param path Pathname of file to send.
365 */
366 void
367 motd_add(const char *mask, const char *path)
368 {
369 struct Motd *motd = motd_create(mask, path); /* Create the motd */
370
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 motd_decache(MotdList.local); /* Decache local and remote MOTDs */
383 motd_decache(MotdList.remote);
384
385 while (MotdList.other.head) /* Destroy other MOTDs */
386 {
387 struct Motd *motd = MotdList.other.head->data;
388 dlinkDelete(&motd->node, &MotdList.other);
389 motd_destroy(motd);
390 }
391
392 /* Now recache local and remote MOTDs */
393 motd_cache(MotdList.local);
394 motd_cache(MotdList.remote);
395 }
396
397 /*! \brief Report list of non-default MOTDs.
398 * \param client_p Client requesting statistics.
399 */
400 void
401 motd_report(struct Client *client_p, int parc, char *parv[])
402 {
403 dlink_node *node;
404
405 DLINK_FOREACH(node, MotdList.other.head)
406 {
407 const struct Motd *motd = node->data;
408
409 sendto_one_numeric(client_p, &me, RPL_STATSTLINE,
410 motd->mask, motd->path);
411 }
412 }
413
414 /*! \brief Report MOTD memory usage to a client.
415 * \param client_p Client requesting memory usage.
416 */
417 void
418 motd_memory_count(struct Client *client_p)
419 {
420 dlink_node *node;
421 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
426 if (MotdList.local)
427 {
428 ++mt;
429 mtm += sizeof(struct Motd);
430 mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0;
431 }
432
433 if (MotdList.remote)
434 {
435 ++mt;
436 mtm += sizeof(struct Motd);
437 mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0;
438 }
439
440 DLINK_FOREACH(node, MotdList.other.head)
441 {
442 const struct Motd *motd = node->data;
443
444 ++mt;
445 mtm += sizeof(struct Motd);
446 mtm += motd->path ? (strlen(motd->path) + 1) : 0;
447 }
448
449 DLINK_FOREACH(node, MotdList.cachelist.head)
450 {
451 const struct MotdCache *cache = node->data;
452
453 ++mtc;
454 mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1));
455 }
456
457 sendto_one_numeric(client_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
458 "z :Motds %u(%zu) Cache %u(%zu)",
459 mt, mtm, mtc, mtcm);
460 }

Properties

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