]> jfr.im git - irc/quakenet/newserv.git/blame - trusts/trusts_commands.c
Implement pernodemax/nodebits for THs.
[irc/quakenet/newserv.git] / trusts / trusts_commands.c
CommitLineData
d2c08930 1#include <stdio.h>
b76fd8e6 2#include <string.h>
be2823bc 3#include "../control/control.h"
2d4ba67d 4#include "../lib/irc_string.h"
b76fd8e6 5#include "../lib/strlfunc.h"
82a316e7 6#include "../core/nsmalloc.h"
be2823bc
CP
7#include "trusts.h"
8
83bccee3
CP
9static void registercommands(int, void *);
10static void deregistercommands(int, void *);
be2823bc 11
34e3de85
GB
12void calculatespaces(int spaces, int width, char *str, char **_prebuf, char **_postbuf) {
13 static char prebuf[512], postbuf[512];
14 int spacelen;
15
16 if(spaces + 5 >= sizeof(prebuf)) {
17 prebuf[0] = prebuf[1] = '\0';
18 } else {
19 memset(prebuf, ' ', spaces);
20 prebuf[spaces] = '\0';
21 }
22
23 spacelen = width - (strlen(str) + spaces);
24 if(spacelen <= 0 || spacelen + 5 >= sizeof(postbuf)) {
25 postbuf[0] = postbuf[1] = '\0';
26 } else {
27 memset(postbuf, ' ', spacelen);
28 postbuf[spacelen] = '\0';
29 }
30
31 *_prebuf = prebuf;
32 *_postbuf = postbuf;
33}
34
35static void traverseandmark(unsigned int marker, trusthost *th) {
36 th->marker = marker;
37
38 for(th=th->children;th;th=th->nextbychild) {
39 th->marker = marker;
40 traverseandmark(marker, th);
41 }
42}
43
44static void insertth(array *parents, trusthost *th) {
45 int i;
46 trusthost **p2 = (trusthost **)(parents->content);
47
48 /* this eliminates common subtrees */
49 for(i=0;i<parents->cursi;i++)
50 if(p2[i] == th)
51 break;
52
53 if(i == parents->cursi) {
54 int pos = array_getfreeslot(parents);
55 ((trusthost **)(parents->content))[pos] = th;
56 }
57}
58
59static void marktree(array *parents, unsigned int marker, trusthost *th) {
60 trusthost *pth;
61 int parentcount = 0;
62
63 for(pth=th->parent;pth;pth=pth->next) {
64 insertth(parents, pth);
65
66 pth->marker = marker;
67 }
68
69 if(parentcount == 0)
70 insertth(parents, th);
71
72 /* sadly we need to recurse down */
73 traverseandmark(marker, th);
74}
75
76static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth) {
77 char *cidrstr, *prespacebuf, *postspacebuf, parentbuf[512];
cebc4cab 78 int mask;
34e3de85
GB
79
80 if(th->marker != marker)
81 return;
82
83 cidrstr = trusts_cidr2str(th->ip, th->mask);
84 calculatespaces(depth + 1, 20 + 1, cidrstr, &prespacebuf, &postspacebuf);
85
86 if(th->group == originalgroup) {
87 prespacebuf[0] = '>';
88
89 parentbuf[0] = '\0';
90 } else {
91 /* show the ids of other groups */
92
93 snprintf(parentbuf, sizeof(parentbuf), "%-10d %s", th->group->id, th->group->name->content);
94 }
95
cebc4cab
GB
96 /*if (!ipv6) */
97 mask = th->nodebits - 96;
98
99 controlreply(np, "%s%s%s %-10d %-10d %-21s %-15d /%-14d%s", prespacebuf, cidrstr, postspacebuf, th->count, th->maxusage, (th->count>0)?"(now)":((th->lastseen>0)?trusts_timetostr(th->lastseen):"(never)"), th->maxpernode, mask, parentbuf);
34e3de85
GB
100
101 for(th=th->children;th;th=th->nextbychild)
102 outputtree(np, marker, originalgroup, th, depth + 1);
103}
104
ee77bc7a 105static void displaygroup(nick *sender, trustgroup *tg) {
34e3de85
GB
106 trusthost *th, **p2;
107 unsigned int marker;
108 array parents;
109 int i;
ee77bc7a 110 time_t t = time(NULL);
2d4ba67d 111
d36ca89c 112 /* abusing the ternary operator a bit :( */
4b003d19 113 controlreply(sender, "Name: : %s", tg->name->content);
2d4ba67d 114 controlreply(sender, "Trusted for : %d", tg->trustedfor);
1bbe1ac3 115 controlreply(sender, "Currently using : %d", tg->count);
2d4ba67d 116 controlreply(sender, "Clients per user : %d (%senforcing ident)", tg->maxperident, tg->mode?"":"not ");
4b003d19 117 controlreply(sender, "Contact: : %s", tg->contact->content);
ad3fd5ee 118 controlreply(sender, "Expires in : %s", (tg->expires)?((tg->expires>t)?longtoduration(tg->expires - t, 2):"(the past -- BUG)"):"never");
4b003d19
CP
119 controlreply(sender, "Last changed by : %s", tg->createdby->content);
120 controlreply(sender, "Comment: : %s", tg->comment->content);
2d4ba67d 121 controlreply(sender, "ID: : %u", tg->id);
d36ca89c 122 controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":((tg->lastseen>0)?trusts_timetostr(tg->lastseen):"(never)"));
4be1aaf2 123 controlreply(sender, "Max usage : %d", tg->maxusage);
1f685425 124 controlreply(sender, "Last max reset : %s", tg->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(never)");
2d4ba67d 125
cebc4cab 126 controlreply(sender, "Host Current Max Last seen Max per Node Node Mask Group ID Group name");
dee7de1b 127
34e3de85
GB
128 marker = nextthmarker();
129 array_init(&parents, sizeof(trusthost *));
dee7de1b 130
34e3de85
GB
131 for(th=tg->hosts;th;th=th->next)
132 marktree(&parents, marker, th);
133
134 p2 = (trusthost **)(parents.content);
135 for(i=0;i<parents.cursi;i++)
136 outputtree(sender, marker, tg, p2[i], 0);
137
138 array_free(&parents);
2d4ba67d
CP
139
140 controlreply(sender, "End of list.");
ee77bc7a
CP
141}
142
143static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
144 nick *sender = source;
ee77bc7a 145 trustgroup *tg = NULL;
73653516
CP
146 int found = 0, remaining = 50;
147 char *name;
3e646f8f
GB
148 trusthost *th;
149 uint32_t ip;
150 short mask;
ee77bc7a
CP
151
152 if(cargc < 1)
153 return CMD_USAGE;
154
155 name = cargv[0];
156
73653516 157 tg = tg_strtotg(name);
ee77bc7a
CP
158
159 if(tg) {
160 displaygroup(sender, tg);
161 return CMD_OK;
162 }
163
3e646f8f
GB
164 if(trusts_parsecidr(name, &ip, &mask)) {
165 th = th_getbyhost(ip);
166
167 if(!th) {
168 controlreply(sender, "Specified IP address is not trusted.");
169 return CMD_OK;
170 }
171
172 displaygroup(sender, th->group);
173 return CMD_OK;
174 }
175
ee77bc7a 176 for(tg=tglist;tg;tg=tg->next) {
c215a421 177 if(match(name, tg->name->content))
ee77bc7a
CP
178 continue;
179
180 displaygroup(sender, tg);
181 if(--remaining == 0) {
182 controlreply(sender, "Maximum number of matches reached.");
183 return CMD_OK;
184 }
185 found = 1;
186 }
187
188 if(!found)
189 controlreply(sender, "No matches found.");
2d4ba67d
CP
190
191 return CMD_OK;
192}
193
82a316e7
CP
194static int comparetgs(const void *_a, const void *_b) {
195 const trustgroup *a = _a;
196 const trustgroup *b = _b;
197
198 if(a->id > b->id)
199 return 1;
200 if(a->id < b-> id)
201 return -1;
202 return 0;
203}
204
205static int trusts_cmdtrustdump(void *source, int argc, char **argv) {
206 trusthost *th;
207 trustgroup *tg, **atg;
208 unsigned int wanted, max, maxid, totalcount, i, groupcount, linecount;
209 nick *np = source;
210
211 if((argc < 2) || (argv[0][0] != '#'))
212 return CMD_USAGE;
213
214 wanted = atoi(&argv[0][1]);
215 max = atoi(argv[1]);
216
217 for(maxid=totalcount=0,tg=tglist;tg;tg=tg->next) {
218 if(totalcount == 0 || tg->id > maxid)
219 maxid = tg->id;
220
221 totalcount++;
222 }
223
224 if(maxid > totalcount) {
225 controlreply(np, "Start ID cannot exceed current maximum group ID (#%u)", maxid);
226 return CMD_OK;
227 }
228
229 atg = nsmalloc(POOL_TRUSTS, sizeof(trusthost *) * totalcount);
230 if(!atg) {
231 controlreply(np, "Memory error.");
232 return CMD_ERROR;
233 }
234
0555113a 235 for(i=0,tg=tglist;i<totalcount&&tg;tg=tg->next,i++)
82a316e7
CP
236 atg[i] = tg;
237
238 qsort(atg, totalcount, sizeof(trustgroup *), comparetgs);
239
240 for(i=0;i<totalcount;i++)
241 if(atg[i]->id >= wanted)
242 break;
243
244 for(groupcount=linecount=0;i<totalcount;i++) {
245 linecount++;
246 groupcount++;
247
248 controlreply(np, "G,%s", dumptg(atg[i], 1));
249
250 for(th=atg[i]->hosts;th;th=th->next) {
251 linecount++;
252 controlreply(np, "H,%s", dumpth(th, 1));
253 }
254
255 if(--max == 0)
256 break;
257 }
258 nsfree(POOL_TRUSTS, atg);
259
260 controlreply(np, "End of list, %u groups and %u lines returned.", groupcount, linecount);
261 return CMD_OK;
262}
263
1f03587c
GB
264static int trusts_cmdtrustgline(void *source, int cargc, char **cargv) {
265 trustgroup *tg;
266 nick *sender = source;
267 char *user, *reason;
ac3af088 268 int duration, count;
1f03587c
GB
269
270 if(cargc < 4)
271 return CMD_USAGE;
272
273 tg = tg_strtotg(cargv[0]);
274 if(!tg) {
275 controlreply(sender, "Couldn't look up trustgroup.");
276 return CMD_ERROR;
277 }
278
279 user = cargv[1];
280
281 duration = durationtolong(cargv[2]);
282 if((duration <= 0) || (duration > MAXDURATION)) {
283 controlreply(sender, "Invalid duration supplied.");
284 return CMD_ERROR;
285 }
286
287 reason = cargv[3];
288
ac3af088 289 count = trustgline(tg, user, duration, reason);
1f03587c
GB
290
291 controlwall(NO_OPER, NL_GLINES|NL_TRUSTS, "%s TRUSTGLINE'd user '%s' on group '%s', %d gline(s) set.", controlid(sender), user, tg->name->content, count);
292 controlreply(sender, "Done. %d gline(s) set.", count);
293
294 return CMD_OK;
295}
296
297static int trusts_cmdtrustungline(void *source, int cargc, char **cargv) {
298 trustgroup *tg;
299 nick *sender = source;
1467e9a4 300 char *user;
ac3af088 301 int count;
1f03587c
GB
302
303 if(cargc < 2)
304 return CMD_USAGE;
305
306 tg = tg_strtotg(cargv[0]);
307 if(!tg) {
308 controlreply(sender, "Couldn't look up trustgroup.");
309 return CMD_ERROR;
310 }
311
312 user = cargv[1];
313
ac3af088 314 count = trustungline(tg, user, 0, "Deactivated.");
1f03587c
GB
315
316 controlwall(NO_OPER, NL_GLINES|NL_TRUSTS, "%s TRUSTUNGLINE'd user '%s' on group '%s', %d gline(s) removed.", controlid(sender), user, tg->name->content, count);
317 controlreply(sender, "Done. %d gline(s) removed.", count);
318
319 return CMD_OK;
320}
321
a99a2041
CP
322static int commandsregistered;
323
324static void registercommands(int hooknum, void *arg) {
325 if(commandsregistered)
326 return;
327 commandsregistered = 1;
328
01bd21d3 329 registercontrolhelpcmd("trustlist", NO_OPER, 1, trusts_cmdtrustlist, "Usage: trustlist <#id|name|IP>\nShows trust data for the specified trust group.");
82a316e7 330 registercontrolhelpcmd("trustdump", NO_OPER, 2, trusts_cmdtrustdump, "Usage: trustdump <#id> <number>");
1f03587c
GB
331 registercontrolhelpcmd("trustgline", NO_OPER, 4, trusts_cmdtrustgline, "Usage: trustgline <#id|name> <user> <duration> <reason>\nGlines a user on all hosts of a trust group.");
332 registercontrolhelpcmd("trustungline", NO_OPER, 4, trusts_cmdtrustungline, "Usage: trustungline <#id|name> <user>\nUnglines a user on all hosts of a trust group.");
a99a2041
CP
333}
334
83bccee3 335static void deregistercommands(int hooknum, void *arg) {
a99a2041
CP
336 if(!commandsregistered)
337 return;
338 commandsregistered = 0;
339
a99a2041 340 deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
82a316e7 341 deregistercontrolcmd("trustdump", trusts_cmdtrustdump);
1f03587c
GB
342 deregistercontrolcmd("trustgline", trusts_cmdtrustgline);
343 deregistercontrolcmd("trustungline", trusts_cmdtrustungline);
be2823bc
CP
344}
345
346void _init(void) {
a99a2041 347 registerhook(HOOK_TRUSTS_DB_LOADED, registercommands);
83bccee3 348 registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
be2823bc
CP
349
350 if(trustsdbloaded)
a99a2041 351 registercommands(0, NULL);
be2823bc
CP
352}
353
354void _fini(void) {
a99a2041 355 deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
83bccee3 356 deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
be2823bc 357
83bccee3
CP
358 deregistercommands(0, NULL);
359}