Statistics
| Branch: | Tag: | Revision:

root / rfb.py @ 66d17b40

History | View | Annotate | Download (1.4 kB)

1
import re
2
import d3des
3
from struct import pack, unpack
4

    
5
RFB_AUTH_SUCCESS = 0
6
RFB_AUTH_ERROR = 1
7
RFB_AUTHTYPE_VNC = 2
8
RFB_AUTHTYPE_NONE = 1
9
RFB_AUTHTYPE_ERROR = 0
10
RFB_SUPPORTED_AUTHTYPES = [RFB_AUTHTYPE_NONE, RFB_AUTHTYPE_VNC]
11
RFB_VERSION_3_8 = "RFB 003.008"
12
RFB_VERSION_3_7 = "RFB 003.007"
13
RFB_VERSION_3_3 = "RFB 003.003"
14
RFB_VALID_VERSIONS = [
15
#    RFB_VERSION_3_3,
16
#    RFB_VERSION_3_7,
17
    RFB_VERSION_3_8,
18
]
19

    
20
class RfbError(Exception):
21
    pass
22

    
23
def check_version(version):
24
    return version.strip()[:11] in RFB_VALID_VERSIONS
25

    
26
def make_auth_request(*auth_methods):
27
    auth_methods = set(auth_methods)
28
    for method in auth_methods:
29
        if method not in RFB_SUPPORTED_AUTHTYPES:
30
            raise RfbError("Unsupported authentication type: %d" % method)
31
    return pack('B' + 'B' * len(auth_methods), len(auth_methods), *auth_methods)
32

    
33
def parse_auth_request(request):
34
    length = unpack('B', request[0])[0]
35
    if length == 0:
36
        return []
37
    return unpack('B' * length, request[1:])
38

    
39
def parse_client_authtype(authtype):
40
    return unpack('B', authtype[0])[0]
41

    
42
def from_u32(val):
43
    return unpack('>L', val)[0]
44

    
45
def to_u32(val):
46
    return pack('>L', val)
47

    
48
def from_u8(val):
49
    return unpack('B', val)[0]
50

    
51
def to_u8(val):
52
    return pack('B', val)
53

    
54
def check_password(challenge, response, password):
55
    return d3des.generate_response((password + '\0' * 8 )[:8],
56
                                   challenge) == response