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