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-2015 ircd-hybrid development team |
5 |
> |
* Copyright (c) 2013-2018 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 |
57 |
|
static struct Motd * |
58 |
|
motd_create(const char *mask, const char *path) |
59 |
|
{ |
60 |
< |
struct Motd *tmp = MyCalloc(sizeof(struct Motd)); |
60 |
> |
struct Motd *motd = xcalloc(sizeof(*motd)); |
61 |
|
|
62 |
|
if (EmptyString(mask)) |
63 |
< |
tmp->type = MOTD_UNIVERSAL; |
63 |
> |
motd->type = MOTD_UNIVERSAL; |
64 |
|
else if (class_find(mask, 1)) |
65 |
< |
tmp->type = MOTD_CLASS; |
65 |
> |
motd->type = MOTD_CLASS; |
66 |
|
else |
67 |
|
{ |
68 |
< |
switch (parse_netmask(mask, &tmp->address, &tmp->addrbits)) |
68 |
> |
switch (parse_netmask(mask, &motd->address, &motd->addrbits)) |
69 |
|
{ |
70 |
|
case HM_IPV4: |
71 |
< |
tmp->type = MOTD_IPMASKV4; |
71 |
> |
motd->type = MOTD_IPMASKV4; |
72 |
|
break; |
73 |
|
case HM_IPV6: |
74 |
< |
tmp->type = MOTD_IPMASKV6; |
74 |
> |
motd->type = MOTD_IPMASKV6; |
75 |
|
break; |
76 |
|
default: /* HM_HOST */ |
77 |
< |
tmp->type = MOTD_HOSTMASK; |
77 |
> |
motd->type = MOTD_HOSTMASK; |
78 |
|
break; |
79 |
|
} |
80 |
|
} |
81 |
|
|
82 |
|
if (mask) |
83 |
< |
tmp->mask = xstrdup(mask); |
83 |
> |
motd->mask = xstrdup(mask); |
84 |
|
|
85 |
< |
tmp->path = xstrdup(path); |
86 |
< |
tmp->maxcount = MOTD_MAXLINES; |
85 |
> |
motd->path = xstrdup(path); |
86 |
> |
motd->maxcount = MOTD_MAXLINES; |
87 |
|
|
88 |
< |
return tmp; |
88 |
> |
return motd; |
89 |
|
} |
90 |
|
|
91 |
|
/*! brief\ This function reads a motd out of a file (if needed) and caches it. |
98 |
|
motd_cache(struct Motd *motd) |
99 |
|
{ |
100 |
|
FILE *file = NULL; |
101 |
– |
struct MotdCache *cache = NULL; |
101 |
|
struct stat sb; |
102 |
|
char line[MOTD_LINESIZE + 2]; /* +2 for \r\n */ |
103 |
|
char *tmp = NULL; |
113 |
|
/* Try to find it in the list of cached files */ |
114 |
|
DLINK_FOREACH(node, MotdList.cachelist.head) |
115 |
|
{ |
116 |
< |
cache = node->data; |
116 |
> |
struct MotdCache *cache = node->data; |
117 |
|
|
118 |
|
if (!strcmp(cache->path, motd->path) && cache->maxcount == motd->maxcount) |
119 |
|
{ |
140 |
|
} |
141 |
|
|
142 |
|
/* Ok, allocate a structure; we'll realloc later to trim memory */ |
143 |
< |
cache = MyCalloc(sizeof(struct MotdCache) + (MOTD_LINESIZE * MOTD_MAXLINES)); |
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; |
159 |
|
fclose(file); /* Close the file */ |
160 |
|
|
161 |
|
/* Trim memory usage a little */ |
162 |
< |
motd->cache = MyCalloc(sizeof(struct MotdCache) + |
163 |
< |
(MOTD_LINESIZE * cache->count)); |
164 |
< |
memcpy(motd->cache, cache, sizeof(struct MotdCache) + |
166 |
< |
(MOTD_LINESIZE * cache->count)); |
167 |
< |
MyFree(cache); |
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); |
188 |
|
if (--cache->ref == 0) /* Reduce reference count */ |
189 |
|
{ |
190 |
|
dlinkDelete(&cache->node, &MotdList.cachelist); |
191 |
< |
MyFree(cache->path); /* Free path info */ |
192 |
< |
MyFree(cache); /* Very simple for a reason */ |
191 |
> |
xfree(cache->path); /* Free path info */ |
192 |
> |
xfree(cache); /* Very simple for a reason */ |
193 |
|
} |
194 |
|
} |
195 |
|
|
205 |
|
if (motd->cache) /* Drop the cache */ |
206 |
|
motd_decache(motd); |
207 |
|
|
208 |
< |
MyFree(motd->path); /* We always must have a path */ |
209 |
< |
MyFree(motd->mask); |
210 |
< |
MyFree(motd); |
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. |
221 |
|
static struct Motd * |
222 |
|
motd_lookup(const struct Client *client_p) |
223 |
|
{ |
224 |
< |
dlink_node *node = NULL; |
228 |
< |
const struct ClassItem *class = NULL; |
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 |
|
|
235 |
– |
class = get_class_ptr(&client_p->connection->confs); |
236 |
– |
assert(class); |
237 |
– |
|
231 |
|
/* Check the motd blocks first */ |
232 |
|
DLINK_FOREACH(node, MotdList.other.head) |
233 |
|
{ |
236 |
|
switch (motd->type) |
237 |
|
{ |
238 |
|
case MOTD_CLASS: |
239 |
+ |
{ |
240 |
+ |
const struct ClassItem *class = get_class_ptr(&client_p->connection->confs); |
241 |
|
if (!match(motd->mask, class->name)) |
242 |
|
return motd; |
243 |
|
break; |
244 |
+ |
} |
245 |
|
case MOTD_HOSTMASK: |
246 |
|
if (!match(motd->mask, client_p->host) || !match(motd->mask, client_p->sockhost)) |
247 |
|
return motd; |
248 |
|
break; |
249 |
|
case MOTD_IPMASKV4: |
250 |
< |
if (client_p->connection->aftype == AF_INET) |
251 |
< |
if (match_ipv4(&client_p->connection->ip, &motd->address, motd->addrbits)) |
252 |
< |
return motd; |
250 |
> |
if (client_p->connection->aftype == AF_INET) |
251 |
> |
if (match_ipv4(&client_p->connection->ip, &motd->address, motd->addrbits)) |
252 |
> |
return motd; |
253 |
|
break; |
254 |
|
case MOTD_IPMASKV6: |
255 |
< |
if (client_p->connection->aftype == AF_INET6) |
256 |
< |
if (match_ipv6(&client_p->connection->ip, &motd->address, motd->addrbits)) |
257 |
< |
return motd; |
255 |
> |
if (client_p->connection->aftype == AF_INET6) |
256 |
> |
if (match_ipv6(&client_p->connection->ip, &motd->address, motd->addrbits)) |
257 |
> |
return motd; |
258 |
|
break; |
259 |
|
default: break; |
260 |
|
} |
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 source_p Client to send MOTD to. |
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 *source_p, const struct MotdCache *cache) |
272 |
> |
motd_forward(struct Client *client_p, const struct MotdCache *cache) |
273 |
|
{ |
274 |
|
if (!cache) /* No motd to send */ |
275 |
|
{ |
276 |
< |
sendto_one_numeric(source_p, &me, ERR_NOMOTD); |
276 |
> |
sendto_one_numeric(client_p, &me, ERR_NOMOTD); |
277 |
|
return; |
278 |
|
} |
279 |
|
|
280 |
|
/* Send the motd */ |
281 |
< |
sendto_one_numeric(source_p, &me, RPL_MOTDSTART, me.name); |
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(source_p, &me, RPL_MOTD, cache->motd[i]); |
284 |
> |
sendto_one_numeric(client_p, &me, RPL_MOTD, cache->motd[i]); |
285 |
|
|
286 |
< |
sendto_one_numeric(source_p, &me, RPL_ENDOFMOTD); |
286 |
> |
sendto_one_numeric(client_p, &me, RPL_ENDOFMOTD); |
287 |
|
} |
288 |
|
|
289 |
|
/*! \brief Find the MOTD for a client and send it. |
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 source_p Client that has just connected. |
304 |
> |
* \param client_p Client that has just connected. |
305 |
|
*/ |
306 |
|
void |
307 |
< |
motd_signon(struct Client *source_p) |
307 |
> |
motd_signon(struct Client *client_p) |
308 |
|
{ |
309 |
< |
const struct MotdCache *cache = motd_cache(motd_lookup(source_p)); |
309 |
> |
const struct MotdCache *cache = motd_cache(motd_lookup(client_p)); |
310 |
|
|
311 |
|
if (!ConfigGeneral.short_motd || !cache) |
312 |
< |
motd_forward(source_p, cache); |
312 |
> |
motd_forward(client_p, cache); |
313 |
|
else |
314 |
|
{ |
315 |
< |
sendto_one_notice(source_p, &me, ":*** Notice -- motd was last changed at %s", |
315 |
> |
sendto_one_notice(client_p, &me, ":*** Notice -- motd was last changed at %s", |
316 |
|
date_iso8601(cache->modtime)); |
317 |
< |
sendto_one_notice(source_p, &me, ":*** Notice -- Please read the motd if you haven't read it"); |
318 |
< |
sendto_one_numeric(source_p, &me, RPL_MOTDSTART, me.name); |
319 |
< |
sendto_one_numeric(source_p, &me, RPL_MOTD, "*** This is the short motd ***"); |
320 |
< |
sendto_one_numeric(source_p, &me, RPL_ENDOFMOTD); |
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 |
|
|
327 |
|
void |
328 |
|
motd_recache(void) |
329 |
|
{ |
330 |
< |
dlink_node *node = NULL; |
330 |
> |
dlink_node *node; |
331 |
|
|
332 |
|
motd_decache(MotdList.local); /* Decache local and remote MOTDs */ |
333 |
|
motd_decache(MotdList.remote); |
379 |
|
void |
380 |
|
motd_clear(void) |
381 |
|
{ |
386 |
– |
dlink_node *node = NULL, *node_next = NULL; |
387 |
– |
|
382 |
|
motd_decache(MotdList.local); /* Decache local and remote MOTDs */ |
383 |
|
motd_decache(MotdList.remote); |
384 |
|
|
385 |
< |
DLINK_FOREACH_SAFE(node, node_next, MotdList.other.head) /* Destroy other MOTDs */ |
385 |
> |
while (MotdList.other.head) /* Destroy other MOTDs */ |
386 |
|
{ |
387 |
< |
dlinkDelete(node, &MotdList.other); |
388 |
< |
motd_destroy(node->data); |
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 */ |
395 |
|
} |
396 |
|
|
397 |
|
/*! \brief Report list of non-default MOTDs. |
398 |
< |
* \param source_p Client requesting statistics. |
398 |
> |
* \param client_p Client requesting statistics. |
399 |
|
*/ |
400 |
|
void |
401 |
< |
motd_report(struct Client *source_p, int parc, char *parv[]) |
401 |
> |
motd_report(struct Client *client_p, int parc, char *parv[]) |
402 |
|
{ |
403 |
< |
const dlink_node *node = NULL; |
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(source_p, &me, RPL_STATSTLINE, |
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 source_p Client requesting memory usage. |
415 |
> |
* \param client_p Client requesting memory usage. |
416 |
|
*/ |
417 |
|
void |
418 |
< |
motd_memory_count(struct Client *source_p) |
418 |
> |
motd_memory_count(struct Client *client_p) |
419 |
|
{ |
420 |
< |
const dlink_node *node = NULL; |
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 */ |
454 |
|
mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1)); |
455 |
|
} |
456 |
|
|
457 |
< |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
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 |
|
} |