aboutsummaryrefslogtreecommitdiff
path: root/boca-1.5.0/tools/updatescore.sh
blob: 65522b79cf2de9af6ec78914e303c70d68c81165 (plain)
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
#!/bin/bash
# ////////////////////////////////////////////////////////////////////////////////
# //BOCA Online Contest Administrator
# //    Copyright (C) 2003-2012 by BOCA Development Team (bocasystem@gmail.com)
# //
# //    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 updated 10/jul/2012 by cassio@ime.usp.br
privatedir=/var/www/boca/src/private/remotescores
others=$privatedir/otherservers
if [ "$1" == "" -o "$2" == "" ]; then
  echo "Usage $0 <remotescorefolder> <serversfile>"
  echo "e.g. $0 $privatedir $others"
  echo "*** When arguments are not given, default values as of the previous line is used"
fi
if [ "$1" != "" ]; then
  privatedir=$1
fi
if [ "$2" != "" ]; then
  others=$2
else
  others=$privatedir/otherservers
fi

for i in id chown chmod md5sum shasum wget tr cut awk tail head grep cat sed sleep; do
  p=`which $i`
  if [ -x "$p" ]; then
    echo -n ""
  else
    echo command "$i" not found
    exit 1
  fi
done
if [ "`id -u`" != "0" ]; then
  echo "Script must run as root"
fi

if [ ! -d $privatedir ]; then
  echo "Could not find directory $privatedir"
  exit 1
fi
tempdir=$privatedir/tmp
mkdir -p $tempdir >/dev/null 2>/dev/null
if [ ! -d $tempdir ]; then
  echo "Could not create directory $tempdir"
  exit 1
fi
httpbocadir=boca
secs=120
apacheuser=
[ -r /etc/icpc/apacheuser ] && apacheuser=`cat /etc/icpc/apacheuser | sed 's/ \t\n//g'`
[ "$apacheuser" == "" ] && apacheuser=www-data
id -u $apacheuser > /dev/null 2>/dev/null
[ $? != 0 ] && echo "User $apacheuser not found -- error to set permissions with chown/chmod"

hash="shasum -a 256 -"
#hash="md5sum -"

#rm -f $privatedir/score_*.dat
chown $apacheuser.root $privatedir/score_*.dat

if [ ! -r $others ]; then
  echo "External server list in $others not found"
  exit 1
fi
echo "Starting loop to get scores from servers defined in $others"
while /bin/true; do
	echo "Getting scores..."
	qtd=1
	for BOCASERVER in `grep -v "^[ \t]*\#" $others | awk '{ print $1; }'`; do
		if [ "$BOCASERVER" == "" ]; then
			continue
		fi
		echo $BOCASERVER | grep -q "http"
		[ $? == 0 ] || BOCASERVER=http://$BOCASERVER/boca

		user=`grep -v "^[ \t]*\#" $others | head -n$qtd | tail -n1 | awk '{ print $2; }'`
		[ "$user" == "" ] && user=score
		pass=`grep -v "^[ \t]*\#" $others | head -n$qtd | tail -n1 | awk '{ print $2; }'`
		[ "$pass" == "" ] && pass=score
		let "qtd = $qtd + 1"

		echo -n "Asking server $BOCASERVER. Authenticating with user '$user'..."
		md=`wget -t3 -T3 -S $BOCASERVER/index.php -O /dev/null --save-cookies $tempdir/.cookie.txt --keep-session-cookies 2>&1 | grep PHPSESS | tail -n1 | cut -f2 -d'=' | cut -f1 -d';'`
		res=`echo -n $pass | $hash | cut -f1 -d' '`
		res=`echo -n "${res}${md}" | $hash | cut -f1 -d' '`
		echo -n "sending password..."
		wget -t3 -T3 "$BOCASERVER/index.php?name=${user}&password=${res}" --load-cookies $tempdir/.cookie.txt --keep-session-cookies --save-cookies $tempdir/.cookie.txt -O $tempdir/.temp.txt 2>/dev/null >/dev/null
		grep -qi incorrect $tempdir/.temp.txt
		if [ $? != 0 ]; then
			rm -f $tempdir/*
			echo "downloading scoretable..."
			wget -t3 -T3 "$BOCASERVER/scoretable.php?remote=-42" --load-cookies $tempdir/.cookie.txt --keep-session-cookies --save-cookies $tempdir/.cookie.txt -O $tempdir/score.zip 2>$tempdir/.bocascore.tmp >$tempdir/.bocascore.tmp
			if [ $? == 0 ]; then
				unzip -qq $tempdir/score.zip
				if [ $? == 0 ]; then
					for fscore in `ls $tempdir/*.dat`; do
						chown $apacheuser.root "$tempdir/$fscore"
						chmod 660 "$tempdir/$fscore"
						mv "$tempdir/$fscore" "$privatedir/score_$fscore"
						echo "Score downloaded successfully into $privatedir/score_$fscore"
					done
				else
					echo "Error: score file from $BOCASERVER is not a valid package"
				fi
			else
				echo "Error getting score file from $BOCASERVER: `cat $tempdir/.bocascore.tmp`"
			fi
		else
			echo "Error authenticating to server $BOCASERVER"
		fi
		rm -f $tempdir/.temp.txt
		rm -f $tempdir/.cookie.txt
	done

	echo -n "Waiting $secs secs..."
	sleep $secs
	echo ""
done