Statistics
| Branch: | Tag: | Revision:

root / vncauthproxy / rfb.py @ 138d0e8b

History | View | Annotate | Download (2.5 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
    if version.strip()[:11] in RFB_VALID_VERSIONS:
44
        return version.strip()[:11]
45
    else:
46
        return None
47

    
48
def make_auth_request(*args, **kwargs):
49
    auth_methods = args
50
    version = kwargs['version']
51
    if version == RFB_VERSION_3_3:
52
        if len(auth_methods) != 1:
53
            raise RfbError("Only single authentication type may be specified for RFB 3.3")
54
    auth_methods = set(auth_methods)
55
    for method in auth_methods:
56
        if method not in RFB_SUPPORTED_AUTHTYPES:
57
            raise RfbError("Unsupported authentication type: %d" % method)
58
    if version == RFB_VERSION_3_3:
59
        return pack('>I', *auth_methods)
60
    else:
61
        return pack('B' + 'B' * len(auth_methods), len(auth_methods), *auth_methods)
62

    
63
def parse_auth_request(request):
64
    length = unpack('B', request[0])[0]
65
    if length == 0:
66
        return []
67
    return unpack('B' * length, request[1:])
68

    
69
def parse_client_authtype(authtype):
70
    return unpack('B', authtype[0])[0]
71

    
72
def from_u32(val):
73
    return unpack('>L', val)[0]
74

    
75
def to_u32(val):
76
    return pack('>L', val)
77

    
78
def from_u8(val):
79
    return unpack('B', val)[0]
80

    
81
def to_u8(val):
82
    return pack('B', val)
83

    
84
def check_password(challenge, response, password):
85
    return d3des.generate_response((password + '\0' * 8 )[:8],
86
                                   challenge) == response