diff options
author | FreeArtMan <dos21h@gmail.com> | 2015-01-11 17:49:03 +0900 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2015-01-11 17:49:03 +0900 |
commit | 9b3d5f87e7718b00f786531b58bf102cdadc8264 (patch) | |
tree | 51c445bbfd347353bcbc1412aaec654819739555 | |
parent | 4e148546810a2902dff9444526e47c9569c79b64 (diff) | |
download | microbbs-9b3d5f87e7718b00f786531b58bf102cdadc8264.tar.gz microbbs-9b3d5f87e7718b00f786531b58bf102cdadc8264.zip |
Added primitive authentification support
-rw-r--r-- | Kconfig | 6 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | door.c | 2 | ||||
-rw-r--r-- | libterm/examples/Makefile | 3 | ||||
-rw-r--r-- | libterm/examples/readline.c | 40 | ||||
-rw-r--r-- | libterm/term.c | 22 | ||||
-rw-r--r-- | libterm/term.h | 26 | ||||
-rw-r--r-- | libterm/term_io.c | 107 | ||||
-rw-r--r-- | libterm/term_io.h | 10 | ||||
-rw-r--r-- | logs.c | 2 | ||||
-rw-r--r-- | logs.h | 2 | ||||
-rw-r--r-- | microbbs.c | 19 | ||||
-rw-r--r-- | user.c | 243 | ||||
-rw-r--r-- | user.h | 54 |
14 files changed, 510 insertions, 28 deletions
@@ -23,6 +23,9 @@ menuconfig DOORGAMES default n if DOORGAMES + config DOOR_DEFAULT_DIR + string "Default game dir" + default "door" endif menuconfig BOARD @@ -39,6 +42,9 @@ menuconfig LOGIN default n if LOGIN + config USER_DEFAULT_DIR + string "Defaul user data directory" + default "./users" endif menuconfig ARTICLES @@ -1,7 +1,7 @@ PROJECT=microbbs CC=gcc CFLAGS= -SOURCES=articles.c bbsconfig.c buildinfo.c captcha.c file_use.c ini.c list.c login.c logs.c mmm.c motd.c sds.c session.c statistics.c sysinfo.c telnetd.c textview.c todo.c vote.c +SOURCES=articles.c bbsconfig.c buildinfo.c captcha.c door.c file_use.c ini.c list.c logs.c mmm.c motd.c sds.c session.c statistics.c sysinfo.c telnetd.c textview.c todo.c user.c vote.c OBJECTS=$(SOURCES:.c=.o) BUILD_DIR=build_dir @@ -88,6 +88,8 @@ int bbs_door( term_screen *ts, const char *dir_name ) sds pathname = sds_new( door_game_dir ); sds_cat( pathname, d_name ); + //!!!possible memleak unfreed d_name? + //check if its file term_cur_set_c( ts, 0 ); if ( stat( pathname, &path_node ) == 0 ) diff --git a/libterm/examples/Makefile b/libterm/examples/Makefile index 00583f8..ffc3d1d 100644 --- a/libterm/examples/Makefile +++ b/libterm/examples/Makefile @@ -1,6 +1,7 @@ CC=gcc CFLAGS=-I../ ../libterm.o -SOURCES=detect_resize.c detect_screen_size.c filtered_input.c invisible_input.c restore_screen.c print_8025.c +SOURCES=detect_resize.c detect_screen_size.c filtered_input.c invisible_input.c \ +readline.c restore_screen.c print_8025.c EXE=$(SOURCES:.c=) BUILD_DIR=. diff --git a/libterm/examples/readline.c b/libterm/examples/readline.c new file mode 100644 index 0000000..cd4467d --- /dev/null +++ b/libterm/examples/readline.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <stdlib.h> + +#include <term.h> +#include <term_io.h> + +int main() +{ + + const int sz = 31; + char name[sz]; + char passsword[sz]; + + struct term_screen ts; memset( &ts, 0, sizeof(ts) ); + + if ( term_init( &ts ) == -1 ) + printf("Some err when init\n"); + + term_set_raw_mode( &ts ); + + term_clr_scr( &ts ); + + term_cur_set_c( &ts, 0); + term_cur_set_r( &ts, 1 ); + term_readline( &ts, name, sz, READLINE_TEXT ); + + term_cur_set_c( &ts, 0 ); + term_cur_set_r( &ts, 2 ); + term_readline( &ts, passsword, sz, READLINE_HIDDEN ); + + + term_clr_scr( &ts ); + + term_set_orig_mode( &ts ); + + printf("Name:%s\n", name); + printf("passsword:%s\n", passsword ); + + return 0; +}
\ No newline at end of file diff --git a/libterm/term.c b/libterm/term.c index e279e81..412253b 100644 --- a/libterm/term.c +++ b/libterm/term.c @@ -2,28 +2,6 @@ #include "debug.h" -enum KEY_ACTION{ - KEY_NULL = 0, /* NULL */ - CTRL_A = 1, /* Ctrl+a */ - CTRL_B = 2, /* Ctrl-b */ - CTRL_C = 3, /* Ctrl-c */ - CTRL_D = 4, /* Ctrl-d */ - CTRL_E = 5, /* Ctrl-e */ - CTRL_F = 6, /* Ctrl-f */ - CTRL_H = 8, /* Ctrl-h */ - TAB = 9, /* Tab */ - CTRL_K = 11, /* Ctrl+k */ - CTRL_L = 12, /* Ctrl+l */ - ENTER = 13, /* Enter */ - CTRL_N = 14, /* Ctrl-n */ - CTRL_P = 16, /* Ctrl-p */ - CTRL_T = 20, /* Ctrl-t */ - CTRL_U = 21, /* Ctrl+u */ - CTRL_W = 23, /* Ctrl+w */ - ESC = 27, /* Escape */ - BACKSPACE = 127 /* Backspace */ -}; - #define T_ESC "\x1b" //setup initial terminal stuff diff --git a/libterm/term.h b/libterm/term.h index 0be7391..b399cd6 100644 --- a/libterm/term.h +++ b/libterm/term.h @@ -16,6 +16,32 @@ #include "screen_modes.h" #include "debug.h" +enum TERM_KEY_ACTION { + KEY_NULL = 0, /* NULL */ + CTRL_A = 1, /* Ctrl+a */ + CTRL_B = 2, /* Ctrl-b */ + CTRL_C = 3, /* Ctrl-c */ + CTRL_D = 4, /* Ctrl-d */ + CTRL_E = 5, /* Ctrl-e */ + CTRL_F = 6, /* Ctrl-f */ + CTRL_H = 8, /* Ctrl-h */ + TAB = 9, /* Tab */ + CTRL_K = 11, /* Ctrl+k */ + CTRL_L = 12, /* Ctrl+l */ + ENTER = 13, /* Enter */ + CTRL_N = 14, /* Ctrl-n */ + CTRL_P = 16, /* Ctrl-p */ + CTRL_T = 20, /* Ctrl-t */ + CTRL_U = 21, /* Ctrl+u */ + CTRL_W = 23, /* Ctrl+w */ + ESC = 27, /* Escape */ + BACKSPACE = 127 /* Backspace */ +}; + + +#define TK_ENTER 13 +#define TK_ESC 27 +#define TK_BACKSPACE 127 typedef struct term_screen { diff --git a/libterm/term_io.c b/libterm/term_io.c index 5ef682b..cd1b3c1 100644 --- a/libterm/term_io.c +++ b/libterm/term_io.c @@ -102,4 +102,111 @@ int term_getc( term_screen *ts ) return ret; } +int term_putc( term_screen *ts, char c ) +{ + int ret = 0; + int fret = -1; + fret = write( ts->ofd, &c, 1 ); + if ( fret != 1 ) + { + ret = -1; + } + + return ret; +} + +//return amoutn fo characters readed +//on error or if nothing is imputed 0 returned +int term_readline( term_screen *ts, char *str, size_t str_size, int flag ) +{ + int ret = 0; + int max_row, max_column; + char *buf = NULL; + int buf_size = str_size-1; + int buf_curent = 0; + int menu_input=0; + char menu_cmd = 0; + int orig_row; + int orig_col; + + buf = malloc( buf_size ); + memset( buf, 0, buf_size ); + + max_row = term_get_maxrow( ts ); + max_column = term_get_maxcol( ts ); + orig_row = term_cur_get_r( ts ); + orig_col = term_cur_get_c( ts ); + + menu_cmd = 0; + buf_curent = 0; + while ( menu_cmd != TK_ESC ) + { + menu_input = term_getc( ts ); + if ( menu_input != -1 ) + { + menu_cmd = (char)menu_input; + //add to buffer any pritable char + //if ( (isgraph( menu_cmd ) && flag == READLINE_TEXT ) || + // (isalpha( menu_cmd ) && flag == READLINE_ALPHA ) ) + if ( isalpha( menu_cmd ) ) + { + if ( buf_curent < buf_size ) + { + buf[ buf_curent ] = menu_cmd; + buf_curent += 1; + } + //deleteone char from buffer + } else if ( menu_cmd == TK_BACKSPACE ) + { + if ( buf_curent > 0) + { + buf[ buf_curent ] = 0x0; + buf_curent -= 1; + } + //input ready + } else if ( menu_cmd == TK_ENTER ) + { + ret = buf_curent; + memcpy( str, buf, buf_size ); + str[ str_size-1 ] = 0x0; + break; + } else if ( menu_cmd == TK_ESC ) + { + ret = -1; + } + + { + int i; + term_cur_set_r( ts, orig_row ); + term_cur_set_c( ts, orig_col ); + for (i=0;i<buf_size;i++) + { + if ( i < buf_curent ) + { + if ( (flag == READLINE_TEXT) || (flag == READLINE_ALPHA) ) + { + term_putc( ts, buf[i] ); + } else if ( flag == READLINE_HIDDEN ) + { + term_putc( ts, '*'); + } + } else + { + term_putc( ts, ' ' ); + } + } + term_cur_set_c( ts, orig_col+buf_curent ); + term_cur_set_r( ts, orig_row ); + } + } + } + + if ( buf != NULL) + { + free( buf ); + buf = NULL; + } + + return ret; +} diff --git a/libterm/term_io.h b/libterm/term_io.h index 8c57282..3c84972 100644 --- a/libterm/term_io.h +++ b/libterm/term_io.h @@ -7,11 +7,19 @@ #include "screen_modes.h" #include "term.h" +#define READLINE_NONE 0 +#define READLINE_ALPHA 1 +#define READLINE_TEXT 2 +#define READLINE_HIDDEN 3 + + int term_fprint( screen_mode_e mode, FILE *f ); int term_print( term_screen *ts, const char *s, size_t n ); int term_print_xy( term_screen *ts, const char *buf, size_t size, - int init_column, int init_row); + int init_column, int init_row); int term_draw_hline( term_screen *ts, int pc, int pr, int sz, char ch ); int term_getc( term_screen *ts ); +int term_putc( term_screen *ts, char c ); +int term_readline( term_screen *ts, char *str, size_t str_size, int flag ); #endif @@ -14,7 +14,7 @@ int bbs_log_main( const char *syslname ) openlog ( syslname, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1 ); } - syslog( LOG_NOTICE, "BBS visitor" ); + syslog( LOG_NOTICE, "BBS visitor %s", BUILD_VERSION ); closelog (); @@ -6,6 +6,8 @@ #include <syslog.h> #include <unistd.h> +#include "buildinfo.h" + #define BBS_DEFAULT_SYSLOG "microbbs" int bbs_log_main( const char* ); @@ -1,16 +1,22 @@ +#define __MICROBBS_MAIN + #include <stdio.h> #include <stdlib.h> + #include "kconfig.h" #include "logs.h" #include "motd.h" #include "sysinfo.h" #include "articles.h" +#include "user.h" + #include "libterm/term.h" #include "libterm/term_io.h" #include "ini.h" + int main( int argc, char **argv ) { int quit_main_menu = 0; @@ -23,6 +29,9 @@ int main( int argc, char **argv ) term_set_raw_mode( &ts ); term_clr_scr( &ts ); + //init global variable of bbs user + bbs_user_init( &g_user ); + //lunch captcha and try to detect if its random bot #ifdef CONFIG_CAPTCHA if ( captcha_test1( ts ) != 1) @@ -50,6 +59,11 @@ int main( int argc, char **argv ) term_cur_set_r( &ts, 0 ); term_cur_set_c( &ts, 0 ); + #ifdef CONFIG_LOGIN + if ( bbs_user_get_status( &g_user ) == BBS_USER_LOGEDIN ) + printf("[LOGEDIN] "); + #endif + #ifdef CONFIG_MOTD printf("(M)otd "); #endif @@ -71,7 +85,8 @@ int main( int argc, char **argv ) #endif #ifdef CONFIG_LOGIN - printf("(L)ogin "); + if ( bbs_user_get_status( &g_user ) != BBS_USER_LOGEDIN ) + printf("(L)ogin "); #endif #ifdef CONFIG_TODO @@ -171,7 +186,7 @@ int main( int argc, char **argv ) case 'l': case 'L': { - printf("Login?\n"); + bbs_login( &ts ); } break; #endif @@ -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; +} + @@ -0,0 +1,54 @@ +#ifndef __MICROBBS_USER_H +#define __MICROBBS_USER_H + +#include <stdlib.h> +#include <stdio.h> +#include <dirent.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <termios.h> + +#include "kconfig.h" +#include "sds.h" + +#include "libterm/term.h" +#include "libterm/term_io.h" + +#define BBS_USER_GUEST 0 +#define BBS_USER_LOGEDIN 1 +#define BBS_USER_BLOCKED 2 +#define BBS_USER_LOGEDOUT 3 + + +#define BBS_USER_UN_SZ 32 +typedef struct bbs_user +{ + int login_status; + char username[BBS_USER_UN_SZ]; +} bbs_user; + +typedef struct user_config_file +{ + char *password; +} user_config_file; + +#define BBS_USER_INIT { BBS_USER_GUEST, {0} }; + +//GLOBAL USER +#ifdef __MICROBBS_MAIN +bbs_user g_user = BBS_USER_INIT; +#else +extern bbs_user g_user; +#endif + +int bbs_login( term_screen *ts ); +int bbs_login_auth( const char *dir, const char *username, const char *password ); + +int bbs_user_init( bbs_user *bu ); +int bbs_user_auth( bbs_user *bu ); +int bbs_user_set_status( bbs_user *bu, int status ); +int bbs_user_get_status( bbs_user *bu ); + +#endif
\ No newline at end of file |