summaryrefslogtreecommitdiff
path: root/user.c
diff options
context:
space:
mode:
Diffstat (limited to 'user.c')
-rw-r--r--user.c243
1 files changed, 243 insertions, 0 deletions
diff --git a/user.c b/user.c
new file mode 100644
index 0000000..ecfde50
--- /dev/null
+++ b/user.c
@@ -0,0 +1,243 @@
+#include "user.h"
+
+int bbs_login( term_screen *ts )
+{
+ int ret=0;
+ int fret = 0;
+
+ int ret_len;
+ size_t in_size=0;
+ char *in_buf=NULL;
+ int quit_loop=0;
+ int menu_input = 0;
+ char menu_cmd = 0;
+ int row = 0;
+ char *todo_fname = NULL;
+ int orig_row, orig_col;
+ 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];
+ int username_ready = 0;
+
+
+ if ( ts == NULL )
+ return ret;
+
+ term_clr_scr( ts );
+ max_row = term_get_maxrow( ts );
+ max_col = term_get_maxcol( ts );
+ orig_col = term_cur_get_c( ts );
+ orig_row = term_cur_get_r( 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 )
+ {
+ 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 );
+ 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 );
+ }
+ return 1;
+
+}
+
+int bbs_login_auth( const char *dir, const char *username, const char *password )
+{
+ int ret=-1;
+ int fret = -1;
+
+ int n,cnt;
+ struct dirent **eps;
+ struct stat dir_node;
+ 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();
+ sds_cat( srch_f, crct_dir );
+ sds_cat( srch_f, crct_fn );
+ sds_cat( srch_f, crct_postfix );
+
+ //check dir for username pass
+ n = scandir( dir, &eps, dir_username_selector, alphasort );
+
+ if ( n < 0 ) 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_new( "" );
+ sds_cat( path_name, path_dir );
+ 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 )
+ {
+ cnt = n;
+
+ 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;
+ 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);
+ }
+ }
+ }
+
+ sds_free( crct_dir );
+ sds_free( crct_fn );
+ sds_free( crct_postfix );
+ sds_free( srch_f );
+
+ return ret;
+}
+
+
+int bbs_user_init( bbs_user *bu )
+{
+ if ( bu != NULL)
+ {
+ memset( bu, 0, sizeof(bbs_user) );
+ bu->login_status = BBS_USER_GUEST;
+ }
+}
+
+
+int bbs_user_auth( bbs_user *bu )
+{
+
+}
+
+
+int bbs_user_set_status( bbs_user *bu, int status )
+{
+ if ( bu != NULL )
+ {
+ bu->login_status = status;
+ }
+}
+
+
+int bbs_user_get_status( bbs_user *bu )
+{
+ if ( bu != NULL )
+ {
+ return bu->login_status;
+ }
+ return BBS_USER_GUEST;
+}
+