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