diff --git a/Lib/netrc.py b/Lib/netrc.py --- a/Lib/netrc.py +++ b/Lib/netrc.py @@ -2,7 +2,7 @@ # Module and documentation by Eric S. Raymond, 21 Dec 1998 -import os, shlex +import os, stat, shlex, pwd __all__ = ["netrc", "NetrcParseError"] @@ -21,12 +21,31 @@ class netrc: def __init__(self, file=None): - if file is None: + if file is not None: + fp = open(file) + else: try: file = os.path.join(os.environ['HOME'], ".netrc") except KeyError: raise IOError("Could not find .netrc: $HOME is not set") - fp = open(file) + fp = open(file) + if os.name == 'posix': + 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: + user = 'uid %s ' % os.getuid() + raise OSError("~/.netrc file owner (%s) does not match" + " current user (%s)" % (fowner, user)) + if (prop.st_mode & (stat.S_IRWXG | stat.S_IRWXO)): + raise OSError("~/.netrc access too permissive: access" + " permissions must restrict access to" + " only the owner") self.hosts = {} self.macros = {} lexer = shlex.shlex(fp)