aboutsummaryrefslogtreecommitdiffstats
path: root/korgupload
blob: 5a5a66dad0efb67f01a232b09bfc37e9be1c786a (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
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
#!/usr/bin/perl -T
#
# This script should be run with the permissions of the user that
# is uploading files.
#
# Arguments are whitespace-separated and URL-escaped; a single % means
# a null argument.
#
# It accepts the following commands:
#
# DATA byte-count
#	- receives a new data blob (follows immediately)
# TAR git-tree tree-ish prefix
#	- generate a data blob from a git tree (git archive)
# DIFF git-tree tree-ish tree-ish
#	- generate a data blob as a git tree diff
# SIGN byte-count
#	- updates the current signature blob (follows immediately)
# PUT pathname
#	- installs the current data blob as <pathname>
# MKDIR pathname
#	- creates a new directory
# MOVE old-path new-path
#	- moves <old-path> to <new-path>
# DONE
#	- optional command, terminates transaction
#
# For future consideration:
#
# LINK old-path:new-path
#	- hard links <old-path> to <new-path>
# SYMLINK old-path:new-path
#	- symlinks <old-path> to <new-path>
# DELETE old-path
#	- removes <old-path>
#

use strict;
use warnings;
use bytes;
use Encode qw(encode decode);
use IPC::Open2 qw(open2);

use File::Temp qw(tempdir);
use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
use BSD::Resource;
use Fcntl qw(:DEFAULT :flock);
use POSIX;

use Sys::Syslog qw(:standard :macros);
use Git;

my $data_path = '/home/hpa/kernel.org/test/pub';
my $git_path  = '/home/hpa/kernel.org/test/git';
my $lock_file = '/home/hpa/kernel.org/test/lock';
my $tmp_path  = '/home/hpa/kernel.org/test/tmp';
my $pgp_path  = '/home/hpa/kernel.org/test/pgp';
my $max_data  = 8*1024*1024*1024;
my $bufsiz    = 1024*1024;

%ENV = ('PATH' => '/bin:/usr/bin',
	'LANG' => 'C',
	'SHELL' => '/bin/sh');

setrlimit(RLIMIT_FSIZE, $max_data, RLIM_INFINITY);

umask(077);
my $tmpdir = tempdir(DIR => $tmp_path, CLEANUP => 1);
umask(002);

my $have_data = 0;
my $have_sign = 0;

sub my_username() {
    my $whoami = getuid();
    my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell,$expire) = getpwuid($whoami);

    return (defined($name) && $whoami == $uid) ? $name : $whoami;
}

my $user_name = my_username();

openlog("korgupload($user_name)", 'ndelay,pid', LOG_LOCAL5);

my $lock_fd   = undef;

sub fatal($) {
    my($msg) = @_;

    syslog(LOG_CRIT, "%s", $msg);
    die $msg."\n";
}

sub lock_tree()
{
    if (!defined($lock_fd)) {
	open($lock_fd, '<', $lock_file)
	    or fatal("Cannot open lock file");
	flock($lock_fd, LOCK_EX)
	    or fatal("Cannot get file tree lock");
    } else {
	fatal("File tree is already locked");
    }
}

sub unlock_tree()
{
    if (defined($lock_fd)) {
	close($lock_fd);
	undef $lock_fd;
    }
}

sub url_unescape($)
{
    my($s) = @_;
    my $c;
    my $o;

    # A single isolated % sign means an empty string
    return '' if ($s eq '%');

    for (my $i = 0; $i < length($s); $i++) {
	$c = substr($s, $i, 1);
	if ($c eq '+') {
	    $o .= ' ';
	} elsif ($c eq '%') {
	    $c = substr($s, $i+1, 2);
	    return undef if (length($c) != 2);
	    $o .= pack("C", hex $c);
	    $i += 2;
	} else {
	    $o .= $c;
	}
    }

    return $o;
}

sub parse_line($)
{
    my($line) = @_;
    chomp $line;

    if ($line !~ /^([A-Z0-9_]+)(|\s+(|\S|\S.*\S|))\s*$/) {
	return undef;		# Invalid syntax
    }

    my $cmd = $1;
    my @args = ();

    if ($2 ne '') {
	my @rawargs = split(/\s+/, $3);

	foreach my $ra (@rawargs) {
	    my $a = url_unescape($ra);
	    return undef if (!defined($a));
	    push(@args, $a);
	}
    }

    return ($cmd, @args);
}

# 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, control characters,
# and malformed UTF-8 is not permitted.
sub is_valid_filename($)
{
    my($f) = @_;

    return 0 if (!is_clean_string($f));
    return 0 if ($f !~ m:^/:);
    return 0 if ($f =~ m:/$:);
    return 0 if ($f =~ m://:);
    return 0 if ($f =~ m:/(\.|\.\.)(/|$):);

    return 1;
}

sub get_raw_data(@)
{
    my @args = @_;

    if (scalar(@args) != 1 || $args[0] !~ /^[0-9]+$/) {
	fatal("Bad DATA command");
    }

    my $output = $tmpdir.'/data';
    anyuncompress(\*STDIN => $output,
		  BinModeOut => 1,
		  InputLength => ($args[0] + 0),
		  Append => 0,
		  AutoClose => 0,
		  Transparent => 1,
		  BlockSize => $bufsiz)
	or fatal("DATA decompression error: $AnyUncompressError");
		  
    $have_data = 1;
}

# Get the canonical name for a git ref and its type
sub check_ref($$)
{
    my($repo, $ref) = @_;

    my $out = undef;

    if (!is_clean_string($ref) || $ref =~ /^-/) {
	return undef;
    }

    # It turns out Git::command_bidi_pipe() is broken under -T
    $ENV{'GIT_DIR'} = $repo->repo_path();

    my $pipe_in;
    my $pipe_out;
    my $pid = open2($pipe_in, $pipe_out, 'git', 'cat-file', '--batch-check');
    print $pipe_out $ref, "\n";
    close($pipe_out);
    $out = <$pipe_in>;
    chomp $out;
    waitpid($pid, 0);
    
    if ($? == 0 && $out =~ /^([0-9a-f]{40}) (\S+) ([0-9]+)$/) {
	return ($1, $2, $3+0);
    } else {
	return undef;
    }
}

sub get_tar_data(@)
{
    my @args = @_;

    if (scalar(@args) != 3) {
	fatal("Bad TAR command");
    }

    my($tree, $ref, $prefix) = @args;

    if (!is_valid_filename($tree)) {
	fatal("Invalid pathname in TAR command");
    }

    if (!is_clean_string($prefix)) {
	fatal("Invalid prefix string");
    }

    if ($tree !~ /\.git$/ || ! -d $git_path.$tree ||
	! -d $git_path.$tree.'/objects') {
	fatal("No such git tree");
    }

    my $repo;
    git_cmd_try {
	$repo = Git->repository(Repository => $git_path.$tree);
    } "Invalid git repository\n";

    my ($sha, $type, $len) = check_ref($repo, $ref);
    if ($type !~ /^(tree|commit|tag)$/) {
	fatal("Invalid tree reference");
    }

    syslog(LOG_INFO, "tar ref ${sha}");

    git_cmd_try {
	$repo->command_noisy('archive', '--format=tar', '--prefix='.$prefix,
			     '-o', $tmpdir.'/data', $ref);
    } "Failed to acquire tarball\n";
		  
    $have_data = 1;
}

sub get_diff_data(@)
{
    my @args = @_;

    if (scalar(@args) != 3) {
	fatal("Bad DIFF command");
    }

    my($tree, $ref1, $ref2) = @args;

    if (!is_valid_filename($tree)) {
	fatal("Invalid pathname in DIFF command");
    }

    if ($tree !~ /\.git$/ || ! -d $git_path.$tree ||
	! -d $git_path.$tree.'/objects') {
	fatal("No such git tree");
    }

    my $repo;
    git_cmd_try {
	$repo = Git->repository(Repository => $git_path.$tree);
    } "Invalid git repository\n";

    my ($sha1, $type1, $len1) = check_ref($repo, $ref1);
    if ($type1 !~ /^(tree|commit|tag)$/) {
	fatal("Invalid tree reference");
    }
    
    my ($sha2, $type2, $len2) = check_ref($repo, $ref2);
    if ($type2 !~ /^(tree|commit|tag)$/) {
	fatal("Invalid tree reference");
    }

    syslog(LOG_INFO, "diff refs ${sha1}..${sha2}");

    git_cmd_try {
	my $oldstdout;
	my $out;

	open($oldstdout, '>&', \*STDOUT) or die;
	sysopen($out, $tmpdir.'/data', O_WRONLY|O_CREAT|O_TRUNC) or die;
	open(STDOUT, '>&', $out) or die;
	close($out);
	
	$repo->command_noisy('diff-tree', '-p', $sha1, $sha2);

	open(STDOUT, '>&', $oldstdout);
	close($oldstdout);
    } "Failed to acquire patch file\n";
		  
    $have_data = 1;
}

sub get_sign_data(@)
{
    my @args = @_;

    if (scalar(@args) != 1 || $args[0] !~ /^[0-9]+$/) {
	fatal("Bad SIGN command");
    }

    my $output = $tmpdir.'/sign';
    anyuncompress(\*STDIN => $output,
		  BinModeOut => 1,
		  InputLength => ($args[0] + 0),
		  Append => 0,
		  AutoClose => 0,
		  Transparent => 1,
		  BlockSize => $bufsiz)
	or fatal("SIGN decompression error: $AnyUncompressError");
		  
    if ((-s $output) >= 65536) {
	fatal("SIGN output impossibly large");
    }

    $have_sign = 1;
}

sub make_compressed_data()
{
    die if (!$have_data);

    my %workers;
    my @cmds = (['/bin/gzip', '-9'],
		['/usr/bin/bzip2', '-9'],
		['/usr/bin/xz', '-9']);
    my @exts = ('.gz', '.bz2', '.xz');
    my $nworkers = 0;

    for (my $i = 0; $i < scalar @cmds; $i++) {
	my @c = @{$cmds[$i]};
	my $e = $exts[$i];

	my $w = fork();

	if (!defined($w)) {
	    fatal("Fork failed");
	}

	if ($w == 0) {
	    sysopen(RAW, $tmpdir.'/data', O_RDONLY)
		or exit 127;
	    sysopen(OUT, $tmpdir.'/data'.$e, O_WRONLY|O_CREAT|O_TRUNC, 0666)
		or exit 127;

	    open(STDIN,  '<&', \*RAW) or exit 127;
	    open(STDOUT, '>&', \*OUT) or exit 127;
	    close(RAW);
	    close(OUT);

	    exec {$c[0]} @c;
	    exit 127;
	}

	$workers{$w}++;
	$nworkers++;
    }

    while ($nworkers) {
	my $w = wait();
	my $status = $?;

	if ($workers{$w}) {
	    undef $workers{$w};
	    if ($status) {
		foreach my $c (keys %workers) {
		    kill('TERM', $c);
		}
		fatal("Failed to compress output data");
	    }
	}

	$nworkers--;
    }
}

sub make_timestamps_match()
{
    die if (!$have_data || !$have_sign);

    my $now = time();
    
    utime($now, $now, $tmpdir.'/data');
    utime($now, $now, $tmpdir.'/data.gz');
    utime($now, $now, $tmpdir.'/data.bz2');
    utime($now, $now, $tmpdir.'/data.xz');
    utime($now, $now, $tmpdir.'/sign');
}

sub cleanup()
{
    unlink($tmpdir.'/data');
    unlink($tmpdir.'/data.gz');
    unlink($tmpdir.'/data.bz2');
    unlink($tmpdir.'/data.xz');
    unlink($tmpdir.'/sign');
    $have_data = 0;
    $have_sign = 0;
}

sub signature_valid()
{
    my $oldstdout;
    my $oldstderr;
    my $devnull;

    # gpg likes to chat on the console no matter what...
    open($devnull, '>', '/dev/null')
	or fatal("Cannot open /dev/null");
    open($oldstdout, '>&', \*STDOUT)
	or fatal("dup error");
    open($oldstderr, '>&', \*STDERR)
	or fatal("dup error");
    open(STDOUT, '>&', $devnull)
	or fatal("dup error");
    open(STDERR, '>&', $devnull)
	or fatal("dup error");
    close($devnull);

    my $status =
	system('/usr/bin/gpg', '--batch', '--no-tty',
	       '--homedir', $tmpdir,
	       '--no-options',
	       '--no-auto-key-locate',
	       '--no-default-keyring',
	       '--keyring', $pgp_path."/${user_name}.gpg",
	       '--display-charset', 'utf-8',
	       '--verify', $tmpdir.'/sign', $tmpdir.'/data');

    open(STDOUT, '>&', $oldstdout);
    close($oldstdout);
    open(STDERR, '>&', $oldstderr);
    close($oldstderr);

    return $status == 0;
}

sub put_file(@)
{
    my @args = @_;

    if (scalar(@args) != 1) {
	fatal("Bad PUT command");
    }

    my($file) = @args;

    if (!$have_data) {
	fatal("PUT without DATA");
    }
    if (!$have_sign) {
	fatal("PUT without SIGN");
    }

    if (!signature_valid()) {
	fatal("Signature invalid");
    }

    if (!is_valid_filename($file)) {
	fatal("Invalid filename in PUT command");
    }

    if ($file =~ /^(.*)\.gz$/) {
	my $stem = $1;
	make_compressed_data();
	make_timestamps_match();

	lock_tree();

	if (-e $data_path.$stem) {
	    fatal("Filename conflict (compressed and uncompressed)");
	}
	if ((-e $data_path.$stem.'.gz' && ! -f _)
	    (-e $data_path.$stem.'.bz2' && ! -f _)
	    (-e $data_path.$stem.'.xz' && ! -f _)
	    (-e $data_path.$stem.'.sign' && ! -f _)) {
	    fatal("Trying to overwrite a non-file");
	}

	if (!rename($tmpdir.'/data.gz',  $data_path.$stem.'.gz') ||
	    !rename($tmpdir.'/data.bz2', $data_path.$stem.'.bz2') ||
	    !rename($tmpdir.'/data.xz',  $data_path.$stem.'.xz') ||
	    !rename($tmpdir.'/sign',     $data_path.$stem.'.sign')) {
	    unlink($data_path.$stem.'.gz');
	    unlink($data_path.$stem.'.bz2');
	    unlink($data_path.$stem.'.xz');
	    unlink($data_path.$stem.'.sign');
	    fatal("Failed to install files");
	}
	unlock_tree();
    } elsif ($file =~ /\.(sign|bz2|xz)$/) {
	fatal("Cannot install .sign, .bz2 or .xz files directly");
    } else {
	make_timestamps_match();

	lock_tree();

	if (-e $data_path.$file.'.gz' ||
	    -e $data_path.$file.'.bz2' ||
	    -e $data_path.$file.'.xz') {
	    fatal("Filename conflict (compressed and uncompressed)");
	}
	if ((-e $data_path.$file && ! -f _) ||
	    (-e $data_path.$file.'.sign' && ! -f _)){
	    fatal("Attempting to overwrite a non-file");
	}

	if (!rename($tmpdir.'/data', $data_path.$file) ||
	    !rename($tmpdir.'/sign', $data_path.$file.'.sign')) {
	    unlink($data_path.$file);
	    unlink($data_path.$file.'.sign');
	    fatal("Failed to install files");
	}
	unlock_tree();
    }

    cleanup();
}

sub do_mkdir(@)
{
    my @args = @_;

    if (scalar(@args) != 1) {
	fatal("Bad MKDIR command");
    }

    my($file) = @args;

    if (!is_valid_filename($file)) {
	fatal("Invalid filename in MKDIR command");
    }

    if ($file =~ /\.(gz|bz2|xz|sign)$/) {
	fatal("Protected filename space");
    }

    lock_tree();

    if (-e $data_path.$file.'.gz' ||
	-e $data_path.$file.'.bz2' ||
	-e $data_path.$file.'.xz' ||
	-e $data_path.$file.'.sign') {
	fatal("Filename conflict (file and directory)");
    }

    if (!mkdir($data_path.$file, 0777)) {
	fatal("Failed to MKDIR");
    }

    unlock_tree();
}

sub move_file(@)
{
    my @args = @_;

    if (scalar(@args) != 2) {
	fatal("Bad MOVE command");
    }

    my($from, $to) = @args;

    if (!is_valid_filename($from) || !is_valid_filename($to)) {
	fatal("Invalid filename in MOVE command");
    }

    if ($from =~ /\.(bz2|xz|sign)$/) {
	fatal("MOVE of individual .bz2, .xz, or .sign files not supported");
    }
    if ($to =~ /\.(bz2|xz|sign)$/) {
	fatal("MOVE to filename ending in .bz2, .xz or .sign");
    }
    
    if ($from =~ /\.gz$/ && $to !~ /\.gz$/) {
	fatal("MOVE of .gz file must itself end in .gz");
    }
    if ($from !~ /\.gz$/ && $to =~ /\.gz$/) {
	fatal("MOVE of non-.gz file must not end in .gz");
    }

    lock_tree();

    if (-d $data_path.$from) {
	if (!rename($data_path.$from, $data_path.$to)) {
	    fatal("MOVE of directory failed");
	}
    } elsif (-f $data_path.$from) {
	if ($from =~ /^(.*)\.gz$/) {
	    my $from_stem = $1;

	    die if ($to !~ /^(.*)\.gz$/); # Should already be checked
	    my $to_stem = $1;

	    if (-e $data_path.$to_stem) {
		fatal("Filename conflict (compressed and uncompressed)");
	    }

	    if ((-e $data_path.$to_stem.'.gz' && ! -f _)
		(-e $data_path.$to_stem.'.bz2' && ! -f _)
		(-e $data_path.$to_stem.'.xz' && ! -f _)
		(-e $data_path.$to_stem.'.sign' && ! -f _)) {
		fatal("Trying to overwrite a non-file");
	    }

	    if (!rename($data_path.$from_stem.'.gz',   $data_path.$to_stem.'.gz') ||
		!rename($data_path.$from_stem.'.bz2',  $data_path.$to_stem.'.bz2') ||
		!rename($data_path.$from_stem.'.xz',   $data_path.$to_stem.'.xz') ||
		!rename($data_path.$from_stem.'.sign', $data_path.$to_stem.'.sign')) {
		fatal("MOVE of compressed file failed");
	    }
	} else {
	    if (-e $data_path.$to.'.gz' ||
		-e $data_path.$to.'.bz2' ||
		-e $data_path.$to.'.xz') {
		fatal("Filename conflict (compressed and uncompressed)");
	    }

	    if ((-e $data_path.$to && ! -f _) ||
		(-e $data_path.$to.'.sign' && ! -f _)) {
		fatal("Trying to overwrite non-directory");
	    }

	    if (!rename($data_path.$from, $data_path.$to) ||
		!rename($data_path.$from.'.sign', $data_path.$to.'.sign') {
		fatal("MOVE of plain file failed");
	    }
	}
    } else {
	fatal("MOVE of non-directory/non-file not currently supported");
    }

    unlock_tree();
}

my $line;
while (defined($line = <STDIN>)) {
    # Ignore lines with only whitespace or starting with #
    next if ($line =~ /^\s*(|\#.*)$/);

    chomp $line;

    if (!is_clean_string($line) || length($line) > 4096) {
	syslog(LOG_ERR, "Received garbage input");
	fatal("Invalid command");
    }

    syslog(LOG_NOTICE, "Cmd: $line");

    my($cmd, @args) = parse_line($line);

    if (!defined($cmd)) {
	fatal("Syntax error");
    }

    if ($cmd eq 'DATA') {
	get_raw_data(@args);
    } elsif ($cmd eq 'TAR') {
	get_tar_data(@args);
    } elsif ($cmd eq 'DIFF') {
	get_diff_data(@args);
    } elsif ($cmd eq 'SIGN') {
	get_sign_data(@args);
    } elsif ($cmd eq 'PUT') {
	put_file(@args);
    } elsif ($cmd eq 'MKDIR') {
	do_mkdir(@args);
    } elsif ($cmd eq 'MOVE') {
	move_file(@args);
    } elsif ($cmd eq 'DONE') {
	last;
    } else {
	fatal("Invalid command");
    }
}

syslog(LOG_NOTICE, "Session completed successfully");
exit 0;