]> jfr.im git - irc/quakenet/newserv.git/blame - trusts2/trusts_db.c
sync http://hg.quakenet.org/snircd/diff/6a655306abe8/ircd/ircd_string.c
[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 );
ef9fab2e
P
128 if (!t) {
129 Error("trusts", ERR_ERROR, "Error loading trustblock.");
130 return;
131 }
132
e2527cba
P
133 if(t->id > trusts_lasttrustgroupid)
134 trusts_lasttrustgroupid = t->id;
135
136 rows++;
137 }
138
139 Error("trusts",ERR_INFO,"Loaded %d trusts (highest ID was %lu)",rows,trusts_lasttrustgroupid);
140
141 dbclear(pgres);
142}
143
144void trusts_loadtrusthosts(DBConn *dbconn, void *arg) {
145 DBResult *pgres = dbgetresult(dbconn);
146 int rows=0;
147 trusthost_t *t;
148 trustgroup_t *tg;
149 patricia_node_t *node;
150 struct irc_in_addr sin;
151 unsigned char bits;
152
153 if(!dbquerysuccessful(pgres)) {
154 Error("trusts", ERR_ERROR, "Error loading trusthost list.");
155 dbclear(pgres);
156 return;
157 }
158
159 trusts_lasttrusthostid = 1;
160
161 while(dbfetchrow(pgres)) {
162 /*node*/
163 ipmask_parse(dbgetvalue(pgres,4), &sin, &bits);
164 node = refnode(iptree, &sin, bits);
165
166 /*tg*/
167 int tgid = strtoul(dbgetvalue(pgres,1),NULL,10);
168 tg=findtrustgroupbyid(tgid);
169 if (!tg) {
170 Error("trusts", ERR_ERROR, "Error loading trusthosts - invalid group: %d.", tgid);
171 continue;
172 }
173
174 t = createtrusthostfromdb(
175 /*id*/ strtoul(dbgetvalue(pgres,0),NULL,10),
176 /*node*/ node,
177 /*startdate*/ strtoul(dbgetvalue(pgres,2),NULL,10),
178 /*lastused*/ strtoul(dbgetvalue(pgres,6),NULL,10),
179 /*expire*/ strtoul(dbgetvalue(pgres,7),NULL,10),
180 /*maxusage*/ strtoul(dbgetvalue(pgres,5),NULL,10),
181 /*trustgroup*/ tg,
182 /*created*/ strtoul(dbgetvalue(pgres,9),NULL,10),
183 /*modified*/ strtoul(dbgetvalue(pgres,8),NULL,10)
184 );
ef9fab2e
P
185 if (!t) {
186 Error("trusts", ERR_ERROR, "Error loading trusthost.");
187 return;
188 }
e2527cba
P
189 node = 0;
190 if(t->id > trusts_lasttrusthostid)
191 trusts_lasttrusthostid = t->id;
192
193 trusthost_addcounters(t);
194 rows++;
195 }
196
197 Error("trusts",ERR_INFO,"Loaded %d trusthosts (highest ID was %lu)",rows,trusts_lasttrusthostid);
198
199 dbclear(pgres);
200}
201
202void trusts_loadtrustblocks(DBConn *dbconn, void *arg) {
203 DBResult *pgres = dbgetresult(dbconn);
204 int rows=0;
205 trustblock_t *t;
ef9fab2e
P
206 struct irc_in_addr sin;
207 unsigned char bits;
208 patricia_node_t *node;
e2527cba
P
209
210 if(!dbquerysuccessful(pgres)) {
211 Error("trusts", ERR_ERROR, "Error loading trustblock list.");
212 dbclear(pgres);
213 return;
214 }
215
216 trusts_lasttrustblockid = 1;
217
218 while(dbfetchrow(pgres)) {
ef9fab2e
P
219 /*node*/
220 ipmask_parse(dbgetvalue(pgres,1), &sin, &bits);
221 node = refnode(iptree, &sin, bits);
222
223 t = createtrustblockfromdb(
224 /* id */ strtoul(dbgetvalue(pgres,0),NULL,10),
225 /* node */ node,
226 /* ownerid */ strtoul(dbgetvalue(pgres,2),NULL,10),
227 /* expire */ strtoul(dbgetvalue(pgres,3),NULL,10),
228 /* startdate*/ strtoul(dbgetvalue(pgres,4),NULL,10),
229 /* reason_private */ dbgetvalue(pgres,5),
230 /* reason_public */ dbgetvalue(pgres,6)
231 );
232 if (!t) {
233 Error("trusts", ERR_ERROR, "Error loading trustblock.");
234 return;
235 }
236
237 node->exts[tgb_ext] = t;
e2527cba
P
238
239 if(t->id > trusts_lasttrustblockid)
240 trusts_lasttrustblockid = t->id;
241
e2527cba
P
242 rows++;
243 }
244
245 Error("trusts",ERR_INFO,"Loaded %d trustblocks (highest ID was %lu)",rows,trusts_lasttrustblockid);
246
247 dbclear(pgres);
9a8ffb84
P
248
249 trusts_loaded = 1;
250 triggerhook(HOOK_TRUSTS_DBLOADED, NULL);
e2527cba
P
251}
252
253
254/* trust group */
255void trustsdb_addtrustgroup(trustgroup_t *t) {
256 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 );
257}
258
259void trustsdb_updatetrustgroup(trustgroup_t *t) {
260 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);
261}
262
263void trustsdb_deletetrustgroup(trustgroup_t *t) {
264 dbquery("UPDATE trusts.groups SET enddate = %jd WHERE trustid = %lu", (intmax_t)getnettime(), t->id);
265}
266
267/* trust host */
268void trustsdb_addtrusthost(trusthost_t *th) {
269 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);
270}
271
272void trustsdb_updatetrusthost(trusthost_t *th) {
273 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);
274}
275
276void trustsdb_deletetrusthost(trusthost_t *th) {
277 dbquery("UPDATE trusts.hosts SET enddate = %jd WHERE hostid = %lu", (intmax_t)getnettime(), th->id);
278}
279
280/* trust block */
281void trustsdb_addtrustblock(trustblock_t *tb) {
ef9fab2e 282 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 ? tb->reason_private->content : "", tb->reason_public ? tb->reason_public->content : "");
e2527cba
P
283}
284
285void trustsdb_updatetrustblock(trustblock_t *tb) {
ef9fab2e
P
286 char escprivate[2*512+1];
287 char escpublic[2*512+1];
288
289 dbescapestring(escprivate,tb->reason_private ? tb->reason_private->content : "", strlen(tb->reason_private ? tb->reason_private->content : ""));
290 dbescapestring(escpublic,tb->reason_public ? tb->reason_public->content : "", strlen(tb->reason_public ? tb->reason_public->content : ""));
291
292 dbquery("UPDATE trusts.blocks SET block='%s/%d',owner=%lu,expires=%lu,startdate=%lu,reason_private='%s',reason_public='%s' WHERE blockid=%lu", IPtostr(tb->node->prefix->sin), irc_bitlen(&(tb->node->prefix->sin),tb->node->prefix->bitlen), tb->ownerid, tb->expire, tb->startdate, escprivate, escpublic, tb->id);
e2527cba
P
293}
294
295void trustsdb_deletetrustblock(trustblock_t *tb) {
296 dbquery("DELETE from trusts.blocks WHERE blockid = %lu", tb->id);
297}
298
299/* trust log */
300/* logid, trustid, timestamp, userid, type, message */
301/* @@@ TODO */
302
303void trustsdb_logmessage(trustgroup_t *tg, unsigned long userid, int type, char *message) {
304 /* maximum length of a trustlog message is ircd max length */
305 char escmessage[2*512+1];
306
307 dbescapestring(escmessage,message, strlen(message));
308 dbquery("INSERT INTO trusts.log (trustid, timestamp, userid, type, message) VALUES ( %lu, %lu, %lu, %d, '%s')", tg->id, getnettime(), userid, type, escmessage);
309}