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