1 #include "../nick/nick.h"
2 #include "../core/error.h"
3 #include "../lib/irc_string.h"
4 #include "../core/schedule.h"
9 int trustdb_loaded
= 0;
11 static void trusts_dbtriggerdbloaded(void *arg
);
13 int trusts_load_db(void) {
15 Error("trusts", ERR_STOP
, "Could not connect to database.");
24 trusts_create_tables();
26 dbasyncquery(trusts_loadtrustgroups
, NULL
,
27 "SELECT trustid,maxusage,maxclones,maxperident,maxperip,enforceident,startdate,lastused,expires,owneruserid,type,created,modified FROM trusts.groups WHERE enddate = 0");
28 dbasyncquery(trusts_loadtrustgroupsmax
, NULL
,
29 "SELECT max(trustid) from trusts.groups");
31 dbasyncquery(trusts_loadtrusthosts
, NULL
,
32 "SELECT * FROM trusts.hosts WHERE enddate = 0");
33 dbasyncquery(trusts_loadtrusthostsmax
, NULL
,
34 "SELECT max(hostid) FROM trusts.hosts");
36 dbasyncquery(trusts_loadtrustblocks
, NULL
,
37 "SELECT * FROM trusts.blocks");
42 void trusts_create_tables(void) {
45 "CREATE TABLE trusts.groups ("
46 "trustid INT4 NOT NULL PRIMARY KEY,"
47 "startdate INT4 NOT NULL,"
48 "enddate INT4 NOT NULL,"
50 "maxusage INT4 NOT NULL,"
51 "enforceident INT2 NOT NULL,"
53 "maxclones INT4 NOT NULL,"
64 "CREATE TABLE trusts.hosts ("
65 "hostid INT4 NOT NULL PRIMARY KEY,"
66 "trustid INT4 NOT NULL,"
67 "startdate INT4 NOT NULL,"
69 "host VARCHAR NOT NULL,"
70 "maxusage INT4 NOT NULL,"
72 "expires INT4 NOT NULL,"
79 "CREATE TABLE trusts.blocks ("
80 "blockid INT4 NOT NULL PRIMARY KEY,"
81 "block VARCHAR NOT NULL,"
85 "reason_private VARCHAR,"
86 "reason_public VARCHAR"
91 "CREATE TABLE trusts.log ("
92 "logid SERIAL NOT NULL PRIMARY KEY,"
93 "trustid INT4 NOT NULL,"
94 "timestamp INT4 NOT NULL,"
95 "userid INT4 NOT NULL,"
102 void trusts_cleanup_db(void) {
106 void trusts_loadtrustgroups(DBConn
*dbconn
, void *arg
) {
107 DBResult
*pgres
= dbgetresult(dbconn
);
111 if(!dbquerysuccessful(pgres
)) {
112 Error("trusts", ERR_ERROR
, "Error loading trustgroup list.");
117 trusts_lasttrustgroupid
= 1;
119 while(dbfetchrow(pgres
)) {
121 t
= createtrustgroupfromdb(
122 /*id*/ strtoul(dbgetvalue(pgres
,0),NULL
,10),
123 /*maxusage*/ strtoul(dbgetvalue(pgres
,1),NULL
,10),
124 /*maxclones*/ strtoul(dbgetvalue(pgres
,2),NULL
,10),
125 /*maxperident*/ strtoul(dbgetvalue(pgres
,3),NULL
,10),
126 /*maxperip*/ strtoul(dbgetvalue(pgres
,4),NULL
,10),
127 /*TODOTYPE*/ /*enforceident*/strtoul(dbgetvalue(pgres
,5),NULL
,10),
128 /*startdate*/ strtoul(dbgetvalue(pgres
,6),NULL
,10),
129 /*lastused*/ strtoul(dbgetvalue(pgres
,7),NULL
,10),
130 /*expire*/ strtoul(dbgetvalue(pgres
,8),NULL
,10),
131 /*ownerid*/ strtoul(dbgetvalue(pgres
,9),NULL
,10),
132 /*type*/ strtoul(dbgetvalue(pgres
,10),NULL
,10),
133 /*created*/ strtoul(dbgetvalue(pgres
,11),NULL
,10),
134 /*modified*/ strtoul(dbgetvalue(pgres
,12),NULL
,10)
137 Error("trusts", ERR_ERROR
, "Error loading trustblock.");
141 if(t
->id
> trusts_lasttrustgroupid
)
142 trusts_lasttrustgroupid
= t
->id
;
147 Error("trusts",ERR_INFO
,"Loaded %d trusts (highest ID was %lu)",rows
,trusts_lasttrustgroupid
);
152 void trusts_loadtrustgroupsmax(DBConn
*dbconn
, void *arg
) {
153 DBResult
*pgres
= dbgetresult(dbconn
);
154 unsigned long trustmax
= 0;
156 if(!dbquerysuccessful(pgres
)) {
157 Error("trusts", ERR_ERROR
, "Error loading trustgroup max.");
162 while(dbfetchrow(pgres
)) {
163 trustmax
= strtoul(dbgetvalue(pgres
,0),NULL
,10);
166 if ( trustmax
< trusts_lasttrustgroupid
) {
167 Error("trusts",ERR_INFO
,"trust max failed - %lu, %lu", trustmax
, trusts_lasttrustgroupid
);
169 trusts_lasttrustgroupid
= trustmax
;
171 Error("trusts",ERR_INFO
,"Loaded Trust Max %lu", trusts_lasttrustgroupid
);
176 void trusts_loadtrusthosts(DBConn
*dbconn
, void *arg
) {
177 DBResult
*pgres
= dbgetresult(dbconn
);
181 patricia_node_t
*node
;
182 struct irc_in_addr sin
;
185 if(!dbquerysuccessful(pgres
)) {
186 Error("trusts", ERR_ERROR
, "Error loading trusthost list.");
191 trusts_lasttrusthostid
= 1;
193 while(dbfetchrow(pgres
)) {
195 if( ipmask_parse(dbgetvalue(pgres
,4), &sin
, &bits
) == 0) {
196 Error("trusts", ERR_ERROR
, "Failed to parse trusthost: %s", dbgetvalue(pgres
,4));
200 node
= refnode(iptree
, &sin
, bits
);
203 int tgid
= strtoul(dbgetvalue(pgres
,1),NULL
,10);
204 tg
=findtrustgroupbyid(tgid
);
206 Error("trusts", ERR_ERROR
, "Error loading trusthosts - invalid group: %d.", tgid
);
208 /* update last hostid - although we probably should fail here more loudly */
209 if(t
->id
> trusts_lasttrusthostid
)
210 trusts_lasttrusthostid
= t
->id
;
215 t
= createtrusthostfromdb(
216 /*id*/ strtoul(dbgetvalue(pgres
,0),NULL
,10),
218 /*startdate*/ strtoul(dbgetvalue(pgres
,2),NULL
,10),
219 /*lastused*/ strtoul(dbgetvalue(pgres
,6),NULL
,10),
220 /*expire*/ strtoul(dbgetvalue(pgres
,7),NULL
,10),
221 /*maxusage*/ strtoul(dbgetvalue(pgres
,5),NULL
,10),
223 /*created*/ strtoul(dbgetvalue(pgres
,9),NULL
,10),
224 /*modified*/ strtoul(dbgetvalue(pgres
,8),NULL
,10)
227 Error("trusts", ERR_ERROR
, "Error loading trusthost.");
231 if(t
->id
> trusts_lasttrusthostid
)
232 trusts_lasttrusthostid
= t
->id
;
234 trusthost_addcounters(t
);
238 Error("trusts",ERR_INFO
,"Loaded %d trusthosts (highest ID was %lu)",rows
,trusts_lasttrusthostid
);
243 void trusts_loadtrusthostsmax(DBConn
*dbconn
, void *arg
) {
244 DBResult
*pgres
= dbgetresult(dbconn
);
245 unsigned long trustmax
= 0;
247 if(!dbquerysuccessful(pgres
)) {
248 Error("trusts", ERR_ERROR
, "Error loading trusthost max.");
253 while(dbfetchrow(pgres
)) {
254 trustmax
= strtoul(dbgetvalue(pgres
,0),NULL
,10);
257 if ( trustmax
< trusts_lasttrusthostid
) {
258 Error("trusts", ERR_FATAL
, "trusthost max failed - %lu, %lu", trustmax
, trusts_lasttrusthostid
);
260 trusts_lasttrusthostid
= trustmax
;
262 Error("trusts",ERR_INFO
,"Loaded Trust Host Max %lu", trusts_lasttrusthostid
);
267 void trusts_loadtrustblocks(DBConn
*dbconn
, void *arg
) {
268 DBResult
*pgres
= dbgetresult(dbconn
);
271 struct irc_in_addr sin
;
273 patricia_node_t
*node
;
275 if(!dbquerysuccessful(pgres
)) {
276 Error("trusts", ERR_ERROR
, "Error loading trustblock list.");
281 trusts_lasttrustblockid
= 1;
283 while(dbfetchrow(pgres
)) {
285 if( ipmask_parse(dbgetvalue(pgres
,1), &sin
, &bits
) == 0) {
286 Error("trusts", ERR_ERROR
, "Failed to parse trustblock: %s", dbgetvalue(pgres
,1));
289 node
= refnode(iptree
, &sin
, bits
);
291 t
= createtrustblockfromdb(
292 /* id */ strtoul(dbgetvalue(pgres
,0),NULL
,10),
294 /* ownerid */ strtoul(dbgetvalue(pgres
,2),NULL
,10),
295 /* expire */ strtoul(dbgetvalue(pgres
,3),NULL
,10),
296 /* startdate*/ strtoul(dbgetvalue(pgres
,4),NULL
,10),
297 /* reason_private */ dbgetvalue(pgres
,5),
298 /* reason_public */ dbgetvalue(pgres
,6)
301 Error("trusts", ERR_ERROR
, "Error loading trustblock.");
305 node
->exts
[tgb_ext
] = t
;
307 if(t
->id
> trusts_lasttrustblockid
)
308 trusts_lasttrustblockid
= t
->id
;
313 Error("trusts",ERR_INFO
,"Loaded %d trustblocks (highest ID was %lu)",rows
,trusts_lasttrustblockid
);
318 scheduleoneshot(time(NULL
), trusts_dbtriggerdbloaded
, NULL
);
321 static void trusts_dbtriggerdbloaded(void *arg
) {
322 triggerhook(HOOK_TRUSTS_DBLOADED
, NULL
);
326 void trustsdb_addtrustgroup(trustgroup_t
*t
) {
327 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
);
330 void trustsdb_updatetrustgroup(trustgroup_t
*t
) {
331 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
);
334 void trustsdb_deletetrustgroup(trustgroup_t
*t
) {
335 dbquery("UPDATE trusts.groups SET enddate = %jd WHERE trustid = %lu", (intmax_t)getnettime(), t
->id
);
339 void trustsdb_addtrusthost(trusthost_t
*th
) {
340 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
);
343 void trustsdb_updatetrusthost(trusthost_t
*th
) {
344 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
);
347 void trustsdb_deletetrusthost(trusthost_t
*th
) {
348 dbquery("UPDATE trusts.hosts SET enddate = %jd WHERE hostid = %lu", (intmax_t)getnettime(), th
->id
);
352 void trustsdb_addtrustblock(trustblock_t
*tb
) {
353 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
: "");
356 void trustsdb_updatetrustblock(trustblock_t
*tb
) {
357 char escprivate
[2*512+1];
358 char escpublic
[2*512+1];
360 dbescapestring(escprivate
,tb
->reason_private
? tb
->reason_private
->content
: "", strlen(tb
->reason_private
? tb
->reason_private
->content
: ""));
361 dbescapestring(escpublic
,tb
->reason_public
? tb
->reason_public
->content
: "", strlen(tb
->reason_public
? tb
->reason_public
->content
: ""));
363 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
);
366 void trustsdb_deletetrustblock(trustblock_t
*tb
) {
367 dbquery("DELETE from trusts.blocks WHERE blockid = %lu", tb
->id
);
371 /* logid, trustid, timestamp, userid, type, message */
374 void trustsdb_logmessage(trustgroup_t
*tg
, unsigned long userid
, int type
, char *message
) {
375 /* maximum length of a trustlog message is ircd max length */
376 char escmessage
[2*512+1];
378 dbescapestring(escmessage
,message
, strlen(message
));
379 dbquery("INSERT INTO trusts.log (trustid, timestamp, userid, type, message) VALUES ( %lu, %lu, %lu, %d, '%s')", tg
->id
, getnettime(), userid
, type
, escmessage
);
382 void trust_dotrustlog_real(DBConn
*dbconn
, void *arg
) {
383 nick
*np
=getnickbynumeric((unsigned long)arg
);
385 unsigned long logid
, trustid
, userid
, type
;
394 pgres
=dbgetresult(dbconn
);
396 if (!dbquerysuccessful(pgres
)) {
397 Error("trusts", ERR_ERROR
, "Error loading trusts log data.");
402 if (dbnumfields(pgres
) != 6) {
403 Error("trusts", ERR_ERROR
, "trusts log data format error.");
413 while(dbfetchrow(pgres
)) {
414 logid
=strtoul(dbgetvalue(pgres
, 0), NULL
, 10);
415 trustid
=strtoul(dbgetvalue(pgres
, 1), NULL
, 10);
416 timestamp
=strtoul(dbgetvalue(pgres
, 2), NULL
, 10);
417 userid
=strtoul(dbgetvalue(pgres
, 3), NULL
, 10);
418 type
=strtoul(dbgetvalue(pgres
, 4), NULL
, 10);
419 message
=dbgetvalue(pgres
, 5);
423 controlreply(np
, "Display trustlog for trust %lu", trustid
);
424 controlreply(np
, "ID Time OperID Type Message");
426 strftime(timebuf
, 30, "%d/%m/%y %H:%M", localtime(×tamp
));
427 controlreply(np
, "%-3lu %s %-7lu %-2lu %s", logid
, timebuf
, userid
, type
, message
);
431 controlreply(np
, "No trust log entries found.");
433 controlreply(np
, "End Of List.");
439 void trustsdb_retrievetrustlog(nick
*np
, unsigned int trustid
, time_t starttime
) {
440 dbasyncquery(trust_dotrustlog_real
, (void *)np
->numeric
, "SELECT * FROM trusts.log WHERE trustid=%u AND timestamp>%lu order by timestamp desc limit 1000", trustid
, starttime
);
441 Error("trusts", ERR_ERROR
, "SELECT * FROM trusts.log WHERE trustid=%u AND timestamp>%lu order by timestamp desc limit 1000", trustid
, starttime
);