#!/bin/bash # # Common functions. # # Setup main configuration and load preferences function keyringer_config_load { if [ -f "$HOME/.$NAME" ]; then echo "Converting legacy configuration scheme..." mv $HOME/.$NAME $HOME/.$NAME.tmp mkdir $HOME/.$NAME mv $HOME/.$NAME.tmp $CONFIG fi if [ ! -e "$CONFIG" ]; then echo "Creating $CONFIG..." mkdir -p `dirname $CONFIG` touch $CONFIG chmod 600 $CONFIG echo "# Keyringer config file." > $CONFIG echo "" >> $CONFIG fi keyringer_config_load_preferences } # Load config preferences function keyringer_config_load_preferences { # Load custom keyring preferences if [ ! -z "$PREFERENCES" ] && [ -f "$PREFERENCES" ]; then source $PREFERENCES fi } # Load a parameter from config function keyringer_config { if [ -z "$CONFIG" ]; then echo "Your have to set CONFIG variable in the code" exit 1 elif [ -e "$CONFIG" ]; then grep -e "^$1=" $CONFIG | tail -n 1 | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | sed -e 's/ *#.*$//' else echo "Config file not found: $CONFIG" exit 1 fi } # Return the list of recipients function keyringer_recipients { grep -v '^#' $1 | grep -v '^$' | awk '{ print "-r " $2 }' | xargs } # Check if keyringer has a given action function keyringer_has_action { if [ -z "$ACTIONS" ]; then echo "Your have to set ACTIONS variable in the code" exit 1 fi if [ -e "$ACTIONS/$1" ]; then true else false fi } # Execute an action function keyringer_exec { # Setup action="$1" basedir="$2" shift 2 # Dispatch if keyringer_has_action $action; then $ACTIONS/$action $basedir $* fi } # Return a filename with correct extension function keyringer_filename { if [ -z "$1" ]; then return else echo `dirname $1`/`basename $1 .asc`.asc fi } # Check if a folder is inside a git repository function keyringer_is_git { if [ -z "$1" ]; then false elif [ ! -d "$1" ]; then false elif [ -d "$1/.git" ]; then true else cwd="`pwd`" cd $1 && git="`git status &> /dev/null`" && cd $cwd if [ "$git" != "128" ]; then true else false fi fi } # Setup a temporary file function keyringer_set_tmpfile { if [ -z "$BASEDIR" ]; then echo "Please set BASEDIR before creating a tmp file" exit 1 fi if [ -z "$1" ]; then template="$BASEDIR/tmp/keyringer.XXXXXXXXXX" else template="$BASEDIR/tmp/$1.XXXXXXXXXX" fi mkdir -p $BASEDIR/tmp keyringer_git_ignore 'tmp/*' if [ "$2" == "-d" ]; then TMPWORK="`mktemp -d $template`" else TMPWORK="`mktemp $template`" fi if [ "$?" != "0" ]; then echo "Error: can't set TMPWORK $TMPWORK" exit 1 fi trap "keyringer_unset_tmpfile $TMPWORK; exit" INT TERM EXIT } # Remove a temporary file function keyringer_unset_tmpfile { if [ -z "$1" ]; then echo "No tmp file set" fi rm -f $1 if [ "$?" != "0" ]; then echo "Warning: could not delete file $1. Please delete it manually as it might have sensitive information." exit 1 fi } # Add a pattern into gitignore function keyringer_git_ignore { if [ ! -z "$BASEDIR/.gitignore" ]; then echo $1 > $BASEDIR/.gitignore keyringer_exec git $BASEDIR add .gitignore else if ! grep -q -e "^$1$" $BASEDIR/.gitignore; then echo $1 >> $BASEDIR/.gitignore fi fi } # Set needed environment variables and do basic checks. function keyringer_set_env { if [ -z "$1" ]; then echo "Error: missing arguments for keyringer_set_env" exit 1 fi ACTIONS="`dirname $0`" BASENAME="`basename $0`" BASEDIR="$1" KEYDIR="$BASEDIR/keys" RECIPIENTS="$BASEDIR/config/recipients" OPTIONS="$BASEDIR/config/options" if [ -z "$BASEDIR" ]; then keyringer_action_usage exit 1 fi if [ ! -f "$RECIPIENTS" ]; then echo "No recipient config was found" exit 1 fi if [ -z "$EDITOR" ]; then if type sensible-editor > /dev/null 2>&1 ; then EDITOR=sensible-editor elif type editor > /dev/null 2>&1 ; then EDITOR=editor else echo "You have to set EDITOR env variable" exit 1 fi fi if [ ! -f "$OPTIONS" ]; then echo "No option config was found" exit 1 fi # Ensure that keydir exists mkdir -p $KEYDIR && chmod 700 $KEYDIR } # Get a file argument function keyringer_get_file { FILE="`keyringer_filename $1`" if [ -z "$FILE" ]; then keyringer_action_usage exit 1 elif [ ! -f "$KEYDIR/$FILE" ]; then echo "File not found: $KEYDIR/$FILE" exit 1 fi } # Get a new file argument function keyringer_get_new_file { FILE="`keyringer_filename $1`" if [ -z "$FILE" ]; then keyringer_action_usage exit 1 fi } # Get a command argument function keyringer_get_command { # Aditional parameters COMMAND="$1" if [ -z "$COMMAND" ]; then keyringer_action_usage command exit 1 fi } # Run the action usage function keyringer_action_usage { if [ "`type -t "keyringer_usage_$BASENAME"`" == "function" ]; then # Use custom action usage keyringer_usage_$BASENAME else # Default usage if [ "$1" == "command" ]; then echo "Usage: keyringer $BASENAME [arguments]" else echo "Usage: keyringer $BASENAME " fi fi } # Setup environment if [ "`basename $0`" != "keyringer" ]; then keyringer_set_env $* fi