X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-test.git;a=blobdiff_plain;f=pctb%2Fcommod-update-receiver;h=4b52251b9967896aa7b1e70985a3082197df9b26;hp=2c409f48d4b4ce7557d85a9380a25a6ead30a24a;hb=b971a4a2c96ff4dac86857e7fc7f06cc11ff9da4;hpb=fd1dfe6db4a898d7e96a577c68599fc992238326 diff --git a/pctb/commod-update-receiver b/pctb/commod-update-receiver index 2c409f4..4b52251 100755 --- a/pctb/commod-update-receiver +++ b/pctb/commod-update-receiver @@ -36,11 +36,11 @@ use strict (qw(vars)); use POSIX; +use MIME::Entity; use Commods; $CGI::POST_MAX= 3*1024*1024; -$CGI::DISABLE_UPLOADS= 1; use CGI qw/:standard -private_tempfiles/; @@ -63,76 +63,126 @@ sub must_param ($$) { my ($n,$re)= @_; my $v= param($n); fail("missing form parameter $n") unless defined $v; - fail("invalid form parameter $n") unless $v =~ m/$re/; + fail("invalid form parameter $n ($re)") unless $v =~ m/$re/; return $1; } -my $clientname= must_param('clientname',$re_any); -my $clientinfo= $clients{$clientname}; -fail('unknown client') unless defined $client; +my %o; + +$o{'clientname'}= must_param('clientname',$re_any); +my $clientinfo= $clients{$o{'clientname'}}; +fail('unknown client') unless defined $clientinfo; my $clientfixes= must_param('clientfixes', "^([-0-9a-z ]*)\$"); my @clientfixes= sort grep { m/./ } split /\s+/, $clientfixes; -$clientfixes= "@clientfixes"; +$o{'clientfixes'}= "@clientfixes"; foreach my $bug (@$clientinfo) { fail("client out of date - missing bugfix \`$bug'") unless grep { $_ eq $bug } @clientfixes; } -my $cversion= must_param('clientversion', "^(\\d[-+._0-9a-zA-Z]+)\$"); +$o{'clientversion'}= must_param('clientversion', "^(\\d[-+._0-9a-zA-Z]+)\$"); -my $ocean= must_param('ocean', $re_any); -my $island= must_param('island', $re_any); +$o{'ocean'}= must_param('ocean', $re_any); +$o{'island'}= must_param('island', $re_any); -my $arches= $oceans{$ocean}; +my $arches= $oceans{$o{'ocean'}}; fail("unknown ocean") unless $arches; my $island_found= 0; foreach my $islands (values %$arches) { - my $sources= $islands->{$island}; + my $sources= $islands->{$o{'island'}}; next unless $sources; die if $island_found; $island_found= $sources; } fail("unknown island") unless $island_found; -die if $ocean =~ m/\=/; -die if $island =~ m/\=/; +$o{'timestamp'}= must_param('timestamp', "^([1-9]\\d{1,20})\$"); +my $now= time; defined $now or die $!; +fail("clock skew") if $o{'timestamp'} >= $now; -$datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$"); -$indatafh= upload('data'); fail("data is not a file") unless defined $datafh; +my $indatafh= upload('data'); +defined $indatafh or fail("data is not a file"); +my $datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$"); -our %done; +my $mcontent= MIME::Entity->build(To => 'yarrg-commod-updates', + Type => 'multipart/mixed', + Boundary => '=', + Charset => 'utf-8'); + +get_our_version(\%o, 'server'); +foreach my $cs (qw(client server)) { + $o{"${cs}spec"}= join ' ', map { $o{$cs.$_} } qw(name version fixes); +} + +my $metadata= ''; + +sub ksmap ($) { + my ($v) = @_; + my $i=0; grep { return $i if $_ eq $v; $i++ } qw(ocean island timestamp); + sprintf "z %d %s", (length $v) / 8, $v; +} + +foreach my $vn (sort { ksmap($a) cmp ksmap($b) } keys %o) { + my $val= $o{$vn}; + die if $val =~ m/\n|\r|\t/; + $metadata .= "$vn\t$o{$vn}\n"; +} + +my $mdpart= MIME::Entity->build(Top => 0, + Type => 'text/plain', + Charset => 'utf-8', + Disposition => 'inline', + Encoding => 'quoted-printable', + Filename => 'metadata', + Data => $metadata); +$mcontent->add_part($mdpart); + +my $gunzchild= open(GZ, "-|"); defined $gunzchild or die $!; +if (!$gunzchild) { + open STDIN, "<&=", $indatafh or die $!; + exec 'gunzip'; die $!; +} -my $content= MIME::Entity->build(Type => 'multipart/mixed', - Boundary => '=', - Charset => 'utf-8'); +sub bad_data ($) { fail("bad data: line $.: $_[0]"); } -my $clientspec= "$clientname $clientversion $clientfixes"; -foreach $vn (qw(ocean island - clientspec - clientname clientversion clientfixes)) { +our %done; -while (<$datafh>) { +while () { !m/\P{IsPrint}/ or die bad_data('nonprinting char(s)'); + !m/\\/ or bad_data('data contains backslashes'); my @v= split /\t/; @v==6 or bad_data('wrong number of fields'); my ($commod,$stall) = @v; defined $commods{$commod} or bad_data("unknown commodity \`$commod'"); - $stall =~ m/^\p{IsUpper}/ or bad_data("stall not capitalised"); + $stall =~ m/^\p{IsUpper}|^[0-9]/ or bad_data("stall not capitalised"); !exists $done{$commod,$stall} or bad_data("repeated data"); $done{$commod,$stall}= 1; - foreach my $f (@v[2..5]) { - $f =~ m/^(0|[1-9][0-9]{0,5}|\>1000)$/ or bad_data("bad field"); - ($v % 1) or ($v !~ m/\>/) or bad_data("> in price"); + foreach my $i (2..5) { + my $f= $v[$i]; + $f =~ m/^(|0|[1-9][0-9]{0,5}|\>1000)$/ or bad_data("bad field $i"); + ($i % 2) or ($f !~ m/\>/) or bad_data("> in field $i price"); } } - - - - -#foreach my $commod (sort keys %commods) { -# print "$commod\n"; -#} -#STDOUT->error and die $!; -#close STDOUT or die $!; +GZ->error and die $!; +$?=0; close GZ; $? and fail("gunzip for check failed code $?"); + +my $mdatafile= MIME::Entity->build(Top => 0, + Type => 'application/octet-stream', + Disposition => 'attachment', + Encoding => 'base64', + Filename => 'deduped.tsv.gz', + Path => $datafile); +$mcontent->add_part($mdatafile); + +open M, "|/usr/sbin/sendmail -t -oi -oee -odb" + or fail("fork sendmail failed! ($!)"); +$mcontent->print(\*M); + +M->error and fail("write sendmail failed! ($!)"); +$?=0; close M; $? and fail("sendmail failed code $?"); + +print header(-type=>'text/plain', -charset=>'us-ascii'), + "OK\n" + or die $!;