]>
Commit | Line | Data |
---|---|---|
e2527cba P |
1 | #include "../core/schedule.h" |
2 | #include "../lib/irc_string.h" | |
3 | #include "../localuser/localuserchannel.h" | |
4 | #include "../control/control.h" | |
5 | #include "trusts.h" | |
6 | #include <string.h> | |
9a8ffb84 P |
7 | #include <stdlib.h> |
8 | #include <stdarg.h> | |
e2527cba | 9 | |
9a8ffb84 P |
10 | void _init(void) { |
11 | registerhook(HOOK_TRUSTS_DBLOADED, trusts_cmdinit); | |
12 | ||
13 | /* Now that the database is in a separate module it might be loaded already. */ | |
14 | if (trusts_loaded) | |
15 | trusts_cmdinit(HOOK_TRUSTS_DBLOADED, NULL); | |
16 | ||
17 | } | |
18 | ||
19 | void _fini(void) { | |
20 | deregisterhook(HOOK_TRUSTS_DBLOADED, trusts_cmdinit); | |
21 | trusts_cmdfini(); | |
22 | } | |
23 | ||
24 | void trusts_cmdinit(int hooknum, void *arg) { | |
e2527cba P |
25 | registercontrolcmd("trustgroupadd",10,7,trust_groupadd); |
26 | registercontrolcmd("trustgroupmodify",10,4,trust_groupmodify); | |
27 | registercontrolcmd("trustgroupdel",10,2,trust_groupdel); | |
28 | ||
29 | registercontrolcmd("trustcomment",10,2,trust_comment); | |
30 | ||
31 | registercontrolcmd("trustadd",10,3,trust_add); | |
32 | registercontrolcmd("trustdel",10,2,trust_del); | |
33 | ||
34 | registercontrolcmd("trustdenyadd",10,2,trust_denyadd); | |
35 | registercontrolcmd("trustdenylist",10,2,trust_denylist); | |
36 | registercontrolcmd("trustdenydel",10,2,trust_denydel); | |
37 | ||
38 | registercontrolcmd("truststats",10,2,trust_stats); | |
39 | registercontrolcmd("trustdump",10,2,trust_dump); | |
9a8ffb84 P |
40 | |
41 | removeusers = 1; | |
e2527cba P |
42 | } |
43 | ||
44 | void trusts_cmdfini() { | |
45 | deregistercontrolcmd("trustgroupadd",trust_groupadd); | |
46 | deregistercontrolcmd("trustgroupmodify",trust_groupmodify); | |
47 | deregistercontrolcmd("trustgroupdel",trust_groupdel); | |
48 | ||
49 | deregistercontrolcmd("trustcomment",trust_comment); | |
50 | ||
51 | deregistercontrolcmd("trustadd",trust_add); | |
52 | deregistercontrolcmd("trustdel",trust_del); | |
53 | ||
54 | deregistercontrolcmd("trustdenyadd",trust_denyadd); | |
55 | deregistercontrolcmd("trustdenylist",trust_denylist); | |
56 | deregistercontrolcmd("trustdenydel",trust_denydel); | |
57 | ||
58 | deregistercontrolcmd("truststats",trust_stats); | |
59 | deregistercontrolcmd("trustdump",trust_dump); | |
9a8ffb84 P |
60 | |
61 | removeusers = 0; | |
e2527cba P |
62 | } |
63 | ||
64 | /*TODO*/ | |
65 | /* tgh - should this have a 'maxclones limit'? */ | |
66 | ||
67 | int trust_groupadd(void *source, int cargc, char **cargv) { | |
68 | nick *sender=(nick *)source; | |
69 | int expiry; | |
70 | unsigned long maxclones; | |
71 | unsigned short maxperip; | |
72 | unsigned long maxperident; | |
73 | int enforceident; | |
74 | int type; | |
75 | unsigned long ownerid; | |
76 | trustgroup_t *t; | |
77 | ||
78 | if (cargc < 7) { | |
79 | controlreply(sender,"Usage: trustgroupadd howmany howlong maxperident maxperip enforceident type ownerid"); | |
80 | return CMD_ERROR; | |
81 | } | |
82 | ||
83 | maxclones = strtoul(cargv[0],NULL,10); | |
84 | if ( maxclones > 10000 ) { | |
85 | /* we allow 0 for unlimited trusts, and only warn on this */ | |
86 | controlreply(sender, "WARNING: large maximum number of clients - %lu", maxclones); | |
87 | } | |
88 | expiry = durationtolong(cargv[1]); | |
89 | if (expiry > (365 * 86400) ) { | |
90 | controlreply(sender,"ERROR: Invalid duration given - temporary trusts can not be longer then 1 year"); | |
91 | return CMD_ERROR; | |
92 | } | |
93 | ownerid = strtoul(cargv[6],NULL,10); | |
94 | maxperip = strtoul(cargv[3],NULL,10); | |
95 | if (maxperip > 500) { | |
96 | controlreply(sender, "ERROR: MaxPerIP value should be less then 500 (if set)"); | |
97 | return CMD_ERROR; | |
98 | } | |
99 | maxperident = strtoul(cargv[2],NULL,10); | |
100 | if (maxperident > 50) { | |
101 | controlreply(sender, "ERROR: MaxPerIdent value should be less then 50 (if set)"); | |
102 | return CMD_ERROR; | |
103 | } | |
104 | if (((cargv[4][0]!='0') && (cargv[4][0]!='1')) || (cargv[4][1]!='\0')) { | |
105 | controlreply(sender,"ERROR: enforceident is a boolean setting, that means it can only be 0 or 1"); | |
106 | return CMD_ERROR; | |
107 | } | |
108 | enforceident = cargv[4][0] == '1'; | |
109 | ||
110 | if ( findtrustgroupbyownerid(ownerid) ) { | |
111 | controlreply(sender, "ERROR: Q User ID %d already has a trustgroup", ownerid); | |
112 | return CMD_ERROR; | |
113 | } | |
114 | if (ownerid > 2147483646 ) { | |
115 | controlreply(sender, "ERROR: Invalid Q User ID: %d", ownerid); | |
116 | return CMD_ERROR; | |
117 | } | |
118 | ||
119 | type = strtoul(cargv[5],NULL,10); | |
120 | ||
121 | /* check rules */ | |
122 | switch (type ) { | |
123 | case 0: | |
124 | break; | |
125 | default: | |
126 | controlreply(sender, "Invalid Type (%d)", type); | |
127 | return CMD_ERROR; | |
128 | } | |
129 | ||
130 | t = createtrustgroup( ++trusts_lasttrustgroupid, maxclones, maxperident, maxperip, enforceident, getnettime() + expiry, ownerid, type); | |
131 | ||
132 | if(!t) { | |
133 | controlreply(sender,"ERROR: An error occured adding trustgroup"); | |
134 | return CMD_ERROR; | |
135 | } | |
136 | ||
137 | trustsdb_addtrustgroup(t); | |
138 | ||
139 | controlreply(sender,"Adding trustgroup with ID %lu", t->id); | |
140 | controlreply(sender,"Connections: %d, Enforceident %d, Per ident: %d, Per IP %d",maxclones,enforceident,maxperident,maxperip); | |
141 | controlreply(sender,"Expires: %d, User ID: %d", expiry, ownerid); | |
142 | controlwall(NO_OPER, NL_TRUSTS, "NewTrust: ID: %lu, Connections: %d, Enforceident %d, Per ident: %d, Per IP %d, Owner %d", t->id,maxclones,enforceident,maxperident,maxperip, ownerid); | |
143 | return CMD_OK; | |
144 | } | |
145 | ||
146 | int trust_del(void *source, int cargc, char **cargv) { | |
147 | nick *sender=(nick *)source; | |
148 | struct irc_in_addr sin; | |
149 | unsigned char bits; | |
150 | patricia_node_t *node; | |
151 | trustgroup_t *tg; | |
152 | ||
153 | if (cargc<1) { | |
154 | controlreply(sender,"Syntax: trustdel IP[/mask]"); | |
155 | return CMD_OK; | |
156 | } | |
157 | ||
158 | if (ipmask_parse(cargv[0], &sin, &bits) == 0) { | |
159 | controlreply(sender, "ERROR: Invalid mask."); | |
160 | return CMD_ERROR; | |
161 | } | |
162 | ||
163 | node = refnode(iptree, &sin, bits); | |
164 | if(!node->exts[tgh_ext]) { | |
165 | controlreply(sender,"ERROR: That CIDR was not trusted."); | |
166 | return CMD_ERROR; | |
167 | } else { | |
168 | /*TODO: only allow a host to be removed if <X users? subnets? bah */ | |
169 | tg = ((trusthost_t *)node->exts[tgh_ext])->trustgroup; | |
170 | controlreply(sender,"%s removed from trustgroup #%lu",cargv[0],tg->id); | |
171 | controlwall(NO_OPER, NL_TRUSTS, "%s removed from trustgroup #%lu",cargv[0],tg->id); | |
172 | trustsdb_deletetrusthost(node->exts[tgh_ext]); | |
173 | trusthost_free(node->exts[tgh_ext]); | |
174 | node->exts[tgh_ext] = NULL; | |
175 | } | |
176 | return CMD_OK; | |
177 | } | |
178 | ||
179 | int trust_add(void *source, int cargc, char **cargv) { | |
180 | nick *sender=(nick *)source; | |
181 | trustgroup_t *tg; | |
182 | struct irc_in_addr sin; | |
183 | unsigned char bits; | |
184 | patricia_node_t *node, *inode, *parent; | |
185 | int expiry = 0; | |
186 | trusthost_t *th; | |
187 | ||
188 | if (cargc<2) { | |
189 | controlreply(sender,"Syntax: trustadd <#groupid> IP[/mask] <duration>"); | |
190 | return CMD_OK; | |
191 | } | |
192 | ||
193 | if(cargv[0][0]== '#'){ | |
194 | /* find group by id */ | |
195 | tg=findtrustgroupbyid(strtol(&cargv[0][1],NULL,10)); | |
196 | } else { | |
197 | /* find group by id */ | |
198 | tg=findtrustgroupbyid(strtol(cargv[0],NULL,10)); | |
199 | } | |
200 | ||
201 | if(tg == NULL) { | |
202 | controlreply(sender,"ERROR: A trustgroup with that ID does not exist."); | |
203 | return CMD_ERROR; | |
204 | } | |
205 | ||
206 | if (tg->id==0) { | |
207 | controlreply(sender,"INTERNAL ERROR: Trustgroup has ID 0"); | |
208 | return CMD_ERROR; | |
209 | } | |
210 | ||
211 | if (ipmask_parse(cargv[1], &sin, &bits) == 0) { | |
212 | controlreply(sender, "ERROR: Invalid mask."); | |
213 | return CMD_ERROR; | |
214 | } | |
215 | ||
216 | if ( irc_in_addr_is_ipv4(&sin) ) { | |
217 | if (bits>128 || bits<112) { | |
218 | controlreply(sender,"ERROR: Not a valid netmask (needs to be between 8 and 32)"); | |
219 | return CMD_ERROR; | |
220 | } | |
221 | } else { | |
222 | if ( bits<64) { | |
223 | controlreply(sender,"ERROR: Not a valid ipv6 netmask "); | |
224 | return CMD_ERROR; | |
225 | } | |
226 | } | |
227 | ||
228 | if (cargc == 3) { | |
229 | expiry = getnettime() + durationtolong(cargv[2]); | |
230 | if (expiry<1) { | |
231 | controlreply(sender,"ERROR: Invalid duration given"); | |
232 | return CMD_ERROR; | |
233 | } | |
234 | } | |
235 | ||
236 | node = refnode(iptree, &sin, bits); | |
237 | if(node->exts[tgh_ext]) { | |
238 | /* this mask is already trusted */ | |
239 | controlreply(sender,"ERROR: This mask is already trusted by trustgroup %lu.", ((trusthost_t *)node->exts[tgh_ext])->trustgroup->id); | |
240 | return CMD_ERROR; | |
241 | } | |
242 | ||
243 | /* check child status */ | |
244 | PATRICIA_WALK(node, inode) | |
245 | { | |
246 | th = inode->exts[tgh_ext]; | |
247 | if (th) { | |
248 | /* we have a child trustgroup */ | |
249 | /* Criteria 1: we can't add two hosts into the same group */ | |
250 | if (th->trustgroup == tg) { | |
251 | controlreply(sender,"ERROR: A child subnet is already in this trustgroup, remove that subnet first (%s/%d)", IPtostr(inode->prefix->sin),irc_bitlen(&(inode->prefix->sin),inode->prefix->bitlen)); | |
252 | return CMD_ERROR; | |
253 | } | |
254 | } | |
255 | } | |
256 | PATRICIA_WALK_END; | |
257 | ||
258 | /* check parents too */ | |
259 | parent = node->parent; | |
260 | while (parent) { | |
261 | if( parent->exts[tgh_ext]) { | |
262 | th = parent->exts[tgh_ext]; | |
263 | /* we have a parent trustgroup */ | |
264 | /* Criteria 1: we can't add two hosts into the same group */ | |
265 | if (th->trustgroup == tg) { | |
266 | controlreply(sender,"ERROR: A parent subnet is already in this trustgroup (%s/%d)", IPtostr(parent->prefix->sin),irc_bitlen(&(parent->prefix->sin),parent->prefix->bitlen)); | |
267 | return CMD_ERROR; | |
268 | } | |
269 | /* even if we find 1 parent, we continue to the top */ | |
270 | } | |
271 | parent = parent->parent; | |
272 | } | |
273 | ||
274 | th = trusthostadd(node, tg, expiry ); | |
275 | if ( !th ) { | |
276 | controlreply(sender,"ERROR: Unable to add trusted host"); | |
277 | } | |
278 | ||
279 | trustsdb_addtrusthost(th); | |
280 | controlreply(sender,"Added %s to trustgroup #%lu",cargv[1],tg->id); | |
281 | controlwall(NO_OPER, NL_TRUSTS, "Added %s to trustgroup #%lu",cargv[1],tg->id); | |
282 | return CMD_OK; | |
283 | } | |
284 | ||
285 | int trust_denyadd(void *source, int cargc, char **cargv) { | |
286 | nick *sender=(nick *)source; | |
287 | ||
288 | controlreply(sender,"Not Implemented"); | |
289 | return CMD_OK; | |
290 | } | |
291 | ||
292 | int trust_dump(void *source, int cargc, char **cargv) { | |
293 | nick *sender=(nick *)source; | |
294 | ||
295 | char tmps3[512]; | |
296 | trustgroup_t *g; | |
297 | unsigned long startid=0; | |
298 | long num=0, count=0, lines=0; | |
299 | ||
300 | if (cargc<2) { | |
301 | controlreply(sender, "Syntax: trustdump <start #id> <number>"); | |
302 | controlreply(sender, "Dumps <number> trustgroups starting from <start #id>."); | |
303 | controlreply(sender, "This allows to dump very large numbers of groups,"); | |
304 | controlreply(sender, "so use with care."); | |
305 | return CMD_ERROR; | |
306 | } | |
307 | strncpy(tmps3,cargv[0],20); | |
308 | tmps3[20]='\0'; | |
309 | num = atoi(cargv[1]); | |
310 | ||
311 | if (tmps3[0] != '#') { | |
312 | controlreply(sender, "First parameter has to be a trust ID (prefixed with #)."); | |
313 | return CMD_ERROR; | |
314 | } | |
315 | ||
316 | startid=strtoul(&tmps3[1], NULL, 10); | |
317 | if (num < 1) { | |
318 | controlreply(sender, "Cannot return fewer than 1 group."); | |
319 | return CMD_ERROR; | |
320 | } | |
321 | if (num >= 500) { | |
322 | controlreply(sender, "Will not list more than 500 groups in one go."); | |
323 | return CMD_ERROR; | |
324 | } | |
325 | ||
326 | if (startid > trusts_lasttrustgroupid) { | |
327 | controlreply(sender, "Start ID cannot exceed maximum group ID (#%ld).", trusts_lasttrustgroupid); | |
328 | return CMD_ERROR; | |
329 | } | |
330 | ||
331 | do { | |
332 | g=findtrustgroupbyid(startid); | |
333 | startid++; | |
334 | } while ((g == NULL) && (startid <= (trusts_lasttrustgroupid+1))); | |
335 | if (g == NULL) { | |
336 | controlreply(sender, "Failed to find nearest start group."); | |
337 | return CMD_ERROR; | |
338 | } | |
339 | ||
340 | while (startid <= (trusts_lasttrustgroupid+1)) { | |
341 | if (g == NULL) { | |
342 | g=findtrustgroupbyid(startid); | |
343 | startid++; | |
344 | continue; | |
345 | } | |
346 | controlreply(sender, "G,#%lu,%lu,%lu,%d,%lu,%lu,%lu,%lu", | |
347 | g->id, g->currenton, g->maxclones, g->enforceident, g->maxperident, | |
348 | g->maxusage, g->expire, g->lastused); | |
349 | lines++; | |
350 | ||
351 | trusthost_t* thptr; | |
352 | ||
353 | int hash = trusts_gettrusthostgroupidhash(g->id); | |
354 | for (thptr = trusthostgroupidtable[hash]; thptr; thptr = thptr->nextbygroupid ) { | |
355 | if ( thptr->trustgroup->id == g->id ) { | |
356 | /* TODO: expire here - trusthost_free(thptr);*/ | |
357 | controlreply(sender, "H,#%lu,%s/%d,%lu,%lu,%lu", g->id, | |
358 | IPtostr(((patricia_node_t *)thptr->node)->prefix->sin), | |
359 | irc_bitlen(&(((patricia_node_t *)thptr->node)->prefix->sin),((patricia_node_t *)thptr->node)->prefix->bitlen), | |
360 | 0 /*a->currentlyon*/, | |
361 | 0 /*a->maxused*/, | |
362 | 0 /* a->lastused*/); | |
363 | lines++; | |
364 | } | |
365 | } | |
366 | ||
367 | count++; | |
368 | if (count >= num) { | |
369 | break; | |
370 | } | |
371 | g=findtrustgroupbyid(startid); | |
372 | startid++; | |
373 | } | |
374 | controlreply(sender, "End of list, %ld groups and %ld lines returned.", count, lines); | |
375 | return CMD_OK; | |
376 | } | |
377 | ||
378 | int trust_denydel(void *source, int cargc, char **cargv) { | |
379 | nick *sender=(nick *)source; | |
380 | ||
381 | controlreply(sender,"Not Implemented"); | |
382 | return CMD_OK; | |
383 | } | |
384 | ||
385 | int trust_denylist(void *source, int cargc, char **cargv) { | |
386 | nick *sender=(nick *)source; | |
387 | ||
388 | controlreply(sender,"Not Implemented"); | |
389 | return CMD_OK; | |
390 | } | |
391 | ||
392 | int trust_groupmodify(void *source, int cargc, char **cargv) { | |
393 | nick *sender=(nick *)source; | |
394 | unsigned long oldvalue, newvalue; | |
395 | char *mod; | |
396 | int expiry; | |
397 | trustgroup_t *tg; | |
398 | ||
399 | if (cargc<3 || cargc==4) { | |
400 | controlreply(sender,"Syntax: trustgroupmodify <#groupid> <what> [+|-|=]number"); | |
401 | controlreply(sender," +20 means add 20, =20 replaces current value, -20 means subtract"); | |
402 | controlreply(sender," what: maxclones, maxperident, maxperip, expire, enforceident, ownerid"); | |
403 | return CMD_OK; | |
404 | } | |
405 | ||
406 | if(cargv[0][0]== '#'){ | |
407 | /* find group by id */ | |
408 | tg=findtrustgroupbyid(strtol(&cargv[0][1],NULL,10)); | |
409 | } else { | |
410 | /* find group by id */ | |
411 | tg=findtrustgroupbyid(strtol(cargv[0],NULL,10)); | |
412 | } | |
413 | ||
414 | if(tg == NULL) { | |
415 | controlreply(sender,"ERROR: A trustgroup with that ID does not exist."); | |
416 | return CMD_ERROR; | |
417 | } | |
418 | ||
419 | if (tg->id==0) { | |
420 | controlreply(sender,"INTERNAL ERROR: Trustgroup has ID 0"); | |
421 | return CMD_ERROR; | |
422 | } | |
423 | ||
424 | switch ( cargv[2][0] ) { | |
425 | case '+': | |
426 | case '-': | |
427 | case '=': | |
428 | mod = cargv[2]; | |
429 | break; | |
430 | default: | |
431 | controlreply(sender,"ERROR: invalid modifier specified (values values are +,-,=)"); | |
432 | return CMD_ERROR; | |
433 | } | |
434 | newvalue = strtoul(&cargv[2][1],NULL,10); | |
435 | ||
436 | if (ircd_strcmp(cargv[1], "maxclones")==0) { | |
437 | oldvalue = tg->maxclones; | |
438 | switch (*mod) { | |
439 | case '+': | |
440 | newvalue = oldvalue + newvalue; | |
441 | break; | |
442 | case '-': | |
443 | if (newvalue > oldvalue) { | |
444 | controlreply(sender, "ERROR: maxclones cannot be less than 0"); | |
445 | return CMD_ERROR; | |
446 | } | |
447 | newvalue = oldvalue - newvalue; | |
448 | if (newvalue == 0) { | |
449 | controlreply(sender, "ERROR: maxclones limit would be 0 - unlimited maxclones can only be set with '='"); | |
450 | return CMD_ERROR; | |
451 | } | |
452 | break; | |
453 | } | |
454 | ||
455 | if (newvalue > 1000000) { | |
456 | controlreply(sender, "ERROR: large maximum number of clients - %lu", newvalue); | |
457 | return CMD_ERROR; | |
458 | } | |
459 | if (newvalue > 10000) { | |
460 | controlreply(sender, "WARNING: large maximum number of clients - %lu", newvalue); | |
461 | } | |
462 | ||
463 | tg->maxclones = newvalue; | |
464 | } else if (ircd_strcmp(cargv[1], "maxperident")==0) { | |
465 | oldvalue = tg->maxperident; | |
466 | switch (*mod) { | |
467 | case '+': | |
468 | newvalue = oldvalue + newvalue; | |
469 | break; | |
470 | case '-': | |
471 | if (newvalue > oldvalue) { | |
472 | controlreply(sender, "ERROR: maxperident cannot be less than 0"); | |
473 | return CMD_ERROR; | |
474 | } | |
475 | newvalue = oldvalue - newvalue; | |
476 | if (newvalue == 0) { | |
477 | controlreply(sender, "ERROR: maxperident limit would be 0 - unlimited maxclones can only be set with '='"); | |
478 | return CMD_ERROR; | |
479 | } | |
480 | break; | |
481 | } | |
482 | ||
483 | if (newvalue > 50) { | |
484 | controlreply(sender, "ERROR: MaxPerIdent value should be less then 50 (if set)"); | |
485 | return CMD_ERROR; | |
486 | } | |
487 | tg->maxperident=newvalue; | |
488 | } else if (ircd_strcmp(cargv[1], "maxperip")==0) { | |
489 | oldvalue = tg->maxperip; | |
490 | switch (*mod) { | |
491 | case '+': | |
492 | newvalue = oldvalue + newvalue; | |
493 | break; | |
494 | case '-': | |
495 | if (newvalue > oldvalue) { | |
496 | controlreply(sender, "ERROR: maxperip cannot be less than 0"); | |
497 | return CMD_ERROR; | |
498 | } | |
499 | newvalue = oldvalue - newvalue; | |
500 | if (newvalue == 0) { | |
501 | controlreply(sender, "ERROR: maxperip limit would be 0 - unlimited maxclones can only be set with '='"); | |
502 | return CMD_ERROR; | |
503 | } | |
504 | break; | |
505 | } | |
506 | ||
507 | if (newvalue > 500) { | |
508 | controlreply(sender, "ERROR: MaxPerIP value should be less then 500 (if set)"); | |
509 | return CMD_ERROR; | |
510 | } | |
511 | tg->maxperip = newvalue; | |
512 | } else if (ircd_strcmp(cargv[1], "expire")==0) { | |
513 | oldvalue = tg->expire; | |
514 | expiry = durationtolong(&cargv[2][1]); | |
515 | ||
516 | if (expiry > (365 * 86400) ) { | |
517 | controlreply(sender,"ERROR: Invalid duration given - temporary trusts can not be longer then 1 year"); | |
518 | return CMD_ERROR; | |
519 | } | |
520 | ||
521 | switch (*mod) { | |
522 | case '+': | |
523 | newvalue = oldvalue + expiry; | |
524 | break; | |
525 | case '-': | |
526 | newvalue = oldvalue - expiry; | |
527 | if (newvalue < getnettime() ) { | |
528 | controlreply(sender, "ERROR: Can't set expiry before current nettime - use trustgroupdel to delete trust groups"); | |
529 | return CMD_ERROR; | |
530 | } | |
531 | break; | |
532 | case '=': | |
533 | if ( expiry > 0) { | |
534 | newvalue = getnettime() + expiry; | |
535 | } | |
536 | break; | |
537 | } | |
538 | tg->expire = newvalue; | |
539 | } else if (ircd_strcmp(cargv[1], "enforceident")==0) { | |
540 | oldvalue = tg->enforceident; | |
541 | if ( (newvalue != 0 && newvalue != 1) || *mod != '=' ) { | |
542 | controlreply(sender,"ERROR: enforceident is a boolean setting, that means it can only be 0 or 1, and can only be set by '='"); | |
543 | return CMD_ERROR; | |
544 | } | |
545 | tg->enforceident = newvalue; | |
546 | } else if (ircd_strcmp(cargv[1], "ownerid")==0) { | |
547 | oldvalue = tg->ownerid; | |
548 | if ( *mod != '=' ) { | |
549 | controlreply(sender,"ERROR: Q user ID can only be set by '='"); | |
550 | return CMD_ERROR; | |
551 | } | |
552 | if ( findtrustgroupbyownerid(newvalue) ) { | |
553 | controlreply(sender, "ERROR: Q User ID %d already has a trustgroup", newvalue); | |
554 | return CMD_ERROR; | |
555 | } | |
556 | ||
557 | if (newvalue > 2147483646 ) { | |
558 | controlreply(sender, "ERROR: Invalid Q User ID: %d", newvalue); | |
559 | return CMD_ERROR; | |
560 | } | |
561 | ||
562 | tg->ownerid = newvalue; | |
563 | } | |
564 | controlreply(sender, "Modification: %s changed to %lu from %lu for trustgroup %lu", cargv[1], newvalue, oldvalue, tg->id); | |
565 | controlwall(NO_OPER, NL_TRUSTS, "Modification: %s changed to %lu from %lu for trustgroup %lu", cargv[1], newvalue, oldvalue, tg->id); | |
566 | ||
567 | trustsdb_updatetrustgroup(tg); | |
568 | return CMD_OK; | |
569 | } | |
570 | ||
571 | int trust_groupdel(void *source, int cargc, char **cargv) { | |
572 | nick *sender=(nick *)source; | |
573 | trusthost_t *thptr, *nthptr; | |
574 | trustgroup_t *tg; | |
575 | patricia_node_t *node; | |
576 | ||
577 | if (cargc<1) { | |
578 | controlreply(sender,"Syntax: trustgroupdel <#id|id>"); | |
579 | return CMD_OK; | |
580 | } | |
581 | ||
582 | if(cargv[0][0]== '#'){ | |
583 | /* find group by id */ | |
584 | tg=findtrustgroupbyid(strtol(&cargv[0][1],NULL,10)); | |
585 | } else { | |
586 | /* find group by id */ | |
587 | tg=findtrustgroupbyid(strtol(cargv[0],NULL,10)); | |
588 | } | |
589 | ||
590 | if(tg == NULL) { | |
591 | controlreply(sender,"ERROR: A trustgroup with that ID does not exist."); | |
592 | return CMD_ERROR; | |
593 | } | |
594 | ||
595 | if (tg->id==0) { | |
596 | controlreply(sender,"INTERNAL ERROR: Trustgroup has ID 0"); | |
597 | return CMD_ERROR; | |
598 | } | |
599 | ||
600 | /* we have a trustgroup to remove */ | |
601 | int hash = trusts_gettrusthostgroupidhash(tg->id); | |
602 | for (thptr = trusthostgroupidtable[hash]; thptr; thptr = nthptr ) { | |
603 | nthptr = thptr->nextbygroupid; | |
604 | if(thptr->trustgroup == tg) { | |
605 | node = thptr->node; | |
606 | controlwall(NO_OPER, NL_TRUSTS, "%s/%d removed from trustgroup #%lu",IPtostr(thptr->node->prefix->sin),irc_bitlen(&(thptr->node->prefix->sin),thptr->node->prefix->bitlen),tg->id); | |
607 | controlreply(sender,"%s/%d removed from trustgroup #%lu",IPtostr(thptr->node->prefix->sin),irc_bitlen(&(thptr->node->prefix->sin),thptr->node->prefix->bitlen),tg->id); | |
608 | trustsdb_deletetrusthost(thptr); | |
609 | trusthost_free(thptr); | |
610 | node->exts[tgh_ext] = NULL; | |
611 | } | |
612 | } | |
613 | controlwall(NO_OPER, NL_TRUSTS, "removed trustgroup #%lu",tg->id); | |
614 | controlreply(sender,"removed trustgroup #%lu",tg->id); | |
615 | trustsdb_deletetrustgroup(tg); | |
616 | trustgroup_free(tg); | |
617 | return CMD_OK; | |
618 | ||
619 | } | |
620 | ||
621 | int trust_stats(void *source, int cargc, char **cargv) { | |
622 | nick *sender=(nick *)source; | |
623 | trustgroup_t *tg; trusthost_t* thptr; int i; | |
624 | unsigned long thcount=0, ucount=0, mcount=0, tgcount=0; | |
625 | unsigned long hentries=0; | |
626 | unsigned long netcount4[33]; | |
627 | unsigned long netucount4[33]; | |
628 | unsigned long netmcount4[33]; | |
629 | unsigned long netcount6[129]; | |
630 | unsigned long netucount6[129]; | |
631 | unsigned long netmcount6[129]; | |
632 | ||
633 | int maxthmask4 = 32; | |
634 | int maxthmask6 = 128; | |
635 | ||
636 | for (i=0; i<33; i++) { | |
637 | netcount4[i]=0; | |
638 | netucount4[i]=0; | |
639 | netmcount4[i]=0; | |
640 | } | |
641 | ||
642 | for (i=0; i<129; i++) { | |
643 | netcount6[i]=0; | |
644 | netucount6[i]=0; | |
645 | netmcount6[i]=0; | |
646 | } | |
647 | ||
648 | for ( i = 0; i < TRUSTS_HASH_GROUPSIZE ; i++ ) { | |
649 | for ( tg = trustgroupidtable[i]; tg; tg = tg -> nextbyid ) { | |
650 | /*check active*/ | |
651 | tgcount++; | |
652 | } | |
653 | } | |
654 | ||
655 | for ( i = 0; i < TRUSTS_HASH_HOSTSIZE ; i++ ) { | |
656 | for ( thptr = trusthostidtable[i]; thptr; thptr = thptr-> nextbyid ) { | |
657 | /*check active*/ | |
658 | hentries++; | |
659 | thcount++; | |
660 | ucount+=thptr->node->usercount; | |
661 | mcount+=thptr->maxused; | |
662 | if(irc_in_addr_is_ipv4(&((patricia_node_t *)thptr->node)->prefix->sin)) { | |
663 | netcount4[((patricia_node_t *)thptr->node)->prefix->bitlen-96]++; | |
664 | netucount4[((patricia_node_t *)thptr->node)->prefix->bitlen-96]+=thptr->node->usercount; | |
665 | netmcount4[((patricia_node_t *)thptr->node)->prefix->bitlen-96]+=thptr->maxused; | |
666 | if( (((patricia_node_t *)thptr->node)->prefix->bitlen-96) < maxthmask4 ) { | |
667 | maxthmask4 = (((patricia_node_t *)thptr->node)->prefix->bitlen-96); | |
668 | } | |
669 | } else { | |
670 | netcount6[((patricia_node_t *)thptr->node)->prefix->bitlen]++; | |
671 | netucount6[((patricia_node_t *)thptr->node)->prefix->bitlen]+=thptr->node->usercount; | |
672 | netmcount6[((patricia_node_t *)thptr->node)->prefix->bitlen]+=thptr->maxused; | |
673 | if( ((patricia_node_t *)thptr->node)->prefix->bitlen < maxthmask6 ) { | |
674 | maxthmask6 = ((patricia_node_t *)thptr->node)->prefix->bitlen; | |
675 | } | |
676 | } | |
677 | } | |
678 | } | |
679 | controlreply(sender, "Online trust users: %lu", ucount); | |
680 | controlreply(sender, "Maximum online users: %lu", mcount); | |
681 | controlreply(sender, "Trust groups: %lu", tgcount); | |
682 | controlreply(sender, "Maximum group ID: #%lu", trusts_lasttrustgroupid); | |
683 | controlreply(sender, "Trusted hosts/nets: %lu", thcount); | |
684 | controlreply(sender, "Largest subnet (v4): /%d", maxthmask4); | |
685 | controlreply(sender, "Largest subnet (v6): /%d", maxthmask6); | |
686 | controlreply(sender, "IPv4 Subnets:"); | |
687 | for (i=0; i<32; i++) { | |
688 | if (netcount4[i]==0) continue; | |
689 | controlreply(sender, "|-*/%d (Netcount: %lu Cur: %lu Max: %lu)", i, netcount4[i], netucount4[i], netmcount4[i]); | |
690 | } | |
691 | controlreply(sender, "`-*/32 (Netcount: %lu Cur: %lu Max: %lu)", netcount4[32], netucount4[32], netmcount4[32]); | |
692 | controlreply(sender, "IPv6 Subnets:"); | |
693 | for (i=0; i<128; i++) { | |
694 | if (netcount6[i]==0) continue; | |
695 | controlreply(sender, "|-*/%d (Netcount: %lu Cur: %lu Max: %lu)", i, netcount6[i], netucount6[i], netmcount6[i]); | |
696 | } | |
697 | controlreply(sender, "`-*/128 (Netcount: %lu Cur: %lu Max: %lu)", netcount6[128], netucount6[128], netmcount6[128]); | |
698 | ||
699 | return CMD_OK; | |
700 | } | |
701 | ||
702 | ||
703 | int trust_comment(void *source, int cargc, char **cargv) { | |
704 | nick *sender=(nick *)source; | |
705 | trustgroup_t *tg; | |
706 | ||
707 | if (cargc<2) { | |
708 | controlreply(sender,"Syntax: trustcomment <#groupid> <comment>"); | |
709 | return CMD_OK; | |
710 | } | |
711 | ||
712 | if(cargv[0][0]== '#'){ | |
713 | /* find group by id */ | |
714 | tg=findtrustgroupbyid(strtol(&cargv[0][1],NULL,10)); | |
715 | } else { | |
716 | /* find group by id */ | |
717 | tg=findtrustgroupbyid(strtol(cargv[0],NULL,10)); | |
718 | } | |
719 | ||
720 | if(tg == NULL) { | |
721 | controlreply(sender,"A trustgroup with that ID does not exist."); | |
722 | return CMD_ERROR; | |
723 | } | |
724 | ||
725 | if (tg->id==0) { | |
726 | controlreply(sender,"Internal error: Trustgroup has ID 0"); | |
727 | return CMD_ERROR; | |
728 | } | |
729 | ||
730 | trustsdb_logmessage(tg, 0, 1, cargv[1]); | |
731 | ||
732 | controlreply(sender, "Comment: %s for trustgroup %lu", cargv[1], tg->id); | |
733 | controlwall(NO_OPER, NL_TRUSTS, "Comment: %s for trustgroup %lu", cargv[1], tg->id); | |
734 | ||
735 | return CMD_OK; | |
736 | ||
737 | } | |
738 |