#include "captive.h"


/* See captive_adduser.c for info.  This program just reads all the records into
   an array, sorts them by username, then prints them.  */




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

	GDBM_FILE dbf;
	datum key;
	datum content;
	time_t tp;


	time( &tp );


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

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


	content = gdbm_fetch( dbf, key );

	if( content.dptr == NULL ) {
		printf( "user '%s' does not exist.\n", argv[1] );
		exit(0);
	}

	printf( "user '%s'\n", key.dptr );
	printf( "\tpassword:              %s\n", ((userinfo *)content.dptr)->password );
	printf( "\tlogin_dur:             %ld seconds (%s)\n", ((userinfo *)content.dptr)->login_dur_in_seconds, timeleftstring(((userinfo *)content.dptr)->login_dur_in_seconds) );
	if( ((userinfo *)content.dptr)->enabled == 1 )
		printf( "\tenabled:               yes\n" );
	if( ((userinfo *)content.dptr)->enabled == 2 )
		printf( "\tenabled:               yes (one-time use only)\n" );
	else
		printf( "\tenabled:               no\n" );
	if( ((userinfo *)content.dptr)->logged_in_now )
		printf( "\tlogged in:             yes\n" );
	else
		printf( "\tlogged in:             no\n" );
	printf( "\tip:                    %s\n", ((userinfo *)content.dptr)->ip);
	printf( "\tmac:                   %s\n", ((userinfo *)content.dptr)->mac );
	if( ((userinfo *)content.dptr)->login_expires_at != 0 )
		printf( "\tsession expires in:    %s\n", timeleftstring(((userinfo *)content.dptr)->login_expires_at-(long)tp) );
	else
		printf( "\tsession expires in:     \n" );
	gdbm_close( dbf );

	return 0;
}
