#!/usr/bin/bash

##############################################################################
# This file is part of the CRYPTO BONE
# File     : cryptoboneshell
# Version  : 1.1.0 (cryptobone extern)
# License  : BSD
# Date     : Tuesday, 1 November 2016
# Contact  : Please send enquiries and bug-reports to opensource@senderek.ie
#
# Copyright (c) 2016-2017
#	Ralf Senderek, Ireland.  All rights reserved. (http://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.
##############################################################################


#-------------------------------------------------------------#
IFS='#'
while read RAW Rest
do

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

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

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

    #echo ${WORDS[@]}
    #echo "Result:A" ${WORDS[0]} "B" ${WORDS[1]} "C" ${WORDS[2]} "D" ${WORDS[3]}

    # 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
    
    # 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
