]> jfr.im git - irc/quakenet/newserv.git/blob - trusts/data.c
Merge.
[irc/quakenet/newserv.git] / trusts / data.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "../lib/sstring.h"
5 #include "../core/hooks.h"
6 #include "../core/nsmalloc.h"
7 #include "../lib/irc_string.h"
8 #include "trusts.h"
9
10 trustgroup *tglist;
11
12 void th_dbupdatecounts(trusthost *);
13 void tg_dbupdatecounts(trustgroup *);
14
15 void trusts_freeall(void) {
16 trustgroup *tg, *ntg;
17 trusthost *th, *nth;
18
19 for(tg=tglist;tg;tg=ntg) {
20 ntg = tg->next;
21 for(th=tg->hosts;th;th=nth) {
22 nth = th->next;
23
24 th_free(th);
25 }
26
27 tg_free(tg);
28 }
29
30 tglist = NULL;
31 }
32
33 trustgroup *tg_getbyid(unsigned int id) {
34 trustgroup *tg;
35
36 for(tg=tglist;tg;tg=tg->next)
37 if(tg->id == id)
38 return tg;
39
40 return NULL;
41 }
42
43 void th_free(trusthost *th) {
44 nsfree(POOL_TRUSTS, th);
45 }
46
47 int th_add(trustgroup *tg, unsigned int id, char *host, unsigned int maxusage, time_t lastseen) {
48 u_int32_t ip, mask;
49 trusthost *th;
50
51 if(!trusts_str2cidr(host, &ip, &mask))
52 return 0;
53
54 th = nsmalloc(POOL_TRUSTS, sizeof(trusthost));
55 if(!th)
56 return 0;
57
58 th->id = id;
59 th->maxusage = maxusage;
60 th->lastseen = lastseen;
61 th->ip = ip;
62 th->mask = mask;
63
64 th->users = NULL;
65 th->group = tg;
66 th->count = 0;
67
68 th->next = tg->hosts;
69 tg->hosts = th;
70
71 return 1;
72 }
73
74 void tg_free(trustgroup *tg) {
75 triggerhook(HOOK_TRUSTS_LOSTGROUP, tg);
76
77 freesstring(tg->name);
78 freesstring(tg->createdby);
79 freesstring(tg->contact);
80 freesstring(tg->comment);
81 nsfree(POOL_TRUSTS, tg);
82 }
83
84 int tg_add(unsigned int id, char *name, unsigned int trustedfor, int mode, unsigned int maxperident, unsigned int maxusage, time_t expires, time_t lastseen, time_t lastmaxuserreset, char *createdby, char *contact, char *comment) {
85 trustgroup *tg = nsmalloc(POOL_TRUSTS, sizeof(trustgroup));
86 if(!tg)
87 return 0;
88
89 tg->name = getsstring(name, TRUSTNAMELEN);
90 tg->createdby = getsstring(createdby, NICKLEN);
91 tg->contact = getsstring(contact, CONTACTLEN);
92 tg->comment = getsstring(comment, COMMENTLEN);
93 if(!tg->name || !tg->createdby || !tg->contact || !tg->comment) {
94 tg_free(tg);
95 return 0;
96 }
97
98 tg->id = id;
99 tg->trustedfor = trustedfor;
100 tg->mode = mode;
101 tg->maxperident = maxperident;
102 tg->maxusage = maxusage;
103 tg->expires = expires;
104 tg->lastseen = lastseen;
105 tg->lastmaxuserreset = lastmaxuserreset;
106 tg->hosts = NULL;
107
108 tg->count = 0;
109
110 memset(tg->exts, 0, sizeof(tg->exts));
111
112 tg->next = tglist;
113 tglist = tg;
114
115 triggerhook(HOOK_TRUSTS_NEWGROUP, tg);
116
117 return 1;
118 }
119
120 trusthost *th_getbyhost(uint32_t host) {
121 trustgroup *tg;
122 trusthost *th;
123
124 for(tg=tglist;tg;tg=tg->next)
125 for(th=tg->hosts;th;th=th->next)
126 if((host & th->mask) == th->ip)
127 return th;
128
129 return NULL;
130 }
131
132 void trusts_flush(void) {
133 trustgroup *tg;
134 trusthost *th;
135 time_t t = time(NULL);
136
137 for(tg=tglist;tg;tg=tg->next) {
138 if(tg->count > 0)
139 tg->lastseen = t;
140
141 tg_dbupdatecounts(tg);
142
143 for(th=tg->hosts;th;th=th->next) {
144 if(th->count > 0)
145 th->lastseen = t;
146
147 th_dbupdatecounts(th);
148 }
149 }
150 }
151
152 trustgroup *tg_strtotg(char *name) {
153 unsigned long id;
154 trustgroup *tg;
155
156 /* legacy format */
157 if(name[0] == '#') {
158 id = strtoul(&name[1], NULL, 10);
159 if(id == ULONG_MAX)
160 return NULL;
161
162 for(tg=tglist;tg;tg=tg->next)
163 if(tg->id == id)
164 return tg;
165 }
166
167 for(tg=tglist;tg;tg=tg->next)
168 if(!match(name, tg->name->content))
169 return tg;
170
171 id = strtoul(name, NULL, 10);
172 if(id == ULONG_MAX)
173 return NULL;
174
175 /* legacy format */
176 for(tg=tglist;tg;tg=tg->next)
177 if(tg->id == id)
178 return tg;
179
180 return NULL;
181 }
182