-
Notifications
You must be signed in to change notification settings - Fork 5
/
hostabd.py
224 lines (209 loc) · 9.67 KB
/
hostabd.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright © 2023-2024 Björn Victor ([email protected])
# Chaosnet server for HOSTAB protocol.
# As an extension, the request can also be a Chaosnet address (octal), which is looked up
# giving the same response as if the corresponding name had been requested.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys, socket, threading, re, time
from functools import reduce
from chaosnet import PacketConn, ChaosError, dns_info_for, dns_resolver_name, dns_resolver_address, set_dns_resolver_address
# The typical chaosnet end-of-line (#\return)
eol = b"\215"
keynames = dict(os='SYSTEM-TYPE', cpu='MACHINE-TYPE')
# Parse private hosts file.
# Lines not starting with # should be an initial octal address (on subnet 376)
# followed by whitespace-separated names for that address.
def parse_private_hosts_file(fname):
with open(fname) as f:
return reduce(parse_private_hosts_line, f.readlines(), dict())
def parse_private_hosts_line(alist, l):
if not l.startswith("#"):
try:
astring,nstring = l.split(maxsplit=1)
except ValueError:
print("Bad line {!r} in private hosts file".format(l.strip()), file=sys.stderr)
return alist
if re.match("^[0-7]+$",astring):
try:
addr = int(astring,8)
if (addr & 0xff) == 0 or (addr >> 8) == 0 or addr > 0xffff or addr < 0xff:
print("Error: invalid Chaosnet address {:o}, line {!r}".format(addr,l.strip()),
file=sys.stderr)
return alist
elif (addr >> 8) != 0o376:
print("Warning: address {:o} is not on the globally private subnet 376 but on subnet {:o}".format(addr,addr>>8),
file=sys.stderr)
names = nstring.upper().split() # traditions
# @@@@ should check that each of names start with a letter?
alist[addr] = names
except ValueError:
print("Bad address string {!r} in private hosts file, line {!r}".format(astring,l.strip()),
file=sys.stderr)
else:
print("Bad line {!r} in private hosts file".format(l.strip()), file=sys.stderr)
return alist
privhosts = dict()
def private_host_lookup(name):
for addr,names in privhosts.items():
if name in names:
return addr,names
def private_addr_lookup(addr):
if addr in privhosts.keys():
return privhosts[addr]
def hostab_server_response(name,timeout=2,dns_address=None,default_domain=None):
gotip = False
name = name.upper() #Traditions
# First get Chaosnet info - this usually has os and cpu
info = dns_info_for(name, timeout=2, dns_address=dns_address, default_domain=default_domain)
if len(privhosts) > 0:
# Check for private hosts info, and add it after DNS data (if any)
if re.match("^[0-7]+$",name):
a = int(name,8)
names = private_addr_lookup(a)
if names is not None:
if info is None:
info = dict()
info['addrs'] = (info['addrs'] if 'addrs' in info.keys() else []) + [a]
info['name'] = (info['name'] if 'name' in info.keys() else []) + names
else:
addrnames = private_host_lookup(name)
if addrnames is not None:
addr,names = addrnames
if info is None:
info = dict()
info['addrs'] = (info['addrs'] if 'addrs' in info.keys() else []) + [addr]
info['name'] = (info['name'] if 'name' in info.keys() else []) + names
if info is not None and 'addrs' in info.keys():
# Remove any duplicate addrs, while keeping order
info['addrs'] = list(dict.fromkeys(info['addrs']))
if info is None:
# Try Internet class
info = dns_info_for(name, timeout=2, dns_address=dns_address, default_domain=default_domain, rclass="IN")
if info is not None:
if info['addrs']:
gotip = True
elif not name.isdigit():
# Last resort: try gethostbyname_ex - the given DNS server might not serve the IN class to us
try:
hname,aliases,ips = socket.gethostbyname_ex(name)
gotip = True
info = dict()
if hname.upper() != name:
info['name'] = [hname,name]
else:
info['name'] = [hname]
info['name'] += aliases
if ips:
info['addrs'] = ips
except socket.error as msg:
if debug:
print("gethostbyname_ex: {}".format(msg), file=sys.stderr)
return ["ERROR {}".format(msg)]
if debug:
print("Got info {}".format(info), file=sys.stderr)
resp = []
if info:
for n in info['name']:
resp.append("NAME {}".format(n))
for k in keynames.keys():
if k in info and info[k]:
resp.append("{} {}".format(keynames[k],info[k].upper()))
if info['addrs']:
for a in info['addrs']:
if isinstance(a,int):
resp.append("CHAOS {:o}".format(a))
else:
resp.append("INTERNET {}".format(a))
if not gotip:
try:
# If we didn't already get some IP address info, get it now
hname,aliases,ips = socket.gethostbyname_ex(info['name'][0] if 'name' in info else name)
if ips:
for ip in ips:
resp.append("INTERNET {}".format(ip))
except socket.error as msg:
if debug:
print("gethostbyname: {}".format(msg), file=sys.stderr)
else:
resp = ["ERROR No such host"]
return resp
def hostab_server(conn, timeout=2,dns_address=None,default_domain=None):
try:
data = conn.get_message(1) #get a packet
while data:
if debug:
print("Got data {}".format(data), file=sys.stderr)
name = str(re.split(b"[\215\r]",data,maxsplit=1)[0],"ascii").strip() #split("\r",maxsplit=1)[0]
if debug:
print("Got data {} => name {}".format(data,name), file=sys.stderr)
resp = hostab_server_response(name,timeout,dns_address,default_domain)
if debug:
print("Sending response {}".format(resp), file=sys.stderr)
conn.send_data(eol.join(map(lambda s:bytes(s,"ascii"),resp))+eol)
conn.send_eof(True)
data = conn.get_message(1)
except ChaosError as m:
# for example EOF[wait] didn't get an ack, then we're broken
if debug:
print(m, file=sys.stderr)
try:
conn.close()
finally:
return
debug = False
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description="Chaosnet HOSTAB server")
parser.add_argument("-d",'--debug',dest='debug',action='store_true',
help='Turn on debug printouts')
parser.add_argument("-s",'--dns-server',default=dns_resolver_name,
help='DNS server to use, default '+dns_resolver_name)
parser.add_argument("-D",'--default-domain',action='append',
help='Default domain if names have none (repeat to have more than one)')
parser.add_argument("-t",'--timeout',default=2,type=int,
help='DNS timeout in seconds, default 2')
parser.add_argument("-m",'--maxthreads',default=10,type=int,
help='Max number of active threads/connections, default 10')
# Another option would be to parse cbridge.conf and look for it
parser.add_argument("-p",'--private-hosts',
help='File for private hosts (cf subnet 376)')
args = parser.parse_args()
dns_resolver_address = set_dns_resolver_address(args.dns_server)
if args.debug:
debug = True
print(args, file=sys.stderr)
print("DNS addr {}".format(dns_resolver_address))
if args.private_hosts:
privhosts = parse_private_hosts_file(args.private_hosts)
while True:
try:
c = PacketConn()
c.set_debug(debug)
h = c.listen("HOSTAB")
if debug:
print("Conn from {}".format(h), file=sys.stderr)
c.send_opn()
xec = threading.Thread(target=hostab_server, args=(c,args.timeout,dns_resolver_address,args.default_domain))
xec.start()
# hostab_server(c, timeout=args.timeout)
except (BrokenPipeError, socket.error, ChaosError) as msg:
if debug:
print("Error: {}".format(msg), file=sys.stderr)
# Could be cbridge crashed
time.sleep(10)
continue
finally:
while threading.active_count() > args.maxthreads:
# Wait until some terminate and free up
if debug:
print("Active threads now {}, sleeping".format(threading.active_count()), file=sys.stderr)
time.sleep(3)