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