]> jfr.im git - irc/quakenet/newserv.git/blame - trusts2/trusts_db.c
trustscommands as seperate module
[irc/quakenet/newserv.git] / trusts2 / trusts_db.c
CommitLineData
e2527cba
P
1#include "../nick/nick.h"
2#include "../core/error.h"
3#include "../lib/irc_string.h"
4#include "../core/schedule.h"
5#include <stdlib.h>
6
7#include "trusts.h"
8
9int trustdb_loaded = 0;
10
11int trusts_load_db(void) {
12 if(!dbconnected()) {
13 Error("trusts", ERR_STOP, "Could not connect to database.");
14 return 0;
15 }
16
17 if(trustdb_loaded)
18 trusts_cleanup_db();
19
20 trustdb_loaded = 1;
21
22 trusts_create_tables();
23
24 dbasyncquery(trusts_loadtrustgroups, NULL,
25 "SELECT trustid,maxusage,maxclones,maxperident,maxperip,enforceident,startdate,lastused,expires,owneruserid,type,created,modified FROM trusts.groups WHERE enddate = 0");
26 dbasyncquery(trusts_loadtrusthosts, NULL,
27 "SELECT * FROM trusts.hosts WHERE enddate = 0");
28 dbasyncquery(trusts_loadtrustblocks, NULL,
29 "SELECT * FROM trusts.blocks");
30
31 return 1;
32}
33
34void trusts_create_tables(void) {
35 dbattach("trusts");
36 dbcreatequery(
37 "CREATE TABLE trusts.groups ("
38 "trustid INT4 NOT NULL PRIMARY KEY,"
39 "startdate INT4 NOT NULL,"
40 "enddate INT4 NOT NULL,"
41 "owneruserid INT4,"
42 "maxusage INT4 NOT NULL,"
43 "enforceident INT2 NOT NULL,"
44 "type INT2,"
45 "maxclones INT4 NOT NULL,"
46 "maxperident INT4,"
47 "maxperip INT4,"
48 "expires INT4,"
49 "lastused INT4,"
50 "modified INT4,"
51 "created INT4"
52 ") WITHOUT OIDS;"
53 );
54
55 dbcreatequery(
56 "CREATE TABLE trusts.hosts ("
57 "hostid INT4 NOT NULL PRIMARY KEY,"
58 "trustid INT4 NOT NULL,"
59 "startdate INT4 NOT NULL,"
60 "enddate INT4,"
61 "host INET NOT NULL,"
62 "maxusage INT4 NOT NULL,"
63 "lastused INT4,"
64 "expires INT4 NOT NULL,"
65 "modified INT4,"
66 "created INT4"
67 ") WITHOUT OIDS;"
68 );
69
70 dbcreatequery(
71 "CREATE TABLE trusts.blocks ("
72 "blockid INT4 NOT NULL PRIMARY KEY,"
73 "block INET NOT NULL,"
74 "owner INT4,"
75 "expires INT4,"
76 "startdate INT4,"
77 "reason_private VARCHAR,"
78 "reason_public VARCHAR"
79 ") WITHOUT OIDS;"
80 );
81
82 dbcreatequery(
83 "CREATE TABLE trusts.log ("
84 "logid SERIAL NOT NULL PRIMARY KEY,"
85 "trustid INT4 NOT NULL,"
86 "timestamp INT4 NOT NULL,"
87 "userid INT4 NOT NULL,"
88 "type INT2,"
89 "message VARCHAR"
90 ") WITHOUT OIDS;"
91 );
92}
93
94void trusts_cleanup_db(void) {
95 dbdetach("trusts");
96}
97
98void trusts_loadtrustgroups(DBConn *dbconn, void *arg) {
99 DBResult *pgres = dbgetresult(dbconn);
100 int rows=0;
101 trustgroup_t *t;
102
103 if(!dbquerysuccessful(pgres)) {
104 Error("trusts", ERR_ERROR, "Error loading trustgroup list.");
105 dbclear(pgres);
106 return;
107 }
108
109 trusts_lasttrustgroupid = 1;
110
111 while(dbfetchrow(pgres)) {
112
113 t = createtrustgroupfromdb(
114 /*id*/ strtoul(dbgetvalue(pgres,0),NULL,10),
115 /*maxusage*/ strtoul(dbgetvalue(pgres,1),NULL,10),
116 /*maxclones*/ strtoul(dbgetvalue(pgres,2),NULL,10),
117 /*maxperident*/ strtoul(dbgetvalue(pgres,3),NULL,10),
118 /*maxperip*/ strtoul(dbgetvalue(pgres,4),NULL,10),
119 /*TODOTYPE*/ /*enforceident*/strtoul(dbgetvalue(pgres,5),NULL,10),
120 /*startdate*/ strtoul(dbgetvalue(pgres,6),NULL,10),
121 /*lastused*/ strtoul(dbgetvalue(pgres,7),NULL,10),
122 /*expire*/ strtoul(dbgetvalue(pgres,8),NULL,10),
123 /*ownerid*/ strtoul(dbgetvalue(pgres,9),NULL,10),
124 /*type*/ strtoul(dbgetvalue(pgres,10),NULL,10),
125 /*created*/ strtoul(dbgetvalue(pgres,11),NULL,10),
126 /*modified*/ strtoul(dbgetvalue(pgres,12),NULL,10)
127 );
128 if(t->id > trusts_lasttrustgroupid)
129 trusts_lasttrustgroupid = t->id;
130
131 rows++;
132 }
133
134 Error("trusts",ERR_INFO,"Loaded %d trusts (highest ID was %lu)",rows,trusts_lasttrustgroupid);
135
136 dbclear(pgres);
137}
138
139void trusts_loadtrusthosts(DBConn *dbconn, void *arg) {
140 DBResult *pgres = dbgetresult(dbconn);
141 int rows=0;
142 trusthost_t *t;
143 trustgroup_t *tg;
144 patricia_node_t *node;
145 struct irc_in_addr sin;
146 unsigned char bits;
147
148 if(!dbquerysuccessful(pgres)) {
149 Error("trusts", ERR_ERROR, "Error loading trusthost list.");
150 dbclear(pgres);
151 return;
152 }
153
154 trusts_lasttrusthostid = 1;
155
156 while(dbfetchrow(pgres)) {
157 /*node*/
158 ipmask_parse(dbgetvalue(pgres,4), &sin, &bits);
159 node = refnode(iptree, &sin, bits);
160
161 /*tg*/
162 int tgid = strtoul(dbgetvalue(pgres,1),NULL,10);
163 tg=findtrustgroupbyid(tgid);
164 if (!tg) {
165 Error("trusts", ERR_ERROR, "Error loading trusthosts - invalid group: %d.", tgid);
166 continue;
167 }
168
169 t = createtrusthostfromdb(
170 /*id*/ strtoul(dbgetvalue(pgres,0),NULL,10),
171 /*node*/ node,
172 /*startdate*/ strtoul(dbgetvalue(pgres,2),NULL,10),
173 /*lastused*/ strtoul(dbgetvalue(pgres,6),NULL,10),
174 /*expire*/ strtoul(dbgetvalue(pgres,7),NULL,10),
175 /*maxusage*/ strtoul(dbgetvalue(pgres,5),NULL,10),
176 /*trustgroup*/ tg,
177 /*created*/ strtoul(dbgetvalue(pgres,9),NULL,10),
178 /*modified*/ strtoul(dbgetvalue(pgres,8),NULL,10)
179 );
180 node = 0;
181 if(t->id > trusts_lasttrusthostid)
182 trusts_lasttrusthostid = t->id;
183
184 trusthost_addcounters(t);
185 rows++;
186 }
187
188 Error("trusts",ERR_INFO,"Loaded %d trusthosts (highest ID was %lu)",rows,trusts_lasttrusthostid);
189
190 dbclear(pgres);
191}
192
193void trusts_loadtrustblocks(DBConn *dbconn, void *arg) {
194 DBResult *pgres = dbgetresult(dbconn);
195 int rows=0;
196 trustblock_t *t;
197
198 if(!dbquerysuccessful(pgres)) {
199 Error("trusts", ERR_ERROR, "Error loading trustblock list.");
200 dbclear(pgres);
201 return;
202 }
203
204 trusts_lasttrustblockid = 1;
205
206 while(dbfetchrow(pgres)) {
207// t = gettrustblock();
208 if(!t)
209 Error("trusts", ERR_ERROR, "Unable to load trust block");
210
211 t->id = strtoul(dbgetvalue(pgres,0),NULL,10);
212 /* host */
213 t->ownerid = strtoul(dbgetvalue(pgres,3),NULL,10);
214 t->expire = strtoul(dbgetvalue(pgres,11),NULL,10);
215 t->startdate = strtoul(dbgetvalue(pgres,2),NULL,10);
216 /* reason_private */
217 /* reason_public */
218
219 if(t->id > trusts_lasttrustblockid)
220 trusts_lasttrustblockid = t->id;
221
222 //trusts_addtrustgrouptohash(t);
223 rows++;
224 }
225
226 Error("trusts",ERR_INFO,"Loaded %d trustblocks (highest ID was %lu)",rows,trusts_lasttrustblockid);
227
228 dbclear(pgres);
9a8ffb84
P
229
230 trusts_loaded = 1;
231 triggerhook(HOOK_TRUSTS_DBLOADED, NULL);
e2527cba
P
232}
233
234
235/* trust group */
236void trustsdb_addtrustgroup(trustgroup_t *t) {
237 dbquery("INSERT INTO trusts.groups (trustid,startdate,enddate,owneruserid,maxusage,enforceident,type,maxclones,maxperident,maxperip,expires,lastused,modified,created ) VALUES (%lu,%lu,0,%lu,%lu,%d,%d,%lu,%lu,%d,%lu,%lu,%lu,%lu )", t->id,t->startdate,t->ownerid,t->maxusage,t->enforceident,t->type,t->maxclones,t->maxperident,t->maxperip, t->expire, t->lastused, t->modified, t->created );
238}
239
240void trustsdb_updatetrustgroup(trustgroup_t *t) {
241 dbquery("UPDATE trusts.groups SET startdate=%lu,owneruserid=%lu,maxusage=%lu,enforceident=%d,type=%d,maxclones=%lu, maxperident=%lu,maxperip=%d,expires=%lu,lastused=%lu,modified=%lu,created=%lu WHERE trustid = %lu", t->startdate, t->ownerid,t->maxusage,t->enforceident,t->type,t->maxclones,t->maxperident,t->maxperip, t->expire, t->lastused, t->modified, t->created, t->id);
242}
243
244void trustsdb_deletetrustgroup(trustgroup_t *t) {
245 dbquery("UPDATE trusts.groups SET enddate = %jd WHERE trustid = %lu", (intmax_t)getnettime(), t->id);
246}
247
248/* trust host */
249void trustsdb_addtrusthost(trusthost_t *th) {
250 dbquery("INSERT INTO trusts.hosts (hostid,trustid,startdate,enddate,host,maxusage,lastused,expires,modified,created) VALUES (%lu,%lu,%lu,0,'%s/%d',%lu,%lu,%lu,%lu,%lu)", th->id, th->trustgroup->id, th->startdate, IPtostr(th->node->prefix->sin),irc_bitlen(&(th->node->prefix->sin),th->node->prefix->bitlen), th->maxused, th->lastused, th->expire, th->modified, th->created);
251}
252
253void trustsdb_updatetrusthost(trusthost_t *th) {
254 dbquery("UPDATE trusts.hosts SET hostid=%lu,trustid=%lu,startdate=%lu,host='%s/%d',maxusage=%lu,lastused=%lu,expires=%lu,modified=%lu,created=%lu", th->id, th->trustgroup->id, th->startdate, IPtostr(th->node->prefix->sin), irc_bitlen(&(th->node->prefix->sin),th->node->prefix->bitlen), th->maxused, th->lastused, th->expire, th->modified, th->created);
255}
256
257void trustsdb_deletetrusthost(trusthost_t *th) {
258 dbquery("UPDATE trusts.hosts SET enddate = %jd WHERE hostid = %lu", (intmax_t)getnettime(), th->id);
259}
260
261/* trust block */
262void trustsdb_addtrustblock(trustblock_t *tb) {
263 dbquery("INSERT INTO trusts.blocks ( blockid,block,owner,expires,startdate,reason_private,reason_public) VALUES (%lu,'%s/%d',%lu,%lu,%lu,'%s','%s')",tb->id, IPtostr(tb->node->prefix->sin), irc_bitlen(&(tb->node->prefix->sin),tb->node->prefix->bitlen), tb->ownerid, tb->expire, tb->startdate, tb->reason_private->content, tb->reason_public->content);
264}
265
266void trustsdb_updatetrustblock(trustblock_t *tb) {
267 dbquery("UPDATE trusts.blocks SET blockid=%lu,block='%s/%d',owner=%lu,expires=%lu,startdate=%lu,reason_private='%s',reason_public='%s'", tb->id, IPtostr(tb->node->prefix->sin), irc_bitlen(&(tb->node->prefix->sin),tb->node->prefix->bitlen), tb->ownerid, tb->expire, tb->startdate, tb->reason_private->content, tb->reason_public->content);
268}
269
270void trustsdb_deletetrustblock(trustblock_t *tb) {
271 dbquery("DELETE from trusts.blocks WHERE blockid = %lu", tb->id);
272}
273
274/* trust log */
275/* logid, trustid, timestamp, userid, type, message */
276/* @@@ TODO */
277
278void trustsdb_logmessage(trustgroup_t *tg, unsigned long userid, int type, char *message) {
279 /* maximum length of a trustlog message is ircd max length */
280 char escmessage[2*512+1];
281
282 dbescapestring(escmessage,message, strlen(message));
283 dbquery("INSERT INTO trusts.log (trustid, timestamp, userid, type, message) VALUES ( %lu, %lu, %lu, %d, '%s')", tg->id, getnettime(), userid, type, escmessage);
284}