]>
jfr.im git - irc/quakenet/newserv.git/blob - trusts/trusts_commands.c
2 #include "../control/control.h"
3 #include "../lib/irc_string.h"
6 int trusts_migration_start ( TrustDBMigrationCallback
, void *);
7 void trusts_migration_stop ( void );
8 static void registercommands ( int , void *);
9 static void deregistercommands ( int , void *);
11 static void migrate_status ( int errcode
, void * tag
) {
12 long sender
= ( long ) tag
;
13 nick
* np
= getnickbynumeric ( sender
);
18 if (! errcode
|| errcode
== MIGRATION_LASTERROR
) {
20 controlreply ( np
, "Migration complete." );
21 controlreply ( np
, "Attempting to reload database. . ." );
23 controlreply ( np
, "An error occured after the database was unloaded, attempting reload. . ." );
26 controlreply ( np
, "Database reloaded successfully." );
28 controlreply ( np
, "An error occured, please reload the module manually." );
31 controlreply ( np
, "Error %d occured during migration, commands reregistered." , errcode
);
32 registercommands ( 0 , NULL
);
36 static int trusts_cmdmigrate ( void * source
, int cargc
, char ** cargv
) {
37 nick
* sender
= source
;
40 /* iffy but temporary */
41 ret
= trusts_migration_start ( migrate_status
, ( void *)( sender
-> numeric
));
43 controlreply ( sender
, "Migration started, commands deregistered." );
44 deregistercommands ( 0 , NULL
);
46 controlreply ( sender
, "Error %d starting migration." , ret
);
52 static int trusts_cmdtrustlist ( void * source
, int cargc
, char ** cargv
) {
53 nick
* sender
= source
;
61 tg
= tg_strtotg ( cargv
[ 0 ]);
63 controlreply ( sender
, "Couldn't find a trustgroup with that id." );
69 /* abusing the ternary operator a bit :( */
70 controlreply ( sender
, "Name: : %s " , tg
-> name
-> content
);
71 controlreply ( sender
, "Trusted for : %d " , tg
-> trustedfor
);
72 controlreply ( sender
, "Currently using : %d " , tg
-> count
);
73 controlreply ( sender
, "Clients per user : %d ( %s enforcing ident)" , tg
-> maxperident
, tg
-> mode
? "" : "not " );
74 controlreply ( sender
, "Contact: : %s " , tg
-> contact
-> content
);
75 controlreply ( sender
, "Expires in : %s " , ( tg
-> expires
> t
)? longtoduration ( tg
-> expires
- t
, 2 ): "(the past -- BUG)" );
76 controlreply ( sender
, "Last changed by : %s " , tg
-> createdby
-> content
);
77 controlreply ( sender
, "Comment: : %s " , tg
-> comment
-> content
);
78 controlreply ( sender
, "ID: : %u " , tg
-> id
);
79 controlreply ( sender
, "Last used : %s " , ( tg
-> count
> 0 )? "(now)" :(( tg
-> lastseen
> 0 )? trusts_timetostr ( tg
-> lastseen
): "(never)" ));
80 controlreply ( sender
, "Max usage : %d " , tg
-> maxusage
);
81 controlreply ( sender
, "Last max reset : %s " , tg
-> lastmaxuserreset
? trusts_timetostr ( tg
-> lastmaxuserreset
): "(never)" );
83 controlreply ( sender
, "Host Current Max Last seen" );
85 for ( th
= tg
-> hosts
; th
; th
= th
-> next
)
86 controlreply ( sender
, " %-20s %-10d %-10d %s " , trusts_cidr2str ( th
-> ip
, th
-> mask
), th
-> count
, th
-> maxusage
, ( th
-> count
> 0 )? "(now)" :(( th
-> lastseen
> 0 )? trusts_timetostr ( th
-> lastseen
): "(never)" ));
88 controlreply ( sender
, "End of list." );
93 static int trusts_cmdtrustadd ( void * source
, int cargc
, char ** cargv
) {
95 nick
* sender
= source
;
98 trusthost
* th
, * superset
, * subset
;
103 tg
= tg_strtotg ( cargv
[ 0 ]);
105 controlreply ( sender
, "Couldn't look up trustgroup." );
110 if (! trusts_str2cidr ( host
, & ip
, & mask
)) {
111 controlreply ( sender
, "Invalid host." );
115 /* OKAY! Lots of checking here!
118 * - host isn't already covered by given group (reject if it is)
119 * - host doesn't already exist exactly already (reject if it does)
120 * - host is more specific than an existing one (warn if it is, fix up later)
121 * - host is less specific than an existing one (warn if it is, don't need to do anything special)
124 for ( th
= tg
-> hosts
; th
; th
= th
-> next
) {
125 if ( th
-> ip
== ( ip
& th
-> mask
)) {
126 controlreply ( sender
, "This host (or part of it) is already covered in the given group." );
131 if ( th_getbyhostandmask ( ip
, mask
)) {
132 controlreply ( sender
, "This host already exists in another group with the same mask." );
136 /* this function will set both to NULL if it's equal, hence the check above */
137 th_getsuperandsubsets ( ip
, mask
, & superset
, & subset
);
139 /* a superset exists for us, we will be more specific than one existing host */
141 controlreply ( sender
, "Warning: this host already exists in another group, but this new host will override it as it has a smaller prefix." );
144 /* a subset of us exists, we will be less specific than some existing hosts */
146 controlreply ( sender
, "Warning: this host already exists in at least one other group, the new host has a larger prefix and therefore will not override those hosts." );
148 if ( superset
|| subset
)
149 controlreply ( sender
, "Adding anyway..." );
151 th
= th_new ( tg
, host
);
153 controlreply ( sender
, "An error occured adding the host to the group." );
157 controlreply ( sender
, "Host added." );
158 /* TODO: controlwall */
163 static int trusts_cmdtrustgroupadd ( void * source
, int cargc
, char ** cargv
) {
164 nick
* sender
= source
;
165 char * name
, * contact
, * comment
, createdby
[ ACCOUNTLEN
+ 2 ];
166 unsigned int howmany
, maxperident
, enforceident
;
174 howmany
= strtoul ( cargv
[ 1 ], NULL
, 10 );
175 if (! howmany
|| ( howmany
> 50000 )) {
176 controlreply ( sender
, "Bad value maximum number of clients." );
180 howlong
= durationtolong ( cargv
[ 2 ]);
181 if (( howlong
<= 0 ) || ( howlong
> 365 * 86400 * 20 )) {
182 controlreply ( sender
, "Invalid duration supplied." );
186 maxperident
= strtoul ( cargv
[ 3 ], NULL
, 10 );
187 if (! howmany
|| ( maxperident
> 1000 )) {
188 controlreply ( sender
, "Bad value for max per ident." );
192 if ( cargv
[ 4 ][ 0 ] != '1' && cargv
[ 4 ][ 0 ] != '0' ) {
193 controlreply ( sender
, "Bad value for enforce ident (use 0 or 1)." );
196 enforceident
= cargv
[ 4 ][ 0 ] == '1' ;
201 comment
= "(no comment)" ;
206 /* don't allow #id or id forms */
207 if (( name
[ 0 ] == '#' ) || strtoul ( name
, NULL
, 10 )) {
208 controlreply ( sender
, "Invalid trustgroup name." );
212 tg
= tg_strtotg ( name
);
214 controlreply ( sender
, "A group with that name already exists" );
218 snprintf ( createdby
, sizeof ( createdby
), "# %s " , sender
-> authname
);
220 tg
= tg_new ( name
, howmany
, enforceident
, maxperident
, howlong
+ time ( NULL
), createdby
, contact
, comment
);
222 controlreply ( sender
, "An error occured adding the trustgroup." );
226 controlreply ( sender
, "Group added." );
227 /* TODO: controlwall */
232 static int commandsregistered
;
234 static void registercommands ( int hooknum
, void * arg
) {
235 if ( commandsregistered
)
237 commandsregistered
= 1 ;
239 registercontrolhelpcmd ( "trustmigrate" , NO_DEVELOPER
, 0 , trusts_cmdmigrate
, "Usage: trustmigrate \n Copies trust data from O and reloads the database." );
240 registercontrolhelpcmd ( "trustlist" , NO_OPER
, 1 , trusts_cmdtrustlist
, "Usage: trustlist <#id|name|id> \n Shows trust data for the specified trust group." );
241 registercontrolhelpcmd ( "trustgroupadd" , NO_OPER
, 6 , trusts_cmdtrustgroupadd
, "Usage: trustgroupadd <name> <howmany> <howlong> <maxperident> <enforceident> <contact> ?comment?" );
242 registercontrolhelpcmd ( "trustadd" , NO_OPER
, 2 , trusts_cmdtrustadd
, "Usage: trustadd <#id|name|id> <host>" );
245 static void deregistercommands ( int hooknum
, void * arg
) {
246 if (! commandsregistered
)
248 commandsregistered
= 0 ;
250 deregistercontrolcmd ( "trustmigrate" , trusts_cmdmigrate
);
251 deregistercontrolcmd ( "trustlist" , trusts_cmdtrustlist
);
252 deregistercontrolcmd ( "trustgroupadd" , trusts_cmdtrustgroupadd
);
253 deregistercontrolcmd ( "trustadd" , trusts_cmdtrustadd
);
257 registerhook ( HOOK_TRUSTS_DB_LOADED
, registercommands
);
258 registerhook ( HOOK_TRUSTS_DB_CLOSED
, deregistercommands
);
261 registercommands ( 0 , NULL
);
265 deregisterhook ( HOOK_TRUSTS_DB_LOADED
, registercommands
);
266 deregisterhook ( HOOK_TRUSTS_DB_CLOSED
, deregistercommands
);
268 trusts_migration_stop ();
270 deregistercommands ( 0 , NULL
);