Skip to content

Commit

Permalink
Resolve relative paths in the Include directive properly
Browse files Browse the repository at this point in the history
According to ssh_config(5), relative paths in the Include directive is
relative from the directory of the root file, not from that of the
including file.  If `~/.ssh/config` includes `~/.ssh/dir1/file1` which
has a line `Include dir2/file2`, ssh tries to include
`~/.ssh/dir2/file2`, not `~/.ssh/dir1/dir2/file2`.
  • Loading branch information
knu committed Mar 24, 2017
1 parent b02d053 commit 7722c25
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions lib/net/ssh/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def for(host, files=expandable_default_files)
# ones. Returns a hash containing the OpenSSH options. (See
# #translate for how to convert the OpenSSH options into Net::SSH
# options.)
def load(path, host, settings={})
def load(path, host, settings={}, base_dir = nil)
file = File.expand_path(path)
base_dir = File.dirname(file)
base_dir ||= File.dirname(file)
return settings unless File.readable?(file)

globals = {}
Expand Down Expand Up @@ -125,7 +125,7 @@ def load(path, host, settings={})
(globals[key] ||= []) << value
when 'include'
included_file_paths(base_dir, value).each do |file_path|
globals = load(file_path, host, globals)
globals = load(file_path, host, globals, base_dir)
end
else
globals[key] = value unless settings.key?(key)
Expand All @@ -136,7 +136,7 @@ def load(path, host, settings={})
(settings[key] ||= []) << value
when 'include'
included_file_paths(base_dir, value).each do |file_path|
settings = load(file_path, host, settings)
settings = load(file_path, host, settings, base_dir)
end
else
settings[key] = value unless settings.key?(key)
Expand Down Expand Up @@ -304,9 +304,7 @@ def merge_challenge_response_with_keyboard_interactive(hash)
end

def included_file_paths(base_dir, config_path)
paths = Dir.glob(File.expand_path(config_path)).select { |f| File.file?(f) }
paths += Dir.glob(File.join(base_dir, config_path)).select { |f| File.file?(f) }
paths.uniq
Dir.glob(File.expand_path(config_path, base_dir)).select { |f| File.file?(f) }
end

end
Expand Down

0 comments on commit 7722c25

Please sign in to comment.