#!/usr/bin/bash

#***************************************************************************
# This file is part of the CRYPTO BONE
# File     : safewebdropfilerequest (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

REQ=${RAM}/filerequest
FETCH="${RAM}/allfiles"
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 "---files---"

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

     # PHASE 1: send NEW request
     CHALLENGE=$(curl https://${HOST}/cgi-bin/safewebdropgetfiles?${ID}\&NEW 2> /dev/null)
     if (( ${#CHALLENGE} != 64 )) ; then
          log "I: no challenge EXIT"
	  exit 2
     fi
     log "I-files:  ${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 "ALL" >> $REQ

     # generate a hash of the request
     generate_requesthash

     # and send it
     if (( ${#SIG} > 10 )) ; then
          sleep 1
	  REQUEST=$(/usr/bin/cat ${REQ})
          BATCH=$(/usr/bin/curl https://${HOST}/cgi-bin/safewebdropgetfiles?${ID}\&${REQUEST}\&${SIG} 2> /dev/null)
	  log "II-files: $ID  ${#BATCH} bytes received " 
          if [[ ${BATCH} ]] && [[ ${BATCH} != "NOFILES" ]]  ; 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
                         /usr/bin/sleep 1
			 REQUEST=$(/usr/bin/cat ${REQ})
                         # phase 3 : send the hash back to the server, so files can be deleted there
                         OK=$(/usr/bin/curl https://${HOST}/cgi-bin/safewebdropgetfiles?${ID}\&${REQUEST}\&${SIG} 2> /dev/null)
	                 log "deleted: ${OK}"
			 if [[ ${OK} = "OK" ]] ; then
                              log "III-files: END"
			 fi
                    else
                         log "III-files: no signature"
                    fi
	       fi
	  else
	       log "III: EMPTY"
	  fi
     else
          log "II-files: no signature"
     fi

     # split the file batch into files in ${RAM}/FILES
     INFILE="${RAM}/allfiles"
     if [[ -r ${INFILE} ]] ; then
          log "splitting ${INFILE}"
          # remove \r from INFILE
          /usr/bin/cat ${INFILE} | /usr/bin/tr -d '\r' > ${INFILE}.new
          /usr/bin/mv ${INFILE}.new ${INFILE}
          

	  FILENAME=${RAM}/FILES/newattachment.asc
          while read LINE
          do
               if (( ${#LINE} == 1 )) && [[ ${LINE} = "." ]]
               then
		    LASTLINE=$(/usr/bin/tail -1 ${FILENAME})
		    ATTACHMENTNAME=${LASTLINE##*:}
		    NUMBER=${LASTLINE%%:*}
		    ATTACHMENT=${ATTACHMENTNAME}.asc
		    if [[ ${ATTACHMENT} ]]; then
		         /usr/bin/mv ${FILENAME} ${RAM}/FILES/${ATTACHMENT}
			 log " ${ATTACHMENT} moved"
			 log "$(/usr/bin/ls -l ${ATTACHMENT})"
		    fi
               else
                    /usr/bin/echo  ${LINE} >> ${FILENAME}
               fi
          done < ${INFILE}
	  /usr/bin/mv  ${INFILE} "${INFILE}-$(/usr/bin/date +%y.%m.%d-%H.%M)" 2> /dev/null
     fi
else
     log "not enough parameter"
     exit 1
fi
exit 0
