]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/lists.c
Lists_Add(): use size of destination when copying data
[ngircd-alex.git] / src / ngircd / lists.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * Management of IRC lists: ban, invite, etc.
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21
22 #include "defines.h"
23 #include "conn.h"
24 #include "channel.h"
25 #include "log.h"
26 #include "match.h"
27 #include "messages.h"
28 #include "irc-write.h"
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <strings.h>
33
34 #include "exp.h"
35 #include "lists.h"
36
37 #define MASK_LEN        (2*CLIENT_HOST_LEN)
38
39 struct list_elem {
40         struct list_elem *next; /** pointer to next list element */
41         char mask[MASK_LEN];    /** IRC mask */
42         char *reason;           /** Optional "reason" text */
43         time_t valid_until;     /** 0: unlimited; 1: once; t(>1): until t */
44 };
45
46 /**
47  * Get IRC mask stored in list element.
48  *
49  * @param list_elem List element.
50  * @return Pointer to IRC mask
51  */
52 GLOBAL const char *
53 Lists_GetMask(const struct list_elem *e)
54 {
55         assert(e != NULL);
56         return e->mask;
57 }
58
59 /**
60  * Get optional "reason" text stored in list element.
61  *
62  * @param list_elem List element.
63  * @return Pointer to "reason" text or empty string ("").
64  */
65 GLOBAL const char *
66 Lists_GetReason(const struct list_elem *e)
67 {
68         assert(e != NULL);
69         return e->reason ? e->reason : "";
70 }
71
72 /**
73  * Get "validity" value stored in list element.
74  *
75  * @param list_elem List element.
76  * @return Validity: 0=unlimited, 1=once, >1 until this time stamp.
77  */
78 GLOBAL time_t
79 Lists_GetValidity(const struct list_elem *e)
80 {
81         assert(e != NULL);
82         return e->valid_until;
83 }
84
85 /**
86  * Get first list element of a list.
87  *
88  * @param h List head.
89  * @return Pointer to first list element.
90  */
91 GLOBAL struct list_elem*
92 Lists_GetFirst(const struct list_head *h)
93 {
94         assert(h != NULL);
95         return h->first;
96 }
97
98 /**
99  * Get next list element of a list.
100  *
101  * @param e Current list element.
102  * @return Pointer to next list element.
103  */
104 GLOBAL struct list_elem*
105 Lists_GetNext(const struct list_elem *e)
106 {
107         assert(e != NULL);
108         return e->next;
109 }
110
111 /**
112  * Add a new mask to a list.
113  *
114  * @param h List head.
115  * @param Mask The IRC mask to add to the list.
116  * @param ValidUntil 0: unlimited, 1: only once, t>1: until given time_t.
117  * @param Reason Reason string or NULL, if no reason should be saved.
118  * @return true on success, false otherwise.
119  */
120 bool
121 Lists_Add(struct list_head *h, const char *Mask, time_t ValidUntil,
122           const char *Reason)
123 {
124         struct list_elem *e, *newelem;
125
126         assert(h != NULL);
127         assert(Mask != NULL);
128
129         e = Lists_CheckDupeMask(h, Mask);
130         if (e) {
131                 e->valid_until = ValidUntil;
132                 if (Reason) {
133                         free(e->reason);
134                         e->reason = strdup(Reason);
135                 }
136                 return true;
137         }
138
139         e = Lists_GetFirst(h);
140
141         newelem = malloc(sizeof(struct list_elem));
142         if (!newelem) {
143                 Log(LOG_EMERG,
144                     "Can't allocate memory for new list entry!");
145                 return false;
146         }
147
148         strlcpy(newelem->mask, Mask, sizeof(newelem->mask));
149         if (Reason) {
150                 newelem->reason = malloc(strlen(Reason) + 1);
151                 if (newelem->reason)
152                         strlcpy(newelem->reason, Reason,
153                                 sizeof(newelem->reason));
154                 else
155                         Log(LOG_EMERG,
156                             "Can't allocate memory for new list reason text!");
157         }
158         else
159                 newelem->reason = NULL;
160         newelem->valid_until = ValidUntil;
161         newelem->next = e;
162         h->first = newelem;
163
164         return true;
165 }
166
167 /**
168  * Delete a list element from a list.
169  *
170  * @param h List head.
171  * @param p Pointer to previous list element or NULL, if there is none.
172  * @param victim List element to delete.
173  */
174 static void
175 Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim)
176 {
177         assert(victim != NULL);
178         assert(h != NULL);
179
180         if (p)
181                 p->next = victim->next;
182         else
183                 h->first = victim->next;
184
185         if (victim->reason)
186                 free(victim->reason);
187
188         free(victim);
189 }
190
191 /**
192  * Delete a given IRC mask from a list.
193  *
194  * @param h List head.
195  * @param Mask IRC mask to delete from the list.
196  */
197 GLOBAL void
198 Lists_Del(struct list_head *h, const char *Mask)
199 {
200         struct list_elem *e, *last, *victim;
201
202         assert(h != NULL);
203         assert(Mask != NULL);
204
205         last = NULL;
206         e = Lists_GetFirst(h);
207         while (e) {
208                 if (strcasecmp(e->mask, Mask) == 0) {
209                         LogDebug("Deleted \"%s\" from list", e->mask);
210                         victim = e;
211                         e = victim->next;
212                         Lists_Unlink(h, last, victim);
213                         continue;
214                 }
215                 last = e;
216                 e = e->next;
217         }
218 }
219
220 /**
221  * Free a complete list.
222  *
223  * @param head List head.
224  */
225 GLOBAL void
226 Lists_Free(struct list_head *head)
227 {
228         struct list_elem *e, *victim;
229
230         assert(head != NULL);
231
232         e = head->first;
233         head->first = NULL;
234         while (e) {
235                 LogDebug("Deleted \"%s\" from list" , e->mask);
236                 victim = e;
237                 e = e->next;
238                 if (victim->reason)
239                         free(victim->reason);
240                 free(victim);
241         }
242 }
243
244 /**
245  * Check if an IRC mask is already contained in a list.
246  *
247  * @param h List head.
248  * @param Mask IRC mask to test.
249  * @return true if mask is already stored in the list, false otherwise.
250  */
251 GLOBAL struct list_elem *
252 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
253 {
254         struct list_elem *e;
255         e = h->first;
256         while (e) {
257                 if (strcasecmp(e->mask, Mask) == 0)
258                         return e;
259                 e = e->next;
260         }
261         return NULL;
262 }
263
264 /**
265  * Generate a valid IRC mask from "any" string given.
266  *
267  * Attention: This mask is only valid until the next call to Lists_MakeMask(),
268  * because a single global buffer ist used! You have to copy the generated
269  * mask to some sane location yourself!
270  *
271  * @param Pattern Source string to generate an IRC mask for.
272  * @return Pointer to global result buffer.
273  */
274 GLOBAL const char *
275 Lists_MakeMask(const char *Pattern)
276 {
277         static char TheMask[MASK_LEN];
278         char *excl, *at;
279
280         assert(Pattern != NULL);
281
282         excl = strchr(Pattern, '!');
283         at = strchr(Pattern, '@');
284
285         if (at && at < excl)
286                 excl = NULL;
287
288         if (!at && !excl) {
289                 /* Neither "!" nor "@" found: use string as nick name */
290                 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
291                 strlcat(TheMask, "!*@*", sizeof(TheMask));
292                 return TheMask;
293         }
294
295         if (!at && excl) {
296                 /* Domain part is missing */
297                 strlcpy(TheMask, Pattern, sizeof(TheMask) - 3);
298                 strlcat(TheMask, "@*", sizeof(TheMask));
299                 return TheMask;
300         }
301
302         if (at && !excl) {
303                 /* User name is missing */
304                 *at = '\0'; at++;
305                 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
306                 strlcat(TheMask, "!*@", sizeof(TheMask));
307                 strlcat(TheMask, at, sizeof(TheMask));
308                 return TheMask;
309         }
310
311         /* All parts (nick, user and domain name) are given */
312         strlcpy(TheMask, Pattern, sizeof(TheMask));
313         return TheMask;
314 } /* Lists_MakeMask */
315
316 /**
317  * Check if a client is listed in a list.
318  *
319  * @param h List head.
320  * @param Client Client to check.
321  * @return true if client is listed, false if not.
322  */
323 bool
324 Lists_Check(struct list_head *h, CLIENT *Client)
325 {
326         return Lists_CheckReason(h, Client) != NULL;
327 }
328
329 /**
330  * Check if a client is listed in a list and return the "reason".
331  *
332  * @param h List head.
333  * @param Client Client to check.
334  * @return true if client is listed, false if not.
335  */
336 char *
337 Lists_CheckReason(struct list_head *h, CLIENT *Client)
338 {
339         struct list_elem *e, *last, *next;
340
341         assert(h != NULL);
342
343         e = h->first;
344         last = NULL;
345
346         while (e) {
347                 next = e->next;
348                 if (Match(e->mask, Client_Mask(Client))) {
349                         if (e->valid_until == 1) {
350                                 /* Entry is valid only once, delete it */
351                                 LogDebug("Deleted \"%s\" from list (used).",
352                                          e->mask);
353                                 Lists_Unlink(h, last, e);
354                         }
355                         return e->reason ? e->reason : "";
356                 }
357                 last = e;
358                 e = next;
359         }
360
361         return NULL;
362 }
363
364 /**
365  * Check list and purge expired entries.
366  *
367  * @param h List head.
368  */
369 GLOBAL void
370 Lists_Expire(struct list_head *h, const char *ListName)
371 {
372         struct list_elem *e, *last, *next;
373         time_t now;
374
375         assert(h != NULL);
376
377         e = h->first;
378         last = NULL;
379         now = time(NULL);
380
381         while (e) {
382                 next = e->next;
383                 if (e->valid_until > 1 && e->valid_until < now) {
384                         /* Entry is expired, delete it */
385                         if (e->reason)
386                                 Log(LOG_INFO,
387                                     "Deleted \"%s\" (\"%s\") from %s list (expired).",
388                                     e->mask, e->reason, ListName);
389                         else
390                                 Log(LOG_INFO,
391                                     "Deleted \"%s\" from %s list (expired).",
392                                     e->mask, ListName);
393                         Lists_Unlink(h, last, e);
394                         e = next;
395                         continue;
396                 }
397                 last = e;
398                 e = next;
399         }
400 }
401
402 /**
403  * Return the number of entries of a list.
404  *
405  * @param h List head.
406  * @return Number of items.
407  */
408 GLOBAL unsigned long
409 Lists_Count(struct list_head *h)
410 {
411         struct list_elem *e;
412         unsigned long count = 0;
413
414         assert(h != NULL);
415
416         e = h->first;
417         while (e) {
418                 count++;
419                 e = e->next;
420         }
421         return count;
422 }
423
424 /* -eof- */