summaryrefslogblamecommitdiffstats
path: root/user.c
blob: 1f89f4ad65a2d74179511c9923fae8538794c54f (plain) (tree)
1
2
3
4
5
6
7
8
9

                 

                   


                                

                      








                                                        







                                        





















































                                                                                     

                                        







                                                                 
                                                 













                                                              
                 

 
 



                                                                                

                                                                      




                                                
            



                 
 


                                                                                 


                            






                                                





                                                 



                                                                   






                                                                                      




                                                            


                                                            

                                                                       

                                                        
                 

                                       



                                      
         
                    





                                                                          

                                               















                                                                                   

                                             

         







                                 
                     



                                                  
                        
         
                   




                                 

                   




                                                   
                     


                                          
                        
         
                   











                                        
      
#include "user.h"

#ifdef CONFIG_LOGIN

int bbs_login( term_screen *ts )
{
	int ret=0;
	int fret  = 0;

	int max_row, max_col;
	int box_row, box_col;
	char *userdata_dir = CONFIG_USER_DEFAULT_DIR;

	//loginname and password buffers
	const int username_max_size = 32;
	char username_buf[username_max_size];
	const int password_max_size = username_max_size;
	char password_buf[password_max_size];


	if ( ts == NULL )
		return ret;

	term_clr_scr( ts );
	max_row = term_get_maxrow( ts );
	max_col = term_get_maxcol( ts );
	box_row = max_row/2;
	box_col = max_col/2-username_max_size/2;

	memset( username_buf, 0, username_max_size+1 );
	memset( password_buf, 0, password_max_size+1 );


	//get username
	term_cur_set_r( ts, box_row );
	term_cur_set_c( ts, box_col-6);
	term_print( ts, "Login:", 6 );
	term_cur_set_c( ts, box_col );
	term_cur_set_r( ts, box_row );
	fret = term_readline( ts, username_buf, username_max_size, READLINE_ALPHA );
	if ( fret < 1)
	{
		if ( fret == 0)
		{
			term_print( ts, "Username empty!", 15 );
		} else if ( fret == -1 )
		{
			term_print( ts, "Error reading username", 22 );
		}
		sleep( 3 );
		ret = -1;
		goto exit_login;
	};

	//get password
	term_cur_set_r( ts, box_row+2 );
	term_cur_set_c( ts, box_col-9 );
	term_print( ts, "Password:", 9 );
	term_cur_set_c( ts, box_col );
	term_cur_set_r( ts, box_row+2 );
	fret = term_readline( ts, password_buf, password_max_size, READLINE_HIDDEN );
	if ( fret < 1)
	{
		if ( fret == 0)
		{
			term_print( ts, "Password empty!", 15 );
		} else if ( fret == -1 )
		{
			term_print( ts, "Error reading password", 22 );
		}
		sleep( 3 );
		ret = -1;
		goto exit_login;
	};

	//check if username file exists in dir
	//open it check pass and auth user if pass succesfull
	fret = bbs_login_auth( userdata_dir, username_buf, password_buf );
	if ( fret == -1 )
	{
		term_cur_set_r( ts, 0 );
		term_cur_set_c( ts, 0 );
		ERROR("No such user\n"); sleep(3);
	} else if ( fret == 0 )
	{
		bbs_user_set_status( &g_user, BBS_USER_LOGEDIN );

		{
			sds lg = sds_new("Loged in as ");
			sds usrn = sds_new( username_buf );
			lg = sds_cat( lg, usrn );
			bbs_log( NULL, lg );
			sds_free( lg );
			sds_free( usrn );
		}
	}

exit_login:
	return ret;
}


//just for full fill libc example
static int dir_username_selector (const struct dirent *unused)
{
	return 1;
}


static int user_cfg_handler( void *user, const char *section, const char *name, 
                             const char *value )
{
	user_config_file *cfg = (user_config_file *)user;
	
	#define MATCH(s,n) strcmp(section,s) == 0 && strcmp(name,n)==0

	if ( MATCH("user","password") )
	{
		cfg->password = strdup( value );
	}
#undef MATCH
	return 1;

}


int bbs_login_auth( const char *dir, const char *username, const char *password )
{
	int ret=-1;

	int n,cnt;
	struct dirent **eps;
	int found_file=0;

	//name that should have user config file
	sds crct_dir = sds_new( dir );
	sds crct_fn = sds_new( username );
	sds crct_postfix = sds_new( ".user" );
	sds srch_f = sds_empty();
	srch_f = sds_cat( srch_f, crct_dir );
	srch_f = sds_cat( srch_f, crct_fn );
	srch_f = sds_cat( srch_f, crct_postfix );
	sds_free( crct_dir );
	sds_free( crct_fn );
	sds_free( crct_postfix );

	//check dir for username pass
	n = scandir( dir, &eps, dir_username_selector, alphasort );
	
	if ( n < 0 )
	{
		//if there is more stuff to be fried rearrange this block and resource
		//unitilisation code
		sds_free( srch_f );
		return -1;//mem leak
	}

	for ( cnt=0; cnt<n; cnt++ )
	{
		sds file_name = sds_new( eps[cnt]->d_name );
		sds path_dir = sds_new( dir );
		sds path_name = sds_empty( );
		path_name = sds_cat( path_name, path_dir );
		path_name = sds_cat( path_name, file_name );
		//config file exist
		//PRINT("%s || %s \n", path_name, srch_f ); sleep( 3 );
		if ( (sds_cmp( path_name, srch_f ) == 0)
		      && (found_file == 0) )
		{
			found_file = 1;
		}
		sds_free( file_name );
		sds_free( path_dir );
		sds_free( path_name );
		free( eps[cnt] );
	}
	free( eps );

	//not good style
	//now we have patch and just open file and check if password match
	if ( found_file )
	{
		user_config_file cfg;

		memset( &cfg, 0, sizeof(cfg) );
		if ( ini_parse( srch_f, user_cfg_handler, &cfg ) < 0 )
		{
		} else
		{
			//PRINT("config pass [%s][%s]!\n", cfg.password, password);
			//sleep(3);
			if ( strcmp( cfg.password, password ) == 0 )
			{

				//PRINT("Correct pass!\n");sleep(3);
				ret = 0;
			} else
			{
				//ERROR("InCorrect pass!\n");sleep(3);
			}
		}
		if ( cfg.password != NULL )
			free( cfg.password );
	}

	sds_free( srch_f );

	return ret;
}


int bbs_user_init( bbs_user *bu )
{
	int ret = -1;
	if ( bu != NULL)
	{
		memset( bu, 0, sizeof(bbs_user) );
		bu->login_status = BBS_USER_GUEST;
		ret = 0;
	}
	return ret;
}


int bbs_user_auth( bbs_user *bu )
{
	int ret=0;
	return ret;
}


int bbs_user_set_status( bbs_user *bu, int status )
{
	int ret = -1;
	if ( bu != NULL )
	{
		bu->login_status = status;
		ret = 0;
	}
	return ret;
}


int bbs_user_get_status( bbs_user *bu )
{
	if ( bu != NULL )
	{
		return bu->login_status;
	}
	return BBS_USER_GUEST;
}

#endif