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