#!/usr/bin/bash

##############################################################################
# This file is part of the CRYPTO BONE
# File     : safewebdropgetfiles   (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 {
     rm -f ${DEST}/${ID}/attachrequest 2> /dev/null
     rm -f $DEST/$ID/received 2> /dev/null
     rm -f ${DEST}/${ID}/signedrequest 2> /dev/null
     rm -f ${DEST}/${ID}/verified 2> /dev/null
     rm -f ${DEST}/${ID}/delete 2> /dev/null
     rm -f ${DEST}/${ID}/filebatch 2> /dev/null
}
#----------------------------------------------------#


/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}/attachrequest

# request=NEW or it consists of signed data

if [[ ${REQ} = "NEW" ]]; then
     # phase 1
     log "create a new safewebdropfiles request"
     generate_challenge
     return_challenge
fi

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

     # phase 2
     log "signed file 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 "ALL" or "hash(batch)"
     IFS=${OLDIFS}

     check_user_and_challenge

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