]>
Commit | Line | Data |
---|---|---|
e2527cba P |
1 | #include "../nick/nick.h" |
2 | #include "../core/error.h" | |
3 | #include "../lib/irc_string.h" | |
4 | #include "../core/schedule.h" | |
5 | #include <stdlib.h> | |
6 | ||
7 | #include "trusts.h" | |
8 | ||
9 | int trustdb_loaded = 0; | |
10 | ||
4f2ce270 P |
11 | static void trusts_dbtriggerdbloaded(void *arg); |
12 | ||
e2527cba P |
13 | int trusts_load_db(void) { |
14 | if(!dbconnected()) { | |
15 | Error("trusts", ERR_STOP, "Could not connect to database."); | |
16 | return 0; | |
17 | } | |
18 | ||
19 | if(trustdb_loaded) | |
20 | trusts_cleanup_db(); | |
21 | ||
22 | trustdb_loaded = 1; | |
23 | ||
24 | trusts_create_tables(); | |
25 | ||
26 | dbasyncquery(trusts_loadtrustgroups, NULL, | |
27 | "SELECT trustid,maxusage,maxclones,maxperident,maxperip,enforceident,startdate,lastused,expires,owneruserid,type,created,modified FROM trusts.groups WHERE enddate = 0"); | |
4f2ce270 P |
28 | dbasyncquery(trusts_loadtrustgroupsmax, NULL, |
29 | "SELECT max(trustid) from trusts.groups"); | |
30 | ||
e2527cba P |
31 | dbasyncquery(trusts_loadtrusthosts, NULL, |
32 | "SELECT * FROM trusts.hosts WHERE enddate = 0"); | |
4f2ce270 P |
33 | dbasyncquery(trusts_loadtrusthostsmax, NULL, |
34 | "SELECT max(hostid) FROM trusts.hosts"); | |
35 | ||
e2527cba P |
36 | dbasyncquery(trusts_loadtrustblocks, NULL, |
37 | "SELECT * FROM trusts.blocks"); | |
38 | ||
39 | return 1; | |
40 | } | |
41 | ||
42 | void trusts_create_tables(void) { | |
43 | dbattach("trusts"); | |
44 | dbcreatequery( | |
45 | "CREATE TABLE trusts.groups (" | |
46 | "trustid INT4 NOT NULL PRIMARY KEY," | |
47 | "startdate INT4 NOT NULL," | |
48 | "enddate INT4 NOT NULL," | |
49 | "owneruserid INT4," | |
50 | "maxusage INT4 NOT NULL," | |
51 | "enforceident INT2 NOT NULL," | |
52 | "type INT2," | |
53 | "maxclones INT4 NOT NULL," | |
54 | "maxperident INT4," | |
55 | "maxperip INT4," | |
56 | "expires INT4," | |
57 | "lastused INT4," | |
58 | "modified INT4," | |
59 | "created INT4" | |
60 | ") WITHOUT OIDS;" | |
61 | ); | |
62 | ||
63 | dbcreatequery( | |
64 | "CREATE TABLE trusts.hosts (" | |
65 | "hostid INT4 NOT NULL PRIMARY KEY," | |
66 | "trustid INT4 NOT NULL," | |
67 | "startdate INT4 NOT NULL," | |
68 | "enddate INT4," | |
4f2ce270 | 69 | "host VARCHAR NOT NULL," |
e2527cba P |
70 | "maxusage INT4 NOT NULL," |
71 | "lastused INT4," | |
72 | "expires INT4 NOT NULL," | |
73 | "modified INT4," | |
74 | "created INT4" | |
75 | ") WITHOUT OIDS;" | |
76 | ); | |
77 | ||
78 | dbcreatequery( | |
79 | "CREATE TABLE trusts.blocks (" | |
80 | "blockid INT4 NOT NULL PRIMARY KEY," | |
4f2ce270 | 81 | "block VARCHAR NOT NULL," |
e2527cba P |
82 | "owner INT4," |
83 | "expires INT4," | |
84 | "startdate INT4," | |
85 | "reason_private VARCHAR," | |
86 | "reason_public VARCHAR" | |
87 | ") WITHOUT OIDS;" | |
88 | ); | |
89 | ||
90 | dbcreatequery( | |
91 | "CREATE TABLE trusts.log (" | |
92 | "logid SERIAL NOT NULL PRIMARY KEY," | |
93 | "trustid INT4 NOT NULL," | |
94 | "timestamp INT4 NOT NULL," | |
95 | "userid INT4 NOT NULL," | |
96 | "type INT2," | |
97 | "message VARCHAR" | |
98 | ") WITHOUT OIDS;" | |
99 | ); | |
100 | } | |
101 | ||
102 | void trusts_cleanup_db(void) { | |
103 | dbdetach("trusts"); | |
104 | } | |
105 | ||
106 | void trusts_loadtrustgroups(DBConn *dbconn, void *arg) { | |
107 | DBResult *pgres = dbgetresult(dbconn); | |
108 | int rows=0; | |
109 | trustgroup_t *t; | |
110 | ||
111 | if(!dbquerysuccessful(pgres)) { | |
112 | Error("trusts", ERR_ERROR, "Error loading trustgroup list."); | |
113 | dbclear(pgres); | |
114 | return; | |
115 | } | |
116 | ||
117 | trusts_lasttrustgroupid = 1; | |
118 | ||
119 | while(dbfetchrow(pgres)) { | |
120 | ||
121 | t = createtrustgroupfromdb( | |
122 | /*id*/ strtoul(dbgetvalue(pgres,0),NULL,10), | |
123 | /*maxusage*/ strtoul(dbgetvalue(pgres,1),NULL,10), | |
124 | /*maxclones*/ strtoul(dbgetvalue(pgres,2),NULL,10), | |
125 | /*maxperident*/ strtoul(dbgetvalue(pgres,3),NULL,10), | |
126 | /*maxperip*/ strtoul(dbgetvalue(pgres,4),NULL,10), | |
127 | /*TODOTYPE*/ /*enforceident*/strtoul(dbgetvalue(pgres,5),NULL,10), | |
128 | /*startdate*/ strtoul(dbgetvalue(pgres,6),NULL,10), | |
129 | /*lastused*/ strtoul(dbgetvalue(pgres,7),NULL,10), | |
130 | /*expire*/ strtoul(dbgetvalue(pgres,8),NULL,10), | |
131 | /*ownerid*/ strtoul(dbgetvalue(pgres,9),NULL,10), | |
132 | /*type*/ strtoul(dbgetvalue(pgres,10),NULL,10), | |
133 | /*created*/ strtoul(dbgetvalue(pgres,11),NULL,10), | |
134 | /*modified*/ strtoul(dbgetvalue(pgres,12),NULL,10) | |
135 | ); | |
ef9fab2e P |
136 | if (!t) { |
137 | Error("trusts", ERR_ERROR, "Error loading trustblock."); | |
138 | return; | |
139 | } | |
140 | ||
e2527cba P |
141 | if(t->id > trusts_lasttrustgroupid) |
142 | trusts_lasttrustgroupid = t->id; | |
143 | ||
144 | rows++; | |
145 | } | |
146 | ||
4f2ce270 P |
147 | Error("trusts",ERR_INFO,"Loaded %d trusts (highest ID was %lu)",rows,trusts_lasttrustgroupid); |
148 | ||
149 | dbclear(pgres); | |
150 | } | |
151 | ||
152 | void trusts_loadtrustgroupsmax(DBConn *dbconn, void *arg) { | |
153 | DBResult *pgres = dbgetresult(dbconn); | |
154 | unsigned long trustmax = 0; | |
155 | ||
156 | if(!dbquerysuccessful(pgres)) { | |
157 | Error("trusts", ERR_ERROR, "Error loading trustgroup max."); | |
158 | dbclear(pgres); | |
159 | return; | |
160 | } | |
161 | ||
162 | while(dbfetchrow(pgres)) { | |
163 | trustmax = strtoul(dbgetvalue(pgres,0),NULL,10); | |
164 | } | |
165 | ||
166 | if ( trustmax < trusts_lasttrustgroupid ) { | |
167 | Error("trusts",ERR_INFO,"trust max failed - %lu, %lu", trustmax, trusts_lasttrustgroupid); | |
168 | } | |
169 | trusts_lasttrustgroupid = trustmax; | |
170 | ||
171 | Error("trusts",ERR_INFO,"Loaded Trust Max %lu", trusts_lasttrustgroupid); | |
e2527cba P |
172 | |
173 | dbclear(pgres); | |
174 | } | |
175 | ||
176 | void trusts_loadtrusthosts(DBConn *dbconn, void *arg) { | |
177 | DBResult *pgres = dbgetresult(dbconn); | |
178 | int rows=0; | |
179 | trusthost_t *t; | |
180 | trustgroup_t *tg; | |
181 | patricia_node_t *node; | |
182 | struct irc_in_addr sin; | |
183 | unsigned char bits; | |
184 | ||
185 | if(!dbquerysuccessful(pgres)) { | |
186 | Error("trusts", ERR_ERROR, "Error loading trusthost list."); | |
187 | dbclear(pgres); | |
188 | return; | |
189 | } | |
190 | ||
191 | trusts_lasttrusthostid = 1; | |
192 | ||
193 | while(dbfetchrow(pgres)) { | |
194 | /*node*/ | |
4f2ce270 P |
195 | if( ipmask_parse(dbgetvalue(pgres,4), &sin, &bits) == 0) { |
196 | Error("trusts", ERR_ERROR, "Failed to parse trusthost: %s", dbgetvalue(pgres,4)); | |
197 | continue; | |
198 | } | |
199 | ||
e2527cba P |
200 | node = refnode(iptree, &sin, bits); |
201 | ||
202 | /*tg*/ | |
203 | int tgid = strtoul(dbgetvalue(pgres,1),NULL,10); | |
204 | tg=findtrustgroupbyid(tgid); | |
205 | if (!tg) { | |
206 | Error("trusts", ERR_ERROR, "Error loading trusthosts - invalid group: %d.", tgid); | |
4f2ce270 P |
207 | |
208 | /* update last hostid - although we probably should fail here more loudly */ | |
209 | if(t->id > trusts_lasttrusthostid) | |
210 | trusts_lasttrusthostid = t->id; | |
211 | ||
e2527cba P |
212 | continue; |
213 | } | |
214 | ||
215 | t = createtrusthostfromdb( | |
216 | /*id*/ strtoul(dbgetvalue(pgres,0),NULL,10), | |
217 | /*node*/ node, | |
218 | /*startdate*/ strtoul(dbgetvalue(pgres,2),NULL,10), | |
219 | /*lastused*/ strtoul(dbgetvalue(pgres,6),NULL,10), | |
220 | /*expire*/ strtoul(dbgetvalue(pgres,7),NULL,10), | |
221 | /*maxusage*/ strtoul(dbgetvalue(pgres,5),NULL,10), | |
222 | /*trustgroup*/ tg, | |
223 | /*created*/ strtoul(dbgetvalue(pgres,9),NULL,10), | |
224 | /*modified*/ strtoul(dbgetvalue(pgres,8),NULL,10) | |
225 | ); | |
ef9fab2e P |
226 | if (!t) { |
227 | Error("trusts", ERR_ERROR, "Error loading trusthost."); | |
228 | return; | |
229 | } | |
e2527cba P |
230 | node = 0; |
231 | if(t->id > trusts_lasttrusthostid) | |
232 | trusts_lasttrusthostid = t->id; | |
233 | ||
234 | trusthost_addcounters(t); | |
235 | rows++; | |
236 | } | |
237 | ||
238 | Error("trusts",ERR_INFO,"Loaded %d trusthosts (highest ID was %lu)",rows,trusts_lasttrusthostid); | |
239 | ||
240 | dbclear(pgres); | |
241 | } | |
242 | ||
4f2ce270 P |
243 | void trusts_loadtrusthostsmax(DBConn *dbconn, void *arg) { |
244 | DBResult *pgres = dbgetresult(dbconn); | |
245 | unsigned long trustmax = 0; | |
246 | ||
247 | if(!dbquerysuccessful(pgres)) { | |
248 | Error("trusts", ERR_ERROR, "Error loading trusthost max."); | |
249 | dbclear(pgres); | |
250 | return; | |
251 | } | |
252 | ||
253 | while(dbfetchrow(pgres)) { | |
254 | trustmax = strtoul(dbgetvalue(pgres,0),NULL,10); | |
255 | } | |
256 | ||
257 | if ( trustmax < trusts_lasttrusthostid ) { | |
258 | Error("trusts", ERR_FATAL, "trusthost max failed - %lu, %lu", trustmax, trusts_lasttrusthostid); | |
259 | } | |
260 | trusts_lasttrusthostid = trustmax; | |
261 | ||
262 | Error("trusts",ERR_INFO,"Loaded Trust Host Max %lu", trusts_lasttrusthostid); | |
263 | ||
264 | dbclear(pgres); | |
265 | } | |
266 | ||
e2527cba P |
267 | void trusts_loadtrustblocks(DBConn *dbconn, void *arg) { |
268 | DBResult *pgres = dbgetresult(dbconn); | |
269 | int rows=0; | |
270 | trustblock_t *t; | |
ef9fab2e P |
271 | struct irc_in_addr sin; |
272 | unsigned char bits; | |
273 | patricia_node_t *node; | |
e2527cba P |
274 | |
275 | if(!dbquerysuccessful(pgres)) { | |
276 | Error("trusts", ERR_ERROR, "Error loading trustblock list."); | |
277 | dbclear(pgres); | |
278 | return; | |
279 | } | |
280 | ||
281 | trusts_lasttrustblockid = 1; | |
282 | ||
283 | while(dbfetchrow(pgres)) { | |
ef9fab2e | 284 | /*node*/ |
4f2ce270 P |
285 | if( ipmask_parse(dbgetvalue(pgres,1), &sin, &bits) == 0) { |
286 | Error("trusts", ERR_ERROR, "Failed to parse trustblock: %s", dbgetvalue(pgres,1)); | |
287 | continue; | |
288 | } | |
ef9fab2e P |
289 | node = refnode(iptree, &sin, bits); |
290 | ||
291 | t = createtrustblockfromdb( | |
292 | /* id */ strtoul(dbgetvalue(pgres,0),NULL,10), | |
293 | /* node */ node, | |
294 | /* ownerid */ strtoul(dbgetvalue(pgres,2),NULL,10), | |
295 | /* expire */ strtoul(dbgetvalue(pgres,3),NULL,10), | |
296 | /* startdate*/ strtoul(dbgetvalue(pgres,4),NULL,10), | |
297 | /* reason_private */ dbgetvalue(pgres,5), | |
298 | /* reason_public */ dbgetvalue(pgres,6) | |
299 | ); | |
300 | if (!t) { | |
301 | Error("trusts", ERR_ERROR, "Error loading trustblock."); | |
302 | return; | |
303 | } | |
304 | ||
305 | node->exts[tgb_ext] = t; | |
e2527cba P |
306 | |
307 | if(t->id > trusts_lasttrustblockid) | |
308 | trusts_lasttrustblockid = t->id; | |
309 | ||
e2527cba P |
310 | rows++; |
311 | } | |
312 | ||
313 | Error("trusts",ERR_INFO,"Loaded %d trustblocks (highest ID was %lu)",rows,trusts_lasttrustblockid); | |
314 | ||
315 | dbclear(pgres); | |
9a8ffb84 P |
316 | |
317 | trusts_loaded = 1; | |
4f2ce270 | 318 | scheduleoneshot(time(NULL), trusts_dbtriggerdbloaded, NULL); |
e2527cba P |
319 | } |
320 | ||
4f2ce270 P |
321 | static void trusts_dbtriggerdbloaded(void *arg) { |
322 | triggerhook(HOOK_TRUSTS_DBLOADED, NULL); | |
323 | } | |
e2527cba P |
324 | |
325 | /* trust group */ | |
326 | void trustsdb_addtrustgroup(trustgroup_t *t) { | |
327 | dbquery("INSERT INTO trusts.groups (trustid,startdate,enddate,owneruserid,maxusage,enforceident,type,maxclones,maxperident,maxperip,expires,lastused,modified,created ) VALUES (%lu,%lu,0,%lu,%lu,%d,%d,%lu,%lu,%d,%lu,%lu,%lu,%lu )", 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 ); | |
328 | } | |
329 | ||
330 | void trustsdb_updatetrustgroup(trustgroup_t *t) { | |
331 | dbquery("UPDATE trusts.groups SET startdate=%lu,owneruserid=%lu,maxusage=%lu,enforceident=%d,type=%d,maxclones=%lu, maxperident=%lu,maxperip=%d,expires=%lu,lastused=%lu,modified=%lu,created=%lu WHERE trustid = %lu", 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); | |
332 | } | |
333 | ||
334 | void trustsdb_deletetrustgroup(trustgroup_t *t) { | |
335 | dbquery("UPDATE trusts.groups SET enddate = %jd WHERE trustid = %lu", (intmax_t)getnettime(), t->id); | |
336 | } | |
337 | ||
338 | /* trust host */ | |
339 | void trustsdb_addtrusthost(trusthost_t *th) { | |
340 | dbquery("INSERT INTO trusts.hosts (hostid,trustid,startdate,enddate,host,maxusage,lastused,expires,modified,created) VALUES (%lu,%lu,%lu,0,'%s/%d',%lu,%lu,%lu,%lu,%lu)", 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); | |
341 | } | |
342 | ||
343 | void trustsdb_updatetrusthost(trusthost_t *th) { | |
344 | dbquery("UPDATE trusts.hosts SET hostid=%lu,trustid=%lu,startdate=%lu,host='%s/%d',maxusage=%lu,lastused=%lu,expires=%lu,modified=%lu,created=%lu", 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); | |
345 | } | |
346 | ||
347 | void trustsdb_deletetrusthost(trusthost_t *th) { | |
348 | dbquery("UPDATE trusts.hosts SET enddate = %jd WHERE hostid = %lu", (intmax_t)getnettime(), th->id); | |
349 | } | |
350 | ||
351 | /* trust block */ | |
352 | void trustsdb_addtrustblock(trustblock_t *tb) { | |
ef9fab2e | 353 | dbquery("INSERT INTO trusts.blocks ( blockid,block,owner,expires,startdate,reason_private,reason_public) VALUES (%lu,'%s/%d',%lu,%lu,%lu,'%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 ? tb->reason_private->content : "", tb->reason_public ? tb->reason_public->content : ""); |
e2527cba P |
354 | } |
355 | ||
356 | void trustsdb_updatetrustblock(trustblock_t *tb) { | |
ef9fab2e P |
357 | char escprivate[2*512+1]; |
358 | char escpublic[2*512+1]; | |
359 | ||
360 | dbescapestring(escprivate,tb->reason_private ? tb->reason_private->content : "", strlen(tb->reason_private ? tb->reason_private->content : "")); | |
361 | dbescapestring(escpublic,tb->reason_public ? tb->reason_public->content : "", strlen(tb->reason_public ? tb->reason_public->content : "")); | |
362 | ||
363 | dbquery("UPDATE trusts.blocks SET block='%s/%d',owner=%lu,expires=%lu,startdate=%lu,reason_private='%s',reason_public='%s' WHERE blockid=%lu", IPtostr(tb->node->prefix->sin), irc_bitlen(&(tb->node->prefix->sin),tb->node->prefix->bitlen), tb->ownerid, tb->expire, tb->startdate, escprivate, escpublic, tb->id); | |
e2527cba P |
364 | } |
365 | ||
366 | void trustsdb_deletetrustblock(trustblock_t *tb) { | |
367 | dbquery("DELETE from trusts.blocks WHERE blockid = %lu", tb->id); | |
368 | } | |
369 | ||
370 | /* trust log */ | |
371 | /* logid, trustid, timestamp, userid, type, message */ | |
372 | /* @@@ TODO */ | |
373 | ||
374 | void trustsdb_logmessage(trustgroup_t *tg, unsigned long userid, int type, char *message) { | |
375 | /* maximum length of a trustlog message is ircd max length */ | |
376 | char escmessage[2*512+1]; | |
377 | ||
378 | dbescapestring(escmessage,message, strlen(message)); | |
379 | dbquery("INSERT INTO trusts.log (trustid, timestamp, userid, type, message) VALUES ( %lu, %lu, %lu, %d, '%s')", tg->id, getnettime(), userid, type, escmessage); | |
380 | } | |
4f2ce270 P |
381 | |
382 | void trust_dotrustlog_real(DBConn *dbconn, void *arg) { | |
383 | nick *np=getnickbynumeric((unsigned long)arg); | |
384 | DBResult *pgres; | |
385 | unsigned long logid, trustid, userid, type; | |
386 | time_t timestamp; | |
387 | char *message; | |
388 | char timebuf[30]; | |
389 | int header=0; | |
390 | ||
391 | if(!dbconn) | |
392 | return; | |
393 | ||
394 | pgres=dbgetresult(dbconn); | |
395 | ||
396 | if (!dbquerysuccessful(pgres)) { | |
397 | Error("trusts", ERR_ERROR, "Error loading trusts log data."); | |
398 | dbclear(pgres); | |
399 | return; | |
400 | } | |
401 | ||
402 | if (dbnumfields(pgres) != 6) { | |
403 | Error("trusts", ERR_ERROR, "trusts log data format error."); | |
404 | dbclear(pgres); | |
405 | return; | |
406 | } | |
407 | ||
408 | if (!np) { | |
409 | dbclear(pgres); | |
410 | return; | |
411 | } | |
412 | ||
413 | while(dbfetchrow(pgres)) { | |
414 | logid=strtoul(dbgetvalue(pgres, 0), NULL, 10); | |
415 | trustid=strtoul(dbgetvalue(pgres, 1), NULL, 10); | |
416 | timestamp=strtoul(dbgetvalue(pgres, 2), NULL, 10); | |
417 | userid=strtoul(dbgetvalue(pgres, 3), NULL, 10); | |
418 | type=strtoul(dbgetvalue(pgres, 4), NULL, 10); | |
419 | message=dbgetvalue(pgres, 5); | |
420 | ||
421 | if (!header) { | |
422 | header=1; | |
423 | controlreply(np, "Display trustlog for trust %lu", trustid); | |
424 | controlreply(np, "ID Time OperID Type Message"); | |
425 | } | |
426 | strftime(timebuf, 30, "%d/%m/%y %H:%M", localtime(×tamp)); | |
427 | controlreply(np, "%-3lu %s %-7lu %-2lu %s", logid, timebuf, userid, type, message); | |
428 | } | |
429 | ||
430 | if (!header) { | |
431 | controlreply(np, "No trust log entries found."); | |
432 | } else { | |
433 | controlreply(np, "End Of List."); | |
434 | } | |
435 | dbclear(pgres); | |
436 | } | |
437 | ||
438 | ||
439 | void trustsdb_retrievetrustlog(nick *np, unsigned int trustid, time_t starttime) { | |
440 | dbasyncquery(trust_dotrustlog_real, (void *)np->numeric, "SELECT * FROM trusts.log WHERE trustid=%u AND timestamp>%lu order by timestamp desc limit 1000", trustid, starttime); | |
441 | Error("trusts", ERR_ERROR, "SELECT * FROM trusts.log WHERE trustid=%u AND timestamp>%lu order by timestamp desc limit 1000", trustid, starttime); | |
442 | } |