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