ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/motd.c
Revision: 9255
Committed: Sat Feb 1 21:47:51 2020 UTC (6 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 12640 byte(s)
Error occurred while calculating annotation data.
Log Message:
- motd.c:motd_lookup(): check against client->realhost, too

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 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)
223 {
224 dlink_node *node;
225
226 assert(client);
227
228 if (!MyConnect(client)) /* 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->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->realhost) == 0 ||
247 match(motd->mask, client->sockhost) == 0 || match(motd->mask, client->host) == 0)
248 return motd;
249 break;
250 case MOTD_IPMASKV4:
251 if (client->ip.ss.ss_family == AF_INET)
252 if (match_ipv4(&client->ip, &motd->address, motd->addrbits))
253 return motd;
254 break;
255 case MOTD_IPMASKV6:
256 if (client->ip.ss.ss_family == AF_INET6)
257 if (match_ipv6(&client->ip, &motd->address, motd->addrbits))
258 return motd;
259 break;
260 default: break;
261 }
262 }
263
264 return MotdList.local; /* Ok, return the default motd */
265 }
266
267 /*! \brief Send the content of a MotdCache to a user.
268 * If \a cache is NULL, simply send ERR_NOMOTD to the client.
269 * \param client Client to send MOTD to.
270 * \param cache MOTD body to send to client.
271 */
272 static void
273 motd_forward(struct Client *client, const struct MotdCache *cache)
274 {
275 if (cache == NULL) /* No motd to send */
276 {
277 sendto_one_numeric(client, &me, ERR_NOMOTD);
278 return;
279 }
280
281 /* Send the motd */
282 sendto_one_numeric(client, &me, RPL_MOTDSTART, me.name);
283
284 for (unsigned int i = 0; i < cache->count; ++i)
285 sendto_one_numeric(client, &me, RPL_MOTD, cache->motd[i]);
286
287 sendto_one_numeric(client, &me, RPL_ENDOFMOTD);
288 }
289
290 /*! \brief Find the MOTD for a client and send it.
291 * \param client Client being greeted.
292 */
293 void
294 motd_send(struct Client *client)
295 {
296 assert(client);
297
298 motd_forward(client, motd_cache(motd_lookup(client)));
299 }
300
301 /*! \brief Send the signon MOTD to a user.
302 * If general::short_motd is true and a matching MOTD exists for the
303 * user, direct the client to type /MOTD to read it. Otherwise, call
304 * motd_forward() for the user.
305 * \param client Client that has just connected.
306 */
307 void
308 motd_signon(struct Client *client)
309 {
310 const struct MotdCache *cache = motd_cache(motd_lookup(client));
311
312 if (ConfigGeneral.short_motd == 0 || cache == NULL)
313 motd_forward(client, cache);
314 else
315 {
316 sendto_one_notice(client, &me, ":*** Notice -- motd was last changed at %s",
317 date_iso8601(cache->modtime));
318 sendto_one_notice(client, &me, ":*** Notice -- Please read the motd if you haven't read it");
319 sendto_one_numeric(client, &me, RPL_MOTDSTART, me.name);
320 sendto_one_numeric(client, &me, RPL_MOTD, "*** This is the short motd ***");
321 sendto_one_numeric(client, &me, RPL_ENDOFMOTD);
322 }
323 }
324
325 /*! \brief Clear all cached MOTD bodies.
326 * The local and remote MOTDs are re-cached immediately.
327 */
328 void
329 motd_recache(void)
330 {
331 dlink_node *node;
332
333 motd_decache(MotdList.local); /* Decache local and remote MOTDs */
334 motd_decache(MotdList.remote);
335
336 DLINK_FOREACH(node, MotdList.other.head) /* Now all the others */
337 motd_decache(node->data);
338
339 /* Now recache local and remote MOTDs */
340 motd_cache(MotdList.local);
341 motd_cache(MotdList.remote);
342 }
343
344 /*! \brief Re-cache the local and remote MOTDs.
345 * If they already exist, they are deallocated first.
346 */
347 void
348 motd_init(void)
349 {
350 if (MotdList.local) /* Destroy old local MOTD */
351 motd_destroy(MotdList.local);
352
353 MotdList.local = motd_create(NULL, MPATH);
354 motd_cache(MotdList.local); /* Initialize local MOTD and cache it */
355
356 if (MotdList.remote) /* Destroy old remote MOTD */
357 motd_destroy(MotdList.remote);
358
359 MotdList.remote = motd_create(NULL, MPATH);
360 motd_cache(MotdList.remote); /* Initialize remote MOTD and cache it */
361 }
362
363 /* \brief Add a new MOTD.
364 * \param mask Hostmask (or connection class name) to send this to.
365 * \param path Pathname of file to send.
366 */
367 void
368 motd_add(const char *mask, const char *path)
369 {
370 struct Motd *motd = motd_create(mask, path); /* Create the motd */
371
372 dlinkAdd(motd, &motd->node, &MotdList.other);
373 }
374
375 /*! \brief Clear out all MOTDs.
376 * Compared to motd_recache(), this destroys all hostmask- or
377 * class-based MOTDs rather than simply uncaching them.
378 * Re-cache the local and remote MOTDs.
379 */
380 void
381 motd_clear(void)
382 {
383 motd_decache(MotdList.local); /* Decache local and remote MOTDs */
384 motd_decache(MotdList.remote);
385
386 while (MotdList.other.head) /* Destroy other MOTDs */
387 {
388 struct Motd *motd = MotdList.other.head->data;
389 dlinkDelete(&motd->node, &MotdList.other);
390 motd_destroy(motd);
391 }
392
393 /* Now recache local and remote MOTDs */
394 motd_cache(MotdList.local);
395 motd_cache(MotdList.remote);
396 }
397
398 /*! \brief Report list of non-default MOTDs.
399 * \param client Client requesting statistics.
400 */
401 void
402 motd_report(struct Client *client, int parc, char *parv[])
403 {
404 dlink_node *node;
405
406 DLINK_FOREACH(node, MotdList.other.head)
407 {
408 const struct Motd *motd = node->data;
409
410 sendto_one_numeric(client, &me, RPL_STATSTLINE,
411 motd->mask, motd->path);
412 }
413 }
414
415 /*! \brief Report MOTD memory usage to a client.
416 * \param client Client requesting memory usage.
417 */
418 void
419 motd_memory_count(struct Client *client)
420 {
421 dlink_node *node;
422 unsigned int mt = 0; /* Motd count */
423 unsigned int mtc = 0; /* Motd cache count */
424 size_t mtm = 0; /* Memory consumed by motd */
425 size_t mtcm = 0; /* Memory consumed by motd cache */
426
427 if (MotdList.local)
428 {
429 ++mt;
430 mtm += sizeof(struct Motd);
431 mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0;
432 }
433
434 if (MotdList.remote)
435 {
436 ++mt;
437 mtm += sizeof(struct Motd);
438 mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0;
439 }
440
441 DLINK_FOREACH(node, MotdList.other.head)
442 {
443 const struct Motd *motd = node->data;
444
445 ++mt;
446 mtm += sizeof(struct Motd);
447 mtm += motd->path ? (strlen(motd->path) + 1) : 0;
448 }
449
450 DLINK_FOREACH(node, MotdList.cachelist.head)
451 {
452 const struct MotdCache *cache = node->data;
453
454 ++mtc;
455 mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1));
456 }
457
458 sendto_one_numeric(client, &me, RPL_STATSDEBUG | SND_EXPLICIT,
459 "z :Motds %u(%zu) Cache %u(%zu)",
460 mt, mtm, mtc, mtcm);
461 }

Properties

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