#!/bin/sh
# Copyright (C) 2009-2012 David Bird (Coova Technologies) <support@coova.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/>.

. ./functions.sh
. ./uam.sh
. ./wispr.sh

USERS=/tmp/coova/etc/chilli/localusers
COOVA_USERURL=$COOKIE_COOVA_USERURL
COOVA_SESSIONID=$CHI_SESSION_ID
COOVA_CHALLENGE=$CHI_CHALLENGE
PORTAL_SESSIONID=${COOKIE_PORTAL_SESSIONID:-$SESSIONID}
FORM_userurl=${FORM_userurl:-http://www.coova.org/}
    
http_redirect2() {
cat <<EOF
HTTP/1.1 302 Redirect
Location: $1
Set-Cookie: PORTAL_SESSIONID=$PORTAL_SESSIONID
Set-Cookie: COOVA_USERURL=$COOVA_USERURL
Connection: close

EOF
    exit
}

http_redirect() {
    http_header
    cat <<EOF
<body onload="document.form1.submit();">
<form action="$1" name="form1" id="form1" method="post">
<input name="res" value="$FORM_res" type="hidden">
<input name="reply" value="$FORM_reply" type="hidden">
</form>
</body>
EOF
    exit
}

http_header() {
#    [ "$HS_MODE" = "hotspot" ] || {
#	http_redirect2 "/www/disabled.chi"
#    }
    cat<<EOF    
HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: PORTAL_SESSIONID=$PORTAL_SESSIONID
Set-Cookie: COOVA_USERURL=$COOVA_USERURL
Connection: close
Cache: none

EOF
}

header() {
    echo "<html><head>"

    uamfile title 0

    echo "<meta http-equiv=\"Cache-control\" content=\"no-cache\"/>
<meta http-equiv=\"Pragma\" content=\"no-cache\"/>
<style>"
    
    uamfile "css" 0 

    echo "</style>"
    echo "<script>"

    uamfile "js" 0 

    echo "</script>"
    
    echo "<link rel=\"stylesheet\" type=\"text/css\" HREF=\"captive_login.css\">"
    
    echo "$1</head><body$2>"

    uamfile "header" 1 

    echo "<div id=\"body\">"
}

footer() {
    echo "</div>" 

    uamfile "footer" 1 

    echo "</body></html>"
}

error() { echo "<div class=\"err\">$1</div>"; }

href() {
    echo "<a href=\"$1\">$2</a>"
}

form() {
    echo "<form name=\"form\" method=\"post\" action=\"$1\"><INPUT TYPE=\"hidden\" NAME=\"userurl\" VALUE=\"$FORM_userurl\">$2</form>"
}

loginform() {
    case "$AUTHENTICATED" in
	1)
	    ;;
	
	*)
	    [ "$HS_OPENIDAUTH" = "on" ] && { \
		echo "<div id=\"login-label\" style=\"display:none;\"><label><a href=\"javascript:toggleAuth('login')\">&lt;&lt; back</a></label></div>"
		form "login.chi" "$(uamfile openid_form 1)"
	    }
	    
	    form "login.chi" "$(uamfile login_form 1)"
	    ;;
    esac
}

local_login_url() {
    if [ "$HS_USELOCALUSERS" = "on" ]; then
	line=$(head -1 $USERS)
	if [ "$line" = "" ]; then
	    echo "tos:$(echo '$$$(date)'|md5sum|cut -f1)" >> $USERS
	    line=$(head -1 $USERS)
	fi
	if [ "$line" != "" ]; then
	    user=$(echo "$line" | cut -f1 -d:)
	    pass=$(echo "$line" | cut -f2 -d:)
	    echo -n $(chi_login_url "$user" "$pass")
	fi
    else
	user=$REMOTE_MAC
	pass=$HS_ADMPWD
	echo -n $(chi_login_url "$user" "$pass")
    fi
}

reply_message() {
    case "$AUTHENTICATED" in
	1)
	    echo "You are now on-line!"
	    ;;

	*)
	   echo "$FORM_reply"
	   ;;
    esac
}

