aboutsummaryrefslogtreecommitdiff
path: root/xp/navigator/cgi-src/navigator_cgi.cpp
blob: 20e152e2f8b4a1168c9fc34464ffe3c0a542a8c5 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>
#include <sstream>

#include <jsoncpp/json/json.h>

#include <mysql/my_global.h>
#include <mysql/mysql.h>
MYSQL* sql;
const char* rand_ch="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Json::Value do_login(Json::Value o)
{
	MYSQL_RES* sqlr=NULL;
	MYSQL_ROW row;
	char q[256];
	Json::Value ret;
	std::string usrname=o["username"].asString();
	std::string passwd=o["passwd"].asString();
	std::string sessname=o["sessionname"].asString();
	std::string token;
	std::string qpwd;
	if(usrname.length()<1){ret["result"]=1;goto fail;}
	for(size_t i=0;i<usrname.length();++i)if(!isalnum(usrname[i])&&usrname[i]!='-'&&usrname[i]!='_'){ret["result"]=1;goto fail;}
	for(size_t i=0;i<passwd.length();++i)if(!isalnum(passwd[i])){ret["result"]=1;goto fail;}
	for(size_t i=0;i<sessname.length();++i)if(!isalnum(sessname[i])&&sessname[i]!='-'&&sessname[i]!='_'){ret["result"]=1;goto fail;}
	
	snprintf(q,256,"select password from navigator_user where username='%s'",usrname.c_str());
	mysql_query(sql,q);
	sqlr=mysql_store_result(sql);
	if(mysql_num_rows(sqlr)!=1){ret["result"]=4;goto fail;}
	row=mysql_fetch_row(sqlr);
	qpwd=std::string(row[0],64);
	if(passwd!=qpwd)
	{
		ret["result"]=1;
		goto fail;
	}
	mysql_free_result(sqlr);sqlr=NULL;

	if(!sessname.length())
	{
		snprintf(q,256,"delete from navigator_session where sessionname='' and username='%s'",usrname.c_str());
		mysql_query(sql,q);
		mysql_commit(sql);
	}
	else
	{
		snprintf(q,256,"select sessionname from navigator_session where sessionname='%s' and username='%s'",
		sessname.c_str(),usrname.c_str());
		mysql_query(sql,q);
		sqlr=mysql_store_result(sql);
		if(mysql_num_rows(sqlr)>0){ret["result"]=2;goto fail;}
		mysql_free_result(sqlr);sqlr=NULL;
	}

	do{
		if(sqlr){mysql_free_result(sqlr);sqlr=NULL;}
		token="";
		for(int i=0;i<32;++i)token.push_back(rand_ch[rand()%62]);
		snprintf(q,256,"select token from navigator_session where token='%s'",
		token.c_str());
		mysql_query(sql,q);
		sqlr=mysql_store_result(sql);
	}while(mysql_num_rows(sqlr));
	mysql_free_result(sqlr);sqlr=NULL;
	
	snprintf(q,256,"insert into navigator_session values('%s','%s','%s',%lld)",
		usrname.c_str(),
		token.c_str(),
		sessname.c_str(),
		time(NULL)
	);
	mysql_query(sql,q);
	mysql_commit(sql);
	ret["result"]=0;
	ret["token"]=token;
fail:
	if(sqlr){mysql_free_result(sqlr);sqlr=NULL;}
	return ret;
}
const int tsl[]={1800,86400,604800,1296000,2592000,5184000,7776000,15552000,31104000};
void set_session_length(std::string user,int c)
{
	char q[256];
	if(c>7)c=7;if(c<0)c=0;
	snprintf(q,256,"update navigator_user set session_length=%d where username='%s'",user.c_str());
	mysql_query(sql,q);
	mysql_commit(sql);
}
int get_session_length(std::string user)
{
	MYSQL_RES* sqlr=NULL;
	MYSQL_ROW row;
	char q[256];
	snprintf(q,256,"select session_length from navigator_user where username='%s'",user.c_str());
	mysql_query(sql,q);
	sqlr=mysql_store_result(sql);
	if(mysql_num_rows(sqlr)!=1){mysql_free_result(sqlr);return -1;}
	row=mysql_fetch_row(sqlr);
	int sl=atoi(row[0]);
	mysql_free_result(sqlr);
	return sl;
}
std::string authenticate(std::string token)
{
	MYSQL_RES* sqlr=NULL;
	MYSQL_ROW row;
	char q[256];
	if(token.length()!=32)return "";
	snprintf(q,256,"select username,sessionname,date from navigator_session where token='%s'",token.c_str());
	mysql_query(sql,q);
	sqlr=mysql_store_result(sql);
	if(mysql_num_rows(sqlr)!=1){mysql_free_result(sqlr);return "";}
	row=mysql_fetch_row(sqlr);
	std::string ret=std::string(row[0]);
	bool is_temporary=strlen(row[1])>0;
	long long d=atoll(row[2]);
	mysql_free_result(sqlr);
	int sl=get_session_length(ret);
	if(sl<0||sl>7){return "";}
	int reall=tsl[is_temporary?0:sl+1];
	if(time(NULL)-d>reall)
	{
		snprintf(q,256,"delete from navigator_session where token='%s'",token.c_str());
		mysql_query(sql,q);
		mysql_commit(sql);
		return "";
	}
	return ret;
}
Json::Value set_option(Json::Value o)
{
	Json::Value ret;
	std::string token=o["token"].asString();
	std::string usr=authenticate(token);
	int opt=o["option"].asInt();
	int val=o["value"].asInt();
	if(!usr.length()){ret["result"]=1;goto fail;}
	switch(opt)
	{
		case 0:
			ret["result"]=0;
			set_session_length(usr,val);
		break;
		default:
		ret["result"]=1;
	}
fail:
	return ret;
}
Json::Value get_option(Json::Value o)
{
	Json::Value ret;
	std::string token=o["token"].asString();
	std::string usr=authenticate(token);
	int opt=o["option"].asInt();
	if(!usr.length()){ret["result"]=1;goto fail;}
	switch(opt)
	{
		case 0:
			ret["result"]=0;
			ret["value"]=get_session_length(usr);
		break;
		default:
		ret["result"]=1;
	}
fail:
	return ret;
}
Json::Value get_bookmarks(Json::Value o)
{
	MYSQL_RES* sqlr=NULL;
	MYSQL_ROW row;
	char q[256];
	Json::Value ret;
	std::string token=o["token"].asString();
	std::string usr=authenticate(token);
	if(!usr.length()){ret["result"]=1;goto fail;}
	snprintf(q,256,"select bookmarks from navigator_user where username='%s'",usr.c_str());
	mysql_query(sql,q);
	sqlr=mysql_store_result(sql);
	if(mysql_num_rows(sqlr)!=1){mysql_free_result(sqlr);sqlr=NULL;ret["result"]=4;goto fail;}
	row=mysql_fetch_row(sqlr);
	ret["result"]=0;
	ret["bookmarks"]=std::string(row[0]);
	mysql_free_result(sqlr);sqlr=NULL;
fail:
	if(sqlr){mysql_free_result(sqlr);sqlr=NULL;}
	return ret;
}
Json::Value set_bookmarks(Json::Value o)
{
	char *q=(char*)malloc(65537);
	Json::Value ret;
	std::string token=o["token"].asString();
	std::string usr=authenticate(token);
	if(!usr.length()){ret["result"]=1;goto fail;}
	snprintf(q,65536,"update navigator_user set bookmarks='%s' where username='%s'",o["bookmarks"].asString().c_str(),usr.c_str());
	mysql_query(sql,q);
	mysql_commit(sql);
	ret["result"]=0;
fail:
	free(q);
	return ret;
}
Json::Value list_sessions(Json::Value o)
{
	MYSQL_RES* sqlr=NULL;
	MYSQL_ROW row;
	char q[256];
	Json::Value ret,ss;
	std::string token=o["token"].asString();
	std::string usr=authenticate(token);
	if(!usr.length()){ret["result"]=1;goto fail;}
	snprintf(q,256,"select sessionname,date from navigator_session where username='%s'",usr.c_str());
	
	mysql_query(sql,q);
	sqlr=mysql_store_result(sql);
	for(int i=0;row=mysql_fetch_row(sqlr);++i)
	{
		Json::Value c;
		c["sessionname"]=std::string(row[0]);
		c["date"]=atoi(row[1]);
		ss[i]=c;
	}
	mysql_free_result(sqlr);sqlr=NULL;
	ret["result"]=0;ret["sessions"]=ss;
fail:
	return ret;
}
Json::Value remove_session(Json::Value o)
{
	char q[256];
	Json::Value ret;
	std::string token=o["token"].asString();
	std::string usr=authenticate(token);
	std::string sess=o["session"].asString();
	if(!usr.length()){ret["result"]=1;goto fail;}
	snprintf(q,256,"delete from navigator_session where username='%s' and sessionname='%s'",usr.c_str(),sess.c_str());
	mysql_query(sql,q);
	mysql_commit(sql);
	ret["result"]=0;
fail:
	return ret;
}
int main()
{
	if(!getenv("CONTENT_LENGTH"))return -1;
	int len=atoi(getenv("CONTENT_LENGTH"));
	char *buf;buf=(char*)malloc(len+1);
	fread(buf,1,len,stdin);buf[len]=0;
	std::string sbuf(buf,len);
	free(buf);
	std::stringstream ss(sbuf);
	Json::Value o,r;ss>>o;
	
	sql=mysql_init(NULL);
	if(!sql)return -1;
	if(!mysql_real_connect(sql,"localhost","chrisoft",NULL,"chrisoft",0,"/var/run/mysqld/mysqld.sock",0))
	return -1;
	
	switch(o.get("op",-1).asInt())
	{
		case 0://login
			r=do_login(o);
		break;
		case 1://get bookmarks
			r=get_bookmarks(o);
		break;
		case 2://set bookmarks
			r=set_bookmarks(o);
		break;
		case 3://list sessions
			r=list_sessions(o);
		break;
		case 4://remove session
			r=remove_session(o);
		break;
		case 5://set option
			r=set_option(o);
		break;
		case 6://get option
			r=get_option(o);
		break;
	}
	printf("Status: 200 OK\r\n");
	printf("Content-type: application/json; charset=utf-8\r\n\r\n");
	std::ostringstream oss;
	oss<<r;
	fputs(oss.str().c_str(),stdout);
	return 0;
}
//TODO: set cookie in HTTP header

/*
 * session_length:
 * 0=1d=86400
 * 1=7d=604800
 * 2=15d=1296000
 * 3=30d=2592000
 * 4=60d=5184000
 * 5=90d=7776000
 * 6=180d=15552000
 * 7=360d=31104000
 */
/*navigator_user:
 * username:varchar(PRI) passwd:char(64) bookmarks:text
 *navigator_session:
 * username:varchar token:char(32)(PRI) sessionname:varchar date:bigint
 */
/*
create table navigator_user(
  username varchar(32),
  password char(64),
  bookmarks text,
  session_length int,
  primary key(username)
);
create table navigator_session(
  username varchar(32) not null,
  token char(32),
  sessionname varchar(32),
  date bigint,
  primary key(token)
);
insert into navigator_user values('chrisoft','8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92','',6);
*/