root / vncauthproxy / client.py @ 3b98303f
History | View | Annotate | Download (8.6 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
#
|
3 |
# Copyright (c) 2010-2011 Greek Research and Technology Network S.A.
|
4 |
#
|
5 |
# This program is free software; you can redistribute it and/or modify
|
6 |
# it under the terms of the GNU General Public License as published by
|
7 |
# the Free Software Foundation; either version 2 of the License, or
|
8 |
# (at your option) any later version.
|
9 |
#
|
10 |
# This program is distributed in the hope that it will be useful, but
|
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
# General Public License for more details.
|
14 |
#
|
15 |
# You should have received a copy of the GNU General Public License
|
16 |
# along with this program; if not, write to the Free Software
|
17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
18 |
# 02110-1301, USA.
|
19 |
|
20 |
""" vncauthproxy client """
|
21 |
|
22 |
import sys |
23 |
import socket |
24 |
import ssl |
25 |
|
26 |
try:
|
27 |
import simplejson as json |
28 |
except ImportError: |
29 |
import json |
30 |
|
31 |
try:
|
32 |
from gevent import sleep |
33 |
except ImportError: |
34 |
from time import sleep |
35 |
|
36 |
import logging |
37 |
|
38 |
DEFAULT_SERVER_ADDRESS = '127.0.0.1'
|
39 |
DEFAULT_SERVER_PORT = 24999
|
40 |
|
41 |
logger = logging.getLogger(__name__) |
42 |
|
43 |
|
44 |
def parse_arguments(args): |
45 |
from optparse import OptionParser |
46 |
|
47 |
parser = OptionParser() |
48 |
parser.add_option("--server", dest="server_address", |
49 |
default=DEFAULT_SERVER_ADDRESS, |
50 |
metavar="SERVER",
|
51 |
help=("vncauthproxy server address"))
|
52 |
parser.add_option("--server-port", dest="server_port", |
53 |
default=DEFAULT_SERVER_PORT, type="int",
|
54 |
metavar="SERVER_PORT",
|
55 |
help=("vncauthproxy port"))
|
56 |
parser.add_option('-s', dest="sport", |
57 |
default=0, type="int", |
58 |
metavar='PORT',
|
59 |
help=("Use source port PORT for incoming connections "
|
60 |
"(default: allocate a port automatically)"))
|
61 |
parser.add_option("-d", "--dest", |
62 |
dest="daddr",
|
63 |
metavar="HOST",
|
64 |
help="Proxy connection to destination host HOST")
|
65 |
parser.add_option("-p", "--dport", dest="dport", |
66 |
type="int",
|
67 |
metavar="PORT",
|
68 |
help="Proxy connection to destination port PORT")
|
69 |
parser.add_option("-P", "--password", dest="password", |
70 |
metavar="PASSWORD",
|
71 |
help=("Use password PASSWD to authenticate incoming "
|
72 |
"VNC connections"))
|
73 |
parser.add_option("--auth-user", dest="auth_user", |
74 |
metavar="AUTH_USER",
|
75 |
help=("User to authenticate as, for the control "
|
76 |
"connection"))
|
77 |
parser.add_option("--auth-password", dest="auth_password", |
78 |
metavar="AUTH_PASSWORD",
|
79 |
help=("User password for the control connection "
|
80 |
"authentication"))
|
81 |
parser.add_option("--enable-ssl", dest="enable_ssl", |
82 |
action='store_true', default=False, |
83 |
help=("Enable SSL/TLS for control connecions "
|
84 |
"(default: %s)" % False)) |
85 |
parser.add_option("--ca-cert", dest="ca_cert", |
86 |
default=None,
|
87 |
metavar="CACERT",
|
88 |
help=("CA certificate file to use for server auth"))
|
89 |
parser.add_option("--strict", dest="strict", |
90 |
default=False, action='store_true', |
91 |
metavar="STRICT",
|
92 |
help=("Perform strict authentication on the server "
|
93 |
"SSL cert"))
|
94 |
|
95 |
(opts, args) = parser.parse_args(args) |
96 |
|
97 |
return (opts, args)
|
98 |
|
99 |
|
100 |
def request_forwarding(sport, daddr, dport, password, |
101 |
auth_user, auth_password, |
102 |
server_address=DEFAULT_SERVER_ADDRESS, |
103 |
server_port=DEFAULT_SERVER_PORT, enable_ssl=False,
|
104 |
ca_cert=None, strict=False): |
105 |
""" Connect to vncauthproxy and request a VNC forwarding.
|
106 |
|
107 |
@type sport: int
|
108 |
@param sport: Source port for incoming connections
|
109 |
(0 for automatic allocation)"
|
110 |
@type daddr: str
|
111 |
@param daddr: Destination address for the forwarding
|
112 |
@type dport: int
|
113 |
@param dport: Destination port for the forwarding
|
114 |
@type password: str
|
115 |
@param password: VNC server auth password
|
116 |
@type auth_user: str
|
117 |
@param auth_user: vncauthproxy user
|
118 |
@type auth_password: str
|
119 |
@param auth_password: vncauthproxy password
|
120 |
@type server_address: str
|
121 |
@param server_address: Listening address for the vncauthproxy daemon
|
122 |
(default: 127.0.0.1)
|
123 |
@type server_port: int
|
124 |
@param server_port: Listening port for the vncauthproxy daemon
|
125 |
(default: 24999)
|
126 |
@type enable_ssl: bool
|
127 |
@param enable_ssl: Enable / disable SSL on the control socket
|
128 |
@type ca_cert: str
|
129 |
@param ca_cert: Path to the CA cert file
|
130 |
@type strict: bool
|
131 |
@param strict: Enable strict cert checking for SSL
|
132 |
@rtype: dict
|
133 |
@return: Server response in dict / JSON format
|
134 |
|
135 |
"""
|
136 |
|
137 |
# Sanity check
|
138 |
if strict and not ca_cert: |
139 |
raise Exception("strict requires ca-cert to be set") |
140 |
if not enable_ssl and (strict or ca_cert): |
141 |
logger.warning("strict or ca-cert set, but ssl not enabled")
|
142 |
|
143 |
req = { |
144 |
"source_port": int(sport), |
145 |
"destination_address": daddr,
|
146 |
"destination_port": int(dport), |
147 |
"password": password,
|
148 |
"auth_user": auth_user,
|
149 |
"auth_password": auth_password,
|
150 |
} |
151 |
|
152 |
last_error = None
|
153 |
retries = 5
|
154 |
while retries:
|
155 |
# Initiate server connection
|
156 |
for res in socket.getaddrinfo(server_address, server_port, |
157 |
socket.AF_UNSPEC, |
158 |
socket.SOCK_STREAM, 0,
|
159 |
socket.AI_PASSIVE): |
160 |
af, socktype, proto, canonname, sa = res |
161 |
try:
|
162 |
server = socket.socket(af, socktype, proto) |
163 |
except socket.error:
|
164 |
server = None
|
165 |
continue
|
166 |
|
167 |
if enable_ssl:
|
168 |
reqs = ssl.CERT_NONE |
169 |
if strict:
|
170 |
reqs = ssl.CERT_REQUIRED |
171 |
elif ca_cert:
|
172 |
reqs = ssl.CERT_OPTIONAL |
173 |
|
174 |
server = ssl.wrap_socket(server, cert_reqs=reqs, |
175 |
ca_certs=ca_cert, |
176 |
ssl_version=ssl.PROTOCOL_TLSv1) |
177 |
|
178 |
server.settimeout(60.0)
|
179 |
|
180 |
try:
|
181 |
server.connect(sa) |
182 |
except socket.error as err: |
183 |
server.close() |
184 |
server = None
|
185 |
retries -= 1
|
186 |
last_error = err |
187 |
continue
|
188 |
|
189 |
retries = 0
|
190 |
break
|
191 |
|
192 |
sleep(0.2)
|
193 |
|
194 |
if server is None: |
195 |
raise Exception("Failed to connect to server: %s" % last_error) |
196 |
|
197 |
server.send(json.dumps(req)) |
198 |
|
199 |
response = server.recv(1024)
|
200 |
server.close() |
201 |
res = json.loads(response) |
202 |
return res
|
203 |
|
204 |
|
205 |
if __name__ == '__main__': |
206 |
logger.addHandler(logging.StreamHandler()) |
207 |
|
208 |
(opts, args) = parse_arguments(sys.argv[1:])
|
209 |
|
210 |
# Mandatory arguments
|
211 |
if opts.password is None: |
212 |
sys.stderr.write("The password argument is mandatory.\n")
|
213 |
sys.exit(1)
|
214 |
if opts.daddr is None: |
215 |
sys.stderr.write("The daddr argument is mandatory.\n")
|
216 |
if opts.dport is None: |
217 |
sys.stderr.write("The dport argument is mandatory.\n")
|
218 |
if opts.auth_user is None: |
219 |
sys.stderr.write("The auth_user argument is mandatory.\n")
|
220 |
if opts.auth_password is None: |
221 |
sys.stderr.write("The auth_password argument is mandatory.\n")
|
222 |
|
223 |
res = request_forwarding(sport=opts.sport, daddr=opts.daddr, |
224 |
dport=opts.dport, password=opts.password, |
225 |
auth_user=opts.auth_user, |
226 |
auth_password=opts.auth_password, |
227 |
enable_ssl=opts.enable_ssl, ca_cert=opts.ca_cert, |
228 |
strict=opts.strict) |
229 |
|
230 |
reason = None
|
231 |
if 'reason' in res: |
232 |
reason = 'Reason: %s\n' % res['reason'] |
233 |
sys.stderr.write("Forwaring %s -> %s:%s: %s\n%s" % (res['source_port'], |
234 |
opts.daddr, opts.dport, |
235 |
res['status'], reason))
|
236 |
|
237 |
if res['status'] == "OK": |
238 |
sys.exit(0)
|
239 |
else:
|
240 |
sys.exit(1)
|