]> jfr.im git - irc/quakenet/newserv.git/blame - trusts/trusts_commands.c
CHANSERV: remove accidental sendemail from SETEMAIL command.
[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);
ee77bc7a
CP
212}
213
214static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
215 nick *sender = source;
ee77bc7a 216 trustgroup *tg = NULL;
73653516
CP
217 int found = 0, remaining = 50;
218 char *name;
3e646f8f 219 trusthost *th;
6e6e98da
GB
220 struct irc_in_addr ip;
221 unsigned char bits;
a90eb846 222 int showchildren;
b64178fe 223 char *cmpbuf;
ee77bc7a
CP
224
225 if(cargc < 1)
226 return CMD_USAGE;
227
a90eb846
GB
228 if(strcmp(cargv[0], "-v") == 0) {
229 if(cargc < 2)
230 return CMD_USAGE;
231
232 showchildren = 1;
233 name = cargv[1];
234 } else {
235 showchildren = 0;
236 name = cargv[0];
237 }
ee77bc7a 238
73653516 239 tg = tg_strtotg(name);
ee77bc7a
CP
240
241 if(tg) {
a90eb846 242 displaygroup(sender, tg, showchildren);
c2e20419 243 controlreply(sender, "End of list.");
ee77bc7a
CP
244 return CMD_OK;
245 }
246
6e6e98da
GB
247 if(ipmask_parse(name, &ip, &bits)) {
248 th = th_getbyhost(&ip);
3e646f8f
GB
249
250 if(!th) {
251 controlreply(sender, "Specified IP address is not trusted.");
252 return CMD_OK;
253 }
254
a90eb846 255 displaygroup(sender, th->group, showchildren);
c2e20419 256 controlreply(sender, "End of list.");
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
c2e20419
CP
284 if(found) {
285 controlreply(sender, "End of list.");
286 } else {
ee77bc7a 287 controlreply(sender, "No matches found.");
c2e20419 288 }
2d4ba67d
CP
289
290 return CMD_OK;
291}
292
8f128e0d 293static int trusts_cmdtrustglinesuggest(void *source, int cargc, char **cargv) {
1f03587c 294 nick *sender = source;
8f128e0d
GB
295 char mask[512];
296 char *p, *user, *host;
297 struct irc_in_addr ip;
298 unsigned char bits;
ac3af088 299 int count;
a86fc0c4
GB
300 glinebuf gbuf;
301 char creator[32];
1f03587c 302
8f128e0d 303 if(cargc < 1)
1f03587c
GB
304 return CMD_USAGE;
305
8f128e0d
GB
306 strncpy(mask, cargv[0], sizeof(mask));
307
308 p = strchr(mask, '@');
309
310 if(!p)
311 return CMD_USAGE;
312
313 user = mask;
314 host = p + 1;
315 *p = '\0';
316
317 if(!ipmask_parse(host, &ip, &bits)) {
318 controlreply(sender, "Invalid CIDR.");
1f03587c
GB
319 return CMD_ERROR;
320 }
321
a86fc0c4
GB
322 snprintf(creator, sizeof(creator), "#%s", sender->authname);
323
324b4e11 324 glinebufinit(&gbuf, 0);
a86fc0c4 325 glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, "Simulate", getnettime(), getnettime(), getnettime());
ac80e3ab 326 glinebufcounthits(&gbuf, &count, NULL);
a86fc0c4 327 glinebufspew(&gbuf, sender);
0b2e8a55 328 glinebufabort(&gbuf);
1f03587c 329
8f128e0d 330 controlreply(sender, "Total hits: %d", count);
1f03587c
GB
331
332 return CMD_OK;
333}
334
7b26c20e
GB
335static int trusts_cmdtrustspew(void *source, int cargc, char **cargv) {
336 nick *sender = source;
337 searchASTExpr tree;
338
339 if(cargc < 1)
340 return CMD_USAGE;
341
342 tree = NSASTNode(tgroup_parse, NSASTLiteral(cargv[0]));
4860501e 343 return ast_nicksearch(&tree, controlreply, sender, NULL, printnick_channels, NULL, NULL, 2000, NULL);
7b26c20e
GB
344}
345
a99a2041
CP
346static int commandsregistered;
347
348static void registercommands(int hooknum, void *arg) {
349 if(commandsregistered)
350 return;
351 commandsregistered = 1;
352
f52356ae
CP
353 registercontrolhelpcmd("trustlist", NO_OPER, 2, trusts_cmdtrustlist, "Usage: trustlist [-v] <#id|name|IP|&qid>\nShows trust data for the specified trust group.");
354 registercontrolhelpcmd("trustlistrelay", NO_RELAY, 2, trusts_cmdtrustlist, "Same as trustlist, but for the relay.");
8f128e0d 355 registercontrolhelpcmd("trustglinesuggest", NO_OPER, 1, trusts_cmdtrustglinesuggest, "Usage: trustglinesuggest <user@host>\nSuggests glines for the specified hostmask.");
7b26c20e 356 registercontrolhelpcmd("trustspew", NO_OPER, 1, trusts_cmdtrustspew, "Usage: trustspew <#id|name>\nShows currently connected users for the specified trust group.");
a99a2041
CP
357}
358
83bccee3 359static void deregistercommands(int hooknum, void *arg) {
a99a2041
CP
360 if(!commandsregistered)
361 return;
362 commandsregistered = 0;
363
a99a2041 364 deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
f52356ae 365 deregistercontrolcmd("trustlistrelay", trusts_cmdtrustlist);
8f128e0d 366 deregistercontrolcmd("trustglinesuggest", trusts_cmdtrustglinesuggest);
7b26c20e 367 deregistercontrolcmd("trustspew", trusts_cmdtrustspew);
be2823bc
CP
368}
369
370void _init(void) {
a99a2041 371 registerhook(HOOK_TRUSTS_DB_LOADED, registercommands);
83bccee3 372 registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
be2823bc
CP
373
374 if(trustsdbloaded)
a99a2041 375 registercommands(0, NULL);
be2823bc
CP
376}
377
378void _fini(void) {
a99a2041 379 deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
83bccee3 380 deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
be2823bc 381
83bccee3
CP
382 deregistercommands(0, NULL);
383}