Created
March 14, 2020 10:04
-
-
Save michalbednarski/d9345deba33d9c13c5aea6621975f321 to your computer and use it in GitHub Desktop.
/var/run/nscd/socket server in Python, https://github.com/termux/proot/issues/101
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/data/data/com.termux/files/usr/bin/python3 | |
import ipaddress | |
import os | |
import socket | |
import socketserver | |
import struct | |
# version, type, key_len | |
request_header = struct.Struct("<III") | |
# version, found, naddrs, addrslen, canonlen, error | |
ai_response_header = struct.Struct("<IIIIII") | |
class Handler(socketserver.StreamRequestHandler): | |
def handle(self): | |
version, req_type, key_len = request_header.unpack(self.rfile.read(request_header.size)) | |
if version in (2, 3) and req_type == 14 and key_len <= 1024: | |
key = self.rfile.read(key_len).rstrip(b"\0").decode("latin1") | |
try: | |
addrinfo = socket.getaddrinfo(key, 0) | |
familydata = bytearray() | |
addrdata = bytearray() | |
seen = set() | |
for addrinfo_item in addrinfo: | |
addr = ipaddress.ip_address(addrinfo_item[4][0]) | |
if addr in seen: | |
continue | |
seen.add(addr) | |
if addr.version == 4: | |
familydata.append(2) | |
addrdata.extend(addr.packed) | |
elif addr.version == 6: | |
familydata.append(10) | |
addrdata.extend(addr.packed) | |
response = ai_response_header.pack(0, 1, len(familydata), len(addrdata), 0, 0) + addrdata + familydata | |
except socket.gaierror: | |
response = ai_response_header.pack(0, 0, 0, 0, 0, 0) | |
self.wfile.write(response) | |
def serve(path): | |
try: | |
os.remove(path) | |
except FileNotFoundError: | |
pass | |
s = socketserver.UnixStreamServer(path, Handler) | |
s.serve_forever() | |
if __name__ == "__main__": | |
# Note: Path to socket in guest is /var/run/nscd/socket | |
# If guest symlinks /var/run -> /run then "/var" should be omitted from path below, | |
# Otherwise it has to be added. | |
# You'll need to create "nscd" directory yourself and adjust path below. | |
serve("/data/data/com.termux/files/home/ubuntu19-fs/run/nscd/socket") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment