]>
Commit | Line | Data |
---|---|---|
82a316e7 CP |
1 | #include <stdlib.h> |
2 | #include <stdarg.h> | |
3 | #include <stdio.h> | |
4 | #include <string.h> | |
35449aa5 | 5 | #include "../core/hooks.h" |
82a316e7 CP |
6 | #include "../core/config.h" |
7 | #include "../core/error.h" | |
8 | #include "../lib/sha1.h" | |
9 | #include "../lib/hmac.h" | |
10 | #include "../xsb/xsb.h" | |
11 | #include "../control/control.h" | |
35449aa5 CP |
12 | #include "trusts.h" |
13 | ||
82a316e7 CP |
14 | static void broadcast(SHA1_CTX *c, unsigned int replicationid, unsigned int lineno, char *command, char *format, ...) { |
15 | char buf[512], buf2[600]; | |
16 | va_list va; | |
17 | int len; | |
18 | ||
19 | va_start(va, format); | |
20 | vsnprintf(buf, sizeof(buf), format, va); | |
21 | va_end(va); | |
22 | ||
23 | len = snprintf(buf2, sizeof(buf2), "%u %u %s", replicationid, lineno, buf); | |
24 | xsb_broadcast(command, NULL, "%s", buf2); | |
25 | ||
26 | if(len > (sizeof(buf2) - 1)) | |
27 | len = sizeof(buf2) - 1; | |
28 | if(len < 0) | |
29 | len = 0; | |
30 | ||
31 | buf2[len] = '\n'; | |
32 | ||
33 | SHA1Update(c, (unsigned char *)buf2, len + 1); | |
34 | } | |
35 | ||
36 | static void replicate(int forced) { | |
37 | SHA1_CTX s; | |
38 | unsigned int lineno, lines; | |
39 | unsigned char digest[SHA1_DIGESTSIZE]; | |
40 | char digestbuf[SHA1_DIGESTSIZE*2+1]; | |
41 | static unsigned int replicationid = 0; | |
42 | trustgroup *tg; | |
43 | trusthost *th; | |
44 | ||
45 | if(++replicationid > 10000) | |
46 | replicationid = 1; | |
47 | ||
48 | for(lines=2,tg=tglist;tg;tg=tg->next) { | |
49 | lines++; | |
50 | for(th=tg->hosts;th;th=th->next) | |
51 | lines++; | |
52 | } | |
53 | ||
54 | SHA1Init(&s); | |
55 | lineno = 1; | |
56 | broadcast(&s, replicationid, lineno++, "trinit", "%d %u", forced, lines); | |
57 | ||
58 | for(tg=tglist;tg;tg=tg->next) { | |
59 | broadcast(&s, replicationid, lineno++, "trdata", "G %s", dumptg(tg, 0)); | |
60 | ||
61 | for(th=tg->hosts;th;th=th->next) | |
62 | broadcast(&s, replicationid, lineno++, "trdata", "H %s", dumpth(th, 0)); | |
63 | } | |
64 | ||
65 | SHA1Final(digest, &s); | |
66 | xsb_broadcast("trfini", NULL, "%u %u %s", replicationid, lineno, hmac_printhex(digest, digestbuf, SHA1_DIGESTSIZE)); | |
67 | } | |
68 | ||
69 | static int xsb_tr_requeststart(void *source, int argc, char **argv) { | |
70 | static time_t last = 0; | |
71 | time_t t = time(NULL); | |
72 | ||
73 | if(t - last > 5) { | |
74 | last = t; | |
75 | ||
76 | replicate(0); | |
77 | } | |
78 | ||
79 | return CMD_OK; | |
80 | } | |
81 | ||
82 | static void groupadded(int hooknum, void *arg) { | |
83 | trustgroup *tg = arg; | |
84 | ||
85 | xsb_broadcast("traddgroup", NULL, "%s", dumptg(tg, 0)); | |
86 | } | |
87 | ||
88 | static void groupremoved(int hooknum, void *arg) { | |
89 | trustgroup *tg = arg; | |
90 | ||
91 | xsb_broadcast("trdelgroup", NULL, "%u", tg->id); | |
92 | } | |
93 | ||
94 | static void hostadded(int hooknum, void *arg) { | |
95 | trusthost *th = arg; | |
96 | ||
97 | xsb_broadcast("traddhost", NULL, "%s", dumpth(th, 0)); | |
98 | } | |
99 | ||
100 | static void hostremoved(int hooknum, void *arg) { | |
101 | trusthost *th = arg; | |
102 | ||
103 | xsb_broadcast("trdelhost", NULL, "%u", th->id); | |
104 | } | |
105 | ||
2ab0a1e7 CP |
106 | static void groupmodified(int hooknum, void *arg) { |
107 | trustgroup *tg = arg; | |
108 | ||
109 | xsb_broadcast("trmodifygroup", NULL, "%s", dumptg(tg, 0)); | |
110 | } | |
111 | ||
058f68c5 GB |
112 | static void hostmodified(int hooknum, void *arg) { |
113 | trusthost *th = arg; | |
114 | ||
115 | xsb_broadcast("trmodifyhost", NULL, "%s", dumpth(th, 0)); | |
116 | } | |
117 | ||
82a316e7 CP |
118 | static int trusts_cmdtrustforceresync(void *source, int argc, char **argv) { |
119 | nick *np = source; | |
120 | ||
121 | controlreply(np, "Resync in progress. . ."); | |
122 | replicate(1); | |
123 | controlreply(np, "Resync complete."); | |
124 | ||
125 | return CMD_OK; | |
126 | } | |
127 | ||
128 | static int commandsregistered, loaded; | |
129 | static void __dbloaded(int hooknum, void *arg) { | |
35449aa5 CP |
130 | if(commandsregistered) |
131 | return; | |
82a316e7 | 132 | |
35449aa5 CP |
133 | commandsregistered = 1; |
134 | ||
82a316e7 CP |
135 | xsb_addcommand("trrequeststart", 0, xsb_tr_requeststart); |
136 | ||
137 | registerhook(HOOK_TRUSTS_ADDGROUP, groupadded); | |
138 | registerhook(HOOK_TRUSTS_DELGROUP, groupremoved); | |
139 | registerhook(HOOK_TRUSTS_ADDHOST, hostadded); | |
2999f227 | 140 | registerhook(HOOK_TRUSTS_DELHOST, hostremoved); |
2ab0a1e7 | 141 | registerhook(HOOK_TRUSTS_MODIFYGROUP, groupmodified); |
058f68c5 | 142 | registerhook(HOOK_TRUSTS_MODIFYHOST, hostmodified); |
82a316e7 CP |
143 | |
144 | /* we've just reloaded */ | |
145 | /* if we're not online, no problem, other nodes will ask us individually */ | |
146 | if(trusts_fullyonline()) | |
147 | replicate(1); | |
148 | ||
149 | registercontrolhelpcmd("trustforceresync", NO_DEVELOPER, 0, trusts_cmdtrustforceresync, "Usage: trustforceresync"); | |
35449aa5 CP |
150 | } |
151 | ||
82a316e7 | 152 | static void __dbclosed(int hooknum, void *arg) { |
35449aa5 CP |
153 | if(!commandsregistered) |
154 | return; | |
155 | commandsregistered = 0; | |
156 | ||
82a316e7 CP |
157 | xsb_delcommand("trrequeststart", xsb_tr_requeststart); |
158 | ||
159 | registerhook(HOOK_TRUSTS_ADDGROUP, groupadded); | |
160 | registerhook(HOOK_TRUSTS_DELGROUP, groupremoved); | |
161 | registerhook(HOOK_TRUSTS_ADDHOST, hostadded); | |
162 | registerhook(HOOK_TRUSTS_DELGROUP, hostremoved); | |
2ab0a1e7 | 163 | registerhook(HOOK_TRUSTS_MODIFYGROUP, groupmodified); |
058f68c5 | 164 | registerhook(HOOK_TRUSTS_MODIFYHOST, hostmodified); |
82a316e7 CP |
165 | |
166 | deregistercontrolcmd("trustforceresync", trusts_cmdtrustforceresync); | |
35449aa5 CP |
167 | } |
168 | ||
169 | void _init(void) { | |
82a316e7 CP |
170 | sstring *m; |
171 | ||
172 | m = getconfigitem("trusts", "master"); | |
173 | if(!m || (atoi(m->content) != 1)) { | |
174 | Error("trusts_master", ERR_ERROR, "Not a master server, not loaded."); | |
175 | return; | |
176 | } | |
35449aa5 | 177 | |
82a316e7 CP |
178 | loaded = 1; |
179 | ||
180 | registerhook(HOOK_TRUSTS_DB_LOADED, __dbloaded); | |
181 | registerhook(HOOK_TRUSTS_DB_CLOSED, __dbclosed); | |
182 | ||
183 | if(trustsdbloaded) { | |
184 | /* this will force a sync if we're online */ | |
185 | /* if we're not we'll be asked to sync when we come online */ | |
186 | __dbloaded(0, NULL); | |
187 | } else { | |
188 | if(!trusts_loaddb()) | |
189 | Error("trusts_master", ERR_ERROR, "Unable to load database."); | |
190 | ||
191 | /* do nothing else, other servers will ask us when we come online */ | |
192 | /* also the __dbloaded hook will be triggered */ | |
193 | } | |
35449aa5 CP |
194 | } |
195 | ||
196 | void _fini(void) { | |
82a316e7 CP |
197 | if(!loaded) |
198 | return; | |
199 | ||
200 | deregisterhook(HOOK_TRUSTS_DB_LOADED, __dbloaded); | |
201 | deregisterhook(HOOK_TRUSTS_DB_CLOSED, __dbclosed); | |
202 | ||
203 | trusts_closedb(0); | |
35449aa5 | 204 | |
82a316e7 | 205 | __dbclosed(0, NULL); |
35449aa5 | 206 | } |