2011-10-23 03:47:50 -04:00
|
|
|
# Copyright (C) 2006-2007 Jeff Forcier <jeff@bitprophet.org>
|
2006-05-07 20:52:24 -04:00
|
|
|
#
|
2011-10-23 03:00:43 -04:00
|
|
|
# This file is part of ssh.
|
2006-05-07 20:52:24 -04:00
|
|
|
#
|
2011-10-23 03:00:43 -04:00
|
|
|
# 'ssh' is free software; you can redistribute it and/or modify it under the
|
2006-05-07 20:52:24 -04:00
|
|
|
# terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
# Software Foundation; either version 2.1 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
2011-10-23 03:00:43 -04:00
|
|
|
# 'ssh' is distrubuted in the hope that it will be useful, but WITHOUT ANY
|
2006-05-07 20:52:24 -04:00
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
2011-10-23 03:00:43 -04:00
|
|
|
# along with 'ssh'; if not, write to the Free Software Foundation, Inc.,
|
2012-02-25 03:11:54 -05:00
|
|
|
# 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA.
|
2006-05-07 20:52:24 -04:00
|
|
|
|
|
|
|
"""
|
|
|
|
L{SSHConfig}.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import fnmatch
|
|
|
|
|
|
|
|
|
|
|
|
class SSHConfig (object):
|
|
|
|
"""
|
|
|
|
Representation of config information as stored in the format used by
|
2009-07-19 19:08:13 -04:00
|
|
|
OpenSSH. Queries can be made via L{lookup}. The format is described in
|
|
|
|
OpenSSH's C{ssh_config} man page. This class is provided primarily as a
|
|
|
|
convenience to posix users (since the OpenSSH format is a de-facto
|
2006-05-07 20:52:24 -04:00
|
|
|
standard on posix) but should work fine on Windows too.
|
2009-07-19 19:08:13 -04:00
|
|
|
|
2006-05-07 20:52:24 -04:00
|
|
|
@since: 1.6
|
|
|
|
"""
|
2009-07-19 19:08:13 -04:00
|
|
|
|
2006-05-07 20:52:24 -04:00
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Create a new OpenSSH config object.
|
|
|
|
"""
|
|
|
|
self._config = [ { 'host': '*' } ]
|
2009-07-19 19:08:13 -04:00
|
|
|
|
2006-05-07 20:52:24 -04:00
|
|
|
def parse(self, file_obj):
|
|
|
|
"""
|
|
|
|
Read an OpenSSH config from the given file object.
|
2009-07-19 19:08:13 -04:00
|
|
|
|
2006-05-07 20:52:24 -04:00
|
|
|
@param file_obj: a file-like object to read the config file from
|
|
|
|
@type file_obj: file
|
|
|
|
"""
|
2009-07-19 19:08:13 -04:00
|
|
|
configs = [self._config[0]]
|
2006-05-07 20:52:24 -04:00
|
|
|
for line in file_obj:
|
|
|
|
line = line.rstrip('\n').lstrip()
|
|
|
|
if (line == '') or (line[0] == '#'):
|
|
|
|
continue
|
|
|
|
if '=' in line:
|
|
|
|
key, value = line.split('=', 1)
|
|
|
|
key = key.strip().lower()
|
|
|
|
else:
|
|
|
|
# find first whitespace, and split there
|
|
|
|
i = 0
|
|
|
|
while (i < len(line)) and not line[i].isspace():
|
|
|
|
i += 1
|
|
|
|
if i == len(line):
|
|
|
|
raise Exception('Unparsable line: %r' % line)
|
|
|
|
key = line[:i].lower()
|
|
|
|
value = line[i:].lstrip()
|
|
|
|
|
|
|
|
if key == 'host':
|
2009-07-19 19:08:13 -04:00
|
|
|
del configs[:]
|
|
|
|
# the value may be multiple hosts, space-delimited
|
|
|
|
for host in value.split():
|
|
|
|
# do we have a pre-existing host config to append to?
|
|
|
|
matches = [c for c in self._config if c['host'] == host]
|
|
|
|
if len(matches) > 0:
|
|
|
|
configs.append(matches[0])
|
|
|
|
else:
|
|
|
|
config = { 'host': host }
|
|
|
|
self._config.append(config)
|
|
|
|
configs.append(config)
|
2006-05-07 20:52:24 -04:00
|
|
|
else:
|
2009-07-19 19:08:13 -04:00
|
|
|
for config in configs:
|
|
|
|
config[key] = value
|
2006-05-07 20:52:24 -04:00
|
|
|
|
|
|
|
def lookup(self, hostname):
|
|
|
|
"""
|
|
|
|
Return a dict of config options for a given hostname.
|
|
|
|
|
|
|
|
The host-matching rules of OpenSSH's C{ssh_config} man page are used,
|
|
|
|
which means that all configuration options from matching host
|
|
|
|
specifications are merged, with more specific hostmasks taking
|
2009-07-19 19:08:13 -04:00
|
|
|
precedence. In other words, if C{"Port"} is set under C{"Host *"}
|
2006-05-07 20:52:24 -04:00
|
|
|
and also C{"Host *.example.com"}, and the lookup is for
|
|
|
|
C{"ssh.example.com"}, then the port entry for C{"Host *.example.com"}
|
|
|
|
will win out.
|
|
|
|
|
|
|
|
The keys in the returned dict are all normalized to lowercase (look for
|
2009-07-19 19:08:13 -04:00
|
|
|
C{"port"}, not C{"Port"}. No other processing is done to the keys or
|
2006-05-07 20:52:24 -04:00
|
|
|
values.
|
|
|
|
|
|
|
|
@param hostname: the hostname to lookup
|
|
|
|
@type hostname: str
|
|
|
|
"""
|
|
|
|
matches = [x for x in self._config if fnmatch.fnmatch(hostname, x['host'])]
|
|
|
|
# sort in order of shortest match (usually '*') to longest
|
|
|
|
matches.sort(lambda x,y: cmp(len(x['host']), len(y['host'])))
|
|
|
|
ret = {}
|
|
|
|
for m in matches:
|
|
|
|
ret.update(m)
|
2012-03-30 08:08:14 -04:00
|
|
|
ret = self._expand_variables(ret, hostname)
|
2006-05-07 20:52:24 -04:00
|
|
|
del ret['host']
|
|
|
|
return ret
|
2012-03-30 08:08:14 -04:00
|
|
|
|
|
|
|
def _expand_variables(self, config, hostname ):
|
|
|
|
"""
|
|
|
|
Return a dict of config options with expanded substitutions
|
|
|
|
for a given hostname.
|
|
|
|
|
|
|
|
For the moment only expansion of the %h substitution in the
|
|
|
|
hostname config is supported.
|
|
|
|
|
|
|
|
@param config: the config for the hostname
|
|
|
|
@type hostname: dict
|
|
|
|
@param hostname: the hostname that the config belongs to
|
|
|
|
@type hostname: str
|
|
|
|
"""
|
|
|
|
#TODO: Add support for expansion of all substitution parameters
|
|
|
|
#TODO: see man ssh_config(5)
|
|
|
|
if 'hostname' in config:
|
|
|
|
config['hostname'] = config['hostname'].replace('%h',hostname)
|
|
|
|
else:
|
|
|
|
config['hostname'] = hostname
|
|
|
|
return config
|