diff --git a/bin/idmgr b/bin/idmgr index c3d5dc2..a126332 100755 --- a/bin/idmgr +++ b/bin/idmgr @@ -35,6 +35,8 @@ IDM_DIR_ID=${IDM_DIR_ID:-$IDM_CONFIG_DIR/id} IDM_DIR_LIB=${IDM_DIR_LIB:-$IDM_DIR_ROOT/lib} IDM_DIR_CACHE=${IDM_DIR_CACHE:-${XDG_CACHE_HOME:-~/.cache}/idmgr} +IDM_SRC_WORDS='enable disable kill shell quit e d k s q' + mkdir -p $IDM_CONFIG_DIR $IDM_DIR_ID $IDM_DIR_CACHE # Mod vars ... @@ -71,9 +73,12 @@ idm_core__help () printf " %-20s: %s\n" "shred [id|user|dev|ask]" "Safely schred data (definitive)" printf " %-20s: %s\n" "fun " "Show internal function (debug)" printf " %-20s: %s\n" "hier " "Show cli function (debug)" - + + local mods=$(idm_lib_order_get status) + echo + echo "Loaded mods: $mods" - idm_core_exec_mod $id __help "\n%s" $(idm_lib_order_get status) + idm_core_exec_mod $id __help "\n%s" $mods echo lib_log NOTICE "License:" @@ -105,8 +110,11 @@ idm_core__enable () if [ "${SHELL_ID-}" == "$id" ]; then #idm_exit 0 INFO "Your workspace is already activated" lib_log WARN "Your workspace is already activated" + elif [ -n "${SHELL_ID-}" ]; then - idm_exit 0 WARN "Your workspace is already activated with $SHELL_ID" + #idm_exit 0 WARN "Your workspace is already activated with $SHELL_ID" + lib_log WARN "Changing $SHELL_ID workspace to $id" + idmgr disable "${SHELL_ID-}" fi # Retrieve environment config @@ -132,7 +140,7 @@ idm_core__disable () idm_core_exec_mod $id __disable "Disabling %s ..." $(idm_lib_order_get disable) # Inform user - lib_log NOTICE "Id $id is disabled" + lib_log NOTICE "Identity '$id' is unloaded" } idm_core__kill () @@ -155,8 +163,10 @@ idm_core__kill () ########################################## # Dump sub commands that need to be sourved to work -idm_core__shell_words () +idm_core__shell_words_deprected () { + # REPLACED BY IDM_SRC_WORDS, does not peovides ids anymore ... + exit 777 local id=${1-} local words= @@ -170,7 +180,6 @@ idm_core__shell_words () idm_core__shell () { - IDM_SRC_WORDS=$( $IDM_DIR_ROOT/bin/idmgr shell_words ) echo "export IDM_BIN=${IDM_BIN:-$IDM_DIR_ROOT/bin/idmgr}" echo "export IDM_DIR_ROOT='$IDM_DIR_ROOT'" @@ -257,8 +266,8 @@ idm_lib_order_get () local f=${1} # >&2 echo "fmt: ${IDM_LIB_TABLEFMT%%$f*}" #>&2 echo -e "\n>order: $f\n>table: $IDM_LIB_TABLE" - - f=$( wc -w <<< "ignore ${IDM_LIB_TABLEFMT%%$f*}" ) + local w="ignore ${IDM_LIB_TABLEFMT%%$f*}" + f=$( wc -w <<< "$w" ) sort -t, -k${f} <<< "$IDM_LIB_TABLE" | cut -d, -f1 | xargs } @@ -285,27 +294,9 @@ idm_lib_load () local var_name [ -n "$names" ] || names=$(idm_lib_names $type) var_name="IDM_${type^^}_LOADED" - - - # Rebuild vars for all modules ! - case $type in - lib) - - ;; - mod) - local d_val d_var - - ;; - - *) - echo "unsupported type: $type" - return 1 - ;; - esac # Source lib/mods - #set -x for name in $names; do source "$IDM_DIR_LIB/idmgr_${type}_${name}.sh" if [ "$?" -eq 0 ] ; then @@ -359,6 +350,7 @@ idm_mod_files () idm_mod_order () { + >&2 echo "DEPRECATED: idm_mod_order" local mods=$@ export IDM_MOD_FILES="$( xargs <<< $mods)" @@ -420,10 +412,11 @@ idm_core_init () # Force native library orders idm_lib_order_set id 10 90 10 - idm_lib_order_set gpg 11 89 11 + #idm_lib_order_set gpg 11 89 11 idm_lib_order_set ssh 12 88 12 + #idm_lib_order_set git 50 50 50 idm_lib_order_set ps1 60 40 60 - idm_lib_order_set tomb 50 50 50 + #idm_lib_order_set tomb 50 50 50 # Load external lib idm_lib_load lib @@ -750,6 +743,9 @@ idm_core_cli () idm_core_cli $@ + +#declare -p | grep " IDM" + exit idm_lib_src () diff --git a/lib/idmgr_mod_cloud.sh b/lib/idmgr_mod_cloud.sh index d4acf04..de38696 100644 --- a/lib/idmgr_mod_cloud.sh +++ b/lib/idmgr_mod_cloud.sh @@ -1,6 +1,6 @@ #!/bin/bash -IDM_MOD_CLOUD_DEPS="s3" +#IDM_MOD_CLOUD_DEPS="s3" ## Prompt functions ########################################## diff --git a/lib/idmgr_mod_git.sh b/lib/idmgr_mod_git.sh index 638f75b..b095601 100644 --- a/lib/idmgr_mod_git.sh +++ b/lib/idmgr_mod_git.sh @@ -1,7 +1,7 @@ #!/bin/bash -IDM_MOD_GIT_DEPS="s1 id ssh" -IDM_DISABLE_AUTO+=" git__enable git__disable git__kill " +#IDM_MOD_GIT_DEPS="s1 id ssh" +#IDM_DISABLE_AUTO+=" git__enable git__disable git__kill " #idm_hook_register enable idm_git__enable 5 @@ -420,10 +420,11 @@ idm_git__ls () fi # Display repo infos + { echo " Work tree : $git_id_work_tree" echo " Local config : $git_id_config" echo " Git dir : $git_id_dir" - + } | sed "s:$HOME:~:g" } idm_git__enable () diff --git a/lib/idmgr_mod_gpg.sh b/lib/idmgr_mod_gpg.sh index 684f23b..8f14d80 100644 --- a/lib/idmgr_mod_gpg.sh +++ b/lib/idmgr_mod_gpg.sh @@ -1,6 +1,6 @@ #!/bin/bash -IDM_MOD_GPG_DEPS="s0 id" +#IDM_MOD_GPG_DEPS="s0 id" idm_gpg__help () diff --git a/lib/idmgr_mod_id.sh b/lib/idmgr_mod_id.sh index 3f7dc21..fc42bea 100644 --- a/lib/idmgr_mod_id.sh +++ b/lib/idmgr_mod_id.sh @@ -1,6 +1,6 @@ #!/bin/bash -IDM_MOD_ID_DEPS="s0" +#IDM_MOD_ID_DEPS="s0" ## Identity functions ########################################## @@ -32,7 +32,7 @@ idm_id__disable() idm_id__kill () { idm_id__disable ${@-}; } -idm_id__enable() +idm_id__enable () { local id=${1} local conf="$IDM_DIR_ID/$id.env" diff --git a/lib/idmgr_mod_pass.sh b/lib/idmgr_mod_pass.sh index b529e00..0f29bbd 100644 --- a/lib/idmgr_mod_pass.sh +++ b/lib/idmgr_mod_pass.sh @@ -1,6 +1,6 @@ #!/bin/bash -IDM_MOD_PASS_DEPS="s2" +#IDM_MOD_PASS_DEPS="s2" ## Pass functions diff --git a/lib/idmgr_mod_ps1.sh b/lib/idmgr_mod_ps1.sh index e0688b8..8a309f7 100644 --- a/lib/idmgr_mod_ps1.sh +++ b/lib/idmgr_mod_ps1.sh @@ -1,7 +1,7 @@ #!/bin/bash -IDM_MOD_PS1_DEPS="s4 id pass gpg ssh" -IDM_DISABLE_AUTO+="ps1__ls" +#IDM_MOD_PS1_DEPS="s4 id pass gpg ssh" +#IDM_DISABLE_AUTO+="ps1__ls" ## Prompt functions ########################################## @@ -20,10 +20,8 @@ idm_ps1__ls () { local id=${1} - #set -x - #echo "PS1=${SHELL_PS1:-${PS1-}}" - - if grep -q "($id)" <<<"${SHELL_PS1:-${PS1-}}" ; then + # Bug here: PS1 and vars are like nk existing ... weird + if grep -q "($id)" <<<"${IDM_SHELL_PS1:-${PS1-}}" ; then echo " enabled" else echo " disabled" diff --git a/lib/idmgr_mod_ssh.sh b/lib/idmgr_mod_ssh.sh index 2ffef8d..e2fbcd7 100644 --- a/lib/idmgr_mod_ssh.sh +++ b/lib/idmgr_mod_ssh.sh @@ -1,6 +1,6 @@ #!/bin/bash -IDM_MOD_SSH_DEPS="s0 id gpg" +#IDM_MOD_SSH_DEPS="s0 id gpg" # trap 'idm_ssh_kill' 0 @@ -12,7 +12,8 @@ idm_ssh__help () echo "Secure Shell" # printf " %-20s: %s\n" "info" "Info submenu" printf " %-20s: %s\n" "ssh ls" "List unlocked keys" - printf " %-20s: %s\n" "ssh new" "Create new ssh key (ssh-keygen)" + printf " %-20s: %s\n" "ssh tree" "Show keypairs tree" + printf " %-20s: %s\n" "ssh new [dir]" "Create new ssh key dest dir" printf " %-20s: %s\n" "ssh add" "Unlock known keypairs" printf " %-20s: %s\n" "ssh rm" "Lock known keypairs" printf " %-20s: %s\n" "ssh del" "Delete keypair" @@ -130,6 +131,129 @@ idm_ssh__kill () } +## Extra functions +########################################## + +idm_ssh__tree () +{ + local id=$1 + if lib_id_has_config $id &>/dev/null; then + tree -C "$HOME/.ssh/$id" + else + tree -C "$HOME/.ssh/" + fi +} + +idm_ssh__new () +{ + local id=${1-} + local dest=${2-} + + local default= + local key_vers= + local key_user= + local key_host= + local key_sizes= + local key_vers="$(date +'%Y%m%d')" + + #set -x + + # Guess defaults + default=$(id -un) + if lib_id_has_config $id &>/dev/null; then + default=$id + if [ -z "$dest" ]; then + dest="$HOME/.ssh/$default" + fi + else + dest=${dest:-.} + fi + mkdir -p "$dest" + echo "INFO: Key destination dir: $dest" + + # Login + while ! grep -q '\w\+' <<< "$key_user"; do + read -rp "> Username [$default]: " ans + key_user="${ans:-$default}" + done + + + # Host name + default="$(hostname -f)" + while ! grep -q '[a-zA-Z0-9.-]\+' <<< "$key_host"; do + read -rp "> Hostname [$default]: " ans + #echo "" + key_host="${ans:-$default}" + done + + + # Keys sizes + default="ns" + echo "Please choose key types:" + echo "n) ed25519 strongest, fast" + echo "s) rsa4096 most compatible, slow" + echo "o) rsa2048 old compatility" + while ! grep -q '[nso]\+' <<< "$key_sizes"; do + echo -n "> Key types [$default]: " + read -n 3 -r ans + echo "" + key_sizes="${ans:-$default}" + done + + # Ask password + echo "Define key passphrase for the key(s)." + echo "Leave it empty for no password (not recommemded)." + echo -n "> Key passphrase [none]: " + read -rs key_pass + echo + key_pass="${key_pass:-}" + + ans="" + while [ "$ans" != "$key_pass" ]; do + echo -n "> Confirm passphrase: " + read -rs ans + echo + done + + + # Create keys + local size=$key_sizes + while [ -n "$size" ]; do + local k=${size:0:1} + echo -e "\n> Generating key ..." + set +e + case $k in + n) + ssh-keygen -f "$dest/${key_user}_ed25519_${key_vers}" \ + -t ed25519 -a 100 \ + -N "$key_pass" \ + -C "${key_user}@${key_host}:ed25519_${key_vers}" + ;; + s) + ssh-keygen -f "$dest/${key_user}_rsa4096_${key_vers}" \ + -t rsa -b 4096 -o -a 500 \ + -N "$key_pass" \ + -C "${key_user}@${key_host}:rsa4096_${key_vers}" + ;; + o) + ssh-keygen -f "$dest/${key_user}_rsa2048_${key_vers}" \ + -t rsa -b 2048 -o -a 100 \ + -N "$key_pass" \ + -C "${key_user}@${key_host}:rsa2048_${key_vers}" + ;; + esac + set -e + + + size=${size:1} + done + + echo + echo "INFO: Key(s) has been created in $dest" + +} + + ## Agent functions ########################################## @@ -216,7 +340,7 @@ idm_ssh__agent_clean () ## Extended functions ########################################## -idm_ssh_add () +idm_ssh__add () { local id=$1 local key=${2-} @@ -230,7 +354,7 @@ idm_ssh_add () pub_keys=$( { # Compat mode - find ~/.ssh/id -maxdepth $maxdepth -name "${id}_*" -name '*pub' -name "*$1*" | sort + #find ~/.ssh/id -maxdepth $maxdepth -name "${id}_*" -name '*pub' -name "*$1*" | sort # New mode (test) find ~/.ssh/$id -maxdepth $maxdepth -name "${id}_*" -name '*pub' -name "*$1*" | sort @@ -240,7 +364,7 @@ idm_ssh_add () pub_keys=$(find ~/.ssh/$id -maxdepth $maxdepth -name "${id}_*" -name '*pub' | sort) fi - echo "$pub_keys" + #echo "$pub_keys" # Get list of key local key_list="" @@ -260,10 +384,10 @@ idm_ssh_add () idm_exit 0 WARN "No keys found" lib_log INFO "Adding keys:" - xargs -n 1 <<<$key_list | lib_log DUMP - + xargs -n 1 <<<$key_list | sed "s:$HOME:~:" | lib_log DUMP - echo "" - ssh-add $key_list + ssh-add $key_list } diff --git a/lib/idmgr_mod_tomb.sh b/lib/idmgr_mod_tomb.sh index 3e1456a..2e30b51 100644 --- a/lib/idmgr_mod_tomb.sh +++ b/lib/idmgr_mod_tomb.sh @@ -1,12 +1,12 @@ #!/bin/bash -IDM_MOD_TOMB_DEPS="s3 id gpg git" -IDM_MOD_TAGS="id tool" -IDM_MOD_PROG="safe yadm" -IDM_MOD_PREF="core id" - -IDM_DISABLE_AUTO+=" tomb__enable tomb__disable tomb__kill " - +#IDM_MOD_TOMB_DEPS="s3 id gpg git" +#IDM_MOD_TAGS="id tool" +#IDM_MOD_PROG="safe yadm" +#IDM_MOD_PREF="core id" +# +#IDM_DISABLE_AUTO+=" tomb__enable tomb__disable tomb__kill " +# ## Environments diff --git a/shell/bash/startup.sh b/shell/bash/startup.sh index 6890ddd..32f1f40 100644 --- a/shell/bash/startup.sh +++ b/shell/bash/startup.sh @@ -1,45 +1,62 @@ #!/bin/bash -export IDM_SHELL_PS1=${IDM_SHELL_PS1:-${PS1-}} -IDM_SRC_WORDS=${IDM_SRC_WORDS-} -IDM_BIN=${IDM_BIN:-idmgr} - -i () +idmgr_shell_words () { + # Generate command/ids list to be sourced + local IDM_SRC_CMDS='enable disable kill shell quit e d k s q' + local IDM_SRC_IDS=$(find "$XDG_CONFIG_HOME/idmgr/id/" \ + -type f -name "*.env" \ + -printf "%f " | sed 's/\.env//g') - if grep -q ":${1:-NONE}:" <<<"${IDM_SRC_WORDS}"; then + echo "$IDM_SRC_CMDS $IDM_SRC_IDS" +} - result="$( $IDM_BIN $@)" + +idmgr_shell () +{ + IDM_SRC_WORDS="${IDM_SRC_WORDS:-$(idmgr_shell_words)}" + + # Check if must be sourced or not + if [[ "${IDM_SRC_WORDS// /:}" =~ :$1: ]]; then + + # Get output source + >&2 echo "INFO : Running sourced command ..." + shell_exec="$( command idmgr $@)" # Debug module if [ "${ID_DEBUG-}" == "true" ]; then - if [ "${result:-NONE}" == "NONE" ]; then - echo "======= ${result:-NONE}" + if [ "${shell_exec:-NONE}" == "NONE" ]; then + echo "======= ${shell_exec:-NONE}" else echo ======= Shell has sourced ======= - echo "${result:-NONE}" + echo "${shell_exec:-NONE}" echo ======= fi fi - # Parse output - eval "$result" - + # Exec output + eval "$shell_exec" + else - $IDM_BIN $@ + # Execute as regular command + command idmgr $@ fi - } +# Set aliases +alias idmgr='idmgr_shell' +alias i='idmgr' + +# Save current state +export PS1="$PS1" +export IDM_SHELL_PS1=${IDM_SHELL_PS1:-${PS1-}} + # Disable when pressing C-b in shell :) bind -x '"\C-b": i disable' - # Show current identities echo "INFO: idmgr has been loaded, use 'idmgr' or 'i' to call it" -#$IDM_BIN id ls - - +idmgr id ls