]>
Commit | Line | Data |
---|---|---|
d2c08930 | 1 | #include <stdio.h> |
b76fd8e6 | 2 | #include <string.h> |
c4610da5 | 3 | #include "../lib/version.h" |
be2823bc | 4 | #include "../control/control.h" |
2d4ba67d | 5 | #include "../lib/irc_string.h" |
b76fd8e6 | 6 | #include "../lib/strlfunc.h" |
82a316e7 | 7 | #include "../core/nsmalloc.h" |
acd5f58f | 8 | #include "../irc/irc.h" |
7b26c20e | 9 | #include "../newsearch/newsearch.h" |
8f128e0d | 10 | #include "../glines/glines.h" |
be2823bc | 11 | #include "trusts.h" |
938d3b1c | 12 | #include "newsearch/trusts_newsearch.h" |
be2823bc | 13 | |
c4610da5 GB |
14 | MODULE_VERSION(""); |
15 | ||
83bccee3 CP |
16 | static void registercommands(int, void *); |
17 | static void deregistercommands(int, void *); | |
be2823bc | 18 | |
7b26c20e GB |
19 | extern void printnick_channels(searchCtx *, nick *, nick *); |
20 | ||
4dcce883 | 21 | void calculatespaces(int spaces, int width, const char *str, char **_prebuf, char **_postbuf) { |
34e3de85 GB |
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 | ||
a90eb846 | 44 | static void traverseandmark(unsigned int marker, trusthost *th, int markchildren) { |
34e3de85 GB |
45 | th->marker = marker; |
46 | ||
a90eb846 GB |
47 | if(markchildren) { |
48 | for(th=th->children;th;th=th->nextbychild) { | |
49 | th->marker = marker; | |
50 | traverseandmark(marker, th, markchildren); | |
51 | } | |
34e3de85 GB |
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 | ||
a90eb846 | 70 | static void marktree(array *parents, unsigned int marker, trusthost *th, int showchildren) { |
34e3de85 GB |
71 | trusthost *pth; |
72 | int parentcount = 0; | |
73 | ||
40136705 | 74 | for(pth=th->parent;pth;pth=pth->parent) { |
34e3de85 GB |
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 */ | |
a90eb846 | 84 | traverseandmark(marker, th, showchildren); |
34e3de85 GB |
85 | } |
86 | ||
a90eb846 | 87 | static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth, int showchildren) { |
4dcce883 GB |
88 | const char *cidrstr; |
89 | char *prespacebuf, *postspacebuf, parentbuf[512]; | |
34e3de85 GB |
90 | |
91 | if(th->marker != marker) | |
92 | return; | |
93 | ||
3898f973 | 94 | cidrstr = CIDRtostr(th->ip, th->bits); |
a90eb846 | 95 | calculatespaces(depth + 2, 30 + 1, cidrstr, &prespacebuf, &postspacebuf); |
34e3de85 GB |
96 | |
97 | if(th->group == originalgroup) { | |
a90eb846 GB |
98 | if(!showchildren && th->group == originalgroup && th->children) |
99 | prespacebuf[0] = '*'; | |
100 | else | |
101 | prespacebuf[0] = ' '; | |
102 | ||
103 | prespacebuf[1] = '>'; | |
34e3de85 GB |
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 | ||
6e6e98da | 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); |
34e3de85 | 113 | |
afb236cd GB |
114 | /* Make sure we're not seeing this subtree again. */ |
115 | th->marker = -1; | |
116 | ||
34e3de85 | 117 | for(th=th->children;th;th=th->nextbychild) |
a90eb846 | 118 | outputtree(np, marker, originalgroup, th, depth + 1, showchildren); |
34e3de85 GB |
119 | } |
120 | ||
3a8c35c9 GB |
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 | ||
4b40d278 GB |
150 | if(flags & TRUST_UNTHROTTLE) { |
151 | if(buf[0]) | |
152 | strncat(buf, ", ", 512); | |
153 | ||
154 | strncat(buf, "unthrottled", 512); | |
155 | } | |
156 | ||
3a8c35c9 GB |
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 | ||
a90eb846 | 173 | static void displaygroup(nick *sender, trustgroup *tg, int showchildren) { |
34e3de85 GB |
174 | trusthost *th, **p2; |
175 | unsigned int marker; | |
176 | array parents; | |
177 | int i; | |
acd5f58f | 178 | time_t t = getnettime(); |
2d4ba67d | 179 | |
d36ca89c | 180 | /* abusing the ternary operator a bit :( */ |
4b003d19 | 181 | controlreply(sender, "Name: : %s", tg->name->content); |
3a8c35c9 | 182 | controlreply(sender, "Trusted for : %s", formatlimit(tg->trustedfor)); |
1bbe1ac3 | 183 | controlreply(sender, "Currently using : %d", tg->count); |
3a8c35c9 GB |
184 | controlreply(sender, "Clients per user : %s", formatlimit(tg->maxperident)); |
185 | controlreply(sender, "Flags : %s", formatflags(tg->flags)); | |
4b003d19 | 186 | controlreply(sender, "Contact: : %s", tg->contact->content); |
9afc3262 | 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"); |
0daf3b9f | 188 | controlreply(sender, "Created by : %s", tg->createdby->content); |
4b003d19 | 189 | controlreply(sender, "Comment: : %s", tg->comment->content); |
2d4ba67d | 190 | controlreply(sender, "ID: : %u", tg->id); |
d36ca89c | 191 | controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":((tg->lastseen>0)?trusts_timetostr(tg->lastseen):"(never)")); |
4be1aaf2 | 192 | controlreply(sender, "Max usage : %d", tg->maxusage); |
1f685425 | 193 | controlreply(sender, "Last max reset : %s", tg->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(never)"); |
2d4ba67d | 194 | |
a90eb846 | 195 | controlreply(sender, "---"); |
99ebf8d1 | 196 | controlreply(sender, "Attributes: * (has hidden children, show with -v), > (belongs to this trust group)"); |
69876096 | 197 | controlreply(sender, "Host Current Max Last seen Max per Node Node Mask Group ID Group name"); |
dee7de1b | 198 | |
34e3de85 GB |
199 | marker = nextthmarker(); |
200 | array_init(&parents, sizeof(trusthost *)); | |
dee7de1b | 201 | |
34e3de85 | 202 | for(th=tg->hosts;th;th=th->next) |
a90eb846 | 203 | marktree(&parents, marker, th, showchildren); |
34e3de85 GB |
204 | |
205 | p2 = (trusthost **)(parents.content); | |
206 | for(i=0;i<parents.cursi;i++) | |
a90eb846 | 207 | outputtree(sender, marker, tg, p2[i], 0, showchildren); |
34e3de85 GB |
208 | |
209 | array_free(&parents); | |
2d4ba67d CP |
210 | |
211 | controlreply(sender, "End of list."); | |
ee77bc7a CP |
212 | } |
213 | ||
214 | static 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; |
ee77bc7a CP |
223 | |
224 | if(cargc < 1) | |
225 | return CMD_USAGE; | |
226 | ||
a90eb846 GB |
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 | } | |
ee77bc7a | 237 | |
73653516 | 238 | tg = tg_strtotg(name); |
ee77bc7a CP |
239 | |
240 | if(tg) { | |
a90eb846 | 241 | displaygroup(sender, tg, showchildren); |
ee77bc7a CP |
242 | return CMD_OK; |
243 | } | |
244 | ||
6e6e98da GB |
245 | if(ipmask_parse(name, &ip, &bits)) { |
246 | th = th_getbyhost(&ip); | |
3e646f8f GB |
247 | |
248 | if(!th) { | |
249 | controlreply(sender, "Specified IP address is not trusted."); | |
250 | return CMD_OK; | |
251 | } | |
252 | ||
a90eb846 | 253 | displaygroup(sender, th->group, showchildren); |
3e646f8f GB |
254 | return CMD_OK; |
255 | } | |
256 | ||
ee77bc7a | 257 | for(tg=tglist;tg;tg=tg->next) { |
c215a421 | 258 | if(match(name, tg->name->content)) |
ee77bc7a CP |
259 | continue; |
260 | ||
a90eb846 | 261 | displaygroup(sender, tg, showchildren); |
ee77bc7a CP |
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."); | |
2d4ba67d CP |
271 | |
272 | return CMD_OK; | |
273 | } | |
274 | ||
8f128e0d | 275 | static int trusts_cmdtrustglinesuggest(void *source, int cargc, char **cargv) { |
1f03587c | 276 | nick *sender = source; |
8f128e0d GB |
277 | char mask[512]; |
278 | char *p, *user, *host; | |
279 | struct irc_in_addr ip; | |
280 | unsigned char bits; | |
ac3af088 | 281 | int count; |
a86fc0c4 GB |
282 | glinebuf gbuf; |
283 | char creator[32]; | |
1f03587c | 284 | |
8f128e0d | 285 | if(cargc < 1) |
1f03587c GB |
286 | return CMD_USAGE; |
287 | ||
8f128e0d GB |
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."); | |
1f03587c GB |
301 | return CMD_ERROR; |
302 | } | |
303 | ||
a86fc0c4 GB |
304 | snprintf(creator, sizeof(creator), "#%s", sender->authname); |
305 | ||
324b4e11 | 306 | glinebufinit(&gbuf, 0); |
a86fc0c4 | 307 | glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, "Simulate", getnettime(), getnettime(), getnettime()); |
ac80e3ab | 308 | glinebufcounthits(&gbuf, &count, NULL); |
a86fc0c4 | 309 | glinebufspew(&gbuf, sender); |
0b2e8a55 | 310 | glinebufabort(&gbuf); |
1f03587c | 311 | |
8f128e0d | 312 | controlreply(sender, "Total hits: %d", count); |
1f03587c GB |
313 | |
314 | return CMD_OK; | |
315 | } | |
316 | ||
7b26c20e GB |
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])); | |
4860501e | 325 | return ast_nicksearch(&tree, controlreply, sender, NULL, printnick_channels, NULL, NULL, 2000, NULL); |
7b26c20e GB |
326 | } |
327 | ||
a99a2041 CP |
328 | static int commandsregistered; |
329 | ||
330 | static void registercommands(int hooknum, void *arg) { | |
331 | if(commandsregistered) | |
332 | return; | |
333 | commandsregistered = 1; | |
334 | ||
a90eb846 | 335 | registercontrolhelpcmd("trustlist", NO_OPER, 2, trusts_cmdtrustlist, "Usage: trustlist [-v] <#id|name|IP>\nShows trust data for the specified trust group."); |
8f128e0d | 336 | registercontrolhelpcmd("trustglinesuggest", NO_OPER, 1, trusts_cmdtrustglinesuggest, "Usage: trustglinesuggest <user@host>\nSuggests glines for the specified hostmask."); |
7b26c20e | 337 | registercontrolhelpcmd("trustspew", NO_OPER, 1, trusts_cmdtrustspew, "Usage: trustspew <#id|name>\nShows currently connected users for the specified trust group."); |
a99a2041 CP |
338 | } |
339 | ||
83bccee3 | 340 | static void deregistercommands(int hooknum, void *arg) { |
a99a2041 CP |
341 | if(!commandsregistered) |
342 | return; | |
343 | commandsregistered = 0; | |
344 | ||
a99a2041 | 345 | deregistercontrolcmd("trustlist", trusts_cmdtrustlist); |
8f128e0d | 346 | deregistercontrolcmd("trustglinesuggest", trusts_cmdtrustglinesuggest); |
7b26c20e | 347 | deregistercontrolcmd("trustspew", trusts_cmdtrustspew); |
be2823bc CP |
348 | } |
349 | ||
350 | void _init(void) { | |
a99a2041 | 351 | registerhook(HOOK_TRUSTS_DB_LOADED, registercommands); |
83bccee3 | 352 | registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands); |
be2823bc CP |
353 | |
354 | if(trustsdbloaded) | |
a99a2041 | 355 | registercommands(0, NULL); |
be2823bc CP |
356 | } |
357 | ||
358 | void _fini(void) { | |
a99a2041 | 359 | deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands); |
83bccee3 | 360 | deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands); |
be2823bc | 361 | |
83bccee3 CP |
362 | deregistercommands(0, NULL); |
363 | } |