Statistics
| Branch: | Tag: | Revision:

root / vncauthproxy / rfb.py @ 07b0130f

History | View | Annotate | Download (2.1 kB)

1
#
2
#
3

    
4
# Copyright (c) 2010 GRNET SA
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21
import d3des
22
from struct import pack, unpack
23

    
24
RFB_AUTH_SUCCESS = 0
25
RFB_AUTH_ERROR = 1
26
RFB_AUTHTYPE_VNC = 2
27
RFB_AUTHTYPE_NONE = 1
28
RFB_AUTHTYPE_ERROR = 0
29
RFB_SUPPORTED_AUTHTYPES = [RFB_AUTHTYPE_NONE, RFB_AUTHTYPE_VNC]
30
RFB_VERSION_3_8 = "RFB 003.008"
31
RFB_VERSION_3_7 = "RFB 003.007"
32
RFB_VERSION_3_3 = "RFB 003.003"
33
RFB_VALID_VERSIONS = [
34
#    RFB_VERSION_3_3,
35
#    RFB_VERSION_3_7,
36
    RFB_VERSION_3_8,
37
]
38

    
39
class RfbError(Exception):
40
    pass
41

    
42
def check_version(version):
43
    return version.strip()[:11] in RFB_VALID_VERSIONS
44

    
45
def make_auth_request(*auth_methods):
46
    auth_methods = set(auth_methods)
47
    for method in auth_methods:
48
        if method not in RFB_SUPPORTED_AUTHTYPES:
49
            raise RfbError("Unsupported authentication type: %d" % method)
50
    return pack('B' + 'B' * len(auth_methods), len(auth_methods), *auth_methods)
51

    
52
def parse_auth_request(request):
53
    length = unpack('B', request[0])[0]
54
    if length == 0:
55
        return []
56
    return unpack('B' * length, request[1:])
57

    
58
def parse_client_authtype(authtype):
59
    return unpack('B', authtype[0])[0]
60

    
61
def from_u32(val):
62
    return unpack('>L', val)[0]
63

    
64
def to_u32(val):
65
    return pack('>L', val)
66

    
67
def from_u8(val):
68
    return unpack('B', val)[0]
69

    
70
def to_u8(val):
71
    return pack('B', val)
72

    
73
def check_password(challenge, response, password):
74
    return d3des.generate_response((password + '\0' * 8 )[:8],
75
                                   challenge) == response