Statistics
| Branch: | Tag: | Revision:

root / vncauthproxy / rfb.py @ 6149f03e

History | View | Annotate | Download (2.5 kB)

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

    
18
import d3des
19
from struct import pack, unpack
20

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

    
34

    
35
class RfbError(Exception):
36
    pass
37

    
38

    
39
def check_version(version):
40
    if version.strip()[:11] in RFB_VALID_VERSIONS:
41
        return version.strip()[:11]
42
    else:
43
        return None
44

    
45

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

    
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

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

    
73

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

    
77

    
78
def to_u32(val):
79
    return pack('>L', val)
80

    
81

    
82
def from_u8(val):
83
    return unpack('B', val)[0]
84

    
85

    
86
def to_u8(val):
87
    return pack('B', val)
88

    
89

    
90
def check_password(challenge, response, password):
91
    return d3des.generate_response((password + '\0' * 8)[:8],
92
                                   challenge) == response