#include "captive.h"


/* See comments in captive_adduser.c for info.  */

int main( int argc, char *argv[]) {

	GDBM_FILE dbf;
	datum key;
	time_t tp;

	int i;

	time( &tp );

	if( argc != 2 ) {
		fprintf( stderr, "\n\tusage: captive_deluser [username]\n\n" );
		exit(0);
	}

	if( strlen(argv[1]) > USERNAME_LEN-1 ) {
		fprintf( stderr, "\n\tusage: captive_deluser [username]\n" );
		fprintf( stderr, "\tMax username length is %d characters\n\n", USERNAME_LEN-1 );
		exit(0);
	}

	for( i = 0; i < strlen(argv[1]); i++ )
		if( !isalnum(argv[1][i]) ) {
			fprintf( stderr, "\n\tusage: captive_deluser [username]\n" );
			fprintf( stderr, "\tUsernames may contain alphanumeric characters only\n\n" );
			exit(0);
		}

	printf( "Deleting user \"%s\" ... ", argv[1] );


	dbf = gdbm_open( "/var/smoothwall/mods/captive/captivedb", 512, GDBM_WRITER, S_IRWXU, NULL );

	key.dptr = argv[1];
	key.dsize = strlen( argv[1] );

	if( gdbm_delete(dbf,key) == 0 ) {
		printf( "done.\n" );
	} else {
		printf( "failed (user may not exist).\n" );
	}

	gdbm_close( dbf );
	post_to_log( EVENT_USER_DEL, key.dptr, "N/A", "N/A", (long)tp );

	return 0;
}
