-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_websocket_vnc_proxy.c
More file actions
1587 lines (1356 loc) · 56.8 KB
/
mod_websocket_vnc_proxy.c
File metadata and controls
1587 lines (1356 loc) · 56.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012 Flexiant Ltd
*
* Written by Alex Bligh, based upon the dumb_increment_protocol
* example for apache-websocket, written by self.disconnect
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This apache module is a general purpose tcp proxy for apache
* designed to work with libwebsockets. However, it has various
* optimisations for vnc connections. The service to which it connects
* can either be defined in a static manner, or can be looked up in
* a database. The service also supports connecting to an intermediate
* secondary proxy.
*
* Intermediate secondary proxy
* ============================
*
* The system OPTIONALLY allows for use with an intermediate proxy
* which will forward the onbound connection to its ultimate destination
* This feature is activated by the WebSocketTcpProxySendInitialData directive,
* so called as the outbound session has initial data added which is a cryptographically
* signed instruction to the cluster proxy as to where to forward the
* onbound TCP session.
*
* The data sent consists of an XML object containing:
* - the session key (or a generated one if there is none)
* - a parameter block (if database access is set up) consisting of all the data
* supplied by the database search using the fiels names and values supplied therein
* - a hash of the session key, the parameter block, a nonce supplied by
* the secondary proxy, and a shared secret.
*
* The hash allows the secondary proxy to verify that the incoming connection
* has been supplied by a person in posession of the shared secret.
*
* Database lookups
* ================
*
* The system OPTIONALLY allows for dyanmic configuration of vnc port forwards looked
* up with an arbitrary key. The key can be composed of any base64 letters plus
* underscores and minus sigs.
*
* The user can specify a statement (likely to be SELECT
* in an SQL environment) which returns data providing the vnc proxy paaramters
* associated with that particular hardware address.
*
* The query is the SELECT statement passed to the SQL backend, into which
* the following are substituted sprintf style paramaters. Currently only
* one parameter is passed, thus use
* %s : the key
* %% : a percent sign
*
* The query does not need a trailing semicolon. Be careful that quotes in the
* query do not interfere with quotes in the config file.
*
* If no rows are returned, the connection will be rejected. If more than one
* row is returned, the first row will be used to connect to.
*
* Columns returned should be
* * the IP address to connect to (connecthost)
* * the port numebr to connect to (connectport)
* * Any other columns you want sent in the initial data
*
* For example, if the table 'vnc' contained columns vncnodehost, vncnodeport
* vncclusterhost, vncclusterport, and vncclusterkey, corresponding to ip and port
* address of the node, the ip and port of the cluster proxy, and the key,
* the following query might be used:
*
* SELECT vncnodehost AS 'nodehost', vncnodeport AS 'nodeport',
* vncclusterhost AS 'connecthost', vncclusterport AS 'connectport'
* FROM vnc WHERE vnckey='%s'
*
* In which case the initial data would include entries for
* nodehost
* nodeport
* host
* port
*
* The nonce sent from the intermediate proxy will be added.
*/
#include <stdio.h>
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "apr_thread_proc.h"
#include "apr_base64.h"
#include "apr_strings.h"
#include "apr_dbd.h"
#include "apr_random.h"
#include "apr_xml.h"
#include "mod_dbd.h"
#include "websocket_plugin.h"
#define VNCHEADERMAGIC 0xAB15AB1E
#define VNCGREETINGMAGIC 0x564e4321
module AP_MODULE_DECLARE_DATA websocket_vnc_proxy_module;
typedef struct _vncheader {
uint32_t magic;
uint16_t version;
uint16_t length;
} __attribute__ ((packed)) vncheader;
typedef struct
{
char *location;
const char *host;
const char *port;
const char *protocol;
const char *secret;
const char *localip;
int base64;
int sendinitialdata;
int timeout;
int guacamole;
char *query;
} websocket_tcp_proxy_config_rec;
typedef struct _TcpProxyData
{
const WebSocketServer *server;
apr_pool_t *pool;
apr_pool_t *threadpool;
apr_allocator_t *threadallocator;
apr_thread_t *thread;
apr_socket_t *tcpsocket;
apr_pollset_t *sendpollset;
int active;
int base64;
int sendinitialdata;
int timeout;
int guacamole;
char *host;
char *port;
char *localip;
char *initialdata;
char *secret;
char *key;
char *nonce;
apr_hash_t * paramhash;
apr_dbd_prepared_t *statement;
websocket_tcp_proxy_config_rec *conf;
} TcpProxyData;
/* optional functions - look it up once in post_config */
static ap_dbd_t *(*tcp_proxy_dbd_acquire_fn)(request_rec*) = NULL;
static void (*tcp_proxy_dbd_prepare_fn)(server_rec*, const char*, const char*) = NULL;
static const char *tcp_proxy_dbd_prepare(cmd_parms *cmd, void *cfg, const char *query)
{
static unsigned int label_num = 0;
char *label;
if (tcp_proxy_dbd_prepare_fn == NULL) {
tcp_proxy_dbd_prepare_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_prepare);
if (tcp_proxy_dbd_prepare_fn == NULL) {
return "You must load mod_dbd to enable DBD functions";
}
tcp_proxy_dbd_acquire_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
}
label = apr_psprintf(cmd->pool, "tcp_proxy_dbd_%d", ++label_num);
tcp_proxy_dbd_prepare_fn(cmd->server, query, label);
/* save the label here for our own use */
return ap_set_string_slot(cmd, cfg, label);
}
static apr_status_t tcp_proxy_query_key (request_rec * r, TcpProxyData * tpd, apr_pool_t * mp)
{
/* Check we have a config and a datbase connection */
apr_status_t rv;
const char *dbd_password = NULL;
apr_dbd_prepared_t *statement = NULL;
apr_dbd_results_t *res = NULL;
apr_dbd_row_t *row = NULL;
char *c;
if (!tpd || !tpd->conf)
return (APR_BADARG);
websocket_tcp_proxy_config_rec *conf = tpd->conf;
/* If no query is specified, we are fine */
if (!conf->query)
return APR_SUCCESS;
/* Check we have a real key */
if (!tpd->key || !*tpd->key)
return APR_BADARG;
/* Check the key is valid */
for (c = tpd->key; *c; c++) {
if (!isalnum(*c))
switch (*c) {
case ',':
case '-':
case '+':
case '=':
case '/':
case '_':
break;
default:
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "DBI: bad key");
return APR_BADARG;
}
}
ap_dbd_t *dbd = tcp_proxy_dbd_acquire_fn(r);
if (!dbd) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Failed to acquire database connection to look up "
"key '%s'", tpd->key);
return APR_BADARG;
}
statement = apr_hash_get(dbd->prepared, conf->query, APR_HASH_KEY_STRING);
if (!statement) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"A prepared statement could not be found for "
"AuthDBDUserPWQuery with the key '%s'", conf->query);
return APR_BADARG;
}
if (apr_dbd_pvselect(dbd->driver, mp, dbd->handle, &res, statement,
0, tpd->key) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Query execution error looking up '%s' "
"in database", tpd->key);
return APR_BADARG;
}
int found = 0;
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_query_key: running through results");
for (rv = apr_dbd_get_row(dbd->driver, mp, res, &row, -1);
rv != -1;
rv = apr_dbd_get_row(dbd->driver, mp, res, &row, -1)) {
if (rv != 0) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"Error retrieving results while looking up '%s' "
"in database", tpd->key);
return APR_BADARG;
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_query_key: found a matching line");
if (!found) {
char *host = NULL;
char *port = NULL;
const char *fieldname;
int i = 0;
if (NULL != (tpd->paramhash = apr_hash_make(mp))) {
for (fieldname = apr_dbd_get_name(dbd->driver, res, i);
fieldname != NULL;
fieldname = apr_dbd_get_name(dbd->driver, res, i)) {
const char *fieldvalue = apr_dbd_get_entry(dbd->driver, row, i++);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_query_key: found field '%s'='%s'",
fieldname,
fieldvalue);
if (fieldvalue) {
apr_hash_set(tpd->paramhash, apr_pstrdup(mp, fieldname), APR_HASH_KEY_STRING, apr_pstrdup(mp, fieldvalue));
if (!strcmp(fieldname, "connecthost"))
host = apr_pstrdup(mp, fieldvalue);
else if (!strcmp(fieldname, "connectport"))
port = apr_pstrdup(mp, fieldvalue);
}
}
}
if (tpd->paramhash && host && port) {
tpd->host = host;
tpd->port = port;
found = 1;
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_query_key: found parm host=%s port=%s",
tpd->host?tpd->host:"(none)",
tpd->port?tpd->port:"(none)");
/* we can't break out here or row won't get cleaned up */
}
}
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_query_key: found=%d", found);
if (!found)
return APR_BADARG;
return APR_SUCCESS;
}
/**
* return the 'key=XXX' parameter
*/
static char *tcp_proxy_get_key(request_rec * r,
TcpProxyData * tpd, apr_pool_t * mp)
{
const char *args = r->args;
const char *param;
if (!args)
return NULL;
while (*args) {
/* get the next parameter */
param = ap_getword(mp, &args, '&');
if (!param)
return NULL;
if (!strncmp(param, "key=", 4)) {
return apr_pstrdup(mp, param + 4);
}
}
return NULL;
}
/**
* Authenticate the connection. This can modify tpd to change (for instance)
* the host or port to connect to, or set up initialdata. For now it is a stub.
*/
static apr_status_t tcp_proxy_do_authenticate(request_rec * r,
TcpProxyData * tpd,
apr_pool_t * mp)
{
if (!tpd->conf)
return APR_BADARG;
tpd->key = tcp_proxy_get_key(r, tpd, mp);
if (!tpd->conf->query && !tpd->key) {
/* key is option if no query */
tpd->key = apr_pstrdup(mp, "");
}
if (!tpd->key) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_do_authenticate: no key");
return APR_BADARG;
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_do_authenticate: key is '%s'",
tpd->key);
/* Look up tpd->host, tpd->port, and other parameters using key */
if (APR_SUCCESS != tcp_proxy_query_key(r, tpd, mp)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_do_authenticate: query_key failed");
return APR_BADARG;
}
if (!(tpd->host && tpd->port)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_do_authenticate: missing parm host=%s port=%s",
tpd->host?tpd->host:"(none)",
tpd->port?tpd->port:"(none)"
);
return APR_BADARG;
}
return APR_SUCCESS;
}
/**
* Send the initial data - this would normally be generated by tcp_proxy_do_authenticate
*/
static apr_status_t tcp_proxy_send_initial_data(request_rec * r,
TcpProxyData * tpd,
apr_pool_t * mp)
{
vncheader header;
apr_status_t rv;
apr_size_t hlen = sizeof (vncheader);
apr_size_t len;
if (!tpd->sendinitialdata)
return APR_SUCCESS;
rv = apr_socket_recv(tpd->tcpsocket, (void *)&header, &hlen);
if (rv != APR_SUCCESS)
return rv;
if (hlen != sizeof (vncheader)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "tcp_proxy_send_initial_data: could not read whole header");
return APR_BADARG;
}
if (ntohl (header.magic) != VNCGREETINGMAGIC) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "tcp_proxy_send_initial_data: bad magic");
return APR_BADARG;
}
if (ntohs (header.version) != 1) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "tcp_proxy_send_initial_data: bad version");
return APR_BADARG;
}
len = ntohs (header.length);
if (len>1024) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "tcp_proxy_send_initial_data: bad length");
return APR_BADARG;
}
if (NULL == (tpd->nonce = apr_palloc(mp, len+1)))
return APR_BADARG;
tpd->nonce[len] = 0; /* zero terminate */
rv = apr_socket_recv(tpd->tcpsocket, (void *)tpd->nonce, &len);
if (rv != APR_SUCCESS)
return rv;
if (len != ntohs (header.length)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "tcp_proxy_send_initial_data: could not read whole header (2)");
return APR_BADARG;
}
/* ignore /r /n and anything after whitespace */
char *p;
for (p=tpd->nonce; *p; p++) {
if (isspace(*p)) {
*p=0;
break;
}
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "tcp_proxy_send_initial_data: read nonce of '%s'", tpd->nonce);
if (!(tpd->key && tpd->host && tpd->port && tpd->nonce)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_send_initial_data: missing parm key=%s host=%s port=%s nonce=%s",
tpd->key?tpd->key:"(none)",
tpd->host?tpd->host:"(none)",
tpd->port?tpd->port:"(none)",
tpd->nonce?tpd->nonce:"(none)"
);
return APR_BADARG;
}
char *tohash =
apr_psprintf(mp, "%s %s %s", tpd->key, tpd->secret, tpd->nonce);
char *params = apr_pstrdup(mp, "");
if (tpd->paramhash) {
apr_hash_index_t *hi;
for (hi = apr_hash_first(mp, tpd->paramhash); hi; hi = apr_hash_next(hi)) {
char * key = NULL;
char * value = NULL;;
apr_hash_this(hi, (const void **)&key, NULL, (void **)&value);
if (key && value) {
tohash = apr_psprintf(mp, "%s %s %s", tohash, key, value);
const char * quotedstring = apr_xml_quote_string(mp, value, 0);
params = apr_psprintf(mp, "%s<%s>%s</%s>", params, key, quotedstring, key);
}
}
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "tcp_proxy_send_initial_data: Data to hash is '%s'", tohash);
char hashdata[32];
apr_crypto_hash_t *h = apr_crypto_sha256_new(mp);
h->init(h);
h->add(h, tohash, strlen(tohash));
h->finish(h, hashdata);
char hash[32*2+1];
int i;
for (i=0; i<32; i++) {
sprintf(hash+i*2, "%02hhx", hashdata[i]);
}
hash[32*2]=0;
tpd->initialdata = apr_psprintf(mp, "<vncconnection>"
"<key>%s</key><hash>%s</hash><params>%s</params>"
"</vncconnection>",
tpd->key, hash, params);
if (!tpd->initialdata) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"tcp_proxy_send_initial_data: could not generate initial data");
return APR_BADARG;
}
len = strlen(tpd->initialdata);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_send_initial_data: initial data is '%s'",
tpd->initialdata);
header.magic = htonl(VNCHEADERMAGIC);
header.version = htons(1);
header.length = htons(len);
hlen = sizeof (vncheader);
rv = apr_socket_send(tpd->tcpsocket, (void *)&header, &hlen);
if (rv != APR_SUCCESS)
return rv;
return apr_socket_send(tpd->tcpsocket, tpd->initialdata, &len);
}
/**
* Shutdown the tcpsocket which will cause further read/writes
* in either direction to fail
*/
static void tcp_proxy_shutdown_socket(TcpProxyData * tpd)
{
if (tpd && tpd->tcpsocket)
apr_socket_shutdown(tpd->tcpsocket, APR_SHUTDOWN_READWRITE);
}
/**
* Connect to the remote host
*/
static apr_status_t tcp_proxy_do_tcp_connect(request_rec * r,
TcpProxyData * tpd,
apr_pool_t * mp)
{
apr_sockaddr_t *sa;
apr_socket_t *s;
apr_status_t rv;
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_do_tcp_connect: connect to host %s port %s",
tpd->host, tpd->port);
int port = atoi(tpd->port);
rv = apr_sockaddr_info_get(&sa, tpd->host, APR_INET, port, 0, mp);
if (rv != APR_SUCCESS) {
return rv;
}
if (!port) {
rv = apr_getservbyname(sa, tpd->port);
if (rv != APR_SUCCESS) {
return rv;
}
}
rv = apr_socket_create(&s, sa->family, SOCK_STREAM, APR_PROTO_TCP, mp);
if (rv != APR_SUCCESS) {
return rv;
}
apr_interval_time_t timeout = APR_USEC_PER_SEC * ((tpd->timeout)?tpd->timeout:30);
if (tpd->localip) {
apr_sockaddr_t *localsa;
rv = apr_sockaddr_info_get(&localsa, tpd->localip, APR_UNSPEC, 0 /*port*/, 0, mp);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_do_tcp_connect: could not get addr to bind to local address %s",
tpd->localip);
apr_socket_close(s);
return rv;
}
if ((rv = apr_socket_bind(s, localsa)) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"tcp_proxy_do_tcp_connect: could not bind to local address %s",
tpd->localip);
apr_socket_close(s);
return rv;
}
}
/* it is a good idea to specify socket options explicitly.
* in this case, we make a blocking socket with timeout. */
apr_socket_opt_set(s, APR_SO_NONBLOCK, 0);
apr_socket_opt_set(s, APR_SO_KEEPALIVE, 1);
apr_socket_timeout_set(s, timeout);
rv = apr_socket_connect(s, sa);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"Cannot connect to host %s port %s",
tpd->host, tpd->port);
apr_socket_close(s);
return rv;
}
/* Set it to be blocking to start off with */
apr_socket_opt_set(s, APR_SO_NONBLOCK, 0);
apr_socket_opt_set(s, APR_SO_KEEPALIVE, 1);
apr_socket_timeout_set(s, timeout);
tpd->tcpsocket = s;
return APR_SUCCESS;
}
void guacdump (apr_pool_t * p, char * msg, char * buf, size_t start, size_t end)
{
size_t s = end-start+1;
char * b = malloc(s);
if (b) {
memcpy(b, buf+start, s-1);
b[s-1]=0;
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, p, "%s: '%s'", msg, b);
free (b);
}
}
/* This function READS from the tcp socket and WRITES to the web socket */
/* We will not use ap_log_error in this functin because of potential lack of thread
* safety on the allocator. Instead, we shall use ap_log_perror
*/
void *APR_THREAD_FUNC tcp_proxy_run(apr_thread_t * thread, void *data)
{
char buffer[64];
apr_status_t rv;
TcpProxyData *tpd = (TcpProxyData *) data;
if (!tpd)
return NULL;
request_rec *r = (tpd->server)->request(tpd->server);
apr_interval_time_t timeout = APR_USEC_PER_SEC * ((tpd->timeout)?tpd->timeout:30);
apr_pollset_t * recvpollset = NULL;
if ((APR_SUCCESS != (rv = apr_pollset_create (&recvpollset, 32, tpd->threadpool, APR_POLLSET_THREADSAFE))) ||
!recvpollset) {
ap_log_perror(APLOG_MARK, APLOG_DEBUG, rv, tpd->threadpool, "tcp_proxy_run pollset create failed");
return NULL;
}
apr_pollfd_t recvpfd = { tpd->threadpool, APR_POLL_SOCKET, APR_POLLIN, 0, { NULL }, NULL };
recvpfd.desc.s = tpd->tcpsocket;
apr_pollset_add(recvpollset, &recvpfd);
if (!tpd->guacamole) {
/* Non-guacamole mode - buffer as much as we can */
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool, "tcp_proxy_run start");
#define WSTCPBUFSIZ 16384
#define WSTCPCBUFSIZ ((WSTCPBUFSIZ*4/3)+5)
#define GUARDBYTES 64
char buf[WSTCPBUFSIZ];
char cbuf[WSTCPCBUFSIZ];
apr_size_t got=0;
/* Keep sending messages as long as the connection is active */
while (tpd->active && tpd->tcpsocket) {
/* we can read an entire buffer length, less what we have got so far */
apr_size_t len = sizeof(buf) - got;
const apr_pollfd_t *ret_pfd = NULL;
apr_int32_t num = 0;
rv = apr_pollset_poll(recvpollset, got?1000:timeout, &num, &ret_pfd);
if (!(tpd->active && tpd->tcpsocket)) {
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run quitting as connection has been marked inactive");
break;
}
if (num<=0) {
/* We've got nothing to do */
if (APR_STATUS_IS_TIMEUP(rv)) {
len=0;
goto disgorgeandcontinue;
}
if (rv == APR_SUCCESS) {
/* Poll returned success, but no descriptors were ready. Very odd */
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool, "tcp_proxy_run: sleeping 2");
usleep(10000); /* this should not happen */
}
ap_log_perror(APLOG_MARK, APLOG_DEBUG, rv, tpd->threadpool, "tcp_proxy_run: poll returned an error");
break;
}
rv = apr_socket_recv(tpd->tcpsocket, buf+got, &len);
/* recv can return data *AND* an error - deal with data first*/
got+=len;
disgorgeandcontinue:
/* if the buffer is more than half full, or we had nothing to read */
if ((got > WSTCPBUFSIZ/2) || (num<=0)) {
size_t towrite = got;
char *wbuf = buf;
/* Base64 encode it if necessary */
if (tpd->base64) {
towrite = apr_base64_encode(cbuf, buf, towrite);
wbuf = cbuf;
}
size_t written =
tpd->server->send(tpd->server, MESSAGE_TYPE_TEXT /* FIXME */ ,
(unsigned char *) wbuf, towrite);
if (written != towrite) {
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run send failed, wrote %lu bytes of %lu",
(unsigned long) written, (unsigned long) got);
break;
}
got=0;
}
if (APR_STATUS_IS_TIMEUP(rv))
continue;
if (rv == APR_SUCCESS) {
if (!len) {
/* Hmm, we got success, or timeup in which case we want to loop
* but we might get no data again, so we wait just in case - there seem
* to be conditions where this happens in a circumstance where a repeat
* read produces the same error, so sleep so we don't busy-wait CPU
*/
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool, "tcp_proxy_run: sleeping");
usleep(10000); /* this should not happen */
}
continue;
}
char s[1024];
apr_strerror(rv, s, sizeof(s));
ap_log_perror(APLOG_MARK, APLOG_DEBUG, rv, tpd->threadpool,
"tcp_proxy_run apr_socket_recv failed len=%lu rv=%d, %s",
(unsigned long) len, rv, s);
break;
}
tcp_proxy_shutdown_socket(tpd);
tpd->server->close(tpd->server);
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool, "tcp_proxy_run stop");
} else {
/* We're in guacamole mode. Guacamole (unfortunately) requires that its messages
* are not broken across websocket frames. This means we need to understand the
* underlying protocol as we have no idea what tcp buffering might have done on
* the way.
*
* For now we will use one websocket message per guacamole instruction.
*
* Guacamole protocol is described at
* http://guac-dev.org/Guacamole%20Protocol
*
* In essence it is a text base protocol made up of instructions. Each instruction is
* a comma delimited list followed by a terminating semicolon. This semicolon is
* immediately followed by the next instruction. Each instruction takes the form
* OPCODE,ARG1,ARG2,...;
* Each OPCADE and ARG can contain any character (including a semicolon) so we can't
* just look for semicolos. But fortunately each OPCODE or ARG takes the form
* LENGTH.VALUE
* where LENGTH is a decimal integer length of the VALUE field (excluding the
* dot). The VALUE field is not null terminated. So, for instance:
* 4.size,1.0,4.1024,3.768;
*
* We don't use apache memory handling here because of the lack of realloc and/or
* explicit free.
*/
/*
* Buffer arrangement
*
* 0 bufwritep bufreadp bufsize
* V V V V
* XXXXXXXXXXXXXXDDDDDDDDDDDDDDDDDD------------|
* | | | |
* | | | \_ Free memory
* | \ \
* | \ \_____ Data yet to be written to websocket
* | \
* buf \______ Data already written to websocket
*/
size_t bufsize = 0;
size_t bufwritep = 0;
size_t bufreadp = 0;
const size_t minread = 1024;
const size_t maxbufsize = 16*1024*1024;
char * buf = NULL;
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool, "tcp_proxy_run start guacamole mode");
/* Keep sending messages as long as the connection is active */
while (tpd->active && tpd->tcpsocket) {
if ((bufreadp > bufsize) || (bufwritep > bufreadp)) {
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run guacamole pointer error, buf=%lx bufsize=%lu bufreadp=%lu bufwritep=%lu", (intptr_t)buf, bufsize, bufreadp, bufwritep);
goto guacerror;
}
/* First let's see if we've got a completely empty buffer */
if (bufreadp == bufwritep) {
/* If so, junk all the data written to the websocket without
* reallocating the buffer */
bufreadp = 0;
bufwritep = 0;
if (bufsize > minread) {
/* The buffer was grown, and now is empty, so we might as well free it
* up to free memory, which means it will be reallocated down below
*/
free(buf);
buf = NULL;
bufsize=0;
}
}
/* We know we need to read at least minread bytes
* so the easy case is that they just fit in the current buffer
*/
if (bufsize-bufreadp < minread) {
/* Right, we can't fit it in the current buffer. Where
* bufindex > 0 we've got current data, so we'll
* reallocate and expunge that first
*/
if (bufwritep > 0) {
char * newbuf = malloc(bufsize + GUARDBYTES);
if (!newbuf) {
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run could not allocate guacamole buffer");
goto guacerror;
}
if (buf && (bufreadp > bufwritep))
memcpy(newbuf, buf+bufwritep, bufreadp-bufwritep);
bufreadp -= bufwritep;
bufwritep = 0;
if (buf)
free (buf);
buf = newbuf;
}
/* We now know bufwritep is zero, i.e. there is no data that has
* already been written hanging around. So lets see whether we
* can do a read of length minread now
*/
if (bufsize-bufreadp < minread) {
/* No we can't, so we straightforwardly need a larger buffer.
* (a buffer might not have been allocated yet)
*/
size_t newbufsize = bufsize * 2; /* make sure we double the size of the buffer */
if (newbufsize > maxbufsize)
newbufsize = maxbufsize; /* but don't make it larger than the maximum */
if (newbufsize < bufreadp + minread) /* Make it large enough for the read we need */
newbufsize = bufreadp + minread; /* Note this is how the initial size is set */
if ((newbufsize > maxbufsize) || (newbufsize < bufsize))
{
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run guacamole buffer grew to illegal size");
goto guacerror;
}
/*
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run expanding guacamole buffer to %lu bytes", newbufsize);
*/
char * newbuf = realloc (buf, newbufsize + GUARDBYTES); /* realloc when buf in NULL is a malloc */
if (!newbuf) {
/* remember to free buf */
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run could not reallocate guacamole buffer");
goto guacerror;
}
buf = newbuf;
bufsize = newbufsize;
}
}
/* Check we now have a buffer and sace to read into - this should always be the case */
if (!buf || (bufsize-bufreadp < minread)) {
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run guacamole logic error, buf=%lx bufsize=%lu bufread=%lu minread=%lu", (intptr_t)buf, bufsize, bufreadp, minread);
goto guacerror;
}
apr_size_t len;
while (1) {
/* Of course we may be able to read far more than minread, so let's go for that */
len = bufsize - bufreadp;
const apr_pollfd_t *ret_pfd = NULL;
apr_int32_t num = 0;
rv = apr_pollset_poll(recvpollset, timeout, &num, &ret_pfd);
if (!(tpd->active && tpd->tcpsocket)) {
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run quitting guacamole mode as connection has been marked inactive");
goto guacdone;
}
if (APR_STATUS_IS_TIMEUP(rv)) {
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run quitting guacamole mode as ws poll has timed out");
goto guacdone;
}
if (rv != APR_SUCCESS) {
ap_log_perror(APLOG_MARK, APLOG_DEBUG, rv, tpd->threadpool, "tcp_proxy_run: poll returned an error");
goto guacerror;
}
if (num<=0) {
/* Poll returned success, but no descriptors were ready. Very odd */
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool, "tcp_proxy_run: sleeping guac 2");
usleep(10000); /* this should not happen */
continue;
}
rv = apr_socket_recv(tpd->tcpsocket, buf+bufreadp, &len);
if (APR_STATUS_IS_EAGAIN(rv)) { /* we have no data to read yet, we should try rereading */
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool, "tcp_proxy_run: sleeping guac 3");
usleep(10000);
continue;
}
if (APR_STATUS_IS_EOF(rv) || !len) {
/* we lost the TCP session */
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run quitting guacamole mode as TCP connection closed");
goto guacdone;
}
/* We have data */
break;
}
bufreadp += len;
/*
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run ***guac read bytes len=%lu bufwrirep=%lu bufreadpp=%lu", len, bufreadp, bufwritep);
*/
/* So now we have an instruction starting at bufwritep, and terminating either before
* bufreadp (in which case we can write it and look for more) or possibly not terminating
* in which case we need to loop around again to read more data
*/
size_t p = bufwritep;
size_t lastwholecommand = bufwritep;
size_t towrite = 0;
while (p < bufreadp) {
/* Skip along until we find a semicolon */
int write=0;
while (!write) {
/*
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, tpd->threadpool,
"tcp_proxy_run ***guac decode loop p=%lu bufwrirep=%lu bufreadpp=%lu", p, bufreadp, bufwritep);
guacdump(tpd->threadpool, "tcp_proxy_run ***guac string is", buf, p, bufreadp);
*/
if (p >= bufreadp)
goto writelastwholecommand;
size_t arglen = 0;
while (isdigit(buf[p])) {
arglen = arglen * 10 + ( buf[p++] - '0');
if (p >= bufreadp)
goto writelastwholecommand;
}
/* arglen must be non-zero, and we know buf[p] is valid (as p<bufreadp) and must point
* to the dot
*/
if (!arglen || (buf[p] != '.')) {