]> jfr.im git - irc/quakenet/newserv.git/blob - trusts/trusts_commands.c
Implement pernodemax/nodebits for THs.
[irc/quakenet/newserv.git] / trusts / trusts_commands.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "../control/control.h"
4 #include "../lib/irc_string.h"
5 #include "../lib/strlfunc.h"
6 #include "../core/nsmalloc.h"
7 #include "trusts.h"
8
9 static void registercommands(int, void *);
10 static void deregistercommands(int, void *);
11
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 int mask;
79
80 if(th->marker != marker)
81 return;
82
83 cidrstr = trusts_cidr2str(th->ip, th->mask);
84 calculatespaces(depth + 1, 20 + 1, cidrstr, &prespacebuf, &postspacebuf);
85
86 if(th->group == originalgroup) {
87 prespacebuf[0] = '>';
88
89 parentbuf[0] = '\0';
90 } else {
91 /* show the ids of other groups */
92
93 snprintf(parentbuf, sizeof(parentbuf), "%-10d %s", th->group->id, th->group->name->content);
94 }
95
96 /*if (!ipv6) */
97 mask = th->nodebits - 96;
98
99 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, mask, parentbuf);
100
101 for(th=th->children;th;th=th->nextbychild)
102 outputtree(np, marker, originalgroup, th, depth + 1);
103 }
104
105 static void displaygroup(nick *sender, trustgroup *tg) {
106 trusthost *th, **p2;
107 unsigned int marker;
108 array parents;
109 int i;
110 time_t t = time(NULL);
111
112 /* abusing the ternary operator a bit :( */
113 controlreply(sender, "Name: : %s", tg->name->content);
114 controlreply(sender, "Trusted for : %d", tg->trustedfor);
115 controlreply(sender, "Currently using : %d", tg->count);
116 controlreply(sender, "Clients per user : %d (%senforcing ident)", tg->maxperident, tg->mode?"":"not ");
117 controlreply(sender, "Contact: : %s", tg->contact->content);
118 controlreply(sender, "Expires in : %s", (tg->expires)?((tg->expires>t)?longtoduration(tg->expires - t, 2):"(the past -- BUG)"):"never");
119 controlreply(sender, "Last changed by : %s", tg->createdby->content);
120 controlreply(sender, "Comment: : %s", tg->comment->content);
121 controlreply(sender, "ID: : %u", tg->id);
122 controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":((tg->lastseen>0)?trusts_timetostr(tg->lastseen):"(never)"));
123 controlreply(sender, "Max usage : %d", tg->maxusage);
124 controlreply(sender, "Last max reset : %s", tg->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(never)");
125
126 controlreply(sender, "Host Current Max Last seen Max per Node Node Mask Group ID Group name");
127
128 marker = nextthmarker();
129 array_init(&parents, sizeof(trusthost *));
130
131 for(th=tg->hosts;th;th=th->next)
132 marktree(&parents, marker, th);
133
134 p2 = (trusthost **)(parents.content);
135 for(i=0;i<parents.cursi;i++)
136 outputtree(sender, marker, tg, p2[i], 0);
137
138 array_free(&parents);
139
140 controlreply(sender, "End of list.");
141 }
142
143 static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
144 nick *sender = source;
145 trustgroup *tg = NULL;
146 int found = 0, remaining = 50;
147 char *name;
148 trusthost *th;
149 uint32_t ip;
150 short mask;
151
152 if(cargc < 1)
153 return CMD_USAGE;
154
155 name = cargv[0];
156
157 tg = tg_strtotg(name);
158
159 if(tg) {
160 displaygroup(sender, tg);
161 return CMD_OK;
162 }
163
164 if(trusts_parsecidr(name, &ip, &mask)) {
165 th = th_getbyhost(ip);
166
167 if(!th) {
168 controlreply(sender, "Specified IP address is not trusted.");
169 return CMD_OK;
170 }
171
172 displaygroup(sender, th->group);
173 return CMD_OK;
174 }
175
176 for(tg=tglist;tg;tg=tg->next) {
177 if(match(name, tg->name->content))
178 continue;
179
180 displaygroup(sender, tg);
181 if(--remaining == 0) {
182 controlreply(sender, "Maximum number of matches reached.");
183 return CMD_OK;
184 }
185 found = 1;
186 }
187
188 if(!found)
189 controlreply(sender, "No matches found.");
190
191 return CMD_OK;
192 }
193
194 static int comparetgs(const void *_a, const void *_b) {
195 const trustgroup *a = _a;
196 const trustgroup *b = _b;
197
198 if(a->id > b->id)
199 return 1;
200 if(a->id < b-> id)
201 return -1;
202 return 0;
203 }
204
205 static int trusts_cmdtrustdump(void *source, int argc, char **argv) {
206 trusthost *th;
207 trustgroup *tg, **atg;
208 unsigned int wanted, max, maxid, totalcount, i, groupcount, linecount;
209 nick *np = source;
210
211 if((argc < 2) || (argv[0][0] != '#'))
212 return CMD_USAGE;
213
214 wanted = atoi(&argv[0][1]);
215 max = atoi(argv[1]);
216
217 for(maxid=totalcount=0,tg=tglist;tg;tg=tg->next) {
218 if(totalcount == 0 || tg->id > maxid)
219 maxid = tg->id;
220
221 totalcount++;
222 }
223
224 if(maxid > totalcount) {
225 controlreply(np, "Start ID cannot exceed current maximum group ID (#%u)", maxid);
226 return CMD_OK;
227 }
228
229 atg = nsmalloc(POOL_TRUSTS, sizeof(trusthost *) * totalcount);
230 if(!atg) {
231 controlreply(np, "Memory error.");
232 return CMD_ERROR;
233 }
234
235 for(i=0,tg=tglist;i<totalcount&&tg;tg=tg->next,i++)
236 atg[i] = tg;
237
238 qsort(atg, totalcount, sizeof(trustgroup *), comparetgs);
239
240 for(i=0;i<totalcount;i++)
241 if(atg[i]->id >= wanted)
242 break;
243
244 for(groupcount=linecount=0;i<totalcount;i++) {
245 linecount++;
246 groupcount++;
247
248 controlreply(np, "G,%s", dumptg(atg[i], 1));
249
250 for(th=atg[i]->hosts;th;th=th->next) {
251 linecount++;
252 controlreply(np, "H,%s", dumpth(th, 1));
253 }
254
255 if(--max == 0)
256 break;
257 }
258 nsfree(POOL_TRUSTS, atg);
259
260 controlreply(np, "End of list, %u groups and %u lines returned.", groupcount, linecount);
261 return CMD_OK;
262 }
263
264 static int trusts_cmdtrustgline(void *source, int cargc, char **cargv) {
265 trustgroup *tg;
266 nick *sender = source;
267 char *user, *reason;
268 int duration, count;
269
270 if(cargc < 4)
271 return CMD_USAGE;
272
273 tg = tg_strtotg(cargv[0]);
274 if(!tg) {
275 controlreply(sender, "Couldn't look up trustgroup.");
276 return CMD_ERROR;
277 }
278
279 user = cargv[1];
280
281 duration = durationtolong(cargv[2]);
282 if((duration <= 0) || (duration > MAXDURATION)) {
283 controlreply(sender, "Invalid duration supplied.");
284 return CMD_ERROR;
285 }
286
287 reason = cargv[3];
288
289 count = trustgline(tg, user, duration, reason);
290
291 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);
292 controlreply(sender, "Done. %d gline(s) set.", count);
293
294 return CMD_OK;
295 }
296
297 static int trusts_cmdtrustungline(void *source, int cargc, char **cargv) {
298 trustgroup *tg;
299 nick *sender = source;
300 char *user;
301 int count;
302
303 if(cargc < 2)
304 return CMD_USAGE;
305
306 tg = tg_strtotg(cargv[0]);
307 if(!tg) {
308 controlreply(sender, "Couldn't look up trustgroup.");
309 return CMD_ERROR;
310 }
311
312 user = cargv[1];
313
314 count = trustungline(tg, user, 0, "Deactivated.");
315
316 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);
317 controlreply(sender, "Done. %d gline(s) removed.", count);
318
319 return CMD_OK;
320 }
321
322 static int commandsregistered;
323
324 static void registercommands(int hooknum, void *arg) {
325 if(commandsregistered)
326 return;
327 commandsregistered = 1;
328
329 registercontrolhelpcmd("trustlist", NO_OPER, 1, trusts_cmdtrustlist, "Usage: trustlist <#id|name|IP>\nShows trust data for the specified trust group.");
330 registercontrolhelpcmd("trustdump", NO_OPER, 2, trusts_cmdtrustdump, "Usage: trustdump <#id> <number>");
331 registercontrolhelpcmd("trustgline", NO_OPER, 4, trusts_cmdtrustgline, "Usage: trustgline <#id|name> <user> <duration> <reason>\nGlines a user on all hosts of a trust group.");
332 registercontrolhelpcmd("trustungline", NO_OPER, 4, trusts_cmdtrustungline, "Usage: trustungline <#id|name> <user>\nUnglines a user on all hosts of a trust group.");
333 }
334
335 static void deregistercommands(int hooknum, void *arg) {
336 if(!commandsregistered)
337 return;
338 commandsregistered = 0;
339
340 deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
341 deregistercontrolcmd("trustdump", trusts_cmdtrustdump);
342 deregistercontrolcmd("trustgline", trusts_cmdtrustgline);
343 deregistercontrolcmd("trustungline", trusts_cmdtrustungline);
344 }
345
346 void _init(void) {
347 registerhook(HOOK_TRUSTS_DB_LOADED, registercommands);
348 registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
349
350 if(trustsdbloaded)
351 registercommands(0, NULL);
352 }
353
354 void _fini(void) {
355 deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
356 deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
357
358 deregistercommands(0, NULL);
359 }