1
0

lists.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 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. #include "portab.h"
  12. /**
  13. * @file
  14. * Management of IRC lists: ban, invite, etc.
  15. */
  16. #include <assert.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <strings.h>
  20. #include <time.h>
  21. #include "conn.h"
  22. #include "log.h"
  23. #include "match.h"
  24. #include "lists.h"
  25. struct list_elem {
  26. struct list_elem *next; /** pointer to next list element */
  27. char mask[MASK_LEN]; /** IRC mask */
  28. char *reason; /** Optional "reason" text */
  29. time_t valid_until; /** 0: unlimited; 1: once; t(>1): until t */
  30. };
  31. /**
  32. * Get IRC mask stored in list element.
  33. *
  34. * @param list_elem List element.
  35. * @return Pointer to IRC mask
  36. */
  37. GLOBAL const char *
  38. Lists_GetMask(const struct list_elem *e)
  39. {
  40. assert(e != NULL);
  41. return e->mask;
  42. }
  43. /**
  44. * Get optional "reason" text stored in list element.
  45. *
  46. * @param list_elem List element.
  47. * @return Pointer to "reason" text or empty string ("").
  48. */
  49. GLOBAL const char *
  50. Lists_GetReason(const struct list_elem *e)
  51. {
  52. assert(e != NULL);
  53. return e->reason ? e->reason : "";
  54. }
  55. /**
  56. * Get "validity" value stored in list element.
  57. *
  58. * @param list_elem List element.
  59. * @return Validity: 0=unlimited, 1=once, >1 until this time stamp.
  60. */
  61. GLOBAL time_t
  62. Lists_GetValidity(const struct list_elem *e)
  63. {
  64. assert(e != NULL);
  65. return e->valid_until;
  66. }
  67. /**
  68. * Get first list element of a list.
  69. *
  70. * @param h List head.
  71. * @return Pointer to first list element.
  72. */
  73. GLOBAL struct list_elem*
  74. Lists_GetFirst(const struct list_head *h)
  75. {
  76. assert(h != NULL);
  77. return h->first;
  78. }
  79. /**
  80. * Get next list element of a list.
  81. *
  82. * @param e Current list element.
  83. * @return Pointer to next list element.
  84. */
  85. GLOBAL struct list_elem*
  86. Lists_GetNext(const struct list_elem *e)
  87. {
  88. assert(e != NULL);
  89. return e->next;
  90. }
  91. /**
  92. * Add a new mask to a list.
  93. *
  94. * @param h List head.
  95. * @param Mask The IRC mask to add to the list.
  96. * @param ValidUntil 0: unlimited, 1: only once, t>1: until given time_t.
  97. * @param Reason Reason string or NULL, if no reason should be saved.
  98. * @return true on success, false otherwise.
  99. */
  100. bool
  101. Lists_Add(struct list_head *h, const char *Mask, time_t ValidUntil,
  102. const char *Reason)
  103. {
  104. struct list_elem *e, *newelem;
  105. assert(h != NULL);
  106. assert(Mask != NULL);
  107. e = Lists_CheckDupeMask(h, Mask);
  108. if (e) {
  109. e->valid_until = ValidUntil;
  110. if (Reason) {
  111. if (e->reason)
  112. free(e->reason);
  113. e->reason = strdup(Reason);
  114. }
  115. return true;
  116. }
  117. e = Lists_GetFirst(h);
  118. newelem = malloc(sizeof(struct list_elem));
  119. if (!newelem) {
  120. Log(LOG_EMERG,
  121. "Can't allocate memory for new list entry!");
  122. return false;
  123. }
  124. strlcpy(newelem->mask, Mask, sizeof(newelem->mask));
  125. if (Reason) {
  126. newelem->reason = strdup(Reason);
  127. if (!newelem->reason)
  128. Log(LOG_EMERG,
  129. "Can't allocate memory for new list reason text!");
  130. }
  131. else
  132. newelem->reason = NULL;
  133. newelem->valid_until = ValidUntil;
  134. newelem->next = e;
  135. h->first = newelem;
  136. return true;
  137. }
  138. /**
  139. * Delete a list element from a list.
  140. *
  141. * @param h List head.
  142. * @param p Pointer to previous list element or NULL, if there is none.
  143. * @param victim List element to delete.
  144. */
  145. static void
  146. Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim)
  147. {
  148. assert(victim != NULL);
  149. assert(h != NULL);
  150. if (p)
  151. p->next = victim->next;
  152. else
  153. h->first = victim->next;
  154. if (victim->reason)
  155. free(victim->reason);
  156. free(victim);
  157. }
  158. /**
  159. * Delete a given IRC mask from a list.
  160. *
  161. * @param h List head.
  162. * @param Mask IRC mask to delete from the list.
  163. */
  164. GLOBAL void
  165. Lists_Del(struct list_head *h, const char *Mask)
  166. {
  167. struct list_elem *e, *last, *victim;
  168. assert(h != NULL);
  169. assert(Mask != NULL);
  170. last = NULL;
  171. e = Lists_GetFirst(h);
  172. while (e) {
  173. if (strcasecmp(e->mask, Mask) == 0) {
  174. LogDebug("Deleted \"%s\" from list", e->mask);
  175. victim = e;
  176. e = victim->next;
  177. Lists_Unlink(h, last, victim);
  178. continue;
  179. }
  180. last = e;
  181. e = e->next;
  182. }
  183. }
  184. /**
  185. * Free a complete list.
  186. *
  187. * @param head List head.
  188. */
  189. GLOBAL void
  190. Lists_Free(struct list_head *head)
  191. {
  192. struct list_elem *e, *victim;
  193. assert(head != NULL);
  194. e = head->first;
  195. head->first = NULL;
  196. while (e) {
  197. LogDebug("Deleted \"%s\" from list" , e->mask);
  198. victim = e;
  199. e = e->next;
  200. if (victim->reason)
  201. free(victim->reason);
  202. free(victim);
  203. }
  204. }
  205. /**
  206. * Check if an IRC mask is already contained in a list.
  207. *
  208. * @param h List head.
  209. * @param Mask IRC mask to test.
  210. * @return true if mask is already stored in the list, false otherwise.
  211. */
  212. GLOBAL struct list_elem *
  213. Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
  214. {
  215. struct list_elem *e;
  216. e = h->first;
  217. while (e) {
  218. if (strcasecmp(e->mask, Mask) == 0)
  219. return e;
  220. e = e->next;
  221. }
  222. return NULL;
  223. }
  224. /**
  225. * Generate a valid IRC mask from "any" string given.
  226. *
  227. * @param Pattern Source string to generate an IRC mask for.
  228. * @param mask Buffer to store the mask.
  229. * @param len Size of the buffer.
  230. */
  231. GLOBAL void
  232. Lists_MakeMask(const char *Pattern, char *mask, size_t len)
  233. {
  234. char *excl, *at;
  235. assert(Pattern != NULL);
  236. excl = strchr(Pattern, '!');
  237. at = strchr(Pattern, '@');
  238. if (at && at < excl)
  239. excl = NULL;
  240. if (!at && !excl) {
  241. /* Neither "!" nor "@" found: use string as nickname */
  242. strlcpy(mask, Pattern, len - 5);
  243. strlcat(mask, "!*@*", len);
  244. } else if (!at && excl) {
  245. /* Domain part is missing */
  246. strlcpy(mask, Pattern, len - 3);
  247. strlcat(mask, "@*", len);
  248. } else if (at && !excl) {
  249. /* User name is missing */
  250. *at = '\0'; at++;
  251. strlcpy(mask, Pattern, len - 5);
  252. strlcat(mask, "!*@", len);
  253. strlcat(mask, at, len);
  254. at--; *at = '@';
  255. } else {
  256. /* All parts (nick, user and domain name) are given */
  257. strlcpy(mask, Pattern, len);
  258. }
  259. } /* Lists_MakeMask */
  260. /**
  261. * Check if a client is listed in a list.
  262. *
  263. * @param h List head.
  264. * @param Client Client to check.
  265. * @return true if client is listed, false if not.
  266. */
  267. bool
  268. Lists_Check(struct list_head *h, CLIENT *Client)
  269. {
  270. return Lists_CheckReason(h, Client, NULL, 0);
  271. }
  272. /**
  273. * Check if a client is listed in a list and store the reason.
  274. *
  275. * @param h List head.
  276. * @param Client Client to check.
  277. * @param reason Buffer to store the reason.
  278. * @param len Size of the buffer if reason should be saved.
  279. * @return true if client is listed, false if not.
  280. */
  281. bool
  282. Lists_CheckReason(struct list_head *h, CLIENT *Client, char *reason, size_t len)
  283. {
  284. struct list_elem *e, *last, *next;
  285. assert(h != NULL);
  286. e = h->first;
  287. last = NULL;
  288. while (e) {
  289. next = e->next;
  290. if (MatchCaseInsensitive(e->mask, Client_MaskCloaked(Client))) {
  291. if (len && e->reason)
  292. strlcpy(reason, e->reason, len);
  293. if (e->valid_until == 1) {
  294. /* Entry is valid only once, delete it */
  295. LogDebug("Deleted \"%s\" from list (used).",
  296. e->mask);
  297. Lists_Unlink(h, last, e);
  298. }
  299. return true;
  300. }
  301. last = e;
  302. e = next;
  303. }
  304. return false;
  305. }
  306. /**
  307. * Check list and purge expired entries.
  308. *
  309. * @param h List head.
  310. */
  311. GLOBAL void
  312. Lists_Expire(struct list_head *h, const char *ListName)
  313. {
  314. struct list_elem *e, *last, *next;
  315. time_t now;
  316. assert(h != NULL);
  317. e = h->first;
  318. last = NULL;
  319. now = time(NULL);
  320. while (e) {
  321. next = e->next;
  322. if (e->valid_until > 1 && e->valid_until < now) {
  323. /* Entry is expired, delete it */
  324. if (e->reason)
  325. Log(LOG_INFO,
  326. "Deleted \"%s\" (\"%s\") from %s list (expired).",
  327. e->mask, e->reason, ListName);
  328. else
  329. Log(LOG_INFO,
  330. "Deleted \"%s\" from %s list (expired).",
  331. e->mask, ListName);
  332. Lists_Unlink(h, last, e);
  333. e = next;
  334. continue;
  335. }
  336. last = e;
  337. e = next;
  338. }
  339. }
  340. /**
  341. * Return the number of entries of a list.
  342. *
  343. * @param h List head.
  344. * @return Number of items.
  345. */
  346. GLOBAL unsigned long
  347. Lists_Count(struct list_head *h)
  348. {
  349. struct list_elem *e;
  350. unsigned long count = 0;
  351. assert(h != NULL);
  352. e = h->first;
  353. while (e) {
  354. count++;
  355. e = e->next;
  356. }
  357. return count;
  358. }
  359. /* -eof- */