]> 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 "trusts.h"
8
9 trustgroup *tglist;
10
11 void th_dbupdatecounts(trusthost *);
12 void tg_dbupdatecounts(trustgroup *);
13
14 void trusts_freeall(void) {
15 trustgroup *tg, *ntg;
16 trusthost *th, *nth;
17
18 for(tg=tglist;tg;tg=ntg) {
19 ntg = tg->next;
20 for(th=tg->hosts;th;th=nth) {
21 nth = th->next;
22
23 th_free(th);
24 }
25
26 tg_free(tg);
27 }
28
29 tglist = NULL;
30 }
31
32 trustgroup *tg_getbyid(unsigned int id) {
33 trustgroup *tg;
34
35 for(tg=tglist;tg;tg=tg->next)
36 if(tg->id == id)
37 return tg;
38
39 return NULL;
40 }
41
42 void th_free(trusthost *th) {
43 nsfree(POOL_TRUSTS, th);
44 }
45
46 int th_add(trustgroup *tg, unsigned int id, char *host, unsigned int maxusage, time_t lastseen) {
47 u_int32_t ip, mask;
48 trusthost *th;
49
50 if(!trusts_str2cidr(host, &ip, &mask))
51 return 0;
52
53 th = nsmalloc(POOL_TRUSTS, sizeof(trusthost));
54 if(!th)
55 return 0;
56
57 th->id = id;
58 th->maxusage = maxusage;
59 th->lastseen = lastseen;
60 th->ip = ip;
61 th->mask = mask;
62
63 th->users = NULL;
64 th->group = tg;
65 th->count = 0;
66
67 th->next = tg->hosts;
68 tg->hosts = th;
69
70 return 1;
71 }
72
73 void tg_free(trustgroup *tg) {
74 triggerhook(HOOK_TRUSTS_LOSTGROUP, tg);
75
76 freesstring(tg->name);
77 freesstring(tg->createdby);
78 freesstring(tg->contact);
79 freesstring(tg->comment);
80 nsfree(POOL_TRUSTS, tg);
81 }
82
83 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) {
84 trustgroup *tg = nsmalloc(POOL_TRUSTS, sizeof(trustgroup));
85 if(!tg)
86 return 0;
87
88 tg->name = getsstring(name, TRUSTNAMELEN);
89 tg->createdby = getsstring(createdby, NICKLEN);
90 tg->contact = getsstring(contact, CONTACTLEN);
91 tg->comment = getsstring(comment, COMMENTLEN);
92 if(!tg->name || !tg->createdby || !tg->contact || !tg->comment) {
93 tg_free(tg);
94 return 0;
95 }
96
97 tg->id = id;
98 tg->trustedfor = trustedfor;
99 tg->mode = mode;
100 tg->maxperident = maxperident;
101 tg->maxusage = maxusage;
102 tg->expires = expires;
103 tg->lastseen = lastseen;
104 tg->lastmaxuserreset = lastmaxuserreset;
105 tg->hosts = NULL;
106
107 tg->count = 0;
108
109 memset(tg->exts, 0, sizeof(tg->exts));
110
111 tg->next = tglist;
112 tglist = tg;
113
114 triggerhook(HOOK_TRUSTS_NEWGROUP, tg);
115
116 return 1;
117 }
118
119 trusthost *th_getbyhost(uint32_t host) {
120 trustgroup *tg;
121 trusthost *th;
122
123 for(tg=tglist;tg;tg=tg->next)
124 for(th=tg->hosts;th;th=th->next)
125 if((host & th->mask) == th->ip)
126 return th;
127
128 return NULL;
129 }
130
131 void trusts_flush(void) {
132 trustgroup *tg;
133 trusthost *th;
134 time_t t = time(NULL);
135
136 for(tg=tglist;tg;tg=tg->next) {
137 if(tg->count > 0)
138 tg->lastseen = t;
139
140 tg_dbupdatecounts(tg);
141
142 for(th=tg->hosts;th;th=th->next) {
143 if(th->count > 0)
144 th->lastseen = t;
145
146 th_dbupdatecounts(th);
147 }
148 }
149 }