1 | n/a | """An object-oriented interface to .netrc files.""" |
---|
2 | n/a | |
---|
3 | n/a | # Module and documentation by Eric S. Raymond, 21 Dec 1998 |
---|
4 | n/a | |
---|
5 | n/a | import os, shlex, stat |
---|
6 | n/a | |
---|
7 | n/a | __all__ = ["netrc", "NetrcParseError"] |
---|
8 | n/a | |
---|
9 | n/a | |
---|
10 | n/a | class NetrcParseError(Exception): |
---|
11 | n/a | """Exception raised on syntax errors in the .netrc file.""" |
---|
12 | n/a | def __init__(self, msg, filename=None, lineno=None): |
---|
13 | n/a | self.filename = filename |
---|
14 | n/a | self.lineno = lineno |
---|
15 | n/a | self.msg = msg |
---|
16 | n/a | Exception.__init__(self, msg) |
---|
17 | n/a | |
---|
18 | n/a | def __str__(self): |
---|
19 | n/a | return "%s (%s, line %s)" % (self.msg, self.filename, self.lineno) |
---|
20 | n/a | |
---|
21 | n/a | |
---|
22 | n/a | class netrc: |
---|
23 | n/a | def __init__(self, file=None): |
---|
24 | n/a | default_netrc = file is None |
---|
25 | n/a | if file is None: |
---|
26 | n/a | try: |
---|
27 | n/a | file = os.path.join(os.environ['HOME'], ".netrc") |
---|
28 | n/a | except KeyError: |
---|
29 | n/a | raise OSError("Could not find .netrc: $HOME is not set") |
---|
30 | n/a | self.hosts = {} |
---|
31 | n/a | self.macros = {} |
---|
32 | n/a | with open(file) as fp: |
---|
33 | n/a | self._parse(file, fp, default_netrc) |
---|
34 | n/a | |
---|
35 | n/a | def _parse(self, file, fp, default_netrc): |
---|
36 | n/a | lexer = shlex.shlex(fp) |
---|
37 | n/a | lexer.wordchars += r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" |
---|
38 | n/a | lexer.commenters = lexer.commenters.replace('#', '') |
---|
39 | n/a | while 1: |
---|
40 | n/a | # Look for a machine, default, or macdef top-level keyword |
---|
41 | n/a | saved_lineno = lexer.lineno |
---|
42 | n/a | toplevel = tt = lexer.get_token() |
---|
43 | n/a | if not tt: |
---|
44 | n/a | break |
---|
45 | n/a | elif tt[0] == '#': |
---|
46 | n/a | if lexer.lineno == saved_lineno and len(tt) == 1: |
---|
47 | n/a | lexer.instream.readline() |
---|
48 | n/a | continue |
---|
49 | n/a | elif tt == 'machine': |
---|
50 | n/a | entryname = lexer.get_token() |
---|
51 | n/a | elif tt == 'default': |
---|
52 | n/a | entryname = 'default' |
---|
53 | n/a | elif tt == 'macdef': # Just skip to end of macdefs |
---|
54 | n/a | entryname = lexer.get_token() |
---|
55 | n/a | self.macros[entryname] = [] |
---|
56 | n/a | lexer.whitespace = ' \t' |
---|
57 | n/a | while 1: |
---|
58 | n/a | line = lexer.instream.readline() |
---|
59 | n/a | if not line or line == '\012': |
---|
60 | n/a | lexer.whitespace = ' \t\r\n' |
---|
61 | n/a | break |
---|
62 | n/a | self.macros[entryname].append(line) |
---|
63 | n/a | continue |
---|
64 | n/a | else: |
---|
65 | n/a | raise NetrcParseError( |
---|
66 | n/a | "bad toplevel token %r" % tt, file, lexer.lineno) |
---|
67 | n/a | |
---|
68 | n/a | # We're looking at start of an entry for a named machine or default. |
---|
69 | n/a | login = '' |
---|
70 | n/a | account = password = None |
---|
71 | n/a | self.hosts[entryname] = {} |
---|
72 | n/a | while 1: |
---|
73 | n/a | tt = lexer.get_token() |
---|
74 | n/a | if (tt.startswith('#') or |
---|
75 | n/a | tt in {'', 'machine', 'default', 'macdef'}): |
---|
76 | n/a | if password: |
---|
77 | n/a | self.hosts[entryname] = (login, account, password) |
---|
78 | n/a | lexer.push_token(tt) |
---|
79 | n/a | break |
---|
80 | n/a | else: |
---|
81 | n/a | raise NetrcParseError( |
---|
82 | n/a | "malformed %s entry %s terminated by %s" |
---|
83 | n/a | % (toplevel, entryname, repr(tt)), |
---|
84 | n/a | file, lexer.lineno) |
---|
85 | n/a | elif tt == 'login' or tt == 'user': |
---|
86 | n/a | login = lexer.get_token() |
---|
87 | n/a | elif tt == 'account': |
---|
88 | n/a | account = lexer.get_token() |
---|
89 | n/a | elif tt == 'password': |
---|
90 | n/a | if os.name == 'posix' and default_netrc: |
---|
91 | n/a | prop = os.fstat(fp.fileno()) |
---|
92 | n/a | if prop.st_uid != os.getuid(): |
---|
93 | n/a | import pwd |
---|
94 | n/a | try: |
---|
95 | n/a | fowner = pwd.getpwuid(prop.st_uid)[0] |
---|
96 | n/a | except KeyError: |
---|
97 | n/a | fowner = 'uid %s' % prop.st_uid |
---|
98 | n/a | try: |
---|
99 | n/a | user = pwd.getpwuid(os.getuid())[0] |
---|
100 | n/a | except KeyError: |
---|
101 | n/a | user = 'uid %s' % os.getuid() |
---|
102 | n/a | raise NetrcParseError( |
---|
103 | n/a | ("~/.netrc file owner (%s) does not match" |
---|
104 | n/a | " current user (%s)") % (fowner, user), |
---|
105 | n/a | file, lexer.lineno) |
---|
106 | n/a | if (prop.st_mode & (stat.S_IRWXG | stat.S_IRWXO)): |
---|
107 | n/a | raise NetrcParseError( |
---|
108 | n/a | "~/.netrc access too permissive: access" |
---|
109 | n/a | " permissions must restrict access to only" |
---|
110 | n/a | " the owner", file, lexer.lineno) |
---|
111 | n/a | password = lexer.get_token() |
---|
112 | n/a | else: |
---|
113 | n/a | raise NetrcParseError("bad follower token %r" % tt, |
---|
114 | n/a | file, lexer.lineno) |
---|
115 | n/a | |
---|
116 | n/a | def authenticators(self, host): |
---|
117 | n/a | """Return a (user, account, password) tuple for given host.""" |
---|
118 | n/a | if host in self.hosts: |
---|
119 | n/a | return self.hosts[host] |
---|
120 | n/a | elif 'default' in self.hosts: |
---|
121 | n/a | return self.hosts['default'] |
---|
122 | n/a | else: |
---|
123 | n/a | return None |
---|
124 | n/a | |
---|
125 | n/a | def __repr__(self): |
---|
126 | n/a | """Dump the class data in the format of a .netrc file.""" |
---|
127 | n/a | rep = "" |
---|
128 | n/a | for host in self.hosts.keys(): |
---|
129 | n/a | attrs = self.hosts[host] |
---|
130 | n/a | rep = rep + "machine "+ host + "\n\tlogin " + repr(attrs[0]) + "\n" |
---|
131 | n/a | if attrs[1]: |
---|
132 | n/a | rep = rep + "account " + repr(attrs[1]) |
---|
133 | n/a | rep = rep + "\tpassword " + repr(attrs[2]) + "\n" |
---|
134 | n/a | for macro in self.macros.keys(): |
---|
135 | n/a | rep = rep + "macdef " + macro + "\n" |
---|
136 | n/a | for line in self.macros[macro]: |
---|
137 | n/a | rep = rep + line |
---|
138 | n/a | rep = rep + "\n" |
---|
139 | n/a | return rep |
---|
140 | n/a | |
---|
141 | n/a | if __name__ == '__main__': |
---|
142 | n/a | print(netrc()) |
---|