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