#!/bin/bash IDM_MOD_DEPS="id gpg git" IDM_MOD_TAGS="id tool" IDM_MOD_PROG="safe yadm" IDM_MOD_PREF="core id" ## Tomb functions ########################################## # Install yadm # git clone https://github.com/TheLocehiliosan/yadm.git ~/.usr/opt/yadm # # This allow to secure your things .... idm_vars_git_tomb () { git_tomb_work_tree=$HOME git_tomb_dir=$IDM_DIR_CACHE/git/$id/tomb.git git_tomb_config=${IDM_CONFIG_DIR}/git/$id/tomb_gitconfig git_tomb_enc=$IDM_CONFIG_DIR/enc/$id.tomb } ## Front functions ############################## idm_tomb__help () { local id=$1 idm_vars_git_tomb echo "tomb" echo " workflow:" printf " %-20s: %s\n" "tomb ls" "Show tomb status" printf " %-20s: %s\n" "tomb import [] " "Import a config" printf " %-20s: %s\n" "tomb decrypt" "Decrypt the tomb" printf " %-20s: %s\n" "tomb sync" "Synchronise tomb(s)" printf " %-20s: %s\n" "tomb encrypt" "Save the current configuration into the tomb" printf " %-20s: %s\n" "tomb push |all" "Save the current configuration into the tomb" printf " %-20s: %s\n" "tomb leave" "Remove all traces of your passage" echo " config:" printf " %-20s: %s\n" "git_tomb_enc" "$git_tomb_enc" printf " %-20s: %s\n" "git_tomb_dir" "$git_tomb_dir" printf " %-20s: %s\n" "git_tomb_config" "$git_tomb_config" return 0 } idm_tomb__ls () { local id=$1 idm_vars_git_tomb local g_st= local t_st= local d_c= local d_m= local date_today=$(date '+%s') echo " Tombs:" find $IDM_CONFIG_DIR/enc/ -type f -name '*.tomb' -printf "%f (%Tc)\n" | sed -e 's/^/ /' idm_tomb_require_enabled $id || return 0 # Calculate data if [ -d "$git_tomb_dir" ]; then g_st=open g_m=$( lib_date_diff_human $(find $git_tomb_dir -maxdepth 0 -printf "%Ts") ) g_m=" $d_m" else g_st=closed g_m= fi if [ -f "$git_tomb_enc" ]; then t_st=present t_m=$( lib_date_diff_human $(find $git_tomb_enc -printf "%Ts") ) t_m=", $t_m old" else t_st=absent t_m= fi echo " Info:" printf " %-20s: %s\n" "encrypted tomb" "$t_st${t_m}" printf " %-20s: %s\n" "encrypted file" "$git_tomb_enc" printf " %-20s: %s\n" "tomb git status" "$g_st$g_m" printf " %-20s: %s\n" "tomb git dir" "$git_tomb_dir" if lib_git_is_repo $git_tomb_dir $git_tomb_work_tree; then echo " Git remotes:" _git_tomb remote -v | sed 's/^/ /' fi } # This leave everything open at this stage !!! idm_tomb__sync () { local id=$1 local repo_name=${2:-tomb} # Sanity check: id and local repo idm_tomb_require_enabled $id idm_tomb_require_valid_local_repo # Tomb repo check #set -x if ! lib_git_is_repo $git_tomb_dir $git_tomb_work_tree; then if [ -f "$git_tomb_enc" ]; then lib_log WARN "An encrypted tomb has been found. Do you want to decrypt it?" idm_cli_timeout 1 || idm_exit 1 ERR "Refuse to create a tomb duplicate" idm_tomb__decrypt $id || idm_exit 1 ERR "Failed to create tomb repo" elif [ ! -d "$git_tomb_dir" ]; then idm_tomb__init $id || idm_exit 1 ERR "Tomb cannot be used without git" lib_log NOTICE "A tomb has been created" return 0 else idm_exit 1 ERR "Unknow error" fi fi # Work on local _git_tomb remote show $repo_name &>/dev/null || _git_tomb remote add $repo_name $git_tomb_dir || idm_exit 1 ERR "Failed to add tomb remote to local git" { _git_tomb fetch --all --tags && _git_tomb push -u $repo_name --all && _git_tomb push -u $repo_name --tags } >/dev/null || idm_exit 1 ERR "Something where wrong while syncinc" lib_log NOTICE "Tomb and local repository are now synced" # Restore ctx } # We manage distribution of our repo # but maybe it should be the lib_git_local roles ... idm_tomb__push () { local id=$1 local arg=${2-} idm_tomb__init $id # Manage argument if grep -sq "$arg" $IDM_CONFIG_DIR/git/$id/known_hosts ; then arg=$( grep -sq "$arg" $IDM_CONFIG_DIR/git/$id/known_hosts | head -n 1 ) idm_tomb_ssh_sync $arg || idm_exit 1 "Could not copy tomb to $arg" lib_log NOTICE "Tomb has been exported: $arg:$dst/$id.tomb" elif [ "$arg" == 'all' ]; then remotes="$(_git_tomb remote -v | awk '{ print $1 }' | uniq )" for repo_name in $remotes; do lib_log INFO "Synchronising remote $repo_name ..." _git_tomb fetch --all --tags && _git_tomb push -u $repo_name --all && _git_tomb push -u $repo_name --tags || lib_log WARN "Could not sync with $reponame" done elif _git_tomb remote -v | grep -q "^$arg"; then idm_tomb__sync $id $arg else # Actually export the tomb :p #ssh $arg "hostname" || # idm_exit 1 "Could not connect to $arg" #idm_tomb_gen_script_export | lib_log DUMP - lib_log INFO "Trying to connect to $arg ..." dst=$( ssh $arg "$(idm_tomb_gen_script_export)" ) || idm_exit 1 "Something failed $arg" echo "$arg" >> $IDM_CONFIG_DIR/git/$id/known_hosts scp $git_tomb_enc $arg:$dst/$id.tomb || idm_exit 1 "Could not copy tomb to $arg" lib_log NOTICE "Tomb has been exported: $arg:$dst/$id.tomb" fi #if ssh $arg "hostname" > /den/null; then # idm_exit 0 "SSH sync not implemented yet " #else # # Propagate with git # idm_tomb__sync $id #fi } idm_tomb_ssh_sync () { local host=$1 local dst= # Test connection and prepare destination lib_log INFO "Trying to connect to $host ..." dst=$( ssh $host "$(idm_tomb_gen_script_export)" ) || idm_exit 1 "Something failed $host" # Save host echo "$host" >> $IDM_CONFIG_DIR/git/$id/known_hosts # Copy tomb to remote scp $git_tomb_enc $host:$dst/$id.tomb } idm_tomb_gen_script_export () { cat </dev/null \ # && ok=$(( $ok + 1 ))\ # || ko=$(( $ko + 1 )) # # if [[ "$ok" -ne 0 && "$ko" -ne 0 ]]; then # idm_exit 1 ERR "One of the recipients is not known: $r in '$recipients'" # fi # done # # # Act according our pattern # if [[ "$ok" -eq 0 && "$ko" -ne 0 ]]; then # pass="$@" # recipients= # gpg_opts="-c" # lib_log NOTICE "Secret will be encrypted with pass '$pass'" # else # lib_log NOTICE "Secret will be encrypted with key '$key' ${recipients:+ to '$recipients'}" # fi # # else # if [ "$key" == "_ASK" ]; then # pass=_ASK # key= # gpg_opts="--no-default-recipient -e" # lib_log NOTICE "User will be prompted for known recipients" # elif [ -z "$key" -o "$key" == "_PASS" ]; then # pass= # key= # gpg_opts="-c" # lib_log NOTICE "User will be prompted for password (symetric)" # else # # Not available yet, see stdin for password input # # To fix: passwords in clear :/ use stdout3 # pass="$key" # key= # gpg_opts="-c --passphrase $pass --batch " # lib_log NOTICE "Secret will be encrypted with pass '***' (symetric)" # fi # fi # # # Encrypt all the stuffs # $TAR -C "${src%/*}" -cz "${src##*/}" 2>/dev/null | \ # $GPG -a $gpg_opts --yes -o $dst || \ # idm_exit 1 ERR "Could not encrypt directory: $src" # # # File descritor tests ... # #exec 3<> /tmp/foo # #>&3 echo "$pass" # #{ echo "$pass\n" >&3 ; $TAR -C "$(dirname $src)" -cz "$src" 2>/dev/null; } | \ # #exec 3>&- #close fd 3. # # } # # # # # # #