#!/usr/bin/bash

##############################################################################
# This file is part of the CRYPTO BONE
# File     : safewebdrop   (server) 
# Version  : 1.6 (ALL-IN-ONE)
# License  : BSD
# Date     : 12 May 2023
# Contact  : Please send enquiries and bug-reports to innovation@senderek.ie
#
# Copyright (c) 2015-2023
#	Ralf Senderek, Ireland.  All rights reserved. (https://senderek.ie)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#	   This product includes software developed by Ralf Senderek.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND  ANY EXPRESS OR 
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  IMPLIED WARRANTIES 
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.
##############################################################################


#----------------------------------------------------#
# load functions and global variables
. ./safewebdrop-functions

function clean {
     if [[ ${ID} ]] ; then
          /usr/bin/rm -f ${FILE} 2> /dev/null
          /usr/bin/rm -f ${DEST}/${ID}/delete 2> /dev/null
          /usr/bin/rm -f ${DEST}/${ID}/batch 2> /dev/null
     fi
}

#----------------------------------------------------#


/usr/bin/echo Content-type: text/html
/usr/bin/echo

ID=""
REQ=""
# extract ID  and REQ
OLDIFS="${IFS}"
IFS=\&
set -- $QUERY_STRING 
ID=$1
REQ=$2
HASHSIG=$3
IFS=${OLDIFS}

# get the IP
IP=$REMOTE_ADDR

# check if a valid user exists
check_userid ${ID}

LOG=${DEST}/${ID}/log
FILE=${DEST}/${ID}/request

# request=NEW or it consists of signed data
if [[ ${REQ} = "NEW" ]]; then
     # phase 1
     log "create a new safewebdrop request"
     generate_challenge
     return_challenge
fi

if [[ ${REQ} != "NEW" ]] && [[ ${REQ} ]] ; then

     # phase 2
     log "signed request"
     # load the stored credentials
     if [[ -r ${FILE} ]] ; then
          .  ${FILE}
     fi

     # check if new request has same ADDR
     if [[ ${IP} != ${ADDR}  ]] ; then
         # dismiss this request
	 clean
         exit 4
     fi

     
     # verify the signature
     verify_request
     # signature is OK
     
     # split the request
     OLDIFS="${IFS}"
     IFS=\:
     set -- ${REQ} 
     WID=$1
     WCHL=$2
     WREQ=$3
     # WREQ is "GET" or "hash(batch)"
     IFS=${OLDIFS}
    
     check_user_and_challenge
     # both match the stored credentials in the request file

     if [[ ${WREQ} = "GET" ]] ; then
          # check if webdrops are present
	  LIST=$(/usr/bin/ls ${DEST}/${ID}/drop* 2>/dev/null)
	  if (( ${#LIST} == 0 )) ; then
	       # no safewebdrops for this user
               /usr/bin/echo -n EMPTY
	       clean
	       exit 0
	  else
	       # store the list for deletion
	       RES=$(cd ${DEST}/${ID}; /usr/bin/ls drop* 2>/dev/null)
	       /usr/bin/echo -n $RES > ${DEST}/${ID}/delete
	       BATCH=${DEST}/${ID}/batch
               /usr/bin/cat ${DEST}/${ID}/drop* > ${BATCH}
	       HASH=$(safewebdrophash "$(/usr/bin/cat ${BATCH})")
	       /usr/bin/echo -n "HASH=${HASH}" >> $FILE 
	       /usr/bin/echo -n "$(cat ${BATCH})"
	       
	       # end the request
	       exit 0
	  fi
     else
          # check hash(BATCH)
	  if [[ ${WREQ} = ${HASH} ]] ; then
	       # remove transferred files
               RES=$(cd ${DEST}/${ID}; /usr/bin/rm -f drop*  > /dev/null 2> /dev/null)
	       clean
	       log "transferred files deleted"
	       /usr/bin/echo -n "OK"
	       exit 0
	  else
	       clean
               /usr/bin/echo -n "hash failed."
	       exit 3
	  fi
     fi
fi     
clean
exit 0
