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