]>
jfr.im git - irc/quakenet/newserv.git/blob - trusts2/trusts_db.c
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 int trusts_load_db ( void ) {
13 Error ( "trusts" , ERR_STOP
, "Could not connect to database." );
22 trusts_create_tables ();
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" );
34 void trusts_create_tables ( void ) {
37 "CREATE TABLE trusts.groups ("
38 "trustid INT4 NOT NULL PRIMARY KEY,"
39 "startdate INT4 NOT NULL,"
40 "enddate INT4 NOT NULL,"
42 "maxusage INT4 NOT NULL,"
43 "enforceident INT2 NOT NULL,"
45 "maxclones INT4 NOT NULL,"
56 "CREATE TABLE trusts.hosts ("
57 "hostid INT4 NOT NULL PRIMARY KEY,"
58 "trustid INT4 NOT NULL,"
59 "startdate INT4 NOT NULL,"
62 "maxusage INT4 NOT NULL,"
64 "expires INT4 NOT NULL,"
71 "CREATE TABLE trusts.blocks ("
72 "blockid INT4 NOT NULL PRIMARY KEY,"
73 "block INET NOT NULL,"
77 "reason_private VARCHAR,"
78 "reason_public VARCHAR"
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,"
94 void trusts_cleanup_db ( void ) {
98 void trusts_loadtrustgroups ( DBConn
* dbconn
, void * arg
) {
99 DBResult
* pgres
= dbgetresult ( dbconn
);
103 if (! dbquerysuccessful ( pgres
)) {
104 Error ( "trusts" , ERR_ERROR
, "Error loading trustgroup list." );
109 trusts_lasttrustgroupid
= 1 ;
111 while ( dbfetchrow ( pgres
)) {
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 )
128 if ( t
-> id
> trusts_lasttrustgroupid
)
129 trusts_lasttrustgroupid
= t
-> id
;
134 Error ( "trusts" , ERR_INFO
, "Loaded %d trusts (highest ID was %l u)" , rows
, trusts_lasttrustgroupid
);
139 void trusts_loadtrusthosts ( DBConn
* dbconn
, void * arg
) {
140 DBResult
* pgres
= dbgetresult ( dbconn
);
144 patricia_node_t
* node
;
145 struct irc_in_addr sin
;
148 if (! dbquerysuccessful ( pgres
)) {
149 Error ( "trusts" , ERR_ERROR
, "Error loading trusthost list." );
154 trusts_lasttrusthostid
= 1 ;
156 while ( dbfetchrow ( pgres
)) {
158 ipmask_parse ( dbgetvalue ( pgres
, 4 ), & sin
, & bits
);
159 node
= refnode ( iptree
, & sin
, bits
);
162 int tgid
= strtoul ( dbgetvalue ( pgres
, 1 ), NULL
, 10 );
163 tg
= findtrustgroupbyid ( tgid
);
165 Error ( "trusts" , ERR_ERROR
, "Error loading trusthosts - invalid group: %d ." , tgid
);
169 t
= createtrusthostfromdb (
170 /*id*/ strtoul ( dbgetvalue ( pgres
, 0 ), NULL
, 10 ),
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 ),
177 /*created*/ strtoul ( dbgetvalue ( pgres
, 9 ), NULL
, 10 ),
178 /*modified*/ strtoul ( dbgetvalue ( pgres
, 8 ), NULL
, 10 )
181 if ( t
-> id
> trusts_lasttrusthostid
)
182 trusts_lasttrusthostid
= t
-> id
;
184 trusthost_addcounters ( t
);
188 Error ( "trusts" , ERR_INFO
, "Loaded %d trusthosts (highest ID was %l u)" , rows
, trusts_lasttrusthostid
);
193 void trusts_loadtrustblocks ( DBConn
* dbconn
, void * arg
) {
194 DBResult
* pgres
= dbgetresult ( dbconn
);
198 if (! dbquerysuccessful ( pgres
)) {
199 Error ( "trusts" , ERR_ERROR
, "Error loading trustblock list." );
204 trusts_lasttrustblockid
= 1 ;
206 while ( dbfetchrow ( pgres
)) {
207 // t = gettrustblock();
209 Error ( "trusts" , ERR_ERROR
, "Unable to load trust block" );
211 t
-> id
= strtoul ( dbgetvalue ( pgres
, 0 ), NULL
, 10 );
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 );
219 if ( t
-> id
> trusts_lasttrustblockid
)
220 trusts_lasttrustblockid
= t
-> id
;
222 //trusts_addtrustgrouptohash(t);
226 Error ( "trusts" , ERR_INFO
, "Loaded %d trustblocks (highest ID was %l u)" , rows
, trusts_lasttrustblockid
);
231 triggerhook ( HOOK_TRUSTS_DBLOADED
, NULL
);
236 void 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 ( %l u, %l u,0, %l u, %l u, %d , %d , %l u, %l u, %d , %l u, %l u, %l u, %l u )" , 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
);
240 void trustsdb_updatetrustgroup ( trustgroup_t
* t
) {
241 dbquery ( "UPDATE trusts.groups SET startdate= %l u,owneruserid= %l u,maxusage= %l u,enforceident= %d ,type= %d ,maxclones= %l u, maxperident= %l u,maxperip= %d ,expires= %l u,lastused= %l u,modified= %l u,created= %l u WHERE trustid = %l u" , 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
);
244 void trustsdb_deletetrustgroup ( trustgroup_t
* t
) {
245 dbquery ( "UPDATE trusts.groups SET enddate = %j d WHERE trustid = %l u" , ( intmax_t ) getnettime (), t
-> id
);
249 void trustsdb_addtrusthost ( trusthost_t
* th
) {
250 dbquery ( "INSERT INTO trusts.hosts (hostid,trustid,startdate,enddate,host,maxusage,lastused,expires,modified,created) VALUES ( %l u, %l u, %l u,0,' %s / %d ', %l u, %l u, %l u, %l u, %l u)" , 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
);
253 void trustsdb_updatetrusthost ( trusthost_t
* th
) {
254 dbquery ( "UPDATE trusts.hosts SET hostid= %l u,trustid= %l u,startdate= %l u,host=' %s / %d ',maxusage= %l u,lastused= %l u,expires= %l u,modified= %l u,created= %l u" , 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
);
257 void trustsdb_deletetrusthost ( trusthost_t
* th
) {
258 dbquery ( "UPDATE trusts.hosts SET enddate = %j d WHERE hostid = %l u" , ( intmax_t ) getnettime (), th
-> id
);
262 void trustsdb_addtrustblock ( trustblock_t
* tb
) {
263 dbquery ( "INSERT INTO trusts.blocks ( blockid,block,owner,expires,startdate,reason_private,reason_public) VALUES ( %l u,' %s / %d ', %l u, %l u, %l u,' %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
);
266 void trustsdb_updatetrustblock ( trustblock_t
* tb
) {
267 dbquery ( "UPDATE trusts.blocks SET blockid= %l u,block=' %s / %d ',owner= %l u,expires= %l u,startdate= %l u,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
);
270 void trustsdb_deletetrustblock ( trustblock_t
* tb
) {
271 dbquery ( "DELETE from trusts.blocks WHERE blockid = %l u" , tb
-> id
);
275 /* logid, trustid, timestamp, userid, type, message */
278 void 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 ];
282 dbescapestring ( escmessage
, message
, strlen ( message
));
283 dbquery ( "INSERT INTO trusts.log (trustid, timestamp, userid, type, message) VALUES ( %l u, %l u, %l u, %d , ' %s ')" , tg
-> id
, getnettime (), userid
, type
, escmessage
);