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