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