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