summaryrefslogtreecommitdiff
path: root/user.c
blob: ecfde50df60a713243307459e084074d5b480e77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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;
}