aboutsummaryrefslogtreecommitdiffstats
path: root/kup
blob: f3a5d0f58a3120e710f8a27fcc126b718948115d (plain)
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
#!/usr/bin/perl -T
## -----------------------------------------------------------------------
##
##   Copyright 2011 Intel Corporation; author: H. Peter Anvin
##
##   This program is free software; you can redistribute it and/or
##   modify it under the terms of the GNU General Public License as
##   published by the Free Software Foundation, Inc.; either version 2
##   of the License, or (at your option) any later version;
##   incorporated herein by reference.
##
## -----------------------------------------------------------------------

#
# kernel.org bulk file upload client
#

use strict;
use warnings;
use bytes;
use Encode qw(encode decode);
use File::Spec;
use Config::Simple;

my $blksiz = 1024*1024;

# Global options
my %opt = (
	'rsh'	  => 'ssh -a -x -k -T',
	'host'    => undef,
	'subcmd'  => undef,
	'batch'   => 0,
	'verbose' => 0,
	);

# Read the config file settings and override the above
my $cfg_file = $ENV{'HOME'}.'/.kuprc';
my $cfg = new Config::Simple($cfg_file);

if (defined($cfg)) {
	# Update %opt with cfgfile settings (only rsh, subcmd, and host vars)
	my %cfg_opt = $cfg->vars();

	if (defined($cfg_opt{'default.host'})) {
		$opt{'host'} = $cfg_opt{'default.host'};
	}

	if (defined($cfg_opt{'default.subcmd'})) {
		$opt{'subcmd'} = $cfg_opt{'default.subcmd'};
	}

	if (defined($cfg_opt{'default.rsh'})) {
		$opt{'rsh'} = $cfg_opt{'default.rsh'};
	}
}

# If anyone's ssh is somewhere other than /bin:/usr/bin, they can specify
# where it is by setting up their .kuprc. This also lets us run with -T
# without playing untaint tricks.
#
$ENV{'PATH'} = '/bin:/usr/bin';

if (defined $ENV{'KUP_RSH'}) {
	$opt{'rsh'} = $ENV{'KUP_RSH'};
}
if (defined $ENV{'KUP_HOST'}) {
	$opt{'host'} = $ENV{'KUP_HOST'};
}
if (defined $ENV{'KUP_SUBCMD'}) {
	$opt{'subcmd'} = $ENV{'KUP_SUBCMD'};
}
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};   # Make %ENV safer

# We process the command set twice, once as a dry run and one for real,
# to catch as many errors as early as possible
my @args;
my $real;

# Usage description
sub usage($) {
	my($err) = @_;

	print STDERR "Usage: $0 [global options] command [-- command...]\n";
	print STDERR "\n";
	print STDERR "Global options:\n";
	print STDERR "   -b  --batch            Output command stream to stdout\n";
	print STDERR "   -e  --rsh=command      Send output to command, override KUP_RSH\n";
	print STDERR "   -o  --host=[user@]host Connect to [user@]host, override KUP_HOST\n";
	print STDERR "   -c  --subcmd=cmd       After connecting via ssh, issue this subcommand\n";
	print STDERR "   -v  --verbose          Print each command to stderr as it is sent\n";
	print STDERR "\n";
	print STDERR "Commands:\n";
	print STDERR "   put local_file signature remote_path\n";
	print STDERR "   put --tar [--prefix=] remote_tree ref signature remote_path\n";
	print STDERR "   put --diff remote_tree ref1 ref2 signature remote_path\n";
	print STDERR "   mkdir remote_path\n";
	print STDERR "   mv|move old_path new_path\n";
	print STDERR "   ln|link old_path new_path\n";
	print STDERR "   rm|del|delete old_path\n";
	print STDERR "   ls|dir path...\n";
	print STDERR "   info\n";

	exit $err;
}

# Return true if the supplied string is valid UTF-8 without special
# characters
sub is_clean_string($)
{
	no bytes;
	# use feature 'unicode_strings';		-- is this needed here?

	my($b) = @_;
	my $f = decode('UTF-8', $b, Encode::FB_DEFAULT);

	return 0 if ($f =~ m:[\x{0000}-\x{001f}\x{007f}-\x{00a0}\x{fffd}-\x{ffff}]:);
	return 1;
}

# This returns true if the given argument is a valid filename in its
# canonical form.  Double slashes, relative paths, dot files, control
# characters, and malformed UTF-8 is not permitted.  We cap the length
# of each pathname component to 100 bytes so we can add an extension
# without worrying about it, and the entire pathname to 1024 bytes.
sub is_valid_filename($)
{
	use bytes;

	my($f) = @_;

	return 0 if (!defined($f));		# If undefined, clearly not valid

	return 0 if (length($f) > 1024); # Reject ridiculously long paths
	return 0 if (!is_clean_string($f)); # Reject bad UTF-8 and control characters
	return 0 if ($f !~ m:^/:);		# Reject relative paths
	return 0 if ($f =~ m:/$:);		# Reject paths ending in /
	return 0 if ($f =~ m://:);		# Reject double slashes

	# Reject filename components starting with dot or dash, covers . and ..
	return 0 if ($f =~ m:/[\.\-]:);

	# Reject undesirable filename characters anywhere in the name.
	# This isn't inherently security-critical, and could be tuned if
	# users need it...
	return 0 if ($f =~ m:[\!\"\$\&\'\*\;\<\>\?\\\`\|]:);

	# Make sure we can create a filename after adding .bz2 or similar.
	# We can't use the obvious regexp here, because regexps operate on
	# characters, not bytes.  The limit of 100 is semi-arbitrary, but
	# we shouldn't need filenames that long.
	my $n = 0;
	my $nmax = 0;
	for (my $i = 0; $i < length($f); $i++) {
		my $c = substr($f, $i, 1);
		$n = ($c eq '/') ? 0 : $n+1;
		$nmax = ($n > $nmax) ? $n : $nmax;
	}
	return 0 if ($nmax > 100);

	return 1;
}

# Clean up a filename so that it is more likely to pass the
# canonicalization test.  An optional second argument is used with
# two-filename commands (move, link); it should be the already
# canonicalized first argument.
#
# This can return undef for some invalid pathnames.  This needs to be
# caught by is_valid_filename().
sub canonicalize_path($;$)
{
	my($file, $root) = @_;

	$root = '/' unless defined($root);

	my $tail = '';
	if ($root =~ m:^(.*/)([^/]+)$:) {
		$root = $1;
		$tail = $2;
	}

	if ($root !~ m:^/: || $root !~ m:/$:) {
		die "$0: internal error: non-canonical root\n";
	}

	if ($file !~ m:^/:) {
		$file = $root . $file;
	}
	if ($file =~ m:/$:) {
		$file .= $tail;
	}

	my @path = ();
	my $wasspc = 1;
	# The -1 argument to split means "preserve trailing empty fields"
	foreach my $s (split(/\//, $file, -1)) {
		if ($s eq '' || $s eq '.') {
			$wasspc = 1;
		} elsif ($s eq '..') {
			# If this ran off the root, error
			return undef if (!defined(pop(@path)));
			$wasspc = 1;
		} else {
			push(@path, $s);
			$wasspc = 0;
		}
	}

	# If this ended in a special component, error
	return undef if ($wasspc);

	# The initial '' forces the result to begin with a slash
	return join('/', '', @path);
}

# Parse global options
sub parse_global_options()
{
	while (scalar @ARGV && $ARGV[0] =~ /^-/) {
		my $arg = shift(@ARGV);

		if ($arg eq '-b' || $arg eq '--batch') {
			$opt{'batch'} = 1;
		} elsif ($arg eq '-e' || $arg eq '--rsh' || $arg eq '--ssh') {
			$opt{'rsh'} = shift(@ARGV);
		} elsif ($arg =~ /^--rsh=(.+)$/) {
			$opt{'rsh'} = $1;
		} elsif ($arg eq '-o' || $arg eq '--host') {
			$opt{'host'} = shift(@ARGV);
		} elsif ($arg =~ /^--host=(.+)$/) {
			$opt{'host'} = $1;
		} elsif ($arg eq '-c' || $arg eq '--subcmd') {
			$opt{'subcmd'} = shift(@ARGV);
		} elsif ($arg =~ /^--subcmd=(.+)$/) {
			$opt{'subcmd'} = $1;
		} elsif ($arg eq '-v' || $arg eq '--verbose') {
			$opt{'verbose'}++;
		} elsif ($arg eq '-h' || $arg eq '--help') {
			usage(0);
		} else {
			die "$0: unknown option: $arg\n";
		}
	}
}

# Encode a string
sub url_encode($)
{
	my($s) = @_;

	# Hack to encode an empty string
	return '%' if ($s eq '');

	my $o = '';

	foreach my $c (unpack("C*", $s)) {
		if ($c > 32 && $c < 126 && $c != 37 && $c != 43) {
			$o .= chr($c);
		} elsif ($c == 32) {
			$o .= '+';
		} else {
			$o .= sprintf("%%%02X", $c);
		}
	}

	return $o;
}

# Configure the output stream
sub setup_output()
{
	# In batch mode, we dump the output to stdout so the user can
	# aggregate it best they wish
	unless ($opt{'batch'}) {
		if ($opt{'rsh'} !~ /^([-a-zA-Z0-9._=\@:\s\/]+)$/) {
			die "$0: suspicious KUP_RSH setting\n";
		}
		my $rsh = $1;
		if ($opt{'host'} !~ /^([-a-zA-Z0-9._\@]+)$/) {
			die "$0: suspicious KUP_HOST\n";
		}
		$rsh .= " \Q$1";
		if ($opt{'subcmd'}) {
			if ($opt{'subcmd'} !~ /^([-a-zA-Z0-9_]+)$/) {
				die "$0: suspicious KUP_SUBCMD\n";
			}
			# Add the subcommand for the receiving server
			$rsh .= " \Q$1";
		}
		open(STDOUT, '|-', $rsh)
			or die "$0: cannot execute rsh command ", $rsh, "\n";
	}
	binmode(STDOUT);
}

# Terminate the output process
sub close_output()
{
	$| = 1;						# Flush STDOUT
	unless ($opt{'batch'}) {
		close(STDOUT);
	}
}

# Print a command to STDOUT, and if requested, to STDERR
sub command(@)
{
	if ($real) {
		my $cmd = join(' ', @_);

		print STDERR $cmd, "\n" if ($opt{'verbose'});
		print $cmd, "\n";
	}
}

sub cat_file($$$)
{
	my($cmd, $file, $fmt) = @_;

	if (!defined($fmt)) {
		if ($file =~ /\.((gz|bz2|xz))$/) {
			$fmt = $1;
		} else {
			$fmt = '%';
		}
	}

	my $data;
	open($data, '<', $file)
		or die "$0: cannot open: $file: $!\n";
	if (! -f $data) {
		die "$0: not a plain file: $file\n";
	}
	my $size = -s _;

	binmode($data);

	if ($real) {
		if ($size < 2) {
			# Must be a plain file
			$fmt = '%';
		}

		if (defined($fmt)) {
			command($cmd, $size, $fmt);
		}

		my $blk;
		my $len;

		while ($size) {
			$len = ($size < $blksiz) ? $size : $blksiz;
			$len = read($data, $blk, $len);

			if (!$len) {
				die "$0: premature end of data (file changed?): $file\n";
			}

			print $blk;
			$size -= $len;
		}
	}

	close($data);
}

# PUT command
sub cmd_put()
{
	my $file = shift @args;
	my $file_tail = undef;

	if ($file eq '-t' || $file eq '--tar') {
		# tar hack

		my $remote_tree = shift @args;
		my $prefix = '';

		if ($remote_tree eq '-p' || $remote_tree eq '--prefix') {
			$prefix = shift @args;
			$remote_tree = shift @args;
		} elsif ($remote_tree =~ /^--prefix=(.+)$/) {
			$prefix = $1;
			$remote_tree = shift @args;
		}

		my $ref = shift(@args);

		if (!defined($ref)) {
			usage(1);
		}

		my $xrt = $remote_tree;
		$remote_tree = canonicalize_path($remote_tree);
		if (!is_valid_filename($remote_tree) || $remote_tree !~ /\.git$/) {
			die "$0: invalid path name for git tree: $xrt\n";
		}
		if (!is_clean_string($ref)) {
			die "$0: invalid ref: $ref\n";
		}

		command('TAR', url_encode($remote_tree),
				url_encode($ref), url_encode($prefix));
	} elsif ($file eq '-d' || $file eq '--diff') {
		# diff hack

		my $remote_tree = shift @args;
		my $prefix = '';

		my $ref1 = shift(@args);
		my $ref2 = shift(@args);

		if (!defined($ref2)) {
			usage(1);
		}

		my $xrt = $remote_tree;
		$remote_tree = canonicalize_path($remote_tree);
		if (!is_valid_filename($remote_tree) || $remote_tree !~ /\.git$/) {
			die "$0: invalid path name for git tree: $xrt\n";
		}
		if (!is_clean_string($ref1)) {
			die "$0: invalid ref: $ref1\n";
		}
		if (!is_clean_string($ref2)) {
			die "$0: invalid ref: $ref2\n";
		}

		command('DIFF', url_encode($remote_tree), url_encode($ref1),
				url_encode($ref2));
	} elsif ($file =~ /^-/) {
		die "$0: unknown option to put command: $file\n";
	} else {
		# Plain data blob.  We don't actively attempt to compress it
		# since ssh usually has a layer of compression, but if it is
		# already a compressed file we send it as-is and let the
		# server decompress it.

		cat_file('DATA', $file, undef);

		# Get the local filename without directory
		my($vol, $dir);
		($vol, $dir, $file_tail) = File::Spec->splitpath($file);
	}

	my $sign   = shift @args;
	my $remote = shift @args;

	if (!defined($remote)) {
		usage(1);
	}

	# This allows the user to not specify the filename if it is
	# the same as on the local filesystem by ending the pathname
	# with a slash
	if ($remote =~ m:/$: && defined($file_tail)) {
		$remote .= $file_tail;
	}
	
	my $xrt = $remote;
	$remote = canonicalize_path($remote);
	if (!is_valid_filename($remote)) {
		die "$0: invalid pathname: $xrt\n";
	}

	if ($remote =~ /\.sign$/) {
		die "$0: target filename cannot end in .sign\n";
	}

	# DWIM: .bz2, .xz -> .gz
	$remote =~ s/\.(bz2|xz)$/.gz/;

	cat_file('SIGN', $sign, undef);
	command('PUT', url_encode($remote));
}

# MKDIR command
sub cmd_mkdir()
{
	my $remote = shift @args;

	if (!defined($remote)) {
		usage(1);
	}

	my $xrt = $remote;
	$remote = canonicalize_path($remote);
	if (!is_valid_filename($remote)) {
		die "$0: invalid pathname: $xrt\n";
	}

	if ($remote =~ /\.(sign|gz|bz2|xz)$/) {
		die "$0: a directory name cannot end in .sign, .gz, .bz2, .xz\n";
	}

	command('MKDIR', url_encode($remote));
}

# DELETE command
sub cmd_delete()
{
	my $remote = shift @args;

	if (!defined($remote)) {
		usage(1);
	}

	my $xrt = $remote;
	$remote = canonicalize_path($remote);
	if (!is_valid_filename($remote)) {
		die "$0: invalid pathname: $xrt\n";
	}

	if ($remote =~ /\.sign$/) {
		die "$0: cannot delete .sign files directly\n";
	}

	# DWIM: .bz2, .xz -> .gz
	$remote =~ s/\.(bz2|xz)$/.gz/;

	command('DELETE', url_encode($remote));
}

# MOVE or LINK command
sub cmd_move_link($)
{
	my($cmd) = @_;

	my $from = shift @args;
	my $to   = shift @args;

	if (!defined($to)) {
		usage(1);
	}

	my $xrt = $from;
	$from = canonicalize_path($from);
	if (!is_valid_filename($from)) {
		die "$0: invalid pathname: $xrt\n";
	}

	$xrt = $to;
	$to = canonicalize_path($to, $from);
	if (!is_valid_filename($to)) {
		die "$0: invalid pathname: $xrt\n";
	}

	if ($from =~ /\.sign$/ || $to =~ /\.sign$/) {
		die "$0: cannot explicitly move .sign files\n";
	}
	if ($from =~ /\.(gz|bz2|xz)$/ && $to =~ /\.(gz|bz2|xz)$/) {
		$from =~ s/\.(bz2|xz)$/.gz/;
		$to   =~ s/\.(bz2|xz)$/.gz/;
	} elsif ($from =~ /\.(gz|bz2|xz)$/ || $to =~ /\.(gz|bz2|xz)$/) {
		die "$0: cannot move to or from compressed filenames\n";
	}

	if ($from eq $to) {
		die "$0: moving filename to self: $from\n";
	}

	command($cmd, url_encode($from), url_encode($to));
}

# DIR command (supports arbitrary number of arguments)
sub cmd_dir()
{
	while (defined($args[0]) && $args[0] ne '--') {
		my $d = shift @args;
		$d =~ s:/$::g;
		if ($d ne '') {
			my $xrt = $d;
			$d = canonicalize_path($d);
			if (!is_valid_filename($d)) {
				die "$0: invalid pathname: $xrt\n";
			}
		}
		$d .= '/';

		command('DIR', $d);
	}
}

# INFO command (no arguments)
sub cmd_info()
{
	command('INFO');
}

# Process commands
sub process_commands()
{
	while (1) {
		my $cmd = shift(@args);

		if (!defined($cmd)) {
			usage(1);
		}

		$cmd = "\L${cmd}";

		if ($cmd eq 'put') {
			cmd_put();
		} elsif ($cmd eq 'mkdir') {
			cmd_mkdir();
		} elsif ($cmd eq 'move' || $cmd eq 'mv') {
			cmd_move_link('MOVE');
		} elsif ($cmd eq 'link' || $cmd eq 'ln') {
			cmd_move_link('LINK');
		} elsif ($cmd eq 'delete' || $cmd eq 'del' || $cmd eq 'rm') {
			cmd_delete();
		} elsif ($cmd eq 'ls' || $cmd eq 'dir') {
			cmd_dir();
		} elsif ($cmd eq 'info') {
			cmd_info();
		} else {
			die "$0: unknown command: $cmd\n";
		}

		my $sep = shift(@args);

		last if (!defined($sep)); # End of command line

		if ($sep ne '--') {
			die "$0: garbage at end of $cmd command\n";
		}
	}
}

# Main program
parse_global_options();

if (!defined($opt{'host'})) {
	die "$0: please specify --host, KUP_HOST, or set up ~/.kuprc\n";
}

# "Dry run" pass
$real = 0;
@args = @ARGV;
process_commands();

# Establish output stream
setup_output();

# "Real" pass
$real = 1;
@args = @ARGV;
process_commands();

# Close the output to allow the child process to complete
close_output();

exit 0;

# vim: noet