-
Notifications
You must be signed in to change notification settings - Fork 0
/
flog.php
272 lines (262 loc) · 14.6 KB
/
flog.php
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
<?php
////////////////////////////////////////////////////////////////////////////////
//BOCA Online Contest Administrator
// Copyright (C) 2003-2012 by BOCA Development Team ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////
// Last modified 21/jul/2012 by [email protected]
function DBDropLogTable() {
$c = DBConnect();
$r = DBExec($c, "drop table \"logtable\"", "DBDropLogTable(drop table)");
}
function DBCreateLogTable() {
$c = DBConnect();
$conf = globalconf();
if($conf["dbuser"]=="") $conf["dbuser"]="bocauser";
$r = DBExec($c, "
CREATE TABLE \"logtable\" (
\"lognumber\" serial, -- (serial para o log)
\"contestnumber\" int4 NOT NULL, -- (id do concurso)
\"sitenumber\" int4 NOT NULL, -- (id do site local)
\"loguser\" int4, -- (usuario envolvido com o log)
\"logip\" varchar(20) NOT NULL, -- (numero do site do usuario envolvido)
\"logdate\" int4 NOT NULL, -- (dia/hora da criacao deste registro)
\"logtype\" varchar(20) NOT NULL, -- (tipo de registro: error, warn, info, debug)
\"logdata\" text NOT NULL, -- (descricao do registro)
\"logstatus\" varchar(20) DEFAULT '', -- (status do registro)
CONSTRAINT \"log_pkey\" PRIMARY KEY (\"lognumber\"),
CONSTRAINT \"site_fk\" FOREIGN KEY (\"contestnumber\", \"sitenumber\")
REFERENCES \"sitetable\" (\"contestnumber\", \"sitenumber\")
ON DELETE CASCADE ON UPDATE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE,
CONSTRAINT \"loguser\" FOREIGN KEY (\"contestnumber\", \"loguser\", \"sitenumber\")
REFERENCES \"usertable\" (\"contestnumber\", \"usernumber\", \"usersitenumber\")
ON DELETE CASCADE ON UPDATE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE
)", "DBCreateLogTable(create table)");
$r = DBExec($c, "REVOKE ALL ON \"logtable\" FROM PUBLIC", "DBCreateLogTable(revoke public)");
$r = DBExec($c, "GRANT INSERT, SELECT ON \"logtable\" TO \"".$conf["dbuser"]."\"", "DBCreateLogTable(grant bocauser)");
$r = DBExec($c, "CREATE INDEX \"log_index\" ON \"logtable\" USING btree ".
"(\"contestnumber\" int4_ops, \"sitenumber\" int4_ops, \"logdate\" int4_ops)",
"DBCreateLogTable(create log_index)");
$r = DBExec($c, "CREATE INDEX \"log_index2\" ON \"logtable\" USING btree ".
"(\"contestnumber\" int4_ops, \"loguser\" int4_ops, \"sitenumber\" int4_ops)",
"DBCreateLogTable(create log_index2)");
$r = DBExec($c, "REVOKE ALL ON \"logtable_lognumber_seq\" FROM PUBLIC", "DBCreateLogTable(revoke public seq)");
$r = DBExec($c, "GRANT ALL ON \"logtable_lognumber_seq\" TO \"".$conf["dbuser"]."\"", "DBCreateLogTable(grant bocauser seq)");
}
////////////////////funcoes para logar////////////////////////////////////////////////////////////////
function DBNewLog($contest, $site, $user, $type, $ip, $data, $status) {
$t = time();
$data = str_replace("'", "\"", $data);
DBExecNoSQLLog ("insert into logtable (contestnumber, sitenumber, loguser, logdate, logtype, " .
"logip, logdata, logstatus) values ($contest, $site, $user, $t, '$type', '$ip', '$data', '$status')",
"DBNewLog(insert log)");
}
function DBGetLogs($o, $contest, $site, $user, $type, $ip, $limit) {
$c = DBConnect();
$where = "";
if ($site != "") $where .= "sitenumber=$site and ";
if ($user != "") $where .= "loguser=$user and ";
if ($type != "") $where .= "logtype='$type' and ";
if ($ip != "") $where .= "logip='$ip' and ";
$where .= "contestnumber=$contest";
switch ($o) {
case "user": $order="contestnumber, sitenumber, loguser, logdate desc"; break;
case "type": $order="contestnumber, sitenumber, logtype, logdate desc"; break;
case "ip": $order="contestnumber, sitenumber, logip, logdate desc"; break;
default: $order="contestnumber, sitenumber, logdate desc"; break;
}
$r = DBExec ($c, "select contestnumber as contest, sitenumber as site, loguser as user, logdate as date, " .
"logtype as type, logip as ip, logdata as data, logstatus as status from logtable " .
" where $where order by $order limit $limit", "DBGetLogs(get logs)");
$n = DBnlines($r);
$a = array();
for ($i=0;$i<$n;$i++)
$a[$i] = DBRow($r,$i);
return $a;
}
//funcao para fazer o login de um usuario. Vai procurar por um contest ativo, verificar qual o site
//local, e entao procurar pelo usuario no site local do contest ativo. Alem disso, verifica outras
//flags, como logins habilitados, ip correto, se usuario ja esta logado, etc
//$name eh o nome do usuario
//$pass eh o password
function DBLogIn($name,$pass, $msg=true) {
$b = DBGetRow("select * from contesttable where contestnumber=0", 0, null, "DBLogIn(get template contest)");
if ($b != null) {
$r = DBLogInContest($name, $pass, $b["contestnumber"], false);
if($r !== false) return $r;
}
$b = DBGetRow("select * from contesttable where contestactive=true", 0, null, "DBLogIn(get active contest)");
if ($b != null) {
$r = DBLogInContest($name, $pass, $b["contestnumber"], $msg);
if ($r !== false) return $r;
} else {
LOGLevel("There is no active or template contest.",0);
MSGError("There is no active or template contest.");
}
return false;
}
function DBLogInContest($name,$pass,$contest,$msg=true) {
$b = DBGetRow("select * from contesttable where contestnumber=$contest", 0, null, "DBLogIn(get active contest)");
if ($b == null) {
LOGLevel("There is no contest $contest.",0);
if($msg) MSGError("There is no contest $contest, contact an admin.");
return false;
}
$d = DBSiteInfo($b["contestnumber"], $b["contestlocalsite"],null,false);
if ($d == null) {
if($msg) MSGError("There is no active site, contact an admin.");
return false;
}
$a = DBGetRow("select * from usertable where username='$name' and contestnumber=".
$b["contestnumber"]." and " .
"usersitenumber=".$b["contestlocalsite"], 0, null, "DBLogIn(get user)");
if ($a == null) {
if($msg) {
LOGLevel("User $name tried to log in contest $contest but it does not exist.",2);
MSGError("User does not exist or incorrect password.");
}
return false;
}
$a = DBUserInfo($b["contestnumber"], $b["contestlocalsite"],$a['usernumber'],null,false);
$_SESSION['usertable'] = $a;
$_SESSION['usertable']['usersession']='';
$_SESSION['usertable']['userip']='';
$p = myhash($a["userpassword"] . session_id());
$_SESSION['usertable']['userpassword'] = $p;
if ($d["sitepermitlogins"]=="f" && $a["usertype"] != "admin" && $a["usertype"] != "judge" && $a["usertype"] != "site" && $a["usertype"] != "staff") {
LOGLevel("User $name tried to login contest $contest but logins are denied.",2);
if($msg) MSGError("Logins are not allowed.");
unset($_SESSION["usertable"]);
return false;
}
if ($a["userenabled"] != "t") {
LOGLevel("User $name tried to log in contest $contest but it is disabled.",2);
if($msg) MSGError("User disabled.");
unset($_SESSION["usertable"]);
return false;
}
if ($a["userpassword"] != "" && $p != $pass) {
LOGLevel("User $name tried to log in contest $contest but password was incorrect.",2);
if($msg) MSGError("Incorrect password.");
unset($_SESSION["usertable"]);
return false;
}
if(!ctype_alnum($name)) {
LOGLevel("User $name tried to log in contest $contest but username is not alphanum.",2);
if($msg) MSGError("Username must be alpha numeric.");
unset($_SESSION["usertable"]);
return false;
}
$ccode = trim($_SERVER['HTTP_USER_AGENT']);
$ds = DIRECTORY_SEPARATOR;
if($ds=="") $ds = "/";
$dircode=$_SESSION["locr"] . $ds . "private" . $ds . "agentcode";
@mkdir($dircode);
$dircode .= $ds . $contest . '-' . $name;
@file_put_contents($dircode . '.log', $ccode . "\n", FILE_APPEND | LOCK_EX);
if(@file_exists($dircode)) {
if(($prevuser = @file_get_contents($dircode)) === false) {
LOGLevel("User $name tried to log in contest $contest but computer file cannot be read.",2);
if($msg) MSGError("Invalid computer (2).");
unset($_SESSION["usertable"]);
return false;
}
//if($prevuser != $ccode && $a["usertype"] == "team") {
// LOGLevel("User $name tried to log in contest $contest but computer is invalid ($ccode).",2);
// if($msg) MSGError("Invalid computer (3).");
// unset($_SESSION["usertable"]);
// return false;
//}
} else {
if($a["usertype"] == "team")
@file_put_contents($dircode, $ccode);
}
$gip=getIP();
//if(!ValidCookie(true,$gip)) {
// if($msg) MSGError("Invalid computer (4).");
// unset($_SESSION["usertable"]);
// return false;
//}
if ($a["userip"] != $gip && $a["userip"] != "" && $a["usertype"] != "score") {
LOGLevel("User $name is using two different IPs: " . $a["userip"] .
"(" . dateconv($a["userlastlogin"]) .") and " . $gip,1);
if($msg && $a["usertype"] != "admin" && $a["usermultilogin"] != "t") MSGError("You are using two distinct IPs. Admin notified.");
}
if ($a["userpermitip"] != "") {
$ips=explode(';',$a["userpermitip"]);
$gips=explode(';',$gip);
if(count($gips) < count($ips)) {
IntrusionNotify("Invalid IP: " . $gip);
ForceLoad("index.php");
}
for($ipss=0;$ipss<count($ips);$ipss++) {
$gipi=$gips[$ipss];
$ipi=$ips[$ipss];
if(!match_network($ipi, $gipi)) {
IntrusionNotify("Invalid IP: " . $gip);
ForceLoad("index.php");
}
}
}
$_SESSION['usertable']['usersession']=session_id();
$_SESSION['usertable']['userip']=$gip;
$c = DBConnect();
$t = time();
if($a["usertype"] == "team" && $a["usermultilogin"] != "t" && $a["userpermitip"] == "") {
$r = DBExec($c,"update usertable set userip='" . $gip . "', updatetime=" . time() . ", userpermitip='" . $gip . "'," .
"userlastlogin=$t, usersession='".session_id()."' where username='$name' and contestnumber=".
$b["contestnumber"]." and usersitenumber=".$b["contestlocalsite"], "DBLogIn(update session)");
} else {
DBExec($c,"begin work");
$sql = "update usertable set usersessionextra='".session_id()."' where username='$name' and contestnumber=".
$b["contestnumber"]." and usersitenumber=".$b["contestlocalsite"] .
" and (usersessionextra='' or userip != '" . $gip ."' or userlastlogin<=" . ($t-86400) . ")";
DBExec($c,$sql);
DBExec($c,"update usertable set userip='" . $gip . "', updatetime=" . time() . ", userlastlogin=$t, ".
"usersession='".session_id()."' where username='$name' and contestnumber=".
$b["contestnumber"]." and usersitenumber=".$b["contestlocalsite"], "DBLogIn(update user)");
if($name=='admin') {
list($clockstr,$clocktime)=siteclock();
if($clocktime < -600)
DBExec($c,"update contesttable set contestunlockkey='' where contestnumber=" . $b["contestnumber"], "DBLogInContest(update contest)");
}
DBExec($c,"commit work");
}
LOGLevel("User $name authenticated (" . $gip . ")",2);
return $a;
}
//faz o logout. Note que o timestamp de logout fica sem sentido quando o usuario
//eh do tipo multilogin
function DBLogOut($contest, $site, $user, $isadmin=false) {
$c = DBConnect();
$r = DBExec($c,"update usertable set usersession='',usersessionextra='', updatetime=".time().", " .
"userlastlogout=".time()." where usernumber=$user and " .
"contestnumber=$contest and usersitenumber=$site", "DBLogOut(update user)");
if($isadmin) {
list($clockstr,$clocktime)=siteclock();
if($clocktime < -600) {
DBExec($c,"update contesttable set contestunlockkey='' where contestnumber=$contest", "DBLogOut(update contest)");
DBExec($c,"update problemtable set problemfullname='', problembasefilename='' where problemfullname !~ '(DEL)' and contestnumber=$contest", "DBLogOut(update problems)");
$ds = DIRECTORY_SEPARATOR;
if($ds=="") $ds = "/";
$dir=$_SESSION["locr"] . $ds . "private" . $ds . "problemtmp" . $ds;
foreach(glob($dir . '*') as $file) {
cleardir($file,false,true);
}
}
}
LOGLevel("User $user (contest=$contest,site=$site) logged out.",2);
}
// eof
?>