#!/usr/bin/bash

#***************************************************************************
# This file is part of the CRYPTO BONE
# File     : safewebdroprequest (client) 
# 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
. /usr/lib/cryptobone/safewebdrop/bin/client-functions

FETCH="${RAM}/fetch"
SOCK="/usr/lib/cryptobone/secrets.sock"

PASS=$(/usr/bin/echo "get-element webdropsecret" | /usr/bin/socat -t15 - UNIX-connect:$SOCK 2> /dev/null)
HOST=$(/usr/bin/echo "get-element webdropserver" | /usr/bin/socat -t15 - UNIX-connect:$SOCK 2> /dev/null)
ID=$(/usr/bin/echo "get-element webdropuser" | /usr/bin/socat -t15 - UNIX-connect:$SOCK 2> /dev/null)

log "---request---"

if [[ ${PASS} ]] && [[ ${HOST} ]] && [[ ${ID} ]] ; then

     # PHASE 1: send NEW request
     CHALLENGE=$(curl https://${HOST}/cgi-bin/safewebdrop?${ID}\&NEW 2> /dev/null)
     if (( ${#CHALLENGE} != 64 )) ; then
          log "I: no challenge EXIT "
	  exit 2
     fi
     log "I:  ${ID} CHL=${CHALLENGE}" 

     # PHASE 2: create a valid request

     /usr/bin/rm -f ${REQ}.hash.sig  2> /dev/null
     # user:request
     /usr/bin/echo -n "${ID}:" > $REQ
     /usr/bin/echo -n "${CHALLENGE}:" >> $REQ
     /usr/bin/echo -n "GET" >> $REQ

     # generate a hash of the request
     generate_requesthash

     # and send it 
     if (( ${#SIG} > 10 )) ; then
	  REQUEST=$(/usr/bin/cat ${REQ})
          BATCH=$(/usr/bin/curl https://${HOST}/cgi-bin/safewebdrop?${ID}\&${REQUEST}\&${SIG} 2> /dev/null)
	  log "II: $ID  ${#BATCH} bytes received" 
          if [[ ${BATCH} ]] && [[ ${BATCH} != "EMPTY" ]]  ; then
	       /usr/bin/echo -n "${BATCH}" > ${FETCH}
	       HASH=$(safewebdrophash "$(cat ${FETCH})")
	       # now add a line, so that the last message is being processed
	       /usr/bin/echo >> ${FETCH}

               if (( ${#BATCH} > 10 )) ; then
                    # return the hash of the batch to the server
                    /usr/bin/rm -f ${REQ}.hash.sig  2> /dev/null
                    # user:challenge:hash
                    /usr/bin/echo -n "${ID}:" > $REQ
                    /usr/bin/echo -n "${CHALLENGE}:" >> $REQ
                    /usr/bin/echo -n "${HASH}" >> $REQ

                    # generate a hash of the request 
                    generate_requesthash

		    # and send it 
                    if (( ${#SIG} > 10 )) ; then
                         sleep 1
			 # phase 3 : send the hash back to the server, so files can be deleted there
	                 REQUEST=$(/usr/bin/cat ${REQ})
                         OK=$(/usr/bin/curl https://${HOST}/cgi-bin/safewebdrop?${ID}\&${REQUEST}\&${SIG} 2> /dev/null)
	                 log "deleted: ${OK}"
			 if [[ ${OK} ]] ; then
                              log "III: END"
			 fi
                    else
                         log "III: no signature"
                    fi
	       else
	            log "only ${#BATCH} bytes in batch received"
	       fi
	  else
	       log "III: EMPTY"
	  fi
     else
          log "II: no signature"
     fi
else
     # database secrets not available
     exit 1
fi

# now check and download attachments
/usr/lib/cryptobone/safewebdrop/bin/safewebdropfilerequest
exit 0
