-
Notifications
You must be signed in to change notification settings - Fork 1
/
fvncd.pl
executable file
·358 lines (291 loc) · 8.85 KB
/
fvncd.pl
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
#!/usr/bin/perl -T
use strict;
use warnings;
use File::Basename;
use Getopt::Std;
use IO::Socket::IP;
use POSIX qw( setsid);
use Sys::Syslog qw( :standard :macros );
use constant LISTENER_PORT => 5900;
use constant MAX_CONNECTIONS => 20;
use constant SYSLOG_FACILITY => 'LOG_LOCAL0';
use constant SYSLOG_IDENT => 'fvncd';
use constant SYSLOG_PRIORITY => 'LOG_INFO';
use constant SYSLOG_SERVER => '127.0.0.1';
use constant PID_FILE => '/run/fvnc/fvncd.pid';
use constant RFB_VERSION => "RFB 003.008\n";
use constant USAGE => "usage: $0 [-h] [-p port] [-6]
-h: this message
-l: listener port (default=" . LISTENER_PORT . ")
-p: pidfile (default=" . PID_FILE . ")
-6: enable IPv6 listener\n";
local $SIG{CHLD} = 'IGNORE'; # to avoid having defunct children around
$|++; # autoflush
getopts( '6hl:p:', \my %opts );
die USAGE if $opts{h};
my $pidfile = $opts{p} || PID_FILE;
# try to safely use pidfile with taint mode
$pidfile = $pidfile =~ m{ \A ( [\w\/\.-]+ ) \z }xms ? $1 : undef;
if ( not defined $pidfile ) {
die "Unexpected characters in pidfile: $pidfile";
}
my $base_dir = dirname $pidfile;
if ( ! -w $base_dir ) {
die "Can't write to base directory: $base_dir";
}
if ( -e $pidfile && ! -w $pidfile ) {
die "existing pidfile not writeable; $pidfile";
}
my $dport = $opts{l} || LISTENER_PORT;
($dport) = $dport =~ m{ \A (\d{1,5}) \z }xms;
die '-p dport setting invalid' if not defined($dport);
die '-p dport setting out of range' if $dport < 1 || $dport > 65535;
die 'root privs required' if $dport < 1024 && $> != 0;
# it is easier to have a single socket listener
# will transform IPV4MAPPED if v6 listener is enabled and client is v4
my $localhost = $opts{6} ? '::' : '0.0.0.0';
daemonize();
my $pid = $$;
create_pid();
local $SIG{TERM} = \&terminate;
my $server = IO::Socket::IP->new(
LocalHost => $localhost,
Reuse => 1,
V6Only => 0,
LocalService => $dport,
Type => SOCK_STREAM,
Listen => MAX_CONNECTIONS,
) or die "Could not open port $dport : $@\n";
# initialize syslog
$Sys::Syslog::host = SYSLOG_SERVER;
openlog( SYSLOG_IDENT, "nodelay,pid", SYSLOG_FACILITY );
my ( $client, $connection );
while ( $client = $server->accept() ) {
my $_pid;
while ( not defined( $_pid = fork() ) ) {
sleep 5;
}
if ($_pid) {
close $client;
next;
}
# see http://stackoverflow.com/questions/28719064/how-to-set-in-perl-recv-timeout-in-my-code
setsockopt( $client, SOL_SOCKET, SO_RCVTIMEO, pack( 'l!l!', 10, 0 ) );
$client->autoflush(1);
my $daddr = v4mapped( $client->sockhost );
my $saddr = v4mapped( $client->peerhost );
my $sport = $client->peerport;
$connection = "saddr: $saddr; sport: $sport; daddr: $daddr; dport: $dport;";
logit( { message => "$connection new connection" } );
# https://github.com/rfbproto/rfbproto/blob/master/rfbproto.rst
my $vnc_version = vnc_handshake();
if ( $vnc_version !~ m{ \A RFB [ ] \d{3} [.] \d{3} [\n] \z }xms ) {
logit(
{
priority => LOG_WARNING,
message => "$connection vnc_version invalid"
}
);
exit 0;
}
$vnc_version =~ s{ [RFB\s]+ }{}gmxs; # xxx.yyy is all we need now
logit( { message => "$connection ProtocolVersion $vnc_version" } );
$vnc_version = normalize_vnc_version($vnc_version);
my %select_version = (
'3.3' => \&vnc_version_3_3,
'3.7' => \&vnc_version_3_7,
'3.8' => \&vnc_version_3_8,
'UNK' => \&vnc_version_unk, # unknown / unsupported version
);
$select_version{$vnc_version}->();
exit 0;
}
terminate();
exit 0;
# http://stackoverflow.com/questions/1518923/how-can-i-create-a-tcp-server-daemon-process-in-perl
sub daemonize {
chdir '/' or die "Can't chdir to /: $!";
open STDIN, '<', '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>', '/dev/null' or die "Can't write to /dev/null: $!";
defined( my $_pid = fork ) or die "Can't fork: $!";
exit if $_pid;
setsid or die "Can't start a new session: $!";
open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
return;
}
sub create_pid {
my $_pid = $$;
open my $fd, '>', $pidfile or die "Can't write $pidfile: $!";
print $fd $_pid;
close $fd or die "Can't close $pidfile: $!";
return;
}
sub terminate {
if ( $$ == $pid ) {
close $server;
closelog();
unlink $pidfile or die "Can't remove $pidfile: $!";
}
return;
}
sub logit {
my ($arg_ref) = @_;
my $priority = $arg_ref->{priority} || SYSLOG_PRIORITY;
my $message = $arg_ref->{message} or return;
syslog( $priority, "%s", $message );
return;
}
# check for an IPv6 V4MAPPED address and convert to dotted quad if found
sub v4mapped {
my $addr = shift or return;
$addr =~ s{ \A ::ffff: }{}ixms;
return $addr;
}
sub vnc_handshake {
my $sent = $client->send(RFB_VERSION);
if ( not defined $sent ) {
logit(
{
priority => LOG_WARNING,
message => "$connection vnc_hankshake send failure"
}
);
exit 0;
}
my $version;
my $received = $client->recv( $version, 12 );
if ( not defined $received ) {
logit(
{
priority => LOG_WARNING,
message => "$connection vnc_handshake recv timeout"
}
);
exit 0;
}
if ( length $version != 12 ) {
logit(
{
priority => LOG_WARNING,
message => "$connection vnc_handshake version length invalid"
}
);
exit 0;
}
return $version;
}
sub normalize_vnc_version {
my $version = shift or return;
return '3.3' if $version eq '003.003';
return '3.7' if $version eq '003.007';
return '3.8' if $version eq '003.008';
# other proprietary versions exist, but we do not currently support them
return 'UNK';
}
sub vnc_version_3_3 {
# default Authentication type None
# XXX: randomize vnc and none auth?
my $security_type = pack( 'N', 0x00000001 );
my $sent = $client->send($security_type);
if ( not defined $sent ) {
logit(
{
priority => LOG_WARNING,
message => "$connection vnc_version_3_3 send failure"
}
);
exit 0;
}
client_init();
return;
}
sub vnc_version_3_7 {
#TODO: return security_type negotiated and then do something
security_negotiation('3_7');
return;
}
sub vnc_version_3_8 {
#TODO: return security_type negotiated and then do something
security_negotiation('3_8');
return;
}
# version is unknown, unsupported, or unreliable
sub vnc_version_unk {
logit(
{
priority => LOG_WARNING,
message => "$connection vnc_version_unk unnsupported client"
}
);
return;
}
sub client_init {
my $shared_flag;
my $received = $client->recv( $shared_flag, 1 );
if ( not defined $received ) {
logit(
{
priority => LOG_WARNING,
message => "$connection client_init recv timeout"
}
);
exit 0;
}
if ( length $shared_flag == 0 ) {
logit(
{
priority => LOG_WARNING,
message => "$connection client_init shared_flag null"
}
);
exit 0;
}
($shared_flag) = unpack( 'C', $shared_flag );
if ( $shared_flag == 0 ) {
logit( { message => "$connection client wants exclusive access" } );
}
else {
logit( { message => "$connection client wants shared access" } );
}
return;
}
sub security_negotiation {
my $version = shift or return;
# bytes are: 3 types, none, vnc, apple remote desktop
#XXX: remove apple remote desktop since we don't support the client ver?
my $sectypes = pack( "N", 0x0301021E );
my $sent = $client->send($sectypes);
if ( not defined $sent ) {
logit(
{
priority => LOG_WARNING,
message => "$connection security_negotiation send failure"
}
);
exit 0;
}
my $sectype;
my $received = $client->recv( $sectype, 1 );
if ( not defined $received ) {
logit(
{
priority => LOG_WARNING,
message => "$connection security_negotiation recv timeout"
}
);
exit 0;
}
if ( length $sectype == 0 ) {
logit(
{
priority => LOG_WARNING,
message =>
"$connection security_negotiation sectype length null"
}
);
exit 0;
}
($sectype) = unpack( 'C', $sectype );
logit( { message => "$connection client sectype $sectype" } );
return;
}