]>
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, maxpernode INT, nodebits 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
!= 8) {
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);
117 th
.maxpernode
= strtol(result
->get(result
, 6), NULL
, 10);
118 th
.nodebits
= strtol(result
->get(result
, 7), NULL
, 10);
121 Error("trusts", ERR_WARNING
, "Error adding host to trust %d: %s", groupid
, host
);
124 result
->clear(result
);
129 static void loadhosts_fini(const DBAPIResult
*result
, void *tag
) {
130 Error("trusts", ERR_INFO
, "Finished loading hosts, maximum id: %d", thmaxid
);
133 static void loadgroups_data(const DBAPIResult
*result
, void *tag
) {
139 if(!result
->success
) {
140 Error("trusts", ERR_ERROR
, "Error loading group table.");
143 result
->clear(result
);
147 if(result
->fields
!= 12) {
148 Error("trusts", ERR_ERROR
, "Wrong number of fields in groups table.");
151 result
->clear(result
);
155 while(result
->next(result
)) {
158 tg
.id
= strtoul(result
->get(result
, 0), NULL
, 10);
162 tg
.name
= getsstring(rtrim(result
->get(result
, 1)), TRUSTNAMELEN
);
163 tg
.trustedfor
= strtoul(result
->get(result
, 2), NULL
, 10);
164 tg
.mode
= atoi(result
->get(result
, 3));
165 tg
.maxperident
= strtoul(result
->get(result
, 4), NULL
, 10);
166 tg
.maxusage
= strtoul(result
->get(result
, 5), NULL
, 10);
167 tg
.expires
= (time_t)strtoul(result
->get(result
, 6), NULL
, 10);
168 tg
.lastseen
= (time_t)strtoul(result
->get(result
, 7), NULL
, 10);
169 tg
.lastmaxusereset
= (time_t)strtoul(result
->get(result
, 8), NULL
, 10);
170 tg
.createdby
= getsstring(rtrim(result
->get(result
, 9)), CREATEDBYLEN
);
171 tg
.contact
= getsstring(rtrim(result
->get(result
, 10)), CONTACTLEN
);
172 tg
.comment
= getsstring(rtrim(result
->get(result
, 11)), COMMENTLEN
);
174 if(tg
.name
&& tg
.createdby
&& tg
.contact
&& tg
.comment
) {
176 Error("trusts", ERR_WARNING
, "Error adding trustgroup %d: %s", tg
.id
, tg
.name
->content
);
178 Error("trusts", ERR_ERROR
, "Error allocating sstring in group loader, id: %d", tg
.id
);
181 freesstring(tg
.name
);
182 freesstring(tg
.createdby
);
183 freesstring(tg
.contact
);
184 freesstring(tg
.comment
);
187 result
->clear(result
);
190 static void loadgroups_fini(const DBAPIResult
*result
, void *tag
) {
191 Error("trusts", ERR_INFO
, "Finished loading groups, maximum id: %d.", tgmaxid
);
194 static int trusts_connectdb(void) {
196 trustsdb
= dbapi2open(NULL
, "trusts");
198 Error("trusts", ERR_WARNING
, "Unable to connect to db -- not loaded.");
203 createtrusttables(TABLES_REGULAR
);
208 int trusts_loaddb(void) {
209 if(!trusts_connectdb())
214 trustsdb
->loadtable(trustsdb
, NULL
, loadgroups_data
, loadgroups_fini
, NULL
, "groups");
215 trustsdb
->loadtable(trustsdb
, NULL
, loadhosts_data
, loadhosts_fini
, NULL
, "hosts");
220 void trusts_closedb(int closeconnection
) {
225 deleteschedule(flushschedule
, flushdatabase
, NULL
);
226 flushschedule
= NULL
;
234 thmaxid
= tgmaxid
= 0;
236 if(closeconnection
) {
237 trustsdb
->close(trustsdb
);
241 triggerhook(HOOK_TRUSTS_DB_CLOSED
, NULL
);
244 static void th_dbupdatecounts(trusthost
*th
) {
245 trustsdb
->squery(trustsdb
, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuu", "hosts", th
->lastseen
, th
->maxusage
, th
->id
);
248 static void tg_dbupdatecounts(trustgroup
*tg
) {
249 trustsdb
->squery(trustsdb
, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuu", "groups", tg
->lastseen
, tg
->maxusage
, tg
->id
);
252 trusthost
*th_copy(trusthost
*ith
) {
253 trusthost
*th
, *superset
, *subset
;
259 trustsdb_insertth("hosts", th
, th
->group
->id
);
261 th_getsuperandsubsets(ith
->ip
, ith
->mask
, &superset
, &subset
);
262 th_adjusthosts(th
, superset
, subset
);
268 trusthost
*th_new(trustgroup
*tg
, char *host
) {
271 if(!trusts_str2cidr(host
, &nth
.ip
, &nth
.mask
))
275 nth
.id
= thmaxid
+ 1;
276 nth
.created
= time(NULL
);
289 trustgroup
*tg_copy(trustgroup
*itg
) {
290 trustgroup
*tg
= tg_add(itg
);
294 trustsdb_inserttg("groups", tg
);
298 trustgroup
*tg_new(trustgroup
*itg
) {
301 itg
->id
= tgmaxid
+ 1;
304 itg
->lastmaxusereset
= 0;
315 void trustsdb_insertth(char *table
, trusthost
*th
, unsigned int groupid
) {
316 trustsdb
->squery(trustsdb
,
317 "INSERT INTO ? (id, groupid, host, maxusage, created, lastseen, maxpernode, nodebits) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
318 "Tuusuutuu", table
, th
->id
, groupid
, trusts_cidr2str(th
->ip
, th
->mask
), th
->maxusage
, th
->created
, th
->lastseen
, th
->maxpernode
, th
->nodebits
322 void trustsdb_inserttg(char *table
, trustgroup
*tg
) {
323 trustsdb
->squery(trustsdb
,
324 "INSERT INTO ? (id, name, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxusereset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
325 "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
329 void tg_update(trustgroup
*tg
) {
330 trustsdb
->squery(trustsdb
,
331 "UPDATE ? SET name = ?, trustedfor = ?, maxperident = ?, maxusage = ?, expires = ?, lastseen = ?, lastmaxusereset = ?, createdby = ?, contact = ?, comment = ? WHERE id = ?",
332 "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
336 void trustsdb_deletetg(char *table
, trustgroup
*tg
) {
337 trustsdb
->squery(trustsdb
,
338 "DELETE FROM ? WHERE id = ?",
339 "Tu", "groups", tg
->id
);
342 void tg_delete(trustgroup
*tg
) {
345 for(pnext
=&tglist
;*pnext
;pnext
=&((*pnext
)->next
)) {
352 trustsdb_deletetg("groups", tg
);
356 void trustsdb_deleteth(char *table
, trusthost
*th
) {
357 trustsdb
->squery(trustsdb
,
358 "DELETE FROM ? WHERE id = ?",
359 "Tu", "hosts", th
->id
);
362 void th_delete(trusthost
*th
) {
366 for(pnext
=&(th
->group
->hosts
);*pnext
;pnext
=&((*pnext
)->next
)) {
373 for(np
=th
->users
;np
;np
=nextbytrust(np
))
374 settrusthost(np
, NULL
);
376 th
->group
->count
-= th
->count
;
378 trustsdb_deleteth("hosts", th
);
384 void trustlog(trustgroup
*tg
, const char *user
, const char *format
, ...) {
385 char buf
[TRUSTLOGLEN
+1];
389 va_start(va
, format
);
390 vsnprintf(buf
, sizeof(buf
), format
, va
);
395 trustsdb
->squery(trustsdb
,
396 "INSERT INTO ? (ts, groupid, groupname, username, message) VALUES (?, ?, ?, ?, ?)",
397 "Tuusss", "log", now
, tg
->id
, tg
->name
->content
, user
, buf
);
400 static void trustlogquery_callback(const struct DBAPIResult
*result
, void *arg
) {
401 nick
*np
= (nick
*)arg
;
404 if(!result
|| !result
->success
) {
405 controlreply(np
, "Error querying the log.");
408 result
->clear(result
);
413 if(result
->fields
!= 5) {
414 Error("trusts", ERR_ERROR
, "Wrong number of fields in log table.");
416 result
->clear(result
);
420 while(result
->next(result
)) {
421 unsigned int ts
, groupid
;
422 char *groupname
, *user
, *message
;
424 ts
= strtoul(result
->get(result
, 0), NULL
, 10);
425 groupid
= strtoul(result
->get(result
, 1), NULL
, 10);
426 groupname
= result
->get(result
, 2);
427 user
= result
->get(result
, 3);
428 message
= result
->get(result
, 4);
430 controlreply(np
, "[%s] #%d/%s (%s) %s", trusts_timetostr(ts
), groupid
, groupname
, user
, message
);
434 result
->clear(result
);
436 controlreply(np
, "--- Done. Found %d entries.", rows
);
439 void trustlogspewid(nick
*np
, unsigned int groupid
, unsigned int limit
) {
440 trustsdb
->query(trustsdb
, trustlogquery_callback
, (void *)np
,
441 "SELECT ts, groupid, groupname, username, message FROM ? WHERE groupid = ? ORDER BY ts DESC LIMIT ?",
442 "Tuu", "log", groupid
, limit
);
445 void trustlogspewname(nick
*np
, const char *groupname
, unsigned int limit
) {
446 trustsdb
->query(trustsdb
, trustlogquery_callback
, (void *)np
,
447 "SELECT ts, groupid, groupname, username, message FROM ? WHERE groupname = ? ORDER BY ts DESC LIMIT ?",
448 "Tsu", "log", groupname
, limit
);
451 void trustloggrep(nick
*np
, const char *pattern
, unsigned int limit
) {
453 snprintf(buf
, sizeof(buf
), "%%%s%%", pattern
);
455 trustsdb
->query(trustsdb
, trustlogquery_callback
, (void *)np
,
456 "SELECT ts, groupid, groupname, username, message FROM ? WHERE message LIKE ? ORDER BY ts DESC LIMIT ?",
457 "Tsu", "log", buf
, limit
);