Skip to content

Commit

Permalink
Add support to activate ssh-ident only for a given user
Browse files Browse the repository at this point in the history
  • Loading branch information
bendikro committed May 29, 2017
1 parent 644b111 commit 46d6185
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
28 changes: 26 additions & 2 deletions scripts/ssh-ident-completion.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source /usr/share/bash-completion/completions/ssh

_ssh_ident()
_ssh_ident_ssh()
{
# Set the ssh config path if available
if [[ "${SSH_IDENT_CONFIG}" != "" ]]; then
Expand All @@ -9,4 +9,28 @@ _ssh_ident()
_ssh $@

return 0
} && shopt -u hostcomplete && complete -F _ssh_ident ssh slogin autossh
} && shopt -u hostcomplete && complete -F _ssh_ident_ssh ssh slogin autossh


# scp completion with _ssh_ident_scp doesn't work properly due to the extra call
# 'set -- "${words[@]}"' in function _scp() in
# /usr/share/bash-completion/completions/ssh

#_ssh_ident_scp()
#{
# # Set the ssh config path if available
# if [[ "${SSH_IDENT_CONFIG}" != "" ]]; then
# conf="-F$SSH_IDENT_CONFIG"
# conf_len=${#conf}
# set -- "${@:1:1}" "$conf" "${@:2}"
# COMP_WORDS=("$@")
# COMP_CWORD=$((COMP_CWORD+1))
# COMP_LINE=$(printf " %s" "$@")
# COMP_LINE=${COMP_LINE:1}
# COMP_POINT=$((COMP_POINT+$conf_len+1))
# fi
#
# _scp $@
#
# return 0
#} && complete -F _ssh_ident_scp scp
2 changes: 1 addition & 1 deletion scripts/ssh_ident.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ __ssh_ident_update_prompt_id() {

# Activate the ssh-ident shell prompt
ssh_ident_activate() {
ssh_ident -a
ssh_ident -a $1
}

# Activate the ssh-ident shell prompt
Expand Down
31 changes: 21 additions & 10 deletions ssh_ident/ssh_ident_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

def get_identities(config):
identities_path = config.Get("DIR_IDENTITIES")
if not os.path.isdir(identities_path):
return []
return [f for f in os.listdir(identities_path) if isdir(join(identities_path, f))]


Expand All @@ -44,7 +46,9 @@ def main():
group = parser.add_mutually_exclusive_group()
group.add_argument("-l", "--list", action="store_true", help="List identities")
group.add_argument("-i", "--identity", action="store_true", help="Show current identity")
group.add_argument("-a", "--activate", action="store_true", help="Activate ssh-ident with default config settings")
group.add_argument("-a", "--activate", nargs='?', const=True,
help="Activate ssh-ident with default config settings. "
"If a user is provided, only activate if the current user matches the given user.")
group.add_argument("-d", "--deactivate", action="store_true", help="Dectivate ssh-ident")
group.add_argument("-c", "--create", metavar='<identity>', help="Create a new identity")
group.add_argument("-s", "--shell", metavar='<identity>', nargs='?', const='default-ssh-id',
Expand Down Expand Up @@ -107,15 +111,22 @@ def main():
elif args.remove_prompt:
exit_val |= ACTION_FLAGS.DISABLE_PROMPT
elif args.activate:
idents = get_identities(config)
identity, id_type, match = FindIdentity(sys.argv, config)
if identity in idents:
exit_val |= ACTION_FLAGS.SSH_IDENTITY
print(identity, file=stdoutput)
if config.Get("SSH_IDENT_PROMPT"):
exit_val |= ACTION_FLAGS.ENABLE_PROMPT
if config.Get("SSH_IDENT_BASH_FUNCTIONS"):
exit_val |= ACTION_FLAGS.DEFINE_BASH_FUNCTIONS
# If a user is provided, should match current user running the shell.
# if ssh-ident is activated in a users .bashrc, sourcing that .bashrc
# file from another user will fail if that user doesn't also have
# ssh-ident configured. By explicitly providing a user, ssh-ident will
# only be activated if the specified user is the one loading the .bashrc file.
activate = args.activate == True or args.activate == os.environ.get('USER')
if activate:
idents = get_identities(config)
identity, id_type, match = FindIdentity(sys.argv, config)
if identity in idents:
exit_val |= ACTION_FLAGS.SSH_IDENTITY
print(identity, file=stdoutput)
if config.Get("SSH_IDENT_PROMPT"):
exit_val |= ACTION_FLAGS.ENABLE_PROMPT
if config.Get("SSH_IDENT_BASH_FUNCTIONS"):
exit_val |= ACTION_FLAGS.DEFINE_BASH_FUNCTIONS
elif args.deactivate:
exit_val |= ACTION_FLAGS.UNSET_SHELL_ENV
if config.Get("SSH_IDENT_PROMPT"):
Expand Down

0 comments on commit 46d6185

Please sign in to comment.