]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | char *cmpbuf; | |
224 | ||
225 | if(cargc < 1) | |
226 | return CMD_USAGE; | |
227 | ||
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 | } | |
238 | ||
239 | tg = tg_strtotg(name); | |
240 | ||
241 | if(tg) { | |
242 | displaygroup(sender, tg, showchildren); | |
243 | controlreply(sender, "End of list."); | |
244 | return CMD_OK; | |
245 | } | |
246 | ||
247 | if(ipmask_parse(name, &ip, &bits)) { | |
248 | th = th_getbyhost(&ip); | |
249 | ||
250 | if(!th) { | |
251 | controlreply(sender, "Specified IP address is not trusted."); | |
252 | return CMD_OK; | |
253 | } | |
254 | ||
255 | displaygroup(sender, th->group, showchildren); | |
256 | controlreply(sender, "End of list."); | |
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, "End of list."); | |
286 | } else { | |
287 | controlreply(sender, "No matches found."); | |
288 | } | |
289 | ||
290 | return CMD_OK; | |
291 | } | |
292 | ||
293 | static int trusts_cmdtrustglinesuggest(void *source, int cargc, char **cargv) { | |
294 | nick *sender = source; | |
295 | char mask[512]; | |
296 | char *p, *user, *host; | |
297 | struct irc_in_addr ip; | |
298 | unsigned char bits; | |
299 | int count; | |
300 | glinebuf gbuf; | |
301 | char creator[32]; | |
302 | ||
303 | if(cargc < 1) | |
304 | return CMD_USAGE; | |
305 | ||
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."); | |
319 | return CMD_ERROR; | |
320 | } | |
321 | ||
322 | snprintf(creator, sizeof(creator), "#%s", sender->authname); | |
323 | ||
324 | glinebufinit(&gbuf, 0); | |
325 | glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, "Simulate", getnettime(), getnettime(), getnettime()); | |
326 | glinebufcounthits(&gbuf, &count, NULL); | |
327 | glinebufspew(&gbuf, sender); | |
328 | glinebufabort(&gbuf); | |
329 | ||
330 | controlreply(sender, "Total hits: %d", count); | |
331 | ||
332 | return CMD_OK; | |
333 | } | |
334 | ||
335 | static 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])); | |
343 | return ast_nicksearch(&tree, controlreply, sender, NULL, printnick_channels, NULL, NULL, 2000, NULL); | |
344 | } | |
345 | ||
346 | static int commandsregistered; | |
347 | ||
348 | static void registercommands(int hooknum, void *arg) { | |
349 | if(commandsregistered) | |
350 | return; | |
351 | commandsregistered = 1; | |
352 | ||
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."); | |
355 | registercontrolhelpcmd("trustglinesuggest", NO_OPER, 1, trusts_cmdtrustglinesuggest, "Usage: trustglinesuggest <user@host>\nSuggests glines for the specified hostmask."); | |
356 | registercontrolhelpcmd("trustspew", NO_OPER, 1, trusts_cmdtrustspew, "Usage: trustspew <#id|name>\nShows currently connected users for the specified trust group."); | |
357 | } | |
358 | ||
359 | static void deregistercommands(int hooknum, void *arg) { | |
360 | if(!commandsregistered) | |
361 | return; | |
362 | commandsregistered = 0; | |
363 | ||
364 | deregistercontrolcmd("trustlist", trusts_cmdtrustlist); | |
365 | deregistercontrolcmd("trustlistrelay", trusts_cmdtrustlist); | |
366 | deregistercontrolcmd("trustglinesuggest", trusts_cmdtrustglinesuggest); | |
367 | deregistercontrolcmd("trustspew", trusts_cmdtrustspew); | |
368 | } | |
369 | ||
370 | void _init(void) { | |
371 | registerhook(HOOK_TRUSTS_DB_LOADED, registercommands); | |
372 | registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands); | |
373 | ||
374 | if(trustsdbloaded) | |
375 | registercommands(0, NULL); | |
376 | } | |
377 | ||
378 | void _fini(void) { | |
379 | deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands); | |
380 | deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands); | |
381 | ||
382 | deregistercommands(0, NULL); | |
383 | } |