]> jfr.im git - irc/quakenet/newserv.git/blame - trusts/db.c
Merge.
[irc/quakenet/newserv.git] / trusts / db.c
CommitLineData
2b6e02e2
CP
1#include "../dbapi2/dbapi2.h"
2#include "../core/error.h"
be2823bc 3#include "../core/hooks.h"
4be1aaf2 4#include "../core/schedule.h"
2b6e02e2
CP
5#include "trusts.h"
6
7DBAPIConn *trustsdb;
9bf9e8a1 8static int tgmaxid, thmaxid;
be2823bc 9static int loaderror;
4be1aaf2
CP
10static void *flushschedule;
11
be2823bc 12int trustsdbloaded;
2b6e02e2 13
be2823bc 14void createtrusttables(int migration);
4be1aaf2 15void trusts_flush(void);
83bccee3 16void trusts_freeall(void);
2b6e02e2 17
be2823bc
CP
18void createtrusttables(int migration) {
19 char *groups, *hosts;
20
21 if(migration) {
22 groups = "migration_groups";
23 hosts = "migration_hosts";
24 } else {
25 groups = "groups";
26 hosts = "hosts";
27 }
28
29 trustsdb->createtable(trustsdb, NULL, NULL,
4be1aaf2 30 "CREATE TABLE ? (id INT PRIMARY KEY, name VARCHAR(?), trustedfor INT, mode INT, maxperident INT, maxusage INT, expires INT, lastseen INT, lastmaxuserreset INT, createdby VARCHAR(?), contact VARCHAR(?), comment VARCHAR(?))",
be2823bc
CP
31 "Tdddd", groups, TRUSTNAMELEN, NICKLEN, CONTACTLEN, COMMENTLEN
32 );
9bf9e8a1
CP
33
34 /* I'd like multiple keys here but that's not gonna happen on a cross-database platform :( */
35 trustsdb->createtable(trustsdb, NULL, NULL, "CREATE TABLE ? (id INT PRIMARY KEY, groupid INT, host VARCHAR(?), maxusage INT, lastseen INT)", "Td", hosts, TRUSTHOSTLEN);
4be1aaf2
CP
36}
37
38static void flushdatabase(void *arg) {
39 trusts_flush();
be2823bc
CP
40}
41
7938a2c3
CP
42static void triggerdbloaded(void *arg) {
43 triggerhook(HOOK_TRUSTS_DB_LOADED, NULL);
44}
45
4be1aaf2 46static void loadcomplete(void) {
bf5b66e5
CP
47 /* error has already been shown */
48 if(loaderror)
49 return;
2b6e02e2 50
bf5b66e5 51 trustsdbloaded = 1;
4be1aaf2
CP
52 flushschedule = schedulerecurring(time(NULL) + 300, 0, 300, flushdatabase, NULL);
53
7938a2c3 54 scheduleoneshot(time(NULL), triggerdbloaded, NULL);
be2823bc
CP
55}
56
be2823bc
CP
57static void loadhosts_data(const DBAPIResult *result, void *tag) {
58 if(!result) {
59 loaderror = 1;
60 return;
61 }
62
63 if(!result->success) {
64 Error("trusts", ERR_ERROR, "Error loading hosts table.");
65 loaderror = 1;
66
67 result->clear(result);
68 return;
69 }
70
9bf9e8a1 71 if(result->fields != 5) {
be2823bc
CP
72 Error("trusts", ERR_ERROR, "Wrong number of fields in hosts table.");
73 loaderror = 1;
74
75 result->clear(result);
76 return;
77 }
78
79 while(result->next(result)) {
9bf9e8a1 80 unsigned int groupid, id;
5ada3782 81 char *host;
4be1aaf2 82 unsigned int maxusage, lastseen;
be2823bc
CP
83 trustgroup *tg;
84
9bf9e8a1
CP
85 id = strtoul(result->get(result, 0), NULL, 10);
86 if(id > thmaxid)
87 thmaxid = id;
88
89 groupid = strtoul(result->get(result, 1), NULL, 10);
be2823bc
CP
90
91 tg = tg_getbyid(groupid);
92 if(!tg) {
93 Error("trusts", ERR_WARNING, "Orphaned trust group host: %d", groupid);
94 continue;
95 }
96
9bf9e8a1
CP
97 /* NOTE: 2 is at the bottom */
98 maxusage = strtoul(result->get(result, 3), NULL, 10);
99 lastseen = (time_t)strtoul(result->get(result, 4), NULL, 10);
100 host = result->get(result, 2);
be2823bc 101
9bf9e8a1 102 if(!th_add(tg, id, host, maxusage, lastseen))
5ada3782 103 Error("trusts", ERR_WARNING, "Error adding host to trust %d: %s", groupid, host);
be2823bc
CP
104 }
105
106 result->clear(result);
107
bf5b66e5 108 loadcomplete();
be2823bc
CP
109}
110
111static void loadhosts_fini(const DBAPIResult *result, void *tag) {
9bf9e8a1 112 Error("trusts", ERR_INFO, "Finished loading hosts, maximum id: %d", thmaxid);
be2823bc
CP
113}
114
115static void loadgroups_data(const DBAPIResult *result, void *tag) {
116 if(!result) {
117 loaderror = 1;
118 return;
119 }
120
121 if(!result->success) {
122 Error("trusts", ERR_ERROR, "Error loading group table.");
123 loaderror = 1;
124
125 result->clear(result);
126 return;
127 }
128
129 if(result->fields != 12) {
130 Error("trusts", ERR_ERROR, "Wrong number of fields in groups table.");
131 loaderror = 1;
132
133 result->clear(result);
134 return;
135 }
136
137 while(result->next(result)) {
138 unsigned int id;
5ada3782 139 sstring *name, *createdby, *contact, *comment;
4be1aaf2 140 unsigned int trustedfor, mode, maxperident, maxusage;
5ada3782 141 time_t expires, lastseen, lastmaxuserreset;
be2823bc
CP
142
143 id = strtoul(result->get(result, 0), NULL, 10);
144 if(id > tgmaxid)
145 tgmaxid = id;
146
5ada3782
CP
147 name = getsstring(result->get(result, 1), TRUSTNAMELEN);
148 trustedfor = strtoul(result->get(result, 2), NULL, 10);
149 mode = atoi(result->get(result, 3));
150 maxperident = strtoul(result->get(result, 4), NULL, 10);
4be1aaf2 151 maxusage = strtoul(result->get(result, 5), NULL, 10);
5ada3782
CP
152 expires = (time_t)strtoul(result->get(result, 6), NULL, 10);
153 lastseen = (time_t)strtoul(result->get(result, 7), NULL, 10);
154 lastmaxuserreset = (time_t)strtoul(result->get(result, 8), NULL, 10);
155 createdby = getsstring(result->get(result, 9), NICKLEN);
156 contact = getsstring(result->get(result, 10), CONTACTLEN);
157 comment = getsstring(result->get(result, 11), COMMENTLEN);
158
159 if(name && createdby && contact && comment) {
4be1aaf2 160 if(!tg_add(id, name->content, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxuserreset, createdby->content, contact->content, comment->content))
5ada3782
CP
161 Error("trusts", ERR_WARNING, "Error adding trustgroup %d: %s", id, name->content);
162 } else {
163 Error("trusts", ERR_ERROR, "Error allocating sstring in group loader, id: %d", id);
164 }
be2823bc 165
5ada3782
CP
166 freesstring(name);
167 freesstring(createdby);
168 freesstring(contact);
169 freesstring(comment);
be2823bc
CP
170 }
171
172 result->clear(result);
173}
174
175static void loadgroups_fini(const DBAPIResult *result, void *tag) {
176 Error("trusts", ERR_INFO, "Finished loading groups, maximum id: %d.", tgmaxid);
2b6e02e2
CP
177}
178
bf5b66e5 179int trusts_loaddb(void) {
bf5b66e5 180 if(!trustsdb) {
83bccee3
CP
181 trustsdb = dbapi2open(NULL, "trusts");
182 if(!trustsdb) {
183 Error("trusts", ERR_WARNING, "Unable to connect to db -- not loaded.");
184 return 0;
185 }
bf5b66e5
CP
186 }
187
188 createtrusttables(0);
2b6e02e2 189
be2823bc 190 loaderror = 0;
2b6e02e2 191
be2823bc
CP
192 trustsdb->loadtable(trustsdb, NULL, loadgroups_data, loadgroups_fini, NULL, "groups");
193 trustsdb->loadtable(trustsdb, NULL, loadhosts_data, loadhosts_fini, NULL, "hosts");
bf5b66e5
CP
194
195 return 1;
2b6e02e2
CP
196}
197
83bccee3 198void trusts_closedb(int closeconnection) {
2b6e02e2
CP
199 if(!trustsdb)
200 return;
201
83bccee3
CP
202 if(flushschedule) {
203 deleteschedule(flushschedule, flushdatabase, NULL);
204 flushschedule = NULL;
205
206 flushdatabase(NULL);
207 }
4be1aaf2 208
bf5b66e5 209 trusts_freeall();
83bccee3 210
bf5b66e5 211 trustsdbloaded = 0;
9bf9e8a1 212 thmaxid = tgmaxid = 0;
bf5b66e5 213
83bccee3
CP
214 if(closeconnection) {
215 trustsdb->close(trustsdb);
216 trustsdb = NULL;
217 }
218
219 triggerhook(HOOK_TRUSTS_DB_CLOSED, NULL);
2b6e02e2 220}
4be1aaf2
CP
221
222void th_dbupdatecounts(trusthost *th) {
1b5bf791 223 trustsdb->squery(trustsdb, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuu", "hosts", th->lastseen, th->maxusage, th->id);
4be1aaf2
CP
224}
225
226void tg_dbupdatecounts(trustgroup *tg) {
1b5bf791 227 trustsdb->squery(trustsdb, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuu", "groups", tg->lastseen, tg->maxusage, tg->id);
4be1aaf2 228}