thelper GUI script for creating hooks - coffin - secure lan file storage on a device
 (HTM) git clone git://parazyd.org/coffin.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 7689b3271deae39655d3114d5264c47adb23bf1a
 (DIR) parent e65bdd247750cddf2e3adc4ac796e74865995215
 (HTM) Author: parazyd <parazyd@dyne.org>
       Date:   Wed,  2 Mar 2016 15:10:47 +0100
       
       helper GUI script for creating hooks
       
       Diffstat:
         A makehook.sh                         |     335 +++++++++++++++++++++++++++++++
       
       1 file changed, 335 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/makehook.sh b/makehook.sh
       t@@ -0,0 +1,335 @@
       +#!/usr/bin/env zsh
       +#
       +# Zenity helper script to make tombox hooks more user-friendly
       +#
       +# ~ parazyd
       +
       +typeset -H UNDERTAKER
       +typeset -H TOMBPASS
       +typeset -H TOMBSIZE
       +typeset -H TOMBNAME
       +typeset -H sudoassword
       +
       +keymount="/media/tombkey"
       +coffindot="$keymount/.coffin"
       +hooks="$coffindot/hook"
       +
       +lsblk -npl | awk -F" " '{print $1}' | grep ^/dev/sd.. > ./.devs
       +
       +# Hook syntax
       +# create:username:tombname:tombsize:passphrase
       +
       +# {{{ monmort icon
       +MONMORT="/tmp/monmort.png"
       +ICONB64="iVBORw0KGgoAAAANSUhEUgAAACAAAAAgEAQAAACJ4248AAAAAmJLR0T//xSrMc0AAAAJcEhZcwAA\n
       +AEgAAABIAEbJaz4AAAAJdnBBZwAAACAAAAAgAIf6nJ0AAADrSURBVFjD5ZZBEsMgCEU/TO/l2XMx\n
       +04VjQ1K1CDYswkwWJnH+E/EL4RP7jluDCACoim/bvfIpFQiKEJcQHCN9xEtLCgDMQM7f33sZrPNG\n
       +/05loCXujfAtCAVgNgLwIuycjQAra8G9Fm823ADabPRA1QDelfZAVUZktWrNvL8ew5YTnsStx3Am\n
       +AyOInJVbYF1prZuU+tsR1g9UMDqGuo5oFWhtSEQNEGmeVrqv73Tj0pIZirANMYqRhyw5Bb9MauSW\n
       +SwA8l9OzG5LnAsiiDQGQRRvaEwInK54J390hndAIYIeQ4k6AAjE/h06ab0SjP08MA1xDAAAAJXRF\n
       +WHRkYXRlOmNyZWF0ZQAyMDExLTAxLTEyVDA5OjM0OjI0KzAxOjAwo//d7wAAACV0RVh0ZGF0ZTpt\n
       +b2RpZnkAMjAxMS0wMS0xMlQwOTozNDoyNCswMTowMNKiZVMAAAAASUVORK5CYII="
       +echo -e "$ICONB64" | base64 --decode > $MONMORT
       +# }}}
       +# {{{ sudo functions
       +function _sudo {
       +    sudoassword=$(ask_password "Insert sudo password for user $USER")
       +    echo -e "$sudoassword\n" | sudo -S -v
       +    _sudowrong
       +}
       +function _sudowrong {
       +    if [[ $? == 1 ]]; then
       +        sudoassword=$(ask_password "Wrong password. Insert sudo password for user $USER")
       +        echo -e "$sudoassword\n" | sudo -S -v
       +        _sudowrong
       +    fi
       +}
       +# }}}
       +
       +# {{{ Some pinentry code shamelessly stolen from tomb
       +# Ask user for a password
       +# Wraps around the pinentry command, from the GnuPG project, as it
       +# provides better security and conveniently use the right toolkit.
       +ask_password() {
       +    local description="$1"
       +    local title="${2:-Enter tomb password.}"
       +    local output
       +    local password
       +    local gtkrc
       +    local theme
       +
       +    # Distributions have broken wrappers for pinentry: they do
       +    # implement fallback, but they disrupt the output somehow.  We are
       +    # better off relying on less intermediaries, so we implement our
       +    # own fallback mechanisms. Pinentry supported: curses, gtk-2, qt4
       +    # and x11.
       +
       +    # make sure LANG is set, default to C
       +    LANG=${LANG:-C}
       +
       +
       +    if [[ "$DISPLAY" = "" ]]; then
       +
       +        if _is_found "pinentry-curses"; then
       +            output=`cat <<EOF | pinentry-curses
       +OPTION ttyname=$TTY
       +OPTION lc-ctype=$LANG
       +SETTITLE $title
       +SETDESC $description
       +SETPROMPT Password:
       +GETPIN
       +EOF`
       +        fi
       +
       +    else # a DISPLAY is found to be active
       +
       +        # customized gtk2 dialog with a skull (if extras are installed)
       +        if _is_found "pinentry-gtk-2"; then
       +
       +            gtkrc=""
       +            theme=/share/themes/tomb/gtk-2.0-key/gtkrc
       +            for i in /usr/local /usr; do
       +                [[ -r $i/$theme ]] && {
       +                    gtkrc="$i/$theme"
       +                    break
       +                }
       +            done
       +            [[ "$gtkrc" = "" ]] || {
       +                gtkrc_old="$GTK2_RC_FILES"
       +                export GTK2_RC_FILES="$gtkrc"
       +            }
       +            output=`cat <<EOF | pinentry-gtk-2
       +OPTION ttyname=$TTY
       +OPTION lc-ctype=$LANG
       +SETTITLE $title
       +SETDESC $description
       +SETPROMPT Password:
       +GETPIN
       +EOF`
       +            [[ "$gtkrc" = "" ]] || export GTK2_RC_FILES="$gtkrc_old"
       +
       +            # TODO QT4 customization of dialog
       +        elif _is_found "pinentry-qt4"; then
       +
       +            # TODO X11 customization of dialog
       +        elif _is_found "pinentry-x11"; then
       +
       +            output=`cat <<EOF | pinentry-x11
       +OPTION ttyname=$TTY
       +OPTION lc-ctype=$LANG
       +SETTITLE $title
       +SETDESC $description
       +SETPROMPT Password:
       +GETPIN
       +EOF`
       +
       +        else
       +
       +            if _is_found "pinentry-curses"; then
       +
       +                output=`cat <<EOF | pinentry-curses
       +OPTION ttyname=$TTY
       +OPTION lc-ctype=$LANG
       +SETTITLE $title
       +SETDESC $description
       +SETPROMPT Password:
       +GETPIN
       +EOF`
       +            else
       +            fi
       +
       +        fi
       +
       +    fi # end of DISPLAY block
       +
       +    # parse the pinentry output
       +    for i in ${(f)output}; do
       +        [[ "$i" =~ "^ERR.*" ]] && {
       +            print "canceled"
       +            return 1 }
       +
       +        # here the password is found
       +        [[ "$i" =~ "^D .*" ]] && password="${i##D }"
       +    done
       +
       +    [[ "$password" = "" ]] && {
       +        print "empty"
       +        return 1 }
       +
       +    print "$password"
       +    return 0
       +}
       +
       +_is_found() {
       +    # returns 0 if binary is found in path
       +    [[ "$1" = "" ]] && return 1
       +    command -v "$1" 1>/dev/null 2>/dev/null
       +    return $?
       +}
       +# }}}
       +
       +# {{{ _clean - Clean function, removes sensitive stuff from memory
       +function _clean {
       +    sudo umount $keymount && sudo rmdir $keymount
       +    unset $?
       +    local rr="$RANDOM"
       +    while [[ ${#rr} -lt 500 ]]; do
       +        rr+="$RANDOM"
       +    done
       +
       +    command="$rr";      unset command
       +    UNDERTAKER="$rr";     unset UNDERTAKER
       +    TOMBPASS="$rr";     unset TOMBPASS
       +    TOMBSIZE="$rr";      unset TOMBSIZE
       +    sudoassword="$rr";  unset sudoassword
       +    tombtmp="$rr";      unset tombtmp
       +    TOMBNAME="$rr";       unset TOMBNAME
       +}
       +# }}}
       +
       +function _mountkey {
       +    if ! [[ -d $keymount ]]; then
       +        sudo mkdir -p $keymount
       +    fi
       +    
       +    sudo mount $USBKEY $keymount
       +
       +    if ! [[ -d $coffindot ]]; then
       +        sudo mkdir $coffindot
       +    fi
       +    
       +}
       +
       +# {{{ zenity dialogs
       +function _zenques {
       +     zenity \
       +        --window-icon="$MONMORT" \
       +        --question \
       +        --text="$1"
       +}
       +function _zenwarn {
       +    zenity \
       +        --window-icon="$MONMORT" \
       +        --warning \
       +        --title="$1" \
       +        --text="$2"
       +}
       +function _zeninfo {
       +    zenity \
       +        --window-icon="$MONMORT" \
       +        --info \
       +        --title="$1" \
       +        --text="$2"
       +}
       +function _zenerr {
       +    zenity \
       +        --window-icon="$MONMORT" \
       +        --error \
       +        --title="$1" \
       +        --text="$2"
       +}
       +function _zenentry {
       +    zenity \
       +        --window-icon="$MONMORT" \
       +        --entry \
       +        --title="$1" \
       +        --text="$2" \
       +        --entry-text="$3"
       +}
       +# }}}
       +
       +function _main {
       +    _clean
       +    command=$(zenity \
       +        --window-icon="$MONMORT" \
       +        --title="tombox hook helper" \
       +        --list \
       +        --hide-header \
       +        --text="tombox hook helper" \
       +        --separator=" & " \
       +        --column=Function \
       +        --column=Description \
       +        "create" "a new tomb on the tombox" \
       +        "delete" "a tomb from the tombox" \
       +        "backup" "a tomb from the tombox" \
       +        "foobar" "on the tombox")
       +    eval "_$command"
       +}
       +
       +function _create {
       +    UNDERTAKER=$(_zenentry "Choose username" "Choose a username for your tombox" "foobar")    
       +    res=$?
       +
       +    case $res in
       +        0)
       +            TOMBNAME=$(_zenentry "Choose tomb name" "Choose a name for your new tomb" "foobar")
       +            res=$?
       +
       +            case $res in
       +                0)
       +                    TOMBPASS=$(_zenentry "Choose password" "Choose a password for your tomb's keyfile" "9898yvc0982yh08H@*Y@(Y*C")
       +                    res=$?
       +
       +                    case $res in
       +                        0)
       +                            TOMBSIZE=$(_zenentry "Choose tomb size" "Choose the size of your tomb in MiB" "100")
       +                            res=$?
       +
       +                            case $res in 
       +                                0)
       +                                    USBKEY=$(zenity \
       +                                        --window-icon="$MONMORT" \
       +                                        --title="Choose USB key" \
       +                                        --list \
       +                                        --hide-header \
       +                                        --text="Choose your USB key to use" \
       +                                        --column=Device \
       +                                        $(cat ./.devs)
       +                                    )
       +                                    _sudo
       +                                    _mountkey
       +                                    if [[ -f $hooks ]]; then
       +                                        _zenques "Warning! There is already an existing hook. Do you want to overwrite?"
       +                                        res=$?
       +                                        case $res in
       +                                            0)
       +                                                print "create:${UNDERTAKER}:${TOMBNAME}:${TOMBSIZE}:${TOMBPASS}" | sudo tee $hooks
       +                                                _zeninfo "Success" "$hooks written successfully!"
       +                                                _clean
       +                                                _main
       +                                                ;;
       +                                            1)
       +                                                _zeninfo "Info" "Postponing..."
       +                                                _clean
       +                                                _main
       +                                                ;;
       +                                        esac
       +                                    else
       +                                         print "create:${UNDERTAKER}:${TOMBNAME}:${TOMBSIZE}:${TOMBPASS}" | sudo tee $hooks
       +                                        _zeninfo "Success" "$hooks written successfully!"
       +                                        _clean
       +                                        _main
       +                                    fi
       +                                    ;;
       +                                1) 
       +                                    _main
       +                                    ;;
       +                            esac
       +                            ;;
       +                        1)
       +                            _main
       +                            ;;
       +                    esac
       +                    ;;
       +                1)
       +                    _main
       +                    ;;
       +            esac
       +            ;;
       +        1)
       +            _main
       +            ;;
       +    esac
       +}
       +
       +_main