]>
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 | ||
106 | static int trusts_cmdtrustforceresync(void *source, int argc, char **argv) { | |
107 | nick *np = source; | |
108 | ||
109 | controlreply(np, "Resync in progress. . ."); | |
110 | replicate(1); | |
111 | controlreply(np, "Resync complete."); | |
112 | ||
113 | return CMD_OK; | |
114 | } | |
115 | ||
116 | static int commandsregistered, loaded; | |
117 | static void __dbloaded(int hooknum, void *arg) { | |
35449aa5 CP |
118 | if(commandsregistered) |
119 | return; | |
82a316e7 | 120 | |
35449aa5 CP |
121 | commandsregistered = 1; |
122 | ||
82a316e7 CP |
123 | xsb_addcommand("trrequeststart", 0, xsb_tr_requeststart); |
124 | ||
125 | registerhook(HOOK_TRUSTS_ADDGROUP, groupadded); | |
126 | registerhook(HOOK_TRUSTS_DELGROUP, groupremoved); | |
127 | registerhook(HOOK_TRUSTS_ADDHOST, hostadded); | |
128 | registerhook(HOOK_TRUSTS_DELGROUP, hostremoved); | |
129 | ||
130 | /* we've just reloaded */ | |
131 | /* if we're not online, no problem, other nodes will ask us individually */ | |
132 | if(trusts_fullyonline()) | |
133 | replicate(1); | |
134 | ||
135 | registercontrolhelpcmd("trustforceresync", NO_DEVELOPER, 0, trusts_cmdtrustforceresync, "Usage: trustforceresync"); | |
35449aa5 CP |
136 | } |
137 | ||
82a316e7 | 138 | static void __dbclosed(int hooknum, void *arg) { |
35449aa5 CP |
139 | if(!commandsregistered) |
140 | return; | |
141 | commandsregistered = 0; | |
142 | ||
82a316e7 CP |
143 | xsb_delcommand("trrequeststart", xsb_tr_requeststart); |
144 | ||
145 | registerhook(HOOK_TRUSTS_ADDGROUP, groupadded); | |
146 | registerhook(HOOK_TRUSTS_DELGROUP, groupremoved); | |
147 | registerhook(HOOK_TRUSTS_ADDHOST, hostadded); | |
148 | registerhook(HOOK_TRUSTS_DELGROUP, hostremoved); | |
149 | ||
150 | deregistercontrolcmd("trustforceresync", trusts_cmdtrustforceresync); | |
35449aa5 CP |
151 | } |
152 | ||
153 | void _init(void) { | |
82a316e7 CP |
154 | sstring *m; |
155 | ||
156 | m = getconfigitem("trusts", "master"); | |
157 | if(!m || (atoi(m->content) != 1)) { | |
158 | Error("trusts_master", ERR_ERROR, "Not a master server, not loaded."); | |
159 | return; | |
160 | } | |
35449aa5 | 161 | |
82a316e7 CP |
162 | loaded = 1; |
163 | ||
164 | registerhook(HOOK_TRUSTS_DB_LOADED, __dbloaded); | |
165 | registerhook(HOOK_TRUSTS_DB_CLOSED, __dbclosed); | |
166 | ||
167 | if(trustsdbloaded) { | |
168 | /* this will force a sync if we're online */ | |
169 | /* if we're not we'll be asked to sync when we come online */ | |
170 | __dbloaded(0, NULL); | |
171 | } else { | |
172 | if(!trusts_loaddb()) | |
173 | Error("trusts_master", ERR_ERROR, "Unable to load database."); | |
174 | ||
175 | /* do nothing else, other servers will ask us when we come online */ | |
176 | /* also the __dbloaded hook will be triggered */ | |
177 | } | |
35449aa5 CP |
178 | } |
179 | ||
180 | void _fini(void) { | |
82a316e7 CP |
181 | if(!loaded) |
182 | return; | |
183 | ||
184 | deregisterhook(HOOK_TRUSTS_DB_LOADED, __dbloaded); | |
185 | deregisterhook(HOOK_TRUSTS_DB_CLOSED, __dbclosed); | |
186 | ||
187 | trusts_closedb(0); | |
35449aa5 | 188 | |
82a316e7 | 189 | __dbclosed(0, NULL); |
35449aa5 | 190 | } |