]>
Commit | Line | Data |
---|---|---|
d2c08930 | 1 | #include <stdio.h> |
b76fd8e6 | 2 | #include <string.h> |
be2823bc | 3 | #include "../control/control.h" |
2d4ba67d | 4 | #include "../lib/irc_string.h" |
b76fd8e6 | 5 | #include "../lib/strlfunc.h" |
82a316e7 | 6 | #include "../core/nsmalloc.h" |
acd5f58f | 7 | #include "../irc/irc.h" |
7b26c20e | 8 | #include "../newsearch/newsearch.h" |
8f128e0d | 9 | #include "../glines/glines.h" |
be2823bc | 10 | #include "trusts.h" |
938d3b1c | 11 | #include "newsearch/trusts_newsearch.h" |
be2823bc | 12 | |
83bccee3 CP |
13 | static void registercommands(int, void *); |
14 | static void deregistercommands(int, void *); | |
be2823bc | 15 | |
7b26c20e GB |
16 | extern void printnick_channels(searchCtx *, nick *, nick *); |
17 | ||
34e3de85 GB |
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 | ||
40136705 | 69 | for(pth=th->parent;pth;pth=pth->parent) { |
34e3de85 GB |
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 | ||
6e6e98da | 88 | cidrstr = trusts_cidr2str(&th->ip, th->bits); |
69876096 | 89 | calculatespaces(depth + 1, 30 + 1, cidrstr, &prespacebuf, &postspacebuf); |
34e3de85 GB |
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 | ||
6e6e98da | 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); |
34e3de85 | 102 | |
afb236cd GB |
103 | /* Make sure we're not seeing this subtree again. */ |
104 | th->marker = -1; | |
105 | ||
34e3de85 GB |
106 | for(th=th->children;th;th=th->nextbychild) |
107 | outputtree(np, marker, originalgroup, th, depth + 1); | |
108 | } | |
109 | ||
ee77bc7a | 110 | static void displaygroup(nick *sender, trustgroup *tg) { |
34e3de85 GB |
111 | trusthost *th, **p2; |
112 | unsigned int marker; | |
113 | array parents; | |
114 | int i; | |
acd5f58f | 115 | time_t t = getnettime(); |
2d4ba67d | 116 | |
d36ca89c | 117 | /* abusing the ternary operator a bit :( */ |
4b003d19 | 118 | controlreply(sender, "Name: : %s", tg->name->content); |
2d4ba67d | 119 | controlreply(sender, "Trusted for : %d", tg->trustedfor); |
1bbe1ac3 | 120 | controlreply(sender, "Currently using : %d", tg->count); |
de723023 | 121 | controlreply(sender, "Clients per user : %d (%senforcing ident)", tg->maxperident, (tg->flags & TRUST_ENFORCE_IDENT)?"":"not "); |
4b003d19 | 122 | controlreply(sender, "Contact: : %s", tg->contact->content); |
9afc3262 | 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"); |
e40626f0 GB |
124 | controlreply(sender, "CIDR cleanup : %s", (tg->flags & TRUST_NO_CLEANUP)?"disabled":"enabled"); |
125 | controlreply(sender, "Protected : %s", (tg->flags & TRUST_PROTECTED)?"protected":"not protected"); | |
4b003d19 CP |
126 | controlreply(sender, "Last changed by : %s", tg->createdby->content); |
127 | controlreply(sender, "Comment: : %s", tg->comment->content); | |
2d4ba67d | 128 | controlreply(sender, "ID: : %u", tg->id); |
d36ca89c | 129 | controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":((tg->lastseen>0)?trusts_timetostr(tg->lastseen):"(never)")); |
4be1aaf2 | 130 | controlreply(sender, "Max usage : %d", tg->maxusage); |
1f685425 | 131 | controlreply(sender, "Last max reset : %s", tg->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(never)"); |
2d4ba67d | 132 | |
69876096 | 133 | controlreply(sender, "Host Current Max Last seen Max per Node Node Mask Group ID Group name"); |
dee7de1b | 134 | |
34e3de85 GB |
135 | marker = nextthmarker(); |
136 | array_init(&parents, sizeof(trusthost *)); | |
dee7de1b | 137 | |
34e3de85 GB |
138 | for(th=tg->hosts;th;th=th->next) |
139 | marktree(&parents, marker, th); | |
140 | ||
141 | p2 = (trusthost **)(parents.content); | |
142 | for(i=0;i<parents.cursi;i++) | |
143 | outputtree(sender, marker, tg, p2[i], 0); | |
144 | ||
145 | array_free(&parents); | |
2d4ba67d CP |
146 | |
147 | controlreply(sender, "End of list."); | |
ee77bc7a CP |
148 | } |
149 | ||
150 | static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) { | |
151 | nick *sender = source; | |
ee77bc7a | 152 | trustgroup *tg = NULL; |
73653516 CP |
153 | int found = 0, remaining = 50; |
154 | char *name; | |
3e646f8f | 155 | trusthost *th; |
6e6e98da GB |
156 | struct irc_in_addr ip; |
157 | unsigned char bits; | |
ee77bc7a CP |
158 | |
159 | if(cargc < 1) | |
160 | return CMD_USAGE; | |
161 | ||
162 | name = cargv[0]; | |
163 | ||
73653516 | 164 | tg = tg_strtotg(name); |
ee77bc7a CP |
165 | |
166 | if(tg) { | |
167 | displaygroup(sender, tg); | |
168 | return CMD_OK; | |
169 | } | |
170 | ||
6e6e98da GB |
171 | if(ipmask_parse(name, &ip, &bits)) { |
172 | th = th_getbyhost(&ip); | |
3e646f8f GB |
173 | |
174 | if(!th) { | |
175 | controlreply(sender, "Specified IP address is not trusted."); | |
176 | return CMD_OK; | |
177 | } | |
178 | ||
179 | displaygroup(sender, th->group); | |
180 | return CMD_OK; | |
181 | } | |
182 | ||
ee77bc7a | 183 | for(tg=tglist;tg;tg=tg->next) { |
c215a421 | 184 | if(match(name, tg->name->content)) |
ee77bc7a CP |
185 | continue; |
186 | ||
187 | displaygroup(sender, tg); | |
188 | if(--remaining == 0) { | |
189 | controlreply(sender, "Maximum number of matches reached."); | |
190 | return CMD_OK; | |
191 | } | |
192 | found = 1; | |
193 | } | |
194 | ||
195 | if(!found) | |
196 | controlreply(sender, "No matches found."); | |
2d4ba67d CP |
197 | |
198 | return CMD_OK; | |
199 | } | |
200 | ||
82a316e7 CP |
201 | static int comparetgs(const void *_a, const void *_b) { |
202 | const trustgroup *a = _a; | |
203 | const trustgroup *b = _b; | |
204 | ||
205 | if(a->id > b->id) | |
206 | return 1; | |
207 | if(a->id < b-> id) | |
208 | return -1; | |
209 | return 0; | |
210 | } | |
211 | ||
212 | static int trusts_cmdtrustdump(void *source, int argc, char **argv) { | |
213 | trusthost *th; | |
214 | trustgroup *tg, **atg; | |
215 | unsigned int wanted, max, maxid, totalcount, i, groupcount, linecount; | |
216 | nick *np = source; | |
217 | ||
218 | if((argc < 2) || (argv[0][0] != '#')) | |
219 | return CMD_USAGE; | |
220 | ||
221 | wanted = atoi(&argv[0][1]); | |
222 | max = atoi(argv[1]); | |
223 | ||
224 | for(maxid=totalcount=0,tg=tglist;tg;tg=tg->next) { | |
225 | if(totalcount == 0 || tg->id > maxid) | |
226 | maxid = tg->id; | |
227 | ||
228 | totalcount++; | |
229 | } | |
230 | ||
231 | if(maxid > totalcount) { | |
232 | controlreply(np, "Start ID cannot exceed current maximum group ID (#%u)", maxid); | |
233 | return CMD_OK; | |
234 | } | |
235 | ||
236 | atg = nsmalloc(POOL_TRUSTS, sizeof(trusthost *) * totalcount); | |
237 | if(!atg) { | |
238 | controlreply(np, "Memory error."); | |
239 | return CMD_ERROR; | |
240 | } | |
241 | ||
0555113a | 242 | for(i=0,tg=tglist;i<totalcount&&tg;tg=tg->next,i++) |
82a316e7 CP |
243 | atg[i] = tg; |
244 | ||
245 | qsort(atg, totalcount, sizeof(trustgroup *), comparetgs); | |
246 | ||
247 | for(i=0;i<totalcount;i++) | |
248 | if(atg[i]->id >= wanted) | |
249 | break; | |
250 | ||
251 | for(groupcount=linecount=0;i<totalcount;i++) { | |
252 | linecount++; | |
253 | groupcount++; | |
254 | ||
255 | controlreply(np, "G,%s", dumptg(atg[i], 1)); | |
256 | ||
257 | for(th=atg[i]->hosts;th;th=th->next) { | |
258 | linecount++; | |
259 | controlreply(np, "H,%s", dumpth(th, 1)); | |
260 | } | |
261 | ||
262 | if(--max == 0) | |
263 | break; | |
264 | } | |
265 | nsfree(POOL_TRUSTS, atg); | |
266 | ||
267 | controlreply(np, "End of list, %u groups and %u lines returned.", groupcount, linecount); | |
268 | return CMD_OK; | |
269 | } | |
270 | ||
8f128e0d GB |
271 | static void trusts_suggestgline_cb(const char *mask, int hits, void *uarg) { |
272 | nick *sender = uarg; | |
1f03587c | 273 | |
8f128e0d | 274 | controlreply(sender, "mask: %s, hits: %d", mask, hits); |
1f03587c GB |
275 | } |
276 | ||
8f128e0d | 277 | static int trusts_cmdtrustglinesuggest(void *source, int cargc, char **cargv) { |
1f03587c | 278 | nick *sender = source; |
8f128e0d GB |
279 | char mask[512]; |
280 | char *p, *user, *host; | |
281 | struct irc_in_addr ip; | |
282 | unsigned char bits; | |
ac3af088 | 283 | int count; |
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 | ||
8f128e0d | 304 | count = glinesuggestbyip(user, &ip, 128, 0, trusts_suggestgline_cb, sender); |
1f03587c | 305 | |
8f128e0d | 306 | controlreply(sender, "Total hits: %d", count); |
1f03587c GB |
307 | |
308 | return CMD_OK; | |
309 | } | |
310 | ||
7b26c20e GB |
311 | static int trusts_cmdtrustspew(void *source, int cargc, char **cargv) { |
312 | nick *sender = source; | |
313 | searchASTExpr tree; | |
314 | ||
315 | if(cargc < 1) | |
316 | return CMD_USAGE; | |
317 | ||
318 | tree = NSASTNode(tgroup_parse, NSASTLiteral(cargv[0])); | |
319 | return ast_nicksearch(&tree, controlreply, sender, NULL, printnick_channels, NULL, NULL, 2000); | |
320 | } | |
321 | ||
a99a2041 CP |
322 | static int commandsregistered; |
323 | ||
324 | static void registercommands(int hooknum, void *arg) { | |
325 | if(commandsregistered) | |
326 | return; | |
327 | commandsregistered = 1; | |
328 | ||
01bd21d3 | 329 | registercontrolhelpcmd("trustlist", NO_OPER, 1, trusts_cmdtrustlist, "Usage: trustlist <#id|name|IP>\nShows trust data for the specified trust group."); |
82a316e7 | 330 | registercontrolhelpcmd("trustdump", NO_OPER, 2, trusts_cmdtrustdump, "Usage: trustdump <#id> <number>"); |
8f128e0d | 331 | registercontrolhelpcmd("trustglinesuggest", NO_OPER, 1, trusts_cmdtrustglinesuggest, "Usage: trustglinesuggest <user@host>\nSuggests glines for the specified hostmask."); |
7b26c20e | 332 | registercontrolhelpcmd("trustspew", NO_OPER, 1, trusts_cmdtrustspew, "Usage: trustspew <#id|name>\nShows currently connected users for the specified trust group."); |
a99a2041 CP |
333 | } |
334 | ||
83bccee3 | 335 | static void deregistercommands(int hooknum, void *arg) { |
a99a2041 CP |
336 | if(!commandsregistered) |
337 | return; | |
338 | commandsregistered = 0; | |
339 | ||
a99a2041 | 340 | deregistercontrolcmd("trustlist", trusts_cmdtrustlist); |
82a316e7 | 341 | deregistercontrolcmd("trustdump", trusts_cmdtrustdump); |
8f128e0d | 342 | deregistercontrolcmd("trustglinesuggest", trusts_cmdtrustglinesuggest); |
7b26c20e | 343 | deregistercontrolcmd("trustspew", trusts_cmdtrustspew); |
be2823bc CP |
344 | } |
345 | ||
346 | void _init(void) { | |
a99a2041 | 347 | registerhook(HOOK_TRUSTS_DB_LOADED, registercommands); |
83bccee3 | 348 | registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands); |
be2823bc CP |
349 | |
350 | if(trustsdbloaded) | |
a99a2041 | 351 | registercommands(0, NULL); |
be2823bc CP |
352 | } |
353 | ||
354 | void _fini(void) { | |
a99a2041 | 355 | deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands); |
83bccee3 | 356 | deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands); |
be2823bc | 357 | |
83bccee3 CP |
358 | deregistercommands(0, NULL); |
359 | } |