]>
jfr.im git - irc/quakenet/newserv.git/blob - trusts/trusts_db.c
3 #include "../dbapi2/dbapi2.h"
4 #include "../core/error.h"
5 #include "../core/hooks.h"
6 #include "../core/schedule.h"
7 #include "../control/control.h"
11 static int tgmaxid
, thmaxid
;
13 static void *flushschedule
;
15 void createtrusttables(int migration
);
16 void trusts_flush(void (*)(trusthost
*), void (*)(trustgroup
*));
17 void trusts_freeall(void);
18 static void th_dbupdatecounts(trusthost
*th
);
19 static void tg_dbupdatecounts(trustgroup
*tg
);
21 void createtrusttables(int mode
) {
24 if(mode
== TABLES_MIGRATION
) {
25 groups
= "migration_groups";
26 hosts
= "migration_hosts";
27 } else if(mode
== TABLES_REPLICATION
) {
28 groups
= "replication_groups";
29 hosts
= "replication_hosts";
35 trustsdb
->createtable(trustsdb
, NULL
, NULL
,
36 "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(?))",
37 "Tdddd", groups
, TRUSTNAMELEN
, CREATEDBYLEN
, CONTACTLEN
, COMMENTLEN
40 /* I'd like multiple keys here but that's not gonna happen on a cross-database platform :( */
41 trustsdb
->createtable(trustsdb
, NULL
, NULL
, "CREATE TABLE ? (id INT PRIMARY KEY, groupid INT, host VARCHAR(?), maxusage INT, created INT, lastseen INT)", "Td", hosts
, TRUSTHOSTLEN
);
43 trustsdb
->createtable(trustsdb
, NULL
, NULL
,
44 "CREATE TABLE ? (id INT PRIMARY KEY, groupid INT, groupname VARCHAR(?), ts INT, username VARCHAR(?), message VARCHAR(?))",
45 "Tddd", "log", TRUSTNAMELEN
, CREATEDBYLEN
, TRUSTLOGLEN
49 static void flushdatabase(void *arg
) {
50 trusts_flush(th_dbupdatecounts
, tg_dbupdatecounts
);
53 static void triggerdbloaded(void *arg
) {
54 triggerhook(HOOK_TRUSTS_DB_LOADED
, NULL
);
57 static void loadcomplete(void) {
58 /* error has already been shown */
64 flushschedule
= schedulerecurring(time(NULL
) + 300, 0, 300, flushdatabase
, NULL
);
66 scheduleoneshot(time(NULL
), triggerdbloaded
, NULL
);
69 static void loadhosts_data(const DBAPIResult
*result
, void *tag
) {
75 if(!result
->success
) {
76 Error("trusts", ERR_ERROR
, "Error loading hosts table.");
79 result
->clear(result
);
83 if(result
->fields
!= 6) {
84 Error("trusts", ERR_ERROR
, "Wrong number of fields in hosts table.");
87 result
->clear(result
);
91 while(result
->next(result
)) {
96 th
.id
= strtoul(result
->get(result
, 0), NULL
, 10);
100 groupid
= strtoul(result
->get(result
, 1), NULL
, 10);
102 th
.group
= tg_getbyid(groupid
);
104 Error("trusts", ERR_WARNING
, "Orphaned trust group host: %d", groupid
);
108 host
= result
->get(result
, 2);
109 if(!trusts_str2cidr(host
, &th
.ip
, &th
.mask
)) {
110 Error("trusts", ERR_WARNING
, "Error parsing cidr for host: %s", host
);
114 th
.maxusage
= strtoul(result
->get(result
, 3), NULL
, 10);
115 th
.created
= (time_t)strtoul(result
->get(result
, 4), NULL
, 10);
116 th
.lastseen
= (time_t)strtoul(result
->get(result
, 5), NULL
, 10);
119 Error("trusts", ERR_WARNING
, "Error adding host to trust %d: %s", groupid
, host
);
122 result
->clear(result
);
127 static void loadhosts_fini(const DBAPIResult
*result
, void *tag
) {
128 Error("trusts", ERR_INFO
, "Finished loading hosts, maximum id: %d", thmaxid
);
131 static void loadgroups_data(const DBAPIResult
*result
, void *tag
) {
137 if(!result
->success
) {
138 Error("trusts", ERR_ERROR
, "Error loading group table.");
141 result
->clear(result
);
145 if(result
->fields
!= 12) {
146 Error("trusts", ERR_ERROR
, "Wrong number of fields in groups table.");
149 result
->clear(result
);
153 while(result
->next(result
)) {
156 tg
.id
= strtoul(result
->get(result
, 0), NULL
, 10);
160 tg
.name
= getsstring(rtrim(result
->get(result
, 1)), TRUSTNAMELEN
);
161 tg
.trustedfor
= strtoul(result
->get(result
, 2), NULL
, 10);
162 tg
.mode
= atoi(result
->get(result
, 3));
163 tg
.maxperident
= strtoul(result
->get(result
, 4), NULL
, 10);
164 tg
.maxusage
= strtoul(result
->get(result
, 5), NULL
, 10);
165 tg
.expires
= (time_t)strtoul(result
->get(result
, 6), NULL
, 10);
166 tg
.lastseen
= (time_t)strtoul(result
->get(result
, 7), NULL
, 10);
167 tg
.lastmaxusereset
= (time_t)strtoul(result
->get(result
, 8), NULL
, 10);
168 tg
.createdby
= getsstring(rtrim(result
->get(result
, 9)), CREATEDBYLEN
);
169 tg
.contact
= getsstring(rtrim(result
->get(result
, 10)), CONTACTLEN
);
170 tg
.comment
= getsstring(rtrim(result
->get(result
, 11)), COMMENTLEN
);
172 if(tg
.name
&& tg
.createdby
&& tg
.contact
&& tg
.comment
) {
174 Error("trusts", ERR_WARNING
, "Error adding trustgroup %d: %s", tg
.id
, tg
.name
->content
);
176 Error("trusts", ERR_ERROR
, "Error allocating sstring in group loader, id: %d", tg
.id
);
179 freesstring(tg
.name
);
180 freesstring(tg
.createdby
);
181 freesstring(tg
.contact
);
182 freesstring(tg
.comment
);
185 result
->clear(result
);
188 static void loadgroups_fini(const DBAPIResult
*result
, void *tag
) {
189 Error("trusts", ERR_INFO
, "Finished loading groups, maximum id: %d.", tgmaxid
);
192 static int trusts_connectdb(void) {
194 trustsdb
= dbapi2open(NULL
, "trusts");
196 Error("trusts", ERR_WARNING
, "Unable to connect to db -- not loaded.");
201 createtrusttables(TABLES_REGULAR
);
206 int trusts_loaddb(void) {
207 if(!trusts_connectdb())
212 trustsdb
->loadtable(trustsdb
, NULL
, loadgroups_data
, loadgroups_fini
, NULL
, "groups");
213 trustsdb
->loadtable(trustsdb
, NULL
, loadhosts_data
, loadhosts_fini
, NULL
, "hosts");
218 void trusts_closedb(int closeconnection
) {
223 deleteschedule(flushschedule
, flushdatabase
, NULL
);
224 flushschedule
= NULL
;
232 thmaxid
= tgmaxid
= 0;
234 if(closeconnection
) {
235 trustsdb
->close(trustsdb
);
239 triggerhook(HOOK_TRUSTS_DB_CLOSED
, NULL
);
242 static void th_dbupdatecounts(trusthost
*th
) {
243 trustsdb
->squery(trustsdb
, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuu", "hosts", th
->lastseen
, th
->maxusage
, th
->id
);
246 static void tg_dbupdatecounts(trustgroup
*tg
) {
247 trustsdb
->squery(trustsdb
, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuu", "groups", tg
->lastseen
, tg
->maxusage
, tg
->id
);
250 trusthost
*th_copy(trusthost
*ith
) {
251 trusthost
*th
, *superset
, *subset
;
257 trustsdb_insertth("hosts", th
, th
->group
->id
);
259 th_getsuperandsubsets(ith
->ip
, ith
->mask
, &superset
, &subset
);
260 th_adjusthosts(th
, subset
, superset
);
266 trusthost
*th_new(trustgroup
*tg
, char *host
) {
269 if(!trusts_str2cidr(host
, &nth
.ip
, &nth
.mask
))
273 nth
.id
= thmaxid
+ 1;
274 nth
.created
= time(NULL
);
287 trustgroup
*tg_copy(trustgroup
*itg
) {
288 trustgroup
*tg
= tg_add(itg
);
292 trustsdb_inserttg("groups", tg
);
296 trustgroup
*tg_new(trustgroup
*itg
) {
299 itg
->id
= tgmaxid
+ 1;
302 itg
->lastmaxusereset
= 0;
313 void trustsdb_insertth(char *table
, trusthost
*th
, unsigned int groupid
) {
314 trustsdb
->squery(trustsdb
,
315 "INSERT INTO ? (id, groupid, host, maxusage, lastseen) VALUES (?, ?, ?, ?, ?)",
316 "Tuusut", table
, th
->id
, groupid
, trusts_cidr2str(th
->ip
, th
->mask
), th
->maxusage
, th
->lastseen
320 void trustsdb_inserttg(char *table
, trustgroup
*tg
) {
321 trustsdb
->squery(trustsdb
,
322 "INSERT INTO ? (id, name, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxusereset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
323 "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
327 void tg_update(trustgroup
*tg
) {
328 trustsdb
->squery(trustsdb
,
329 "UPDATE ? SET name = ?, trustedfor = ?, maxperident = ?, maxusage = ?, expires = ?, lastseen = ?, lastmaxusereset = ?, createdby = ?, contact = ?, comment = ? WHERE id = ?",
330 "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
334 void trustsdb_deletetg(char *table
, trustgroup
*tg
) {
335 trustsdb
->squery(trustsdb
,
336 "DELETE FROM ? WHERE id = ?",
337 "Tu", "groups", tg
->id
);
340 void tg_delete(trustgroup
*tg
) {
343 for(pnext
=&tglist
;*pnext
;pnext
=&((*pnext
)->next
)) {
350 trustsdb_deletetg("groups", tg
);
354 void trustsdb_deleteth(char *table
, trusthost
*th
) {
355 trustsdb
->squery(trustsdb
,
356 "DELETE FROM ? WHERE id = ?",
357 "Tu", "hosts", th
->id
);
360 void th_delete(trusthost
*th
) {
364 for(pnext
=&(th
->group
->hosts
);*pnext
;pnext
=&((*pnext
)->next
)) {
371 for(np
=th
->users
;np
;np
=nextbytrust(np
))
372 settrusthost(np
, NULL
);
374 th
->group
->count
-= th
->count
;
376 trustsdb_deleteth("hosts", th
);
382 void trustlog(trustgroup
*tg
, const char *user
, const char *format
, ...) {
383 char buf
[TRUSTLOGLEN
+1];
387 va_start(va
, format
);
388 vsnprintf(buf
, sizeof(buf
), format
, va
);
393 trustsdb
->squery(trustsdb
,
394 "INSERT INTO ? (ts, groupid, groupname, username, message) VALUES (?, ?, ?, ?, ?)",
395 "Tuusss", "log", now
, tg
->id
, tg
->name
->content
, user
, buf
);
398 static void trustlogquery_callback(const struct DBAPIResult
*result
, void *arg
) {
399 nick
*np
= (nick
*)arg
;
402 if(!result
|| !result
->success
) {
403 controlreply(np
, "Error querying the log.");
406 result
->clear(result
);
411 while(result
->next(result
)) {
412 unsigned int ts
, groupid
;
413 char *groupname
, *user
, *message
;
415 ts
= strtoul(result
->get(result
, 0), NULL
, 10);
416 groupid
= strtoul(result
->get(result
, 1), NULL
, 10);
417 groupname
= result
->get(result
, 2);
418 user
= result
->get(result
, 3);
419 message
= result
->get(result
, 4);
421 controlreply(np
, "[%s] #%d/%s (%s) %s", trusts_timetostr(ts
), groupid
, groupname
, user
, message
);
425 controlreply(np
, "--- Done. Found %d entries.", rows
);
428 void trustlogspewid(nick
*np
, unsigned int groupid
, unsigned int limit
) {
429 trustsdb
->query(trustsdb
, trustlogquery_callback
, (void *)np
,
430 "SELECT ts, groupid, groupname, username, message FROM ? WHERE groupid = ? ORDER BY ts DESC LIMIT ?",
431 "Tuu", "log", groupid
, limit
);
434 void trustlogspewname(nick
*np
, const char *groupname
, unsigned int limit
) {
435 trustsdb
->query(trustsdb
, trustlogquery_callback
, (void *)np
,
436 "SELECT ts, groupid, groupname, username, message FROM ? WHERE groupname = ? ORDER BY ts DESC LIMIT ?",
437 "Tsu", "log", groupname
, limit
);
440 void trustloggrep(nick
*np
, const char *pattern
, unsigned int limit
) {
442 snprintf(buf
, sizeof(buf
), "%%%s%%", pattern
);
444 trustsdb
->query(trustsdb
, trustlogquery_callback
, (void *)np
,
445 "SELECT ts, groupid, username, message FROM ? WHERE message LIKE ? ORDER BY ts DESC LIMIT ?",
446 "Tsu", "log", buf
, limit
);