]>
jfr.im git - irc/quakenet/newserv.git/blob - trusts/trusts_db.c
1 #include "../dbapi2/dbapi2.h"
2 #include "../core/error.h"
3 #include "../core/hooks.h"
4 #include "../core/schedule.h"
8 static int tgmaxid
, thmaxid
;
10 static void *flushschedule
;
12 void createtrusttables(int migration
);
13 void trusts_flush(void (*)(trusthost
*), void (*)(trustgroup
*));
14 void trusts_freeall(void);
15 static void th_dbupdatecounts(trusthost
*th
);
16 static void tg_dbupdatecounts(trustgroup
*tg
);
18 void createtrusttables(int mode
) {
21 if(mode
== TABLES_MIGRATION
) {
22 groups
= "migration_groups";
23 hosts
= "migration_hosts";
24 } else if(mode
== TABLES_REPLICATION
) {
25 groups
= "replication_groups";
26 hosts
= "replication_hosts";
32 trustsdb
->createtable(trustsdb
, NULL
, NULL
,
33 "CREATE TABLE ? (id INT PRIMARY KEY, name VARCHAR(?), trustedfor INT, mode INT, maxperident INT, maxusage INT, expires INT, lastseen INT, lastmaxusereset INT, createdby VARCHAR(?), contact VARCHAR(?), comment VARCHAR(?))",
34 "Tdddd", groups
, TRUSTNAMELEN
, CREATEDBYLEN
, CONTACTLEN
, COMMENTLEN
37 /* I'd like multiple keys here but that's not gonna happen on a cross-database platform :( */
38 trustsdb
->createtable(trustsdb
, NULL
, NULL
, "CREATE TABLE ? (id INT PRIMARY KEY, groupid INT, host VARCHAR(?), maxusage INT, lastseen INT)", "Td", hosts
, TRUSTHOSTLEN
);
41 static void flushdatabase(void *arg
) {
42 trusts_flush(th_dbupdatecounts
, tg_dbupdatecounts
);
45 static void triggerdbloaded(void *arg
) {
46 triggerhook(HOOK_TRUSTS_DB_LOADED
, NULL
);
49 static void loadcomplete(void) {
50 /* error has already been shown */
56 flushschedule
= schedulerecurring(time(NULL
) + 300, 0, 300, flushdatabase
, NULL
);
58 scheduleoneshot(time(NULL
), triggerdbloaded
, NULL
);
61 static void loadhosts_data(const DBAPIResult
*result
, void *tag
) {
67 if(!result
->success
) {
68 Error("trusts", ERR_ERROR
, "Error loading hosts table.");
71 result
->clear(result
);
75 if(result
->fields
!= 5) {
76 Error("trusts", ERR_ERROR
, "Wrong number of fields in hosts table.");
79 result
->clear(result
);
83 while(result
->next(result
)) {
88 th
.id
= strtoul(result
->get(result
, 0), NULL
, 10);
92 groupid
= strtoul(result
->get(result
, 1), NULL
, 10);
94 th
.group
= tg_getbyid(groupid
);
96 Error("trusts", ERR_WARNING
, "Orphaned trust group host: %d", groupid
);
100 host
= result
->get(result
, 2);
101 if(!trusts_str2cidr(host
, &th
.ip
, &th
.mask
)) {
102 Error("trusts", ERR_WARNING
, "Error parsing cidr for host: %s", host
);
106 th
.maxusage
= strtoul(result
->get(result
, 3), NULL
, 10);
107 th
.lastseen
= (time_t)strtoul(result
->get(result
, 4), NULL
, 10);
110 Error("trusts", ERR_WARNING
, "Error adding host to trust %d: %s", groupid
, host
);
113 result
->clear(result
);
118 static void loadhosts_fini(const DBAPIResult
*result
, void *tag
) {
119 Error("trusts", ERR_INFO
, "Finished loading hosts, maximum id: %d", thmaxid
);
122 static void loadgroups_data(const DBAPIResult
*result
, void *tag
) {
128 if(!result
->success
) {
129 Error("trusts", ERR_ERROR
, "Error loading group table.");
132 result
->clear(result
);
136 if(result
->fields
!= 12) {
137 Error("trusts", ERR_ERROR
, "Wrong number of fields in groups table.");
140 result
->clear(result
);
144 while(result
->next(result
)) {
147 tg
.id
= strtoul(result
->get(result
, 0), NULL
, 10);
151 tg
.name
= getsstring(rtrim(result
->get(result
, 1)), TRUSTNAMELEN
);
152 tg
.trustedfor
= strtoul(result
->get(result
, 2), NULL
, 10);
153 tg
.mode
= atoi(result
->get(result
, 3));
154 tg
.maxperident
= strtoul(result
->get(result
, 4), NULL
, 10);
155 tg
.maxusage
= strtoul(result
->get(result
, 5), NULL
, 10);
156 tg
.expires
= (time_t)strtoul(result
->get(result
, 6), NULL
, 10);
157 tg
.lastseen
= (time_t)strtoul(result
->get(result
, 7), NULL
, 10);
158 tg
.lastmaxusereset
= (time_t)strtoul(result
->get(result
, 8), NULL
, 10);
159 tg
.createdby
= getsstring(rtrim(result
->get(result
, 9)), CREATEDBYLEN
);
160 tg
.contact
= getsstring(rtrim(result
->get(result
, 10)), CONTACTLEN
);
161 tg
.comment
= getsstring(rtrim(result
->get(result
, 11)), COMMENTLEN
);
163 if(tg
.name
&& tg
.createdby
&& tg
.contact
&& tg
.comment
) {
165 Error("trusts", ERR_WARNING
, "Error adding trustgroup %d: %s", tg
.id
, tg
.name
->content
);
167 Error("trusts", ERR_ERROR
, "Error allocating sstring in group loader, id: %d", tg
.id
);
170 freesstring(tg
.name
);
171 freesstring(tg
.createdby
);
172 freesstring(tg
.contact
);
173 freesstring(tg
.comment
);
176 result
->clear(result
);
179 static void loadgroups_fini(const DBAPIResult
*result
, void *tag
) {
180 Error("trusts", ERR_INFO
, "Finished loading groups, maximum id: %d.", tgmaxid
);
183 static int trusts_connectdb(void) {
185 trustsdb
= dbapi2open(NULL
, "trusts");
187 Error("trusts", ERR_WARNING
, "Unable to connect to db -- not loaded.");
192 createtrusttables(TABLES_REGULAR
);
197 int trusts_loaddb(void) {
198 if(!trusts_connectdb())
203 trustsdb
->loadtable(trustsdb
, NULL
, loadgroups_data
, loadgroups_fini
, NULL
, "groups");
204 trustsdb
->loadtable(trustsdb
, NULL
, loadhosts_data
, loadhosts_fini
, NULL
, "hosts");
209 void trusts_closedb(int closeconnection
) {
214 deleteschedule(flushschedule
, flushdatabase
, NULL
);
215 flushschedule
= NULL
;
223 thmaxid
= tgmaxid
= 0;
225 if(closeconnection
) {
226 trustsdb
->close(trustsdb
);
230 triggerhook(HOOK_TRUSTS_DB_CLOSED
, NULL
);
233 static void th_dbupdatecounts(trusthost
*th
) {
234 trustsdb
->squery(trustsdb
, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuu", "hosts", th
->lastseen
, th
->maxusage
, th
->id
);
237 static void tg_dbupdatecounts(trustgroup
*tg
) {
238 trustsdb
->squery(trustsdb
, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuu", "groups", tg
->lastseen
, tg
->maxusage
, tg
->id
);
241 trusthost
*th_copy(trusthost
*ith
) {
242 trusthost
*th
, *superset
, *subset
;
248 trustsdb_insertth("hosts", th
, th
->group
->id
);
250 th_getsuperandsubsets(ith
->ip
, ith
->mask
, &superset
, &subset
);
251 th_adjusthosts(th
, subset
, superset
);
257 trusthost
*th_new(trustgroup
*tg
, char *host
) {
260 if(!trusts_str2cidr(host
, &nth
.ip
, &nth
.mask
))
264 nth
.id
= thmaxid
+ 1;
277 trustgroup
*tg_copy(trustgroup
*itg
) {
278 trustgroup
*tg
= tg_add(itg
);
282 trustsdb_inserttg("groups", tg
);
286 trustgroup
*tg_new(trustgroup
*itg
) {
289 itg
->id
= tgmaxid
+ 1;
292 itg
->lastmaxusereset
= 0;
303 void trustsdb_insertth(char *table
, trusthost
*th
, unsigned int groupid
) {
304 trustsdb
->squery(trustsdb
,
305 "INSERT INTO ? (id, groupid, host, maxusage, lastseen) VALUES (?, ?, ?, ?, ?)",
306 "Tuusut", table
, th
->id
, groupid
, trusts_cidr2str(th
->ip
, th
->mask
), th
->maxusage
, th
->lastseen
310 void trustsdb_inserttg(char *table
, trustgroup
*tg
) {
311 trustsdb
->squery(trustsdb
,
312 "INSERT INTO ? (id, name, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxusereset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
313 "Tusuuuutttsss", table
, tg
->id
, tg
->name
->content
, tg
->trustedfor
, tg
->mode
, tg
->maxperident
, tg
->maxusage
, tg
->expires
, tg
->lastseen
, tg
->lastmaxusereset
, tg
->createdby
->content
, tg
->contact
->content
, tg
->comment
->content
317 void tg_update(trustgroup
*tg
) {
318 trustsdb
->squery(trustsdb
,
319 "UPDATE ? SET name = ?, trustedfor = ?, maxperident = ?, maxusage = ?, expires = ?, lastseen = ?, lastmaxusereset = ?, createdby = ?, contact = ?, comment = ? WHERE id = ?",
320 "Tsuuuutttsssu", "groups", tg
->name
->content
, tg
->trustedfor
, tg
->mode
, tg
->maxperident
, tg
->maxusage
, tg
->expires
, tg
->lastseen
, tg
->lastmaxusereset
, tg
->createdby
->content
, tg
->contact
->content
, tg
->comment
->content
, tg
->id
324 void tg_delete(trustgroup
*tg
) {
328 void th_delete(trusthost
*th
) {