Statistics
| Branch: | Tag: | Revision:

root / vncauthproxy / rfb.py @ 4a1dd7af

History | View | Annotate | Download (2.5 kB)

1 31965126 Vangelis Koukis
# Copyright (c) 2010-2013 GRNET SA
2 8790958b Apollon Oikonomopoulos
#
3 8790958b Apollon Oikonomopoulos
# This program is free software; you can redistribute it and/or modify
4 8790958b Apollon Oikonomopoulos
# it under the terms of the GNU General Public License as published by
5 8790958b Apollon Oikonomopoulos
# the Free Software Foundation; either version 2 of the License, or
6 8790958b Apollon Oikonomopoulos
# (at your option) any later version.
7 8790958b Apollon Oikonomopoulos
#
8 8790958b Apollon Oikonomopoulos
# This program is distributed in the hope that it will be useful, but
9 8790958b Apollon Oikonomopoulos
# WITHOUT ANY WARRANTY; without even the implied warranty of
10 8790958b Apollon Oikonomopoulos
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 8790958b Apollon Oikonomopoulos
# General Public License for more details.
12 8790958b Apollon Oikonomopoulos
#
13 8790958b Apollon Oikonomopoulos
# You should have received a copy of the GNU General Public License
14 8790958b Apollon Oikonomopoulos
# along with this program; if not, write to the Free Software
15 8790958b Apollon Oikonomopoulos
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 8790958b Apollon Oikonomopoulos
# 02110-1301, USA.
17 8790958b Apollon Oikonomopoulos
18 66d17b40 Apollon Oikonomopoulos
import d3des
19 66d17b40 Apollon Oikonomopoulos
from struct import pack, unpack
20 66d17b40 Apollon Oikonomopoulos
21 66d17b40 Apollon Oikonomopoulos
RFB_AUTH_SUCCESS = 0
22 66d17b40 Apollon Oikonomopoulos
RFB_AUTH_ERROR = 1
23 66d17b40 Apollon Oikonomopoulos
RFB_AUTHTYPE_VNC = 2
24 66d17b40 Apollon Oikonomopoulos
RFB_AUTHTYPE_NONE = 1
25 66d17b40 Apollon Oikonomopoulos
RFB_AUTHTYPE_ERROR = 0
26 66d17b40 Apollon Oikonomopoulos
RFB_SUPPORTED_AUTHTYPES = [RFB_AUTHTYPE_NONE, RFB_AUTHTYPE_VNC]
27 66d17b40 Apollon Oikonomopoulos
RFB_VERSION_3_8 = "RFB 003.008"
28 66d17b40 Apollon Oikonomopoulos
RFB_VERSION_3_7 = "RFB 003.007"
29 66d17b40 Apollon Oikonomopoulos
RFB_VERSION_3_3 = "RFB 003.003"
30 31965126 Vangelis Koukis
RFB_VALID_VERSIONS = [RFB_VERSION_3_3,
31 31965126 Vangelis Koukis
                      # RFB_VERSION_3_7,
32 31965126 Vangelis Koukis
                      RFB_VERSION_3_8]
33 31965126 Vangelis Koukis
34 66d17b40 Apollon Oikonomopoulos
35 66d17b40 Apollon Oikonomopoulos
class RfbError(Exception):
36 66d17b40 Apollon Oikonomopoulos
    pass
37 66d17b40 Apollon Oikonomopoulos
38 31965126 Vangelis Koukis
39 66d17b40 Apollon Oikonomopoulos
def check_version(version):
40 8799ab6d Vangelis Koukis
    if version.strip()[:11] in RFB_VALID_VERSIONS:
41 8799ab6d Vangelis Koukis
        return version.strip()[:11]
42 8799ab6d Vangelis Koukis
    else:
43 8799ab6d Vangelis Koukis
        return None
44 66d17b40 Apollon Oikonomopoulos
45 31965126 Vangelis Koukis
46 8799ab6d Vangelis Koukis
def make_auth_request(*args, **kwargs):
47 8799ab6d Vangelis Koukis
    auth_methods = args
48 8799ab6d Vangelis Koukis
    version = kwargs['version']
49 8799ab6d Vangelis Koukis
    if version == RFB_VERSION_3_3:
50 8799ab6d Vangelis Koukis
        if len(auth_methods) != 1:
51 31965126 Vangelis Koukis
            raise RfbError("Only one auth type may be specified for RFB 3.3")
52 66d17b40 Apollon Oikonomopoulos
    auth_methods = set(auth_methods)
53 66d17b40 Apollon Oikonomopoulos
    for method in auth_methods:
54 66d17b40 Apollon Oikonomopoulos
        if method not in RFB_SUPPORTED_AUTHTYPES:
55 66d17b40 Apollon Oikonomopoulos
            raise RfbError("Unsupported authentication type: %d" % method)
56 8799ab6d Vangelis Koukis
    if version == RFB_VERSION_3_3:
57 8799ab6d Vangelis Koukis
        return pack('>I', *auth_methods)
58 8799ab6d Vangelis Koukis
    else:
59 31965126 Vangelis Koukis
        return pack('B' + 'B' * len(auth_methods), len(auth_methods),
60 31965126 Vangelis Koukis
                    *auth_methods)
61 31965126 Vangelis Koukis
62 66d17b40 Apollon Oikonomopoulos
63 66d17b40 Apollon Oikonomopoulos
def parse_auth_request(request):
64 66d17b40 Apollon Oikonomopoulos
    length = unpack('B', request[0])[0]
65 66d17b40 Apollon Oikonomopoulos
    if length == 0:
66 66d17b40 Apollon Oikonomopoulos
        return []
67 66d17b40 Apollon Oikonomopoulos
    return unpack('B' * length, request[1:])
68 66d17b40 Apollon Oikonomopoulos
69 31965126 Vangelis Koukis
70 66d17b40 Apollon Oikonomopoulos
def parse_client_authtype(authtype):
71 66d17b40 Apollon Oikonomopoulos
    return unpack('B', authtype[0])[0]
72 66d17b40 Apollon Oikonomopoulos
73 31965126 Vangelis Koukis
74 66d17b40 Apollon Oikonomopoulos
def from_u32(val):
75 66d17b40 Apollon Oikonomopoulos
    return unpack('>L', val)[0]
76 66d17b40 Apollon Oikonomopoulos
77 31965126 Vangelis Koukis
78 66d17b40 Apollon Oikonomopoulos
def to_u32(val):
79 66d17b40 Apollon Oikonomopoulos
    return pack('>L', val)
80 66d17b40 Apollon Oikonomopoulos
81 31965126 Vangelis Koukis
82 66d17b40 Apollon Oikonomopoulos
def from_u8(val):
83 66d17b40 Apollon Oikonomopoulos
    return unpack('B', val)[0]
84 66d17b40 Apollon Oikonomopoulos
85 31965126 Vangelis Koukis
86 66d17b40 Apollon Oikonomopoulos
def to_u8(val):
87 66d17b40 Apollon Oikonomopoulos
    return pack('B', val)
88 66d17b40 Apollon Oikonomopoulos
89 31965126 Vangelis Koukis
90 66d17b40 Apollon Oikonomopoulos
def check_password(challenge, response, password):
91 31965126 Vangelis Koukis
    return d3des.generate_response((password + '\0' * 8)[:8],
92 66d17b40 Apollon Oikonomopoulos
                                   challenge) == response