24
24
25
25
def get_identities (config ):
26
26
identities_path = config .Get ("DIR_IDENTITIES" )
27
+ if not os .path .isdir (identities_path ):
28
+ return []
27
29
return [f for f in os .listdir (identities_path ) if isdir (join (identities_path , f ))]
28
30
29
31
@@ -44,7 +46,9 @@ def main():
44
46
group = parser .add_mutually_exclusive_group ()
45
47
group .add_argument ("-l" , "--list" , action = "store_true" , help = "List identities" )
46
48
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." )
48
52
group .add_argument ("-d" , "--deactivate" , action = "store_true" , help = "Dectivate ssh-ident" )
49
53
group .add_argument ("-c" , "--create" , metavar = '<identity>' , help = "Create a new identity" )
50
54
group .add_argument ("-s" , "--shell" , metavar = '<identity>' , nargs = '?' , const = 'default-ssh-id' ,
@@ -107,15 +111,22 @@ def main():
107
111
elif args .remove_prompt :
108
112
exit_val |= ACTION_FLAGS .DISABLE_PROMPT
109
113
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
119
130
elif args .deactivate :
120
131
exit_val |= ACTION_FLAGS .UNSET_SHELL_ENV
121
132
if config .Get ("SSH_IDENT_PROMPT" ):
0 commit comments