forked from mit-athena/python-hesiod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhesiod.py
executable file
·138 lines (118 loc) · 4.85 KB
/
hesiod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
Present both functional and object-oriented interfaces for executing
lookups in Hesiod, Project Athena's service name resolution protocol.
"""
from _hesiod import bind, resolve
from pwd import struct_passwd
from grp import struct_group
class HesiodParseError(Exception):
pass
class Lookup(object):
"""
A Generic Hesiod lookup
"""
def __init__(self, hes_name, hes_type):
self.results = resolve(hes_name, hes_type)
self.parseRecords()
def parseRecords(self):
pass
class FilsysLookup(Lookup):
def __init__(self, name, **kwargs):
self.parseTypes = True
if 'parseFilsysTypes' in kwargs:
self.parseTypes = kwargs['parseFilsysTypes']
Lookup.__init__(self, name, 'filsys')
def parseRecords(self):
Lookup.parseRecords(self)
self.filsys = []
# An FSGROUP need not contain multiple members.
# We are defining an FSGROUP as "Something for which all
# returned records have an integer as the last field"
self.isFSGroup = True
for result in self.results:
try:
if int(result.rsplit(" ", 1)[1]) < 1:
self.isFSGroup = False
break
except ValueError:
self.isFSGroup = False
break
except IndexError:
raise HesiodParseError("No fields found in result '%s'; shouldn't happen" % (result,))
for result in self.results:
priority = 0
if self.isFSGroup:
result, priority = result.rsplit(" ", 1)
priority = int(priority)
parts = result.split(" ")
if len(parts) < 2:
raise HesiodParseError("Invalid filsys record: %s" % (result))
if not self.parseTypes:
self.filsys.append(dict(type=parts[0],
data=" ".join(parts[1::]),
priority=priority))
continue
type = parts[0]
if type == 'AFS':
self.filsys.append(dict(type=type,
location=parts[1],
mode=parts[2],
mountpoint=parts[3],
priority=priority))
elif type == 'NFS':
self.filsys.append(dict(type=type,
remote_location=parts[1],
server=parts[2],
mode=parts[3],
mountpoint=parts[4],
priority=priority))
elif type == 'ERR':
self.filsys.append(dict(type=type,
message=" ".join(parts[1::]),
priority=priority))
elif type == 'UFS':
self.filsys.append(dict(type=type,
device=parts[1],
mode=parts[2],
mountpoint=parts[3],
priority=priority))
elif type == 'LOC':
self.filsys.append(dict(type=type,
location=parts[1],
mode=parts[2],
mountpoint=parts[3],
priority=priority))
else:
raise HesiodParseError('Unknown filsys type: %s' % type)
self.filsys.sort(key=(lambda x: x['priority']))
class PasswdLookup(Lookup):
def __init__(self, name):
Lookup.__init__(self, name, 'passwd')
def parseRecords(self):
passwd_info = self.results[0].split(':')
passwd_info[2] = int(passwd_info[2])
passwd_info[3] = int(passwd_info[3])
self.passwd = struct_passwd(passwd_info)
class UidLookup(PasswdLookup):
def __init__(self, uid):
Lookup.__init__(self, uid, 'uid')
class GroupLookup(Lookup):
def __init__(self, group):
Lookup.__init__(self, group, 'group')
def parseRecords(self):
group_info = self.results[0].split(':')
group_info[2] = int(group_info[2])
members = group_info[3]
if members != '':
members = members.split(',')
else:
members = []
group_info[3] = members
self.group = struct_group(group_info)
class GidLookup(GroupLookup):
def __init__(self, gid):
Lookup.__init__(self, gid, 'gid')
__all__ = ['bind', 'resolve',
'Lookup', 'FilsysLookup', 'PasswdLookup', 'UidLookup',
'GroupLookup', 'GidLookup',
'HesiodParseError']