]> jfr.im git - irc/quakenet/newserv.git/blob - trusts/trusts_commands.c
baa7f1544aa03c47a9214dc02828e42a1402bbf4
[irc/quakenet/newserv.git] / trusts / trusts_commands.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "../lib/version.h"
4 #include "../control/control.h"
5 #include "../lib/irc_string.h"
6 #include "../lib/strlfunc.h"
7 #include "../core/nsmalloc.h"
8 #include "../irc/irc.h"
9 #include "../newsearch/newsearch.h"
10 #include "../glines/glines.h"
11 #include "trusts.h"
12 #include "newsearch/trusts_newsearch.h"
13
14 MODULE_VERSION("");
15
16 static void registercommands(int, void *);
17 static void deregistercommands(int, void *);
18
19 extern void printnick_channels(searchCtx *, nick *, nick *);
20
21 void calculatespaces(int spaces, int width, const 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
44 static void traverseandmark(unsigned int marker, trusthost *th, int markchildren) {
45 th->marker = marker;
46
47 if(markchildren) {
48 for(th=th->children;th;th=th->nextbychild) {
49 th->marker = marker;
50 traverseandmark(marker, th, markchildren);
51 }
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
70 static void marktree(array *parents, unsigned int marker, trusthost *th, int showchildren) {
71 trusthost *pth;
72 int parentcount = 0;
73
74 for(pth=th->parent;pth;pth=pth->parent) {
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 */
84 traverseandmark(marker, th, showchildren);
85 }
86
87 static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth, int showchildren) {
88 const char *cidrstr;
89 char *prespacebuf, *postspacebuf, parentbuf[512];
90
91 if(th->marker != marker)
92 return;
93
94 cidrstr = CIDRtostr(th->ip, th->bits);
95 calculatespaces(depth + 2, 30 + 1, cidrstr, &prespacebuf, &postspacebuf);
96
97 if(th->group == originalgroup) {
98 if(!showchildren && th->group == originalgroup && th->children)
99 prespacebuf[0] = '*';
100 else
101 prespacebuf[0] = ' ';
102
103 prespacebuf[1] = '>';
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
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);
113
114 /* Make sure we're not seeing this subtree again. */
115 th->marker = -1;
116
117 for(th=th->children;th;th=th->nextbychild)
118 outputtree(np, marker, originalgroup, th, depth + 1, showchildren);
119 }
120
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
150 buf[512-1] = '\0';
151
152 return buf;
153 }
154
155 static char *formatlimit(unsigned int limit) {
156 static char buf[64];
157
158 if(limit)
159 snprintf(buf, sizeof(buf), "%u", limit);
160 else
161 strncpy(buf, "unlimited", sizeof(buf));
162
163 return buf;
164 }
165
166 static void displaygroup(nick *sender, trustgroup *tg, int showchildren) {
167 trusthost *th, **p2;
168 unsigned int marker;
169 array parents;
170 int i;
171 time_t t = getnettime();
172
173 /* abusing the ternary operator a bit :( */
174 controlreply(sender, "Name: : %s", tg->name->content);
175 controlreply(sender, "Trusted for : %s", formatlimit(tg->trustedfor));
176 controlreply(sender, "Currently using : %d", tg->count);
177 controlreply(sender, "Clients per user : %s", formatlimit(tg->maxperident));
178 controlreply(sender, "Flags : %s", formatflags(tg->flags));
179 controlreply(sender, "Contact: : %s", tg->contact->content);
180 controlreply(sender, "Expires in : %s", (tg->expires)?((tg->expires>t)?longtoduration(tg->expires - t, 2):"the past (will be removed during next cleanup)"):"never");
181 controlreply(sender, "Created by : %s", tg->createdby->content);
182 controlreply(sender, "Comment: : %s", tg->comment->content);
183 controlreply(sender, "ID: : %u", tg->id);
184 controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":((tg->lastseen>0)?trusts_timetostr(tg->lastseen):"(never)"));
185 controlreply(sender, "Max usage : %d", tg->maxusage);
186 controlreply(sender, "Last max reset : %s", tg->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(never)");
187
188 controlreply(sender, "---");
189 controlreply(sender, "Attributes: * (has hidden children, show with -v), > (belongs to this trust group)");
190 controlreply(sender, "Host Current Max Last seen Max per Node Node Mask Group ID Group name");
191
192 marker = nextthmarker();
193 array_init(&parents, sizeof(trusthost *));
194
195 for(th=tg->hosts;th;th=th->next)
196 marktree(&parents, marker, th, showchildren);
197
198 p2 = (trusthost **)(parents.content);
199 for(i=0;i<parents.cursi;i++)
200 outputtree(sender, marker, tg, p2[i], 0, showchildren);
201
202 array_free(&parents);
203
204 controlreply(sender, "End of list.");
205 }
206
207 static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
208 nick *sender = source;
209 trustgroup *tg = NULL;
210 int found = 0, remaining = 50;
211 char *name;
212 trusthost *th;
213 struct irc_in_addr ip;
214 unsigned char bits;
215 int showchildren;
216
217 if(cargc < 1)
218 return CMD_USAGE;
219
220 if(strcmp(cargv[0], "-v") == 0) {
221 if(cargc < 2)
222 return CMD_USAGE;
223
224 showchildren = 1;
225 name = cargv[1];
226 } else {
227 showchildren = 0;
228 name = cargv[0];
229 }
230
231 tg = tg_strtotg(name);
232
233 if(tg) {
234 displaygroup(sender, tg, showchildren);
235 return CMD_OK;
236 }
237
238 if(ipmask_parse(name, &ip, &bits)) {
239 th = th_getbyhost(&ip);
240
241 if(!th) {
242 controlreply(sender, "Specified IP address is not trusted.");
243 return CMD_OK;
244 }
245
246 displaygroup(sender, th->group, showchildren);
247 return CMD_OK;
248 }
249
250 for(tg=tglist;tg;tg=tg->next) {
251 if(match(name, tg->name->content))
252 continue;
253
254 displaygroup(sender, tg, showchildren);
255 if(--remaining == 0) {
256 controlreply(sender, "Maximum number of matches reached.");
257 return CMD_OK;
258 }
259 found = 1;
260 }
261
262 if(!found)
263 controlreply(sender, "No matches found.");
264
265 return CMD_OK;
266 }
267
268 static int trusts_cmdtrustglinesuggest(void *source, int cargc, char **cargv) {
269 nick *sender = source;
270 char mask[512];
271 char *p, *user, *host;
272 struct irc_in_addr ip;
273 unsigned char bits;
274 int count;
275 glinebuf gbuf;
276 char creator[32];
277
278 if(cargc < 1)
279 return CMD_USAGE;
280
281 strncpy(mask, cargv[0], sizeof(mask));
282
283 p = strchr(mask, '@');
284
285 if(!p)
286 return CMD_USAGE;
287
288 user = mask;
289 host = p + 1;
290 *p = '\0';
291
292 if(!ipmask_parse(host, &ip, &bits)) {
293 controlreply(sender, "Invalid CIDR.");
294 return CMD_ERROR;
295 }
296
297 snprintf(creator, sizeof(creator), "#%s", sender->authname);
298
299 glinebufinit(&gbuf, 0);
300 glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, "Simulate", getnettime(), getnettime(), getnettime());
301 glinebufcounthits(&gbuf, &count, NULL);
302 glinebufspew(&gbuf, sender);
303 glinebufabort(&gbuf);
304
305 controlreply(sender, "Total hits: %d", count);
306
307 return CMD_OK;
308 }
309
310 static int trusts_cmdtrustspew(void *source, int cargc, char **cargv) {
311 nick *sender = source;
312 searchASTExpr tree;
313
314 if(cargc < 1)
315 return CMD_USAGE;
316
317 tree = NSASTNode(tgroup_parse, NSASTLiteral(cargv[0]));
318 return ast_nicksearch(&tree, controlreply, sender, NULL, printnick_channels, NULL, NULL, 2000);
319 }
320
321 static int commandsregistered;
322
323 static void registercommands(int hooknum, void *arg) {
324 if(commandsregistered)
325 return;
326 commandsregistered = 1;
327
328 registercontrolhelpcmd("trustlist", NO_OPER, 2, trusts_cmdtrustlist, "Usage: trustlist [-v] <#id|name|IP>\nShows trust data for the specified trust group.");
329 registercontrolhelpcmd("trustglinesuggest", NO_OPER, 1, trusts_cmdtrustglinesuggest, "Usage: trustglinesuggest <user@host>\nSuggests glines for the specified hostmask.");
330 registercontrolhelpcmd("trustspew", NO_OPER, 1, trusts_cmdtrustspew, "Usage: trustspew <#id|name>\nShows currently connected users for the specified trust group.");
331 }
332
333 static void deregistercommands(int hooknum, void *arg) {
334 if(!commandsregistered)
335 return;
336 commandsregistered = 0;
337
338 deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
339 deregistercontrolcmd("trustglinesuggest", trusts_cmdtrustglinesuggest);
340 deregistercontrolcmd("trustspew", trusts_cmdtrustspew);
341 }
342
343 void _init(void) {
344 registerhook(HOOK_TRUSTS_DB_LOADED, registercommands);
345 registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
346
347 if(trustsdbloaded)
348 registercommands(0, NULL);
349 }
350
351 void _fini(void) {
352 deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
353 deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
354
355 deregistercommands(0, NULL);
356 }