Skip to content

Commit 46d6185

Browse files
author
bendikro
committed
Add support to activate ssh-ident only for a given user
1 parent 644b111 commit 46d6185

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

scripts/ssh-ident-completion.bash

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
source /usr/share/bash-completion/completions/ssh
22

3-
_ssh_ident()
3+
_ssh_ident_ssh()
44
{
55
# Set the ssh config path if available
66
if [[ "${SSH_IDENT_CONFIG}" != "" ]]; then
@@ -9,4 +9,28 @@ _ssh_ident()
99
_ssh $@
1010

1111
return 0
12-
} && shopt -u hostcomplete && complete -F _ssh_ident ssh slogin autossh
12+
} && shopt -u hostcomplete && complete -F _ssh_ident_ssh ssh slogin autossh
13+
14+
15+
# scp completion with _ssh_ident_scp doesn't work properly due to the extra call
16+
# 'set -- "${words[@]}"' in function _scp() in
17+
# /usr/share/bash-completion/completions/ssh
18+
19+
#_ssh_ident_scp()
20+
#{
21+
# # Set the ssh config path if available
22+
# if [[ "${SSH_IDENT_CONFIG}" != "" ]]; then
23+
# conf="-F$SSH_IDENT_CONFIG"
24+
# conf_len=${#conf}
25+
# set -- "${@:1:1}" "$conf" "${@:2}"
26+
# COMP_WORDS=("$@")
27+
# COMP_CWORD=$((COMP_CWORD+1))
28+
# COMP_LINE=$(printf " %s" "$@")
29+
# COMP_LINE=${COMP_LINE:1}
30+
# COMP_POINT=$((COMP_POINT+$conf_len+1))
31+
# fi
32+
#
33+
# _scp $@
34+
#
35+
# return 0
36+
#} && complete -F _ssh_ident_scp scp

scripts/ssh_ident.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ __ssh_ident_update_prompt_id() {
7777

7878
# Activate the ssh-ident shell prompt
7979
ssh_ident_activate() {
80-
ssh_ident -a
80+
ssh_ident -a $1
8181
}
8282

8383
# Activate the ssh-ident shell prompt

ssh_ident/ssh_ident_cli.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
def get_identities(config):
2626
identities_path = config.Get("DIR_IDENTITIES")
27+
if not os.path.isdir(identities_path):
28+
return []
2729
return [f for f in os.listdir(identities_path) if isdir(join(identities_path, f))]
2830

2931

@@ -44,7 +46,9 @@ def main():
4446
group = parser.add_mutually_exclusive_group()
4547
group.add_argument("-l", "--list", action="store_true", help="List identities")
4648
group.add_argument("-i", "--identity", action="store_true", help="Show current identity")
47-
group.add_argument("-a", "--activate", action="store_true", help="Activate ssh-ident with default config settings")
49+
group.add_argument("-a", "--activate", nargs='?', const=True,
50+
help="Activate ssh-ident with default config settings. "
51+
"If a user is provided, only activate if the current user matches the given user.")
4852
group.add_argument("-d", "--deactivate", action="store_true", help="Dectivate ssh-ident")
4953
group.add_argument("-c", "--create", metavar='<identity>', help="Create a new identity")
5054
group.add_argument("-s", "--shell", metavar='<identity>', nargs='?', const='default-ssh-id',
@@ -107,15 +111,22 @@ def main():
107111
elif args.remove_prompt:
108112
exit_val |= ACTION_FLAGS.DISABLE_PROMPT
109113
elif args.activate:
110-
idents = get_identities(config)
111-
identity, id_type, match = FindIdentity(sys.argv, config)
112-
if identity in idents:
113-
exit_val |= ACTION_FLAGS.SSH_IDENTITY
114-
print(identity, file=stdoutput)
115-
if config.Get("SSH_IDENT_PROMPT"):
116-
exit_val |= ACTION_FLAGS.ENABLE_PROMPT
117-
if config.Get("SSH_IDENT_BASH_FUNCTIONS"):
118-
exit_val |= ACTION_FLAGS.DEFINE_BASH_FUNCTIONS
114+
# If a user is provided, should match current user running the shell.
115+
# if ssh-ident is activated in a users .bashrc, sourcing that .bashrc
116+
# file from another user will fail if that user doesn't also have
117+
# ssh-ident configured. By explicitly providing a user, ssh-ident will
118+
# only be activated if the specified user is the one loading the .bashrc file.
119+
activate = args.activate == True or args.activate == os.environ.get('USER')
120+
if activate:
121+
idents = get_identities(config)
122+
identity, id_type, match = FindIdentity(sys.argv, config)
123+
if identity in idents:
124+
exit_val |= ACTION_FLAGS.SSH_IDENTITY
125+
print(identity, file=stdoutput)
126+
if config.Get("SSH_IDENT_PROMPT"):
127+
exit_val |= ACTION_FLAGS.ENABLE_PROMPT
128+
if config.Get("SSH_IDENT_BASH_FUNCTIONS"):
129+
exit_val |= ACTION_FLAGS.DEFINE_BASH_FUNCTIONS
119130
elif args.deactivate:
120131
exit_val |= ACTION_FLAGS.UNSET_SHELL_ENV
121132
if config.Get("SSH_IDENT_PROMPT"):

0 commit comments

Comments
 (0)