2000-02-04 11:10:34 -04:00
|
|
|
"""An object-oriented interface to .netrc files."""
|
|
|
|
|
2001-01-14 20:50:52 -04:00
|
|
|
# Module and documentation by Eric S. Raymond, 21 Dec 1998
|
1998-12-22 01:19:29 -04:00
|
|
|
|
2013-09-18 09:49:25 -03:00
|
|
|
import os, stat, shlex
|
|
|
|
if os.name == 'posix':
|
|
|
|
import pwd
|
1998-12-22 01:19:29 -04:00
|
|
|
|
2001-03-06 02:33:08 -04:00
|
|
|
__all__ = ["netrc", "NetrcParseError"]
|
|
|
|
|
|
|
|
|
|
|
|
class NetrcParseError(Exception):
|
|
|
|
"""Exception raised on syntax errors in the .netrc file."""
|
|
|
|
def __init__(self, msg, filename=None, lineno=None):
|
2001-04-15 09:51:42 -03:00
|
|
|
self.filename = filename
|
2001-03-06 02:33:08 -04:00
|
|
|
self.lineno = lineno
|
|
|
|
self.msg = msg
|
|
|
|
Exception.__init__(self, msg)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s (%s, line %s)" % (self.msg, self.filename, self.lineno)
|
|
|
|
|
2001-02-05 21:07:02 -04:00
|
|
|
|
1998-12-22 01:19:29 -04:00
|
|
|
class netrc:
|
|
|
|
def __init__(self, file=None):
|
2013-09-16 14:48:44 -03:00
|
|
|
default_netrc = file is None
|
2002-06-01 13:07:16 -03:00
|
|
|
if file is None:
|
2002-03-17 22:13:48 -04:00
|
|
|
try:
|
|
|
|
file = os.path.join(os.environ['HOME'], ".netrc")
|
|
|
|
except KeyError:
|
|
|
|
raise IOError("Could not find .netrc: $HOME is not set")
|
1998-12-22 01:19:29 -04:00
|
|
|
self.hosts = {}
|
|
|
|
self.macros = {}
|
2009-05-28 00:10:59 -03:00
|
|
|
with open(file) as fp:
|
2013-09-16 15:32:54 -03:00
|
|
|
self._parse(file, fp, default_netrc)
|
2009-05-28 00:10:59 -03:00
|
|
|
|
2013-09-16 15:32:54 -03:00
|
|
|
def _parse(self, file, fp, default_netrc):
|
1998-12-22 01:19:29 -04:00
|
|
|
lexer = shlex.shlex(fp)
|
2003-04-23 15:59:54 -03:00
|
|
|
lexer.wordchars += r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
|
2010-12-01 23:16:23 -04:00
|
|
|
lexer.commenters = lexer.commenters.replace('#', '')
|
1998-12-22 01:19:29 -04:00
|
|
|
while 1:
|
|
|
|
# Look for a machine, default, or macdef top-level keyword
|
|
|
|
toplevel = tt = lexer.get_token()
|
2000-12-12 19:20:45 -04:00
|
|
|
if not tt:
|
1998-12-22 01:19:29 -04:00
|
|
|
break
|
2010-12-01 23:16:23 -04:00
|
|
|
elif tt[0] == '#':
|
2011-06-10 13:32:52 -03:00
|
|
|
# seek to beginning of comment, in case reading the token put
|
|
|
|
# us on a new line, and then skip the rest of the line.
|
|
|
|
pos = len(tt) + 1
|
|
|
|
lexer.instream.seek(-pos, 1)
|
|
|
|
lexer.instream.readline()
|
|
|
|
continue
|
1998-12-22 01:19:29 -04:00
|
|
|
elif tt == 'machine':
|
|
|
|
entryname = lexer.get_token()
|
|
|
|
elif tt == 'default':
|
|
|
|
entryname = 'default'
|
2001-01-14 20:50:52 -04:00
|
|
|
elif tt == 'macdef': # Just skip to end of macdefs
|
1998-12-22 01:19:29 -04:00
|
|
|
entryname = lexer.get_token()
|
|
|
|
self.macros[entryname] = []
|
2002-03-21 22:46:41 -04:00
|
|
|
lexer.whitespace = ' \t'
|
1998-12-22 01:19:29 -04:00
|
|
|
while 1:
|
|
|
|
line = lexer.instream.readline()
|
2002-03-21 22:46:41 -04:00
|
|
|
if not line or line == '\012':
|
|
|
|
lexer.whitespace = ' \t\r\n'
|
1998-12-22 01:19:29 -04:00
|
|
|
break
|
|
|
|
self.macros[entryname].append(line)
|
2002-03-21 22:46:41 -04:00
|
|
|
continue
|
1998-12-22 01:19:29 -04:00
|
|
|
else:
|
2001-03-06 02:33:08 -04:00
|
|
|
raise NetrcParseError(
|
|
|
|
"bad toplevel token %r" % tt, file, lexer.lineno)
|
1998-12-22 01:19:29 -04:00
|
|
|
|
|
|
|
# We're looking at start of an entry for a named machine or default.
|
2003-04-24 17:11:20 -03:00
|
|
|
login = ''
|
|
|
|
account = password = None
|
2002-03-21 22:46:41 -04:00
|
|
|
self.hosts[entryname] = {}
|
1998-12-22 01:19:29 -04:00
|
|
|
while 1:
|
|
|
|
tt = lexer.get_token()
|
2011-06-10 13:32:52 -03:00
|
|
|
if (tt.startswith('#') or
|
|
|
|
tt in {'', 'machine', 'default', 'macdef'}):
|
2003-04-24 17:11:20 -03:00
|
|
|
if password:
|
1998-12-22 01:19:29 -04:00
|
|
|
self.hosts[entryname] = (login, account, password)
|
|
|
|
lexer.push_token(tt)
|
|
|
|
break
|
|
|
|
else:
|
2001-03-06 02:33:08 -04:00
|
|
|
raise NetrcParseError(
|
|
|
|
"malformed %s entry %s terminated by %s"
|
|
|
|
% (toplevel, entryname, repr(tt)),
|
|
|
|
file, lexer.lineno)
|
1998-12-22 01:19:29 -04:00
|
|
|
elif tt == 'login' or tt == 'user':
|
|
|
|
login = lexer.get_token()
|
|
|
|
elif tt == 'account':
|
|
|
|
account = lexer.get_token()
|
|
|
|
elif tt == 'password':
|
2013-09-16 14:48:44 -03:00
|
|
|
if os.name == 'posix' and default_netrc:
|
|
|
|
prop = os.fstat(fp.fileno())
|
|
|
|
if prop.st_uid != os.getuid():
|
|
|
|
try:
|
|
|
|
fowner = pwd.getpwuid(prop.st_uid)[0]
|
|
|
|
except KeyError:
|
|
|
|
fowner = 'uid %s' % prop.st_uid
|
|
|
|
try:
|
|
|
|
user = pwd.getpwuid(os.getuid())[0]
|
|
|
|
except KeyError:
|
2013-09-17 21:08:09 -03:00
|
|
|
user = 'uid %s' % os.getuid()
|
2013-09-16 14:48:44 -03:00
|
|
|
raise NetrcParseError(
|
|
|
|
("~/.netrc file owner (%s) does not match"
|
|
|
|
" current user (%s)") % (fowner, user),
|
|
|
|
file, lexer.lineno)
|
|
|
|
if (prop.st_mode & (stat.S_IRWXG | stat.S_IRWXO)):
|
|
|
|
raise NetrcParseError(
|
|
|
|
"~/.netrc access too permissive: access"
|
|
|
|
" permissions must restrict access to only"
|
|
|
|
" the owner", file, lexer.lineno)
|
1998-12-22 01:19:29 -04:00
|
|
|
password = lexer.get_token()
|
|
|
|
else:
|
2001-03-06 02:33:08 -04:00
|
|
|
raise NetrcParseError("bad follower token %r" % tt,
|
|
|
|
file, lexer.lineno)
|
1998-12-22 01:19:29 -04:00
|
|
|
|
|
|
|
def authenticators(self, host):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Return a (user, account, password) tuple for given host."""
|
2002-06-01 11:18:47 -03:00
|
|
|
if host in self.hosts:
|
1998-12-22 01:19:29 -04:00
|
|
|
return self.hosts[host]
|
2002-06-01 11:18:47 -03:00
|
|
|
elif 'default' in self.hosts:
|
1998-12-22 01:19:29 -04:00
|
|
|
return self.hosts['default']
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __repr__(self):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Dump the class data in the format of a .netrc file."""
|
1998-12-22 01:19:29 -04:00
|
|
|
rep = ""
|
|
|
|
for host in self.hosts.keys():
|
|
|
|
attrs = self.hosts[host]
|
2017-12-10 02:09:58 -04:00
|
|
|
rep += "machine {host}\n\tlogin {attrs[0]}\n".format(host=host, attrs=attrs)
|
1998-12-22 01:19:29 -04:00
|
|
|
if attrs[1]:
|
2017-12-10 02:09:58 -04:00
|
|
|
rep += "\taccount {attrs[1]}\n".format(attrs=attrs)
|
|
|
|
rep += "\tpassword {attrs[2]}\n".format(attrs=attrs)
|
1998-12-22 01:19:29 -04:00
|
|
|
for macro in self.macros.keys():
|
2017-12-10 02:09:58 -04:00
|
|
|
rep += "macdef {macro}\n".format(macro=macro)
|
1998-12-22 01:19:29 -04:00
|
|
|
for line in self.macros[macro]:
|
2017-12-10 02:09:58 -04:00
|
|
|
rep += line
|
|
|
|
rep += "\n"
|
1998-12-22 01:19:29 -04:00
|
|
|
return rep
|
|
|
|
|
2001-01-14 20:50:52 -04:00
|
|
|
if __name__ == '__main__':
|
1998-12-22 01:19:29 -04:00
|
|
|
print netrc()
|