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