]> jfr.im git - irc/quakenet/newserv.git/blame - trusts/data.c
Add trustadd and trustgroupadd.
[irc/quakenet/newserv.git] / trusts / data.c
CommitLineData
5ada3782 1#include <stdlib.h>
2129448c 2#include <string.h>
5ada3782
CP
3
4#include "../lib/sstring.h"
2129448c 5#include "../core/hooks.h"
45989eab 6#include "../core/nsmalloc.h"
4ea9375d 7#include "../lib/irc_string.h"
5ada3782
CP
8#include "trusts.h"
9
10trustgroup *tglist;
11
4be1aaf2
CP
12void th_dbupdatecounts(trusthost *);
13void tg_dbupdatecounts(trustgroup *);
14
5ada3782
CP
15void 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
33trustgroup *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
43void th_free(trusthost *th) {
45989eab 44 nsfree(POOL_TRUSTS, th);
5ada3782
CP
45}
46
d2c08930 47trusthost *th_add(trustgroup *tg, unsigned int id, char *host, unsigned int maxusage, time_t lastseen) {
65f34016
CP
48 u_int32_t ip, mask;
49 trusthost *th;
50
51 if(!trusts_str2cidr(host, &ip, &mask))
d2c08930 52 return NULL;
5ada3782 53
45989eab 54 th = nsmalloc(POOL_TRUSTS, sizeof(trusthost));
65f34016 55 if(!th)
d2c08930 56 return NULL;
65f34016 57
9bf9e8a1 58 th->id = id;
4be1aaf2 59 th->maxusage = maxusage;
5ada3782 60 th->lastseen = lastseen;
65f34016
CP
61 th->ip = ip;
62 th->mask = mask;
5ada3782 63
1bbe1ac3
CP
64 th->users = NULL;
65 th->group = tg;
66 th->count = 0;
67
5ada3782
CP
68 th->next = tg->hosts;
69 tg->hosts = th;
70
d2c08930 71 return th;
5ada3782
CP
72}
73
74void tg_free(trustgroup *tg) {
2129448c
CP
75 triggerhook(HOOK_TRUSTS_LOSTGROUP, tg);
76
5ada3782
CP
77 freesstring(tg->name);
78 freesstring(tg->createdby);
79 freesstring(tg->contact);
80 freesstring(tg->comment);
45989eab 81 nsfree(POOL_TRUSTS, tg);
5ada3782
CP
82}
83
d2c08930 84trustgroup *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) {
45989eab 85 trustgroup *tg = nsmalloc(POOL_TRUSTS, sizeof(trustgroup));
5ada3782 86 if(!tg)
d2c08930 87 return NULL;
5ada3782
CP
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);
d2c08930 95 return NULL;
5ada3782
CP
96 }
97
98 tg->id = id;
99 tg->trustedfor = trustedfor;
100 tg->mode = mode;
101 tg->maxperident = maxperident;
4be1aaf2 102 tg->maxusage = maxusage;
5ada3782
CP
103 tg->expires = expires;
104 tg->lastseen = lastseen;
105 tg->lastmaxuserreset = lastmaxuserreset;
65f34016 106 tg->hosts = NULL;
5ada3782 107
1bbe1ac3
CP
108 tg->count = 0;
109
2129448c
CP
110 memset(tg->exts, 0, sizeof(tg->exts));
111
5ada3782
CP
112 tg->next = tglist;
113 tglist = tg;
114
2129448c
CP
115 triggerhook(HOOK_TRUSTS_NEWGROUP, tg);
116
d2c08930 117 return tg;
5ada3782 118}
1bbe1ac3 119
d2c08930 120trusthost *th_getbyhost(uint32_t ip) {
1bbe1ac3 121 trustgroup *tg;
fdcbe91d
CP
122 trusthost *th, *result = NULL;
123 uint32_t mask;
1bbe1ac3 124
fdcbe91d
CP
125 for(tg=tglist;tg;tg=tg->next) {
126 for(th=tg->hosts;th;th=th->next) {
d2c08930 127 if((ip & th->mask) == th->ip) {
fdcbe91d
CP
128 if(!result || (th->mask > mask)) {
129 mask = th->mask;
130 result = th;
131 }
132 }
133 }
134 }
1bbe1ac3 135
fdcbe91d 136 return result;
1bbe1ac3 137}
4be1aaf2 138
d2c08930
CP
139/* should this return the largest, smallest or any match? currently the latter */
140trusthost *th_getsupersetbyhost(uint32_t ip, uint32_t mask) {
141 trustgroup *tg;
142 trusthost *th;
143
144 for(tg=tglist;tg;tg=tg->next)
145 for(th=tg->hosts;th;th=th->next)
146 if((th->ip & mask) == ip)
147 return th;
148
149 return NULL;
150}
151
4be1aaf2
CP
152void trusts_flush(void) {
153 trustgroup *tg;
154 trusthost *th;
155 time_t t = time(NULL);
156
157 for(tg=tglist;tg;tg=tg->next) {
158 if(tg->count > 0)
159 tg->lastseen = t;
160
161 tg_dbupdatecounts(tg);
162
163 for(th=tg->hosts;th;th=th->next) {
164 if(th->count > 0)
165 th->lastseen = t;
166
167 th_dbupdatecounts(th);
168 }
169 }
170}
4ea9375d
CP
171
172trustgroup *tg_strtotg(char *name) {
173 unsigned long id;
174 trustgroup *tg;
175
176 /* legacy format */
177 if(name[0] == '#') {
178 id = strtoul(&name[1], NULL, 10);
179 if(id == ULONG_MAX)
180 return NULL;
181
182 for(tg=tglist;tg;tg=tg->next)
183 if(tg->id == id)
184 return tg;
185 }
186
187 for(tg=tglist;tg;tg=tg->next)
188 if(!match(name, tg->name->content))
189 return tg;
190
191 id = strtoul(name, NULL, 10);
192 if(id == ULONG_MAX)
193 return NULL;
194
195 /* legacy format */
196 for(tg=tglist;tg;tg=tg->next)
197 if(tg->id == id)
198 return tg;
199
200 return NULL;
201}