#!/bin/bash # generate filename without directory: FILENAME=$(basename $0) CAT=$(which cat) print_help() { ########################################################################## $CAT < description: This script helps to manage the process of handling the network of GnuPG-signatures. The switches mean: sign: a key is fetched, signed and sent so the owner. accept: after verification of the emailaddress, the key is re-imported to the local keyring. import: someone sent me my signed key, import/add it to my local keyring. Please email me suggestions for improvements and errors! I am still learning how to write good shell-scripts and use my GnuPG *g* EOF ########################################################################## } ## 2do: ## * ## * ######################################################## ######################################################## ######################################################## ## ## ## PERSONALISATION ## ## ## ######################################################## ######################################################## ######################################################## # filenames to use for ... # change to "done", if you changed all options above to _your_ needs! CONFIGURED_="notdone" ######################################################## # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # --- normally you DON'T have to change anything below this line! --- # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # ######################################################## ######################################################## ######################################################## ## ## ## hardcoded configuration ## ## ## ######################################################## ######################################################## ######################################################## ## generates a timestamp like "2002_Jan_31_-_09h43m03s" TIMESTAMP1=`/bin/date +%Y_%b_%d_-_%Hh%Mm%Ss` ## generates a syslog-like timestamp like "Mar 16 09:19:50" ## lisa qmail: TIMESTAMP2=`/bin/date +"%b %d %H:%M:%S"` # change to "on" if you want to read the debug-messages (can be much) to trace # down problems: export DEBUG="off" # if "on" then various steps that are require for beginners are skipped. # if "off" then everything is asked. export ADVANCED="off" # same as ADVANCED but with less questions and a fixed way of navigation: # fetches key, gosub --edit-key, mail via mutt, DON'T delete from local # keyring but deletes temporary key-file from current directory: export VERYLAZY="off" ######################################################## ######################################################## ######################################################## ## ## ## F U N C T I O N S ## ## ## ######################################################## ######################################################## ######################################################## ## --------------------------------------------------------- myexit() { doreport debug "function myexit($1) called" [ "$1" -lt 1 ] && echo "$FILENAME done." [ "$1" -gt 0 ] && echo "$FILENAME aborted with errorcode $1." #optionally [ "$1" -gt 0 ] && do_sound_error exit $1 } ## --------------------------------------------------------- ## check, if some files needed are not found testiffound() { doreport debug "function testiffound($1) called" if [ -z "$2" ]; then doreport debug "The tool \"$1\" is missing because \"$2\" is empty." doreport notify "The tool \"$1\" could not be located (missing?)" export SOMETHINGMISSING="yes" fi } ## --------------------------------------------------------- report() { ## reports the parameter to the stdout echo echo "===============================================================" echo " $FILENAME" echo "$1" echo echo "===============================================================" echo } ## --------------------------------------------------------- debugthis() { ## debugs the script #echo $FILENAME: DEBUG: $1 echo "do nothing" >/dev/null } ## --------------------------------------------------------- logthis() { ## logs some text with a timestamp added ## usage: logthis "mytext" ## generates a timestamp like "2002_Jan_31_-_09h43m03s" TIMESTAMPLONG=`/bin/date +%Y_%b_%d_-_%Hh%Mm%Ss` ## generates a syslog-like timestamp like "Mar 16 09:19:50" TIMESTAMPSHORT=`/bin/date +"%b %d %H:%M:%S"` ## add computername and scriptname like "lisa qmail: " LOGTIMESTAMP=$TIMESTAMPSHORT" lisa $FILENAME:" # probably won't annoy logfiles with this crap? # echo $LOGTIMESTAMP "$1" >> $mylogfile # echo $LOGTIMESTAMP "$1" echo "do nothing" >/dev/null } ## --------------------------------------------------------- mailthis() { # usage: $1=To $2=Subject $3=Body echo -e "$3" | mail -s "$2" $1 } ## --------------------------------------------------------- doreport_internal_writestring() { ## !! for use withing function "doreport" only !! ## prints out all strings on stdout ## 2do: error-msg written with { echo 1>&2 "text" } (stderr) echo echo "===$1============================================" echo " $FILENAME" ## FIXXME: ## 2DO: loop instead of this quick-hack!!! if [ ! -z "$2" ]; then echo "$2" fi if [ ! -z "$3" ]; then echo "$3" fi if [ ! -z "$4" ]; then echo "$4" fi if [ ! -z "$5" ]; then echo "$5" fi if [ ! -z "$6" ]; then echo "$6" fi if [ ! -z "$7" ]; then echo "$7" fi if [ ! -z "$8" ]; then echo "$8" fi echo echo "==============================================================="; echo } ## --------------------------------------------------------- doreport() { ## reports the parameter to the stdout ## usage: (shortnote|notify|error|debug) string1 [string2] [string3] [...] [string7] ## NEEDS: doreport_internal_writestring case "$1" in "shortnote") echo "$FILENAME: $2 $3 $4 $5 $6 $7";; "notify") doreport_internal_writestring " notification ==" "$2" "$3" "$4" "$5" "$6" "$7";; "error") doreport_internal_writestring " ERROR =========" "$2" "$3" "$4" "$5" "$6" "$7";; "debug") ## debugs the script if [[ "$DEBUG" = "on" ]]; then echo "$TIMESTAMP $FILENAME: DEBUG: $2 $3 $4 $5 $6 $7"; fi; echo "do nothing" >/dev/null;; "log") ## logs some text with a timestamp added ## usage: $2 == "mytext" ## generates a timestamp like "2002_Jan_31_-_09h43m03s" TEMP_TIMESTAMPLONG=`/bin/date +%Y_%b_%d_-_%Hh%Mm%Ss`; ## generates a syslog-like timestamp like "Mar 16 09:19:50" TEMP_TIMESTAMPSHORT=`/bin/date +"%b %d %H:%M:%S"`; ## add computername and scriptname like "lisa qmail: " TEMP_LOGTIMESTAMP=$TEMP_TIMESTAMPSHORT" lisa $FILENAME:"; # probably won't annoy logfiles with this crap? # echo $LOGTIMESTAMP "$2" >> $mylogfile; # echo $LOGTIMESTAMP "$2" ;; echo "do nothing" >/dev/null;; "mail") # usage: $2=To $3=Subject $4=Body echo -e "$4" | mail -s "$3" $2;; *) doreport_internal_writestring " INTERNAL ERROR " "An error occured, while calling function \"doreport\":" "The parameter that was given ($1) has no target/handle." "Aborting."; myexit 1;; esac } ## --------------------------------------------------------- ask() { ## asks something (parameter 1) the user echo echo "=== question ==================================================" echo " $FILENAME" echo "$1" echo echo "===============================================================" echo ## example: ## ask "Do you want this?" ## example: ## read ANSWER ## example: ## echo ## example: ## case "$ANSWER" in ## example: ## ## example: ## y) ## example: ## CDCPY_docover ;; ## example: ## *) ## example: ## echo " OK, maybe you're right." ; ## example: ## echo " We don't need these fancy stuff anyway ..." ; ## example: ## echo ;; ## example: ## ## example: ## esac } ## --------------------------------------------------------- ## checks, if the directory given exists and tries to create it, if necessary ## returns: 0 = "exists"/"created" or 1 = "error" makesure_directory_exists() { doreport debug "function makesure_directory_exists($1) called" if [ ! -d "$1" ]; then doreport debug "Directory \"$1\" not found! So I try to make it now..." mkdir "$1" if [ $? -eq 0 ] # Test exit status then doreport debug "Directory $1 successfully created!" return 0 else doreport error "Could NOT create directory $1!" "Most likely: no write-permission OR non existing parent-directory." return 1 fi else doreport debug "Directory already found" return 0 fi ## EXAMPLE: ## makesure_directory_exists "$(dirname $FILENAME)" ## ## check, if given dir is really a directory: ## if [ -d "$DIRNAME" ]; then ## doreport debug "Directory $DIRNAME is checked as a valid directory" ## else ## print_help ## doreport "error" "Directory $DIRNAME is not an existing directory! (see error-output above for possible reason!)" "aborting." ## exit 1 ## fi ## unsure, because returns also exits current region! ## if [[ $(makesure_directory_exists "$(dirname $A_file)") ]]; then ## doreport error "The directory for the tempfile could not be found or generated!" ## myexit 1 ## else ## doreport debug "Directory exists." ## fi } ## --------------------------------------------------------- ## checks if file exists; if it does not exist, it will be created makeSureTheFileExists() { doreport debug "function makeSureTheFileExists($1) called" ## wether "touch" exists, is to be checked in preconditions! [ -e "$1" ] || `which touch` "$1" } ## --------------------------------------------------------- handle_sign() { if [ "x$ADVANCED" = "xoff" ]; then ################################################################### ### Step 1 ################################################################### doreport "notify" "Step 1/14:" "Check the authentity of the key-fingerprint" $CAT < fpr EOT if [ "x$ADVANCED" = "xoff" ]; then $CAT < uid EOT if [ "x$ADVANCED" = "xoff" ]; then echo " Select the UID of $EMAILADDRESS" fi $CAT < sign EOT if [ "x$ADVANCED" = "xoff" ]; then $CAT < trust EOT if [ "x$ADVANCED" = "xoff" ]; then $CAT < save EOT if [ "x$ADVANCED" = "xoff" ]; then $CAT < \"$KEYID.gpg.asc\"\"" $CAT < "$KEYID.gpg.asc"; doreport "notify" "finished external execution";; *) echo " (you skipped that step)" ; echo ;; esac ################################################################### ### Step 11 ################################################################### doreport "notify" "Step 11/14:" "Send the signed key to the owner of $EMAILADDRESS" $CAT </dev/null } ######################################################## ######################################################## ######################################################## ## ## ## S C R I P T ## ## ## ######################################################## ######################################################## ######################################################## ## test for important stuff: precondition_asserts "$1" "$2" "$3" "$4" "$4" "$5" "$6" "$7" "$8" "$9" doreport "debug" SWITCH=$SWITCH doreport "debug" EMAILADDRESS=$EMAILADDRESS doreport "debug" KEYID=$KEYID ## parse parameter SWITCH case "$SWITCH" in "help"|"--help"|"?"|"/?") doreport debug "Parameter help|--help|?|/? detected" print_help; myexit 0;; "s"|"sign"|"SIGN"|"Sign") handle_sign;; "a"|"accept"|"ACCEPT"|"Accept") handle_accept;; "i"|"import"|"IMPORT"|"Import") handle_import;; esac doreport "debug" "returned from handle-subprocedures" doreport "shortnote" "Thank you for using $FILENAME! Have a nice day." ######################################################## ## test for important stuff: postcondition_asserts myexit 0 ######################################################## ## E N D ## ########################################################