]> jfr.im git - irc/quakenet/newserv.git/blame - trusts/trusts_commands.c
Implement non-expiring trustgroups.
[irc/quakenet/newserv.git] / trusts / trusts_commands.c
CommitLineData
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"
1f03587c 7#include "../irc/irc.h"
be2823bc
CP
8#include "trusts.h"
9
83bccee3
CP
10static void registercommands(int, void *);
11static void deregistercommands(int, void *);
be2823bc 12
dee7de1b 13void calculatespaces(int spaces, int width, char *str, char **_prebuf, char **_postbuf) {
b76fd8e6
CP
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
dee7de1b 24 spacelen = width - (strlen(str) + spaces);
b76fd8e6
CP
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
dee7de1b
CP
36static void traverseandmark(unsigned int marker, trusthost *th) {
37 th->marker = marker;
b76fd8e6 38
dee7de1b
CP
39 for(th=th->children;th;th=th->nextbychild) {
40 th->marker = marker;
41 traverseandmark(marker, th);
42 }
43}
b76fd8e6 44
82a316e7
CP
45static 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
dee7de1b
CP
60static void marktree(array *parents, unsigned int marker, trusthost *th) {
61 trusthost *pth;
82a316e7 62 int parentcount = 0;
dee7de1b
CP
63
64 for(pth=th->parent;pth;pth=pth->next) {
82a316e7 65 insertth(parents, pth);
dee7de1b
CP
66
67 pth->marker = marker;
b76fd8e6
CP
68 }
69
82a316e7
CP
70 if(parentcount == 0)
71 insertth(parents, th);
72
dee7de1b
CP
73 /* sadly we need to recurse down */
74 traverseandmark(marker, th);
75}
b76fd8e6 76
dee7de1b
CP
77static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth) {
78 char *cidrstr, *prespacebuf, *postspacebuf, parentbuf[512];
b76fd8e6 79
dee7de1b
CP
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) {
b76fd8e6
CP
87 prespacebuf[0] = '>';
88
89 parentbuf[0] = '\0';
90 } else {
91 /* show the ids of other groups */
92
dee7de1b 93 snprintf(parentbuf, sizeof(parentbuf), "%-10d %s", th->group->id, th->group->name->content);
b76fd8e6
CP
94 }
95
82a316e7 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);
b76fd8e6 97
dee7de1b
CP
98 for(th=th->children;th;th=th->nextbychild)
99 outputtree(np, marker, originalgroup, th, depth + 1);
b76fd8e6
CP
100}
101
ee77bc7a 102static void displaygroup(nick *sender, trustgroup *tg) {
dee7de1b 103 trusthost *th, **p2;
dee7de1b
CP
104 unsigned int marker;
105 array parents;
106 int i;
ee77bc7a 107 time_t t = time(NULL);
2d4ba67d 108
d36ca89c 109 /* abusing the ternary operator a bit :( */
4b003d19 110 controlreply(sender, "Name: : %s", tg->name->content);
2d4ba67d 111 controlreply(sender, "Trusted for : %d", tg->trustedfor);
1bbe1ac3 112 controlreply(sender, "Currently using : %d", tg->count);
2d4ba67d 113 controlreply(sender, "Clients per user : %d (%senforcing ident)", tg->maxperident, tg->mode?"":"not ");
4b003d19 114 controlreply(sender, "Contact: : %s", tg->contact->content);
ad3fd5ee 115 controlreply(sender, "Expires in : %s", (tg->expires)?((tg->expires>t)?longtoduration(tg->expires - t, 2):"(the past -- BUG)"):"never");
4b003d19
CP
116 controlreply(sender, "Last changed by : %s", tg->createdby->content);
117 controlreply(sender, "Comment: : %s", tg->comment->content);
2d4ba67d 118 controlreply(sender, "ID: : %u", tg->id);
d36ca89c 119 controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":((tg->lastseen>0)?trusts_timetostr(tg->lastseen):"(never)"));
4be1aaf2 120 controlreply(sender, "Max usage : %d", tg->maxusage);
1f685425 121 controlreply(sender, "Last max reset : %s", tg->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(never)");
2d4ba67d 122
82a316e7 123 controlreply(sender, "Host Current Max Last seen Group ID Group name");
2d4ba67d 124
dee7de1b
CP
125 marker = nextthmarker();
126 array_init(&parents, sizeof(trusthost *));
127
2d4ba67d 128 for(th=tg->hosts;th;th=th->next)
dee7de1b
CP
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);
2d4ba67d
CP
136
137 controlreply(sender, "End of list.");
ee77bc7a
CP
138}
139
140static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
141 nick *sender = source;
ee77bc7a 142 trustgroup *tg = NULL;
73653516
CP
143 int found = 0, remaining = 50;
144 char *name;
3e646f8f
GB
145 trusthost *th;
146 uint32_t ip;
147 short mask;
ee77bc7a
CP
148
149 if(cargc < 1)
150 return CMD_USAGE;
151
152 name = cargv[0];
153
73653516 154 tg = tg_strtotg(name);
ee77bc7a
CP
155
156 if(tg) {
157 displaygroup(sender, tg);
158 return CMD_OK;
159 }
160
3e646f8f
GB
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
ee77bc7a 173 for(tg=tglist;tg;tg=tg->next) {
c215a421 174 if(match(name, tg->name->content))
ee77bc7a
CP
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.");
2d4ba67d
CP
187
188 return CMD_OK;
189}
190
82a316e7
CP
191static 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
202static 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
0555113a 232 for(i=0,tg=tglist;i<totalcount&&tg;tg=tg->next,i++)
82a316e7
CP
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
1f03587c
GB
261static 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
299static 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
a99a2041
CP
329static int commandsregistered;
330
331static void registercommands(int hooknum, void *arg) {
332 if(commandsregistered)
333 return;
334 commandsregistered = 1;
335
01bd21d3 336 registercontrolhelpcmd("trustlist", NO_OPER, 1, trusts_cmdtrustlist, "Usage: trustlist <#id|name|IP>\nShows trust data for the specified trust group.");
82a316e7 337 registercontrolhelpcmd("trustdump", NO_OPER, 2, trusts_cmdtrustdump, "Usage: trustdump <#id> <number>");
1f03587c
GB
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.");
a99a2041
CP
340}
341
83bccee3 342static void deregistercommands(int hooknum, void *arg) {
a99a2041
CP
343 if(!commandsregistered)
344 return;
345 commandsregistered = 0;
346
a99a2041 347 deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
82a316e7 348 deregistercontrolcmd("trustdump", trusts_cmdtrustdump);
1f03587c
GB
349 deregistercontrolcmd("trustgline", trusts_cmdtrustgline);
350 deregistercontrolcmd("trustungline", trusts_cmdtrustungline);
be2823bc
CP
351}
352
353void _init(void) {
a99a2041 354 registerhook(HOOK_TRUSTS_DB_LOADED, registercommands);
83bccee3 355 registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
be2823bc
CP
356
357 if(trustsdbloaded)
a99a2041 358 registercommands(0, NULL);
be2823bc
CP
359}
360
361void _fini(void) {
a99a2041 362 deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
83bccee3 363 deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
be2823bc 364
83bccee3
CP
365 deregistercommands(0, NULL);
366}