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