#!/usr/bin/bash

##############################################################################
# This file is part of the CRYPTO BONE
# File     : cryptoboneshell
# Version  : 2.0 (EXTERNAL DEVICE)
# License  : BSD-3-Clause
# Date     : 26 May 2025
# Contact  : Please send enquiries and bug-reports to opensource@senderek.ie
#
# Copyright (c) 2016-2025
#	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.
##############################################################################

# This script processes everything that comes in over the dedicated ssh tunnel
# which has been established using the ssh private key (cbb) generated by the 
# external device.
# The input to this script is being split into "words" separated by a space byte.

# The first word in the input must match the client's local.key, which is verified
# by checking the sha256 fingerprint that is stored in the external device.
# Nothing is done if the verification fails.

# If the command word is "ATTACH", the following line is treated as a base64 encoded
# attachment file and is stored for encryption and delivery with a webdrop message.
# All other commands are sent to the cornerstone script cbcontrol to be executed on
# the external device.

IFS='#'
while read RAW Rest
do

    #sanitize input
    LINE=$(echo $RAW | tr -d ';<>&$"(){}[]~*?|')
    if [ x$LINE = "xEXIT" ]
    then
	 echo "Bye for now."
         exit 0
    fi

    # split the line and fork to the functions
    IFS=' '
    
    typeset -i count=0

    for X in $(echo $LINE)
    do
	 WORDS[count]=$X
	 count=$count+1
    done

    # check if WORDS[0] works as the password, compare with sha2 hashvalue
    # if not valid, exit 1 "failed"

    # in bash, there must not be a \n after ${WORDS[0]} !!! in ksh it's necessary
    STOREDHASH=$(cat /usr/lib/cryptobone/ext/EXTERN.local.hash)
    EXTERNHASH=$(echo -n ${WORDS[0]} | /usr/bin/sha256sum | /usr/bin/cut -c-64)
    if [[ ${STOREDHASH} != ${EXTERNHASH} ]]
    then 
         echo "failed: local authentication"
         exit 2
    fi
    
    # write an attachment to file system
    if [[ x${WORDS[1]} = "xATTACH"  ]]; then
          echo -n "${WORDS[2]}" | /usr/bin/base64 -d > /usr/lib/cryptobone/ext/cryptobone/attachment
    fi
   
    # call /usr/lib/cryptobone/ext/cbcontrol as root
    echo "${WORDS[0]}" | sudo -Sk /usr/lib/cryptobone/ext/cbcontrol "${WORDS[@]}" 2> /dev/null

    exit 0
done