image() {
    ext=$(echo "$1"|awk -v FS=. '{ print tolower($NF) }')
    base=$(echo "$1"|awk -v FS=/ '{ gsub(/[^a-zA-Z0-9_\/-]/,""); print tolower($NF) }')
    echo -n "img-$base.$ext"
}

registerform() {
    form "register.chi" "$(uamfile register_form 1)"
}

contactform() {
    form "contact.chi" "$(uamfile contact_form 1)"
}

termsform() {
    form "tos.chi" "$(uamfile terms_form 1)"
}

runlogin() {
    out=$($CHILLI_QUERY login sessionid "$COOVA_SESSIONID" username "$1" password "$2")
}

chi_login_url() {
    case "$HS_RAD_PROTO" in
	pap)
	    response=$($CHILLI_RESPONSE -pap "$CHI_CHALLENGE" "$HS_UAMSECRET" "$2")
	    echo -n "http://$HS_UAMLISTEN:$HS_UAMPORT/login?username=$1&password=${response}&userurl=${3:-$COOVA_USERURL}"
	    ;;
	mschapv2)
	    response=$($CHILLI_RESPONSE -nt "$CHI_CHALLENGE" "$HS_UAMSECRET" "$1" "$2")
	    echo -n "http://$HS_UAMLISTEN:$HS_UAMPORT/login?username=$1&ntresponse=${response}&userurl=${3:-$COOVA_USERURL}"
	    ;;
	*)
	    response=$($CHILLI_RESPONSE "$CHI_CHALLENGE" "$HS_UAMSECRET" "$2")
	    echo -n "http://$HS_UAMLISTEN:$HS_UAMPORT/login?username=$1&response=${response}&userurl=${3:-$COOVA_USERURL}"
	    ;;
    esac
}


dologin() {
    url=$(chi_login_url "$FORM_username" "$FORM_password" "$FORM_userurl")
    cat <<ENDHTML
<html><head>
<meta http-equiv="refresh" content="0;url=$url"/>
</head></html>
ENDHTML
    wisprLoginResultsURL "$url"
}

domail() {
    from=$1;to=$2;file=$3
    (uamfile "$file" 0
	echo
	echo "-------------------------------------------------"
	echo "Powered by Coova - http://www.coova.org/"
	echo) | /usr/sbin/sendmail -t -f "$from" && return 0
    return 1;
}

open_newpage() {
    echo "<script>"
    echo "open_page()"
    echo "</script>" 
}

FORM_username="${FORM_username:-$FORM_UserName}"
FORM_username="${FORM_username:-$FORM_Username}"
FORM_password="${FORM_password:-$FORM_Password}"

# For WISPr 2.0 EAP, bounce back to chilli
[ "$FORM_res" = "wispr" ] && \
    [ "$FORM_WISPrEAPMsg" != "" ] && \
    [ "$FORM_WISPrVersion" = "2.0" ] && {
    http_redirect2 "http://$HS_UAMLISTEN:$HS_UAMPORT/login?username=$FORM_username&WISPrEAPMsg=$FORM_WISPrEAPMsg&WISPrVersion=2.0"
}

if [ "$FORM_uamip" != "" ] && [ "$HS_UAMSECRET" != "" ]
then
    if [ "$FORM_res" != "wispr" ]
    then
	QS=$(echo $QUERY_STRING | sed 's/&md=[^&=]*$//')
	HTTP="http"
	[ "$HTTPS" = "on" ] && HTTP="https"
	URL="$HTTP://$SERVER_NAME/$REQUEST_URI?$QS"
	CHECK="$URL$HS_UAMSECRET"
	CHECK_MD5=$(echo -n "$CHECK" |md5sum|cut -d' ' -f1|tr 'a-z' 'A-Z');
	if [ "$CHECK_MD5" = "$FORM_md" ]; then
	    COOVA_USERURL=$FORM_userurl
	else
	    http_redirect "/www/error.chi"
	fi
    fi
fi

