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