]> jfr.im git - irc/quakenet/newserv.git/blame - trusts/trusts_commands.c
New module: nickwatch
[irc/quakenet/newserv.git] / trusts / trusts_commands.c
CommitLineData
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
14MODULE_VERSION("");
15
83bccee3
CP
16static void registercommands(int, void *);
17static void deregistercommands(int, void *);
be2823bc 18
7b26c20e
GB
19extern void printnick_channels(searchCtx *, nick *, nick *);
20
4dcce883 21void 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 44static 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
55static 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 70static 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 87static 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
121static 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
155static 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
a90eb846 166static void displaygroup(nick *sender, trustgroup *tg, int showchildren) {
34e3de85
GB
167 trusthost *th, **p2;
168 unsigned int marker;
169 array parents;
170 int i;
acd5f58f 171 time_t t = getnettime();
2d4ba67d 172
d36ca89c 173 /* abusing the ternary operator a bit :( */
4b003d19 174 controlreply(sender, "Name: : %s", tg->name->content);
3a8c35c9 175 controlreply(sender, "Trusted for : %s", formatlimit(tg->trustedfor));
1bbe1ac3 176 controlreply(sender, "Currently using : %d", tg->count);
3a8c35c9
GB
177 controlreply(sender, "Clients per user : %s", formatlimit(tg->maxperident));
178 controlreply(sender, "Flags : %s", formatflags(tg->flags));
4b003d19 179 controlreply(sender, "Contact: : %s", tg->contact->content);
9afc3262 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");
0daf3b9f 181 controlreply(sender, "Created by : %s", tg->createdby->content);
4b003d19 182 controlreply(sender, "Comment: : %s", tg->comment->content);
2d4ba67d 183 controlreply(sender, "ID: : %u", tg->id);
d36ca89c 184 controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":((tg->lastseen>0)?trusts_timetostr(tg->lastseen):"(never)"));
4be1aaf2 185 controlreply(sender, "Max usage : %d", tg->maxusage);
1f685425 186 controlreply(sender, "Last max reset : %s", tg->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(never)");
2d4ba67d 187
a90eb846 188 controlreply(sender, "---");
99ebf8d1 189 controlreply(sender, "Attributes: * (has hidden children, show with -v), > (belongs to this trust group)");
69876096 190 controlreply(sender, "Host Current Max Last seen Max per Node Node Mask Group ID Group name");
dee7de1b 191
34e3de85
GB
192 marker = nextthmarker();
193 array_init(&parents, sizeof(trusthost *));
dee7de1b 194
34e3de85 195 for(th=tg->hosts;th;th=th->next)
a90eb846 196 marktree(&parents, marker, th, showchildren);
34e3de85
GB
197
198 p2 = (trusthost **)(parents.content);
199 for(i=0;i<parents.cursi;i++)
a90eb846 200 outputtree(sender, marker, tg, p2[i], 0, showchildren);
34e3de85
GB
201
202 array_free(&parents);
2d4ba67d
CP
203
204 controlreply(sender, "End of list.");
ee77bc7a
CP
205}
206
207static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
208 nick *sender = source;
ee77bc7a 209 trustgroup *tg = NULL;
73653516
CP
210 int found = 0, remaining = 50;
211 char *name;
3e646f8f 212 trusthost *th;
6e6e98da
GB
213 struct irc_in_addr ip;
214 unsigned char bits;
a90eb846 215 int showchildren;
ee77bc7a
CP
216
217 if(cargc < 1)
218 return CMD_USAGE;
219
a90eb846
GB
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 }
ee77bc7a 230
73653516 231 tg = tg_strtotg(name);
ee77bc7a
CP
232
233 if(tg) {
a90eb846 234 displaygroup(sender, tg, showchildren);
ee77bc7a
CP
235 return CMD_OK;
236 }
237
6e6e98da
GB
238 if(ipmask_parse(name, &ip, &bits)) {
239 th = th_getbyhost(&ip);
3e646f8f
GB
240
241 if(!th) {
242 controlreply(sender, "Specified IP address is not trusted.");
243 return CMD_OK;
244 }
245
a90eb846 246 displaygroup(sender, th->group, showchildren);
3e646f8f
GB
247 return CMD_OK;
248 }
249
ee77bc7a 250 for(tg=tglist;tg;tg=tg->next) {
c215a421 251 if(match(name, tg->name->content))
ee77bc7a
CP
252 continue;
253
a90eb846 254 displaygroup(sender, tg, showchildren);
ee77bc7a
CP
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.");
2d4ba67d
CP
264
265 return CMD_OK;
266}
267
8f128e0d 268static int trusts_cmdtrustglinesuggest(void *source, int cargc, char **cargv) {
1f03587c 269 nick *sender = source;
8f128e0d
GB
270 char mask[512];
271 char *p, *user, *host;
272 struct irc_in_addr ip;
273 unsigned char bits;
ac3af088 274 int count;
a86fc0c4
GB
275 glinebuf gbuf;
276 char creator[32];
1f03587c 277
8f128e0d 278 if(cargc < 1)
1f03587c
GB
279 return CMD_USAGE;
280
8f128e0d
GB
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.");
1f03587c
GB
294 return CMD_ERROR;
295 }
296
a86fc0c4
GB
297 snprintf(creator, sizeof(creator), "#%s", sender->authname);
298
324b4e11 299 glinebufinit(&gbuf, 0);
a86fc0c4 300 glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, "Simulate", getnettime(), getnettime(), getnettime());
ac80e3ab 301 glinebufcounthits(&gbuf, &count, NULL);
a86fc0c4 302 glinebufspew(&gbuf, sender);
0b2e8a55 303 glinebufabort(&gbuf);
1f03587c 304
8f128e0d 305 controlreply(sender, "Total hits: %d", count);
1f03587c
GB
306
307 return CMD_OK;
308}
309
7b26c20e
GB
310static 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]));
4860501e 318 return ast_nicksearch(&tree, controlreply, sender, NULL, printnick_channels, NULL, NULL, 2000, NULL);
7b26c20e
GB
319}
320
a99a2041
CP
321static int commandsregistered;
322
323static void registercommands(int hooknum, void *arg) {
324 if(commandsregistered)
325 return;
326 commandsregistered = 1;
327
a90eb846 328 registercontrolhelpcmd("trustlist", NO_OPER, 2, trusts_cmdtrustlist, "Usage: trustlist [-v] <#id|name|IP>\nShows trust data for the specified trust group.");
8f128e0d 329 registercontrolhelpcmd("trustglinesuggest", NO_OPER, 1, trusts_cmdtrustglinesuggest, "Usage: trustglinesuggest <user@host>\nSuggests glines for the specified hostmask.");
7b26c20e 330 registercontrolhelpcmd("trustspew", NO_OPER, 1, trusts_cmdtrustspew, "Usage: trustspew <#id|name>\nShows currently connected users for the specified trust group.");
a99a2041
CP
331}
332
83bccee3 333static void deregistercommands(int hooknum, void *arg) {
a99a2041
CP
334 if(!commandsregistered)
335 return;
336 commandsregistered = 0;
337
a99a2041 338 deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
8f128e0d 339 deregistercontrolcmd("trustglinesuggest", trusts_cmdtrustglinesuggest);
7b26c20e 340 deregistercontrolcmd("trustspew", trusts_cmdtrustspew);
be2823bc
CP
341}
342
343void _init(void) {
a99a2041 344 registerhook(HOOK_TRUSTS_DB_LOADED, registercommands);
83bccee3 345 registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
be2823bc
CP
346
347 if(trustsdbloaded)
a99a2041 348 registercommands(0, NULL);
be2823bc
CP
349}
350
351void _fini(void) {
a99a2041 352 deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
83bccee3 353 deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
be2823bc 354
83bccee3
CP
355 deregistercommands(0, NULL);
356}