]> jfr.im git - irc/quakenet/newserv.git/blob - trusts/trusts_commands.c
TRUSTS: fix silly typos in trustlist output.
[irc/quakenet/newserv.git] / trusts / trusts_commands.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <alloca.h>
4 #include <strings.h>
5 #include "../lib/version.h"
6 #include "../control/control.h"
7 #include "../lib/irc_string.h"
8 #include "../lib/strlfunc.h"
9 #include "../core/nsmalloc.h"
10 #include "../irc/irc.h"
11 #include "../newsearch/newsearch.h"
12 #include "../glines/glines.h"
13 #include "trusts.h"
14 #include "newsearch/trusts_newsearch.h"
15
16 MODULE_VERSION("");
17
18 static void registercommands(int, void *);
19 static void deregistercommands(int, void *);
20
21 extern void printnick_channels(searchCtx *, nick *, nick *);
22
23 void calculatespaces(int spaces, int width, const char *str, char **_prebuf, char **_postbuf) {
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
46 static void traverseandmark(unsigned int marker, trusthost *th, int markchildren) {
47 th->marker = marker;
48
49 if(markchildren) {
50 for(th=th->children;th;th=th->nextbychild) {
51 th->marker = marker;
52 traverseandmark(marker, th, markchildren);
53 }
54 }
55 }
56
57 static 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
72 static void marktree(array *parents, unsigned int marker, trusthost *th, int showchildren) {
73 trusthost *pth;
74 int parentcount = 0;
75
76 for(pth=th->parent;pth;pth=pth->parent) {
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 */
86 traverseandmark(marker, th, showchildren);
87 }
88
89 static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth, int showchildren) {
90 const char *cidrstr;
91 char *prespacebuf, *postspacebuf, parentbuf[512];
92
93 if(th->marker != marker)
94 return;
95
96 cidrstr = CIDRtostr(th->ip, th->bits);
97 calculatespaces(depth + 2, 30 + 1, cidrstr, &prespacebuf, &postspacebuf);
98
99 if(th->group == originalgroup) {
100 if(!showchildren && th->group == originalgroup && th->children)
101 prespacebuf[0] = '*';
102 else
103 prespacebuf[0] = ' ';
104
105 prespacebuf[1] = '>';
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
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);
115
116 /* Make sure we're not seeing this subtree again. */
117 th->marker = -1;
118
119 for(th=th->children;th;th=th->nextbychild)
120 outputtree(np, marker, originalgroup, th, depth + 1, showchildren);
121 }
122
123 static 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
152 if(flags & TRUST_UNTHROTTLE) {
153 if(buf[0])
154 strncat(buf, ", ", 512);
155
156 strncat(buf, "unthrottled", 512);
157 }
158
159 buf[512-1] = '\0';
160
161 return buf;
162 }
163
164 static 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
175 static void displaygroup(nick *sender, trustgroup *tg, int showchildren) {
176 trusthost *th, **p2;
177 unsigned int marker;
178 array parents;
179 int i;
180 time_t t = getnettime();
181
182 /* abusing the ternary operator a bit :( */
183 controlreply(sender, "Name : %s", tg->name->content);
184 controlreply(sender, "Trusted for : %s", formatlimit(tg->trustedfor));
185 controlreply(sender, "Currently using : %d", tg->count);
186 controlreply(sender, "Clients per user : %s", formatlimit(tg->maxperident));
187 controlreply(sender, "Flags : %s", formatflags(tg->flags));
188 controlreply(sender, "Contact : %s", tg->contact->content);
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");
190 controlreply(sender, "Created by : %s", tg->createdby->content);
191 controlreply(sender, "Comment : %s", tg->comment->content);
192 controlreply(sender, "ID : %u", tg->id);
193 controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":((tg->lastseen>0)?trusts_timetostr(tg->lastseen):"(never)"));
194 controlreply(sender, "Max usage : %d", tg->maxusage);
195 controlreply(sender, "Last max reset : %s", tg->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(never)");
196
197 controlreply(sender, "---");
198 controlreply(sender, "Attributes: * (has hidden children, show with -v), > (belongs to this trust group)");
199 controlreply(sender, "Host Current Max Last seen Max per Node Node Mask Group ID Group name");
200
201 marker = nextthmarker();
202 array_init(&parents, sizeof(trusthost *));
203
204 for(th=tg->hosts;th;th=th->next)
205 marktree(&parents, marker, th, showchildren);
206
207 p2 = (trusthost **)(parents.content);
208 for(i=0;i<parents.cursi;i++)
209 outputtree(sender, marker, tg, p2[i], 0, showchildren);
210
211 array_free(&parents);
212
213 controlreply(sender, "End of list.");
214 }
215
216 static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
217 nick *sender = source;
218 trustgroup *tg = NULL;
219 int found = 0, remaining = 50;
220 char *name;
221 trusthost *th;
222 struct irc_in_addr ip;
223 unsigned char bits;
224 int showchildren;
225 char *cmpbuf;
226
227 if(cargc < 1)
228 return CMD_USAGE;
229
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 }
240
241 tg = tg_strtotg(name);
242
243 if(tg) {
244 displaygroup(sender, tg, showchildren);
245 return CMD_OK;
246 }
247
248 if(ipmask_parse(name, &ip, &bits)) {
249 th = th_getbyhost(&ip);
250
251 if(!th) {
252 controlreply(sender, "Specified IP address is not trusted.");
253 return CMD_OK;
254 }
255
256 displaygroup(sender, th->group, showchildren);
257 return CMD_OK;
258 }
259
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
268 for(tg=tglist;tg;tg=tg->next) {
269 if(cmpbuf) {
270 if(!tg->contact->content || strcasecmp(cmpbuf, tg->contact->content))
271 continue;
272 } else if(match(name, tg->name->content)) {
273 continue;
274 }
275
276 displaygroup(sender, tg, showchildren);
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.");
286
287 return CMD_OK;
288 }
289
290 static int trusts_cmdtrustglinesuggest(void *source, int cargc, char **cargv) {
291 nick *sender = source;
292 char mask[512];
293 char *p, *user, *host;
294 struct irc_in_addr ip;
295 unsigned char bits;
296 int count;
297 glinebuf gbuf;
298 char creator[32];
299
300 if(cargc < 1)
301 return CMD_USAGE;
302
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.");
316 return CMD_ERROR;
317 }
318
319 snprintf(creator, sizeof(creator), "#%s", sender->authname);
320
321 glinebufinit(&gbuf, 0);
322 glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, "Simulate", getnettime(), getnettime(), getnettime());
323 glinebufcounthits(&gbuf, &count, NULL);
324 glinebufspew(&gbuf, sender);
325 glinebufabort(&gbuf);
326
327 controlreply(sender, "Total hits: %d", count);
328
329 return CMD_OK;
330 }
331
332 static 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]));
340 return ast_nicksearch(&tree, controlreply, sender, NULL, printnick_channels, NULL, NULL, 2000, NULL);
341 }
342
343 static int commandsregistered;
344
345 static void registercommands(int hooknum, void *arg) {
346 if(commandsregistered)
347 return;
348 commandsregistered = 1;
349
350 registercontrolhelpcmd("trustlist", NO_RELAY, 2, trusts_cmdtrustlist, "Usage: trustlist [-v] <#id|name|IP|&qid>\nShows trust data for the specified trust group.");
351 registercontrolhelpcmd("trustglinesuggest", NO_OPER, 1, trusts_cmdtrustglinesuggest, "Usage: trustglinesuggest <user@host>\nSuggests glines for the specified hostmask.");
352 registercontrolhelpcmd("trustspew", NO_OPER, 1, trusts_cmdtrustspew, "Usage: trustspew <#id|name>\nShows currently connected users for the specified trust group.");
353 }
354
355 static void deregistercommands(int hooknum, void *arg) {
356 if(!commandsregistered)
357 return;
358 commandsregistered = 0;
359
360 deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
361 deregistercontrolcmd("trustglinesuggest", trusts_cmdtrustglinesuggest);
362 deregistercontrolcmd("trustspew", trusts_cmdtrustspew);
363 }
364
365 void _init(void) {
366 registerhook(HOOK_TRUSTS_DB_LOADED, registercommands);
367 registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
368
369 if(trustsdbloaded)
370 registercommands(0, NULL);
371 }
372
373 void _fini(void) {
374 deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
375 deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
376
377 deregistercommands(0, NULL);
378 }