summaryrefslogtreecommitdiffstats
path: root/user.c
blob: 1f89f4ad65a2d74179511c9923fae8538794c54f (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
244
245
246
247
248
249
250
251
252
253
#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