]>
jfr.im git - irc/quakenet/newserv.git/blob - trusts/trusts_commands.c
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"
10 static void registercommands ( int , void *);
11 static void deregistercommands ( int , void *);
13 void calculatespaces ( int spaces
, int width
, char * str
, char ** _prebuf
, char ** _postbuf
) {
14 static char prebuf
[ 512 ], postbuf
[ 512 ];
17 if ( spaces
+ 5 >= sizeof ( prebuf
)) {
18 prebuf
[ 0 ] = prebuf
[ 1 ] = '\0' ;
20 memset ( prebuf
, ' ' , spaces
);
21 prebuf
[ spaces
] = '\0' ;
24 spacelen
= width
- ( strlen ( str
) + spaces
);
25 if ( spacelen
<= 0 || spacelen
+ 5 >= sizeof ( postbuf
)) {
26 postbuf
[ 0 ] = postbuf
[ 1 ] = '\0' ;
28 memset ( postbuf
, ' ' , spacelen
);
29 postbuf
[ spacelen
] = '\0' ;
36 static void traverseandmark ( unsigned int marker
, trusthost
* th
) {
39 for ( th
= th
-> children
; th
; th
= th
-> nextbychild
) {
41 traverseandmark ( marker
, th
);
45 static void insertth ( array
* parents
, trusthost
* th
) {
47 trusthost
** p2
= ( trusthost
**)( parents
-> content
);
49 /* this eliminates common subtrees */
50 for ( i
= 0 ; i
< parents
-> cursi
; i
++)
54 if ( i
== parents
-> cursi
) {
55 int pos
= array_getfreeslot ( parents
);
56 (( trusthost
**)( parents
-> content
))[ pos
] = th
;
60 static void marktree ( array
* parents
, unsigned int marker
, trusthost
* th
) {
64 for ( pth
= th
-> parent
; pth
; pth
= pth
-> next
) {
65 insertth ( parents
, pth
);
71 insertth ( parents
, th
);
73 /* sadly we need to recurse down */
74 traverseandmark ( marker
, th
);
77 static void outputtree ( nick
* np
, unsigned int marker
, trustgroup
* originalgroup
, trusthost
* th
, int depth
) {
78 char * cidrstr
, * prespacebuf
, * postspacebuf
, parentbuf
[ 512 ];
80 if ( th
-> marker
!= marker
)
83 cidrstr
= trusts_cidr2str ( th
-> ip
, th
-> mask
);
84 calculatespaces ( depth
+ 1 , 20 + 1 , cidrstr
, & prespacebuf
, & postspacebuf
);
86 if ( th
-> group
== originalgroup
) {
91 /* show the ids of other groups */
93 snprintf ( parentbuf
, sizeof ( parentbuf
), "%-10d %s " , th
-> group
-> id
, th
-> group
-> name
-> content
);
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
);
98 for ( th
= th
-> children
; th
; th
= th
-> nextbychild
)
99 outputtree ( np
, marker
, originalgroup
, th
, depth
+ 1 );
102 static void displaygroup ( nick
* sender
, trustgroup
* tg
) {
107 time_t t
= time ( NULL
);
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 ( %s enforcing 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)" );
123 controlreply ( sender
, "Host Current Max Last seen Group ID Group name" );
125 marker
= nextthmarker ();
126 array_init (& parents
, sizeof ( trusthost
*));
128 for ( th
= tg
-> hosts
; th
; th
= th
-> next
)
129 marktree (& parents
, marker
, th
);
131 p2
= ( trusthost
**)( parents
. content
);
132 for ( i
= 0 ; i
< parents
. cursi
; i
++)
133 outputtree ( sender
, marker
, tg
, p2
[ i
], 0 );
135 array_free (& parents
);
137 controlreply ( sender
, "End of list." );
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 ;
154 tg
= tg_strtotg ( name
);
157 displaygroup ( sender
, tg
);
161 if ( trusts_parsecidr ( name
, & ip
, & mask
)) {
162 th
= th_getbyhost ( ip
);
165 controlreply ( sender
, "Specified IP address is not trusted." );
169 displaygroup ( sender
, th
-> group
);
173 for ( tg
= tglist
; tg
; tg
= tg
-> next
) {
174 if ( match ( name
, tg
-> name
-> content
))
177 displaygroup ( sender
, tg
);
178 if (-- remaining
== 0 ) {
179 controlreply ( sender
, "Maximum number of matches reached." );
186 controlreply ( sender
, "No matches found." );
191 static int comparetgs ( const void * _a
, const void * _b
) {
192 const trustgroup
* a
= _a
;
193 const trustgroup
* b
= _b
;
202 static int trusts_cmdtrustdump ( void * source
, int argc
, char ** argv
) {
204 trustgroup
* tg
, ** atg
;
205 unsigned int wanted
, max
, maxid
, totalcount
, i
, groupcount
, linecount
;
208 if (( argc
< 2 ) || ( argv
[ 0 ][ 0 ] != '#' ))
211 wanted
= atoi (& argv
[ 0 ][ 1 ]);
214 for ( maxid
= totalcount
= 0 , tg
= tglist
; tg
; tg
= tg
-> next
) {
215 if ( totalcount
== 0 || tg
-> id
> maxid
)
221 if ( maxid
> totalcount
) {
222 controlreply ( np
, "Start ID cannot exceed current maximum group ID (# %u )" , maxid
);
226 atg
= nsmalloc ( POOL_TRUSTS
, sizeof ( trusthost
*) * totalcount
);
228 controlreply ( np
, "Memory error." );
232 for ( i
= 0 , tg
= tglist
; i
< totalcount
&& tg
; tg
= tg
-> next
, i
++)
235 qsort ( atg
, totalcount
, sizeof ( trustgroup
*), comparetgs
);
237 for ( i
= 0 ; i
< totalcount
; i
++)
238 if ( atg
[ i
]-> id
>= wanted
)
241 for ( groupcount
= linecount
= 0 ; i
< totalcount
; i
++) {
245 controlreply ( np
, "G, %s " , dumptg ( atg
[ i
], 1 ));
247 for ( th
= atg
[ i
]-> hosts
; th
; th
= th
-> next
) {
249 controlreply ( np
, "H, %s " , dumpth ( th
, 1 ));
255 nsfree ( POOL_TRUSTS
, atg
);
257 controlreply ( np
, "End of list, %u groups and %u lines returned." , groupcount
, linecount
);
261 static int trusts_cmdtrustgline ( void * source
, int cargc
, char ** cargv
) {
263 nick
* sender
= source
;
265 int duration
, count
= 0 ;
271 tg
= tg_strtotg ( cargv
[ 0 ]);
273 controlreply ( sender
, "Couldn't look up trustgroup." );
279 duration
= durationtolong ( cargv
[ 2 ]);
280 if (( duration
<= 0 ) || ( duration
> MAXDURATION
)) {
281 controlreply ( sender
, "Invalid duration supplied." );
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 %j d : %s " , mynumeric
-> content
, user
, cidrstr
, duration
, ( intmax_t ) getnettime (), reason
);
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
);
299 static int trusts_cmdtrustungline ( void * source
, int cargc
, char ** cargv
) {
301 nick
* sender
= source
;
303 int duration
, count
= 0 ;
309 tg
= tg_strtotg ( cargv
[ 0 ]);
311 controlreply ( sender
, "Couldn't look up trustgroup." );
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
);
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
);
329 static int commandsregistered
;
331 static void registercommands ( int hooknum
, void * arg
) {
332 if ( commandsregistered
)
334 commandsregistered
= 1 ;
336 registercontrolhelpcmd ( "trustlist" , NO_OPER
, 1 , trusts_cmdtrustlist
, "Usage: trustlist <#id|name|IP> \n Shows 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> \n Glines a user on all hosts of a trust group." );
339 registercontrolhelpcmd ( "trustungline" , NO_OPER
, 4 , trusts_cmdtrustungline
, "Usage: trustungline <#id|name> <user> \n Unglines a user on all hosts of a trust group." );
342 static void deregistercommands ( int hooknum
, void * arg
) {
343 if (! commandsregistered
)
345 commandsregistered
= 0 ;
347 deregistercontrolcmd ( "trustlist" , trusts_cmdtrustlist
);
348 deregistercontrolcmd ( "trustdump" , trusts_cmdtrustdump
);
349 deregistercontrolcmd ( "trustgline" , trusts_cmdtrustgline
);
350 deregistercontrolcmd ( "trustungline" , trusts_cmdtrustungline
);
354 registerhook ( HOOK_TRUSTS_DB_LOADED
, registercommands
);
355 registerhook ( HOOK_TRUSTS_DB_CLOSED
, deregistercommands
);
358 registercommands ( 0 , NULL
);
362 deregisterhook ( HOOK_TRUSTS_DB_LOADED
, registercommands
);
363 deregisterhook ( HOOK_TRUSTS_DB_CLOSED
, deregistercommands
);
365 deregistercommands ( 0 , NULL
);