URI: 
       trestructure, add support for hooks - coffin - secure lan file storage on a device
  HTML git clone git://parazyd.org/coffin.git
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
   DIR README
   DIR LICENSE
       ---
   DIR commit e65bdd247750cddf2e3adc4ac796e74865995215
   DIR parent f512d6c5537da9637e00dc3c8840a6a9819fef0c
  HTML Author: parazyd <parazyd@dyne.org>
       Date:   Thu, 25 Feb 2016 17:28:26 +0100
       
       restructure, add support for hooks
       
       Diffstat:
         M bin/sacrist                         |      87 ++++++++++++++++++++++---------
       
       1 file changed, 61 insertions(+), 26 deletions(-)
       ---
   DIR diff --git a/bin/sacrist b/bin/sacrist
       t@@ -9,14 +9,18 @@ happenz=$2
        keyuuid=$(blkid $device | awk -F\" '{print $2}')
        
        # Vars
       -tmptombs="/tmp/tombs" # Info about opened tombs, holds keyuuid, keyhash and tombid
       -tmptombs2="/tmp/tombs2" # Temp tempfile, for updating $tmptombs
        graveyard="/home/graveyard" # Our graveyard, with all the tombs
       +tombs="$graveyard/tombs" # Info about opened tombs, holds keyuuid, keyhash and tombid
       +tmptombs="$graveyard/tmptombs" # Temp tempfile, for updating $tombs
        keymount="/media/tombkey" # Directory where keys get mounted
        coffindot="$keymount/.coffin" # .coffin directory on the usb key
        ttab="$coffindot/ttab" # Our ttab
       -createme="$coffindot/create.me" # New tomb creation trigger
       +hooks="$coffindot/hook"
        tomb="/usr/local/bin/tomb"
       +tombpasswd="$graveyard/passwd"
       +
       +# Hook syntax
       +# create:username:tombname:tombsize:passphrase
        
        # Debugs
        echo "Arg1: $1"
       t@@ -34,38 +38,43 @@ _ttabmagic() {
            # Loop entire ttab and do stuff for tombs that want to be opened
            _msg info "Doing ttab magic..."
            line=0
       +
            for entry in $(cat $ttab); do
                let line=$line+1 
                _msg info "Found line $line..."
       -        if [[ $(echo $entry | awk -F: '{print $4}') == "true" ]]; then
       +
       +        if [[ $(echo $entry | awk -F: '{print $3}') == "true" ]]; then
                    _msg info "Working on tomb from line $line..."
                    undertaker=$(echo $entry | awk -F: '{print $1}')
                    echo "Username: $undertaker"
                    tombid=$(echo $entry | awk -F: '{print $2}')
       -            echo "Tomb name: $tombid"
       +            echo "Tomb name: $tombid.tomb"
                    _comparekey
       +
                    if [[ $happenz == "close" ]]; then
                        _msg warn "Comparekey true"
                        sudo -u $undertaker $tomb slam $tombid    
       -                cp $tmptombs $tmptombs2
       -                grep -v $keyhash $tmptombs2 > $tmptombs; chmod 600 $tmptombs; _msg info "Updated $tmptombs"
       -                rm $tmptombs2
       +                cp $tombs $tmptombs
       +                grep -v $keyhash $tmptombs > $tombs && chmod 600 $tombs && _msg info "Updated $tombs"
       +                rm $tmptombs
                        continue
                    fi
       +
                    _msg warn "Comparekey false"
       -            keypass=$(echo $entry | awk -F: '{print $3}')
       +            keypass=$(cat $tombpasswd | grep $keyhash | awk -F: '{print $2}')
                    echo "Tomb passphrase: $keypass"
                    sudo -u $undertaker $tomb open $graveyard/$tombid.tomb -k $coffindot/$tombid.key \
       -                --unsafe --tomb-pwd $keypass # Deal with this shit somehow!
       +                --unsafe --tomb-pwd "$keypass" # Deal with this shit somehow!
        
                    if [[ -d "/media/$tombid" ]]; then
       -                echo "$undertaker:$keyhash:$keyuuid" >> $tmptombs; chmod 600 $tmptombs; _msg info "Added info to $tmptombs" 
       +                echo "$undertaker:$keyhash:$keyuuid" >> $tombs && chmod 600 $tombs && _msg info "Added info to $tombs" 
                    else
       -                _msg warn "Nothing added to $tmptombs"
       +                _msg warn "Nothing added to $tombs"
                    fi
                fi
       +
            done
       -    umount $keymount; rmdir $keymount; _msg info "Unmounted and deleted $keymount"
       +    umount $keymount && rmdir $keymount && _msg info "Unmounted and deleted $keymount"
        }
        
        _hashkey() {
       t@@ -74,19 +83,38 @@ _hashkey() {
        
        _comparekey() {
            keyhash=$(_hashkey)
       -    if [[ ( $(cat $tmptombs | grep $keyhash | grep $keyuuid) ) ]]; then
       +    if [[ ( $(cat $tombs | grep $keyhash | grep $keyuuid) ) ]]; then
                happenz=close
            else
                happenz=open
            fi
        }
        
       +_hooks() {
       +    for entry in $(cat $hooks); do
       +        let hook=$hook+1
       +        _msg info "Found hook $hook..."
       +
       +        # Check what's hook supposed to do
       +        if [[ $(echo $entry | awk -F: '{print $1}') == "create" ]]; then
       +            _create_new_tomb
       +        elif [[ $(echo $entry | awk -F: '{print $1}') == "delete" ]]; then
       +            #DELETE TOMB
       +        elif [[ $(echo $entry | awk -F: '{print $1}') == "foo" ]]; then
       +            # do foo
       +        else
       +            _msg error "No valid hook syntax on hook $hook"
       +        fi
       +        rm $hooks && _msg info "Removed $hooks"
       +    done
       +}
       +
        _create_new_tomb() {
            _msg info "Creating new tomb!"
       -    undertaker=$(cat $createme | awk -F: '{print $1}')
       -    tombid=$(cat $createme | awk -F: '{print $2}')
       -    keypass=$(cat $createme | awk -F: '{print $3}')
       -    tombsize=$(cat $createme | awk -F: '{print $4}')
       +    undertaker=$(echo $entry | awk -F: '{print $2}')
       +    tombid=$(echo $entry | awk -F: '{print $3}')
       +    tombsize=$(echo $entry| awk -F: '{print $4}')
       +    keypass=$(echo $entry | awk -F: '{print $5}')
        
            if ! [[ ( $(id $undertaker) ) ]]; then
                _msg warn "No user called $undertaker found. Creating..."
       t@@ -98,11 +126,14 @@ _create_new_tomb() {
            sudo -u $undertaker $tomb forge $graveyard/$tombid.key --unsafe --tomb-pwd "$keypass"
            sudo -u $undertaker $tomb lock $graveyard/$tombid.tomb -k $graveyard/$tombid.key \
                --unsafe --tomb-pwd "$keypass"
       +
            mv $graveyard/$tombid.key $coffindot/ && chown $undertaker:$undertaker $coffindot/$tombid.key && \
                _msg info "Moved and chowned keyfile"
       -    echo "$undertaker:$tombid:$keypass:true" >> $ttab 
       -    _msg info "Wrote to $ttab"
       -    rm $createme && _msg info "Removed $createme"
       +    echo "$undertaker:${tombid}:true" >> $ttab 
       +    keyhash=$(_hashkey)
       +    echo "${keyhash}:${keypass}" >> $tombpasswd
       +    _msg info "Wrote to $ttab and $tombpasswd"
       +    # rm $createme && _msg info "Removed $createme"
        }
        
        _endgame() {
       t@@ -120,25 +151,29 @@ _msg() {
        }
        # }}}
        
       -# Main
       +#---------------------------------------------------------------------------------------------------
       +#------------------------------------------------------ Main ---------------------------------------
       +#---------------------------------------------------------------------------------------------------
        
        if [[ $happenz == "CREATE" ]]; then
            _mountkey
            if [[ -d "$coffindot" ]]; then
                _msg info "$coffindot found..."
        
       -        if [[ -f "$createme" ]]; then
       -            _create_new_tomb
       +        if [[ -f "$hooks" ]]; then
       +            _hooks
       +        else
       +            _msg warn "No hooks detected"
                fi
                
                if ! [[ -f "$ttab" ]]; then
                    _msg error "No ttab!"
       -            umount $keymount; rmdir $keymount; _msg info "Unmounted and removed $keymount"
       +            umount $keymount && rmdir $keymount && _msg info "Unmounted and removed $keymount"
                else
                    _ttabmagic
                fi
            else
                _msg error "No valid .coffin directory! Exiting..."
       -        umount $keymount; rmdir $keymount; _msg info "Umounted and removed $keymount"
       +        umount $keymount && rmdir $keymount && _msg info "Umounted and removed $keymount"
            fi
        fi