summaryrefslogtreecommitdiffstats
path: root/test_container/behave_agent.py
blob: 49d32d4c389a10cccac5c1d148a4152db983b4a8 (plain)
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
#!/usr/bin/env python3
import io
import os
import pwd
import socket
import struct
import subprocess


MSG_EOF  = 0
MSG_USER = 1
MSG_CMD  = 2
MSG_OUT  = 4 
MSG_ERR  = 5
MSG_RC   = 6


class Message:
    @staticmethod
    def write(output, type: int, data: bytes):
        output.write(struct.pack('!ii', type, len(data)))
        output.write(data)

    @staticmethod
    def read(input):
        buf = input.read(8)
        type, length = struct.unpack('!ii', buf)
        if length > 0:
            buf = input.read(length)
        else:
            buf = b''
        return type, buf


class SocketIO(io.RawIOBase):
    def __init__(self, s: socket.socket):
        self._socket = s

    def readable(self) -> bool:
        return True

    def writable(self) -> bool:
        return True

    def read(self, __size: int = -1) -> bytes:
        return self._socket.recv(__size)

    def readinto(self, __buffer) -> int:
        return self._socket.recv_into(__buffer)

    def readall(self) -> bytes:
        raise NotImplementedError

    def write(self, __b) -> int:
        return self._socket.send(__b)


def call(host: str, port: int, cmdline: str):
    family, type, proto, _, sockaddr =  socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)[0]
    with socket.socket(family, type, proto) as s:
        s.connect(sockaddr)
        sout = io.BufferedWriter(SocketIO(s), 4096)
        Message.write(sout, MSG_USER, _getuser().encode('utf-8'))
        Message.write(sout, MSG_CMD, cmdline.encode('utf-8'))
        Message.write(sout, MSG_EOF, b'')
        sout.flush()
        s.shutdown(socket.SHUT_WR)
        rc = None
        stdout = []
        stderr = []
        sin = io.BufferedReader(SocketIO(s), 4096)
        while True:
            type, buf = Message.read(sin)
            if type == MSG_OUT:
                stdout.append(buf)
            elif type == MSG_ERR:
                stderr.append(buf)
            elif type == MSG_RC:
                rc, = struct.unpack('!i', buf)
            elif type == MSG_EOF:
                s.shutdown(socket.SHUT_RD)
                assert rc is not None
                return rc, b''.join(stdout), b''.join(stderr)
            else:
                raise ValueError(f"Unknown message type: {type}")


def serve(stdin, stdout, stderr):
    assert os.geteuid() == 0
    user = None
    cmd = None
    sin = io.BufferedReader(stdin)
    while True:
        type, buf = Message.read(sin)
        if type == MSG_USER:
            user = buf.decode('utf-8')
        elif type == MSG_CMD:
            cmd = buf.decode('utf-8')
        elif type == MSG_EOF:
            assert user is not None
            assert cmd is not None
            break
        else:
            raise ValueError(f"Unknown message type: {type}")
    if user == 'root':
        args = ['/bin/sh']
    else:
        args = ['/bin/su', '-', user, '-c', '/bin/sh']
    result = subprocess.run(
        args,
        input=cmd.encode('utf-8'),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    sout = io.BufferedWriter(stdout)
    Message.write(sout, MSG_RC, struct.pack('!i', result.returncode))
    Message.write(sout, MSG_OUT, result.stdout)
    Message.write(sout, MSG_ERR, result.stderr)
    Message.write(sout, MSG_EOF, b'')
    stdout.flush()


def _getuser():
    return pwd.getpwuid(os.geteuid()).pw_name


if __name__ == '__main__':
    with open(0, 'rb') as stdin, \
         open(1, 'wb') as stdout, \
         open(2, 'wb') as stderr:
        serve(stdin, stdout, stderr)