-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmanual.pl
executable file
·254 lines (221 loc) · 7.77 KB
/
manual.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
#!/usr/bin/perl
#
# PlugBuild Builder Manual Upload Client (primary architecture)
#
use strict;
use FindBin qw($Bin);
use Config::General qw(ParseConfig);
use Switch;
use AnyEvent;
use AnyEvent::TLS;
use AnyEvent::Handle;
use AnyEvent::Socket;
use JSON::XS;
my %config = ParseConfig("$Bin/client.conf");
# other variables, probably shouldn't touch these
my $state;
my %files;
my $current_filename;
my $current_fh;
# check arguments, set upload file list
if (($#ARGV + 1) < 3) {
print "usage: ./manual.pl <repo> <package> <file1.tar.xz ...>\n";
exit 1;
}
$state->{repo} = $ARGV[0];
$state->{pkgbase} = $ARGV[1];
$state->{arch} = $config{primary};
# get package files from arguments
foreach my $n (2 .. $#ARGV) {
my $filename = $ARGV[$n];
my $md5sum_file = `md5sum $filename`;
$md5sum_file = (split(/ /, $md5sum_file))[0];
$files{$filename} = $md5sum_file;
$current_filename = $filename;
if (defined $config{farmer}) {
# send new packages to farmer
`rsync -rtl $filename $config{farmer}/$state->{arch}`;
}
}
# AnyEvent setup
my $condvar = AnyEvent->condvar;
my $h;
my $w = AnyEvent->signal(signal => "INT", cb => sub { $condvar->broadcast; });
my $timer_retry;
# main event loop
con();
$condvar->wait;
# shutdown
$h->destroy;
### control subroutines
# connect to service
sub con {
$h = new AnyEvent::Handle
connect => [$config{server} => $config{port}],
tls => "connect",
tls_ctx => {
verify => 1,
ca_file => "$Bin/$config{ca_file}",
cert_file => "$Bin/$config{cert_file_manual}",
cert_password => $config{password},
verify_cb => sub { cb_verify_cb(@_); }
},
keepalive => 1,
no_delay => 1,
rtimeout => 3, # 3 seconds to authenticate with SSL before destruction
on_rtimeout => sub { $h->destroy; $condvar->broadcast; },
on_error => sub { cb_error(@_); },
on_starttls => sub { cb_starttls(@_); },
on_connect_error => sub { cb_error($_[0], 0, $_[1]); }
;
}
# callback that handles peer certificate verification
sub cb_verify_cb {
my ($tls, $ref, $cn, $depth, $preverify_ok, $x509_store_ctx, $cert) = @_;
# depth is zero when we're verifying peer certificate
return $preverify_ok if $depth;
# get certificate information
my $orgunit = Net::SSLeay::X509_NAME_get_text_by_NID(Net::SSLeay::X509_get_subject_name($cert), Net::SSLeay->NID_organizationalUnitName);
my $common = Net::SSLeay::X509_NAME_get_text_by_NID(Net::SSLeay::X509_get_subject_name($cert), Net::SSLeay->NID_commonName);
my @cert_alt = Net::SSLeay::X509_get_subjectAltNames($cert);
my $ip = AnyEvent::Socket::parse_address $cn;
# verify ip address in client cert subject alt name against connecting ip
while (my ($type, $name) = splice @cert_alt, 0, 2) {
if ($type == Net::SSLeay::GEN_IPADD()) {
if ($ip eq $name) {
print "verified ". Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_subject_name($cert)) ."\n";
return 1;
}
}
}
print "failed verification for $ref->{peername}: ". Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_subject_name($cert)) ."\n";
return 0;
}
# callback on socket error
sub cb_error {
my ($handle, $fatal, $message) = @_;
if ($fatal) {
print "fatal ";
}
print "error from $handle->{peername} - $message\n";
$condvar->broadcast;
}
# callback on whether ssl auth succeeded
sub cb_starttls {
my ($handle, $success, $error) = @_;
if ($success) {
$handle->rtimeout(0); # stop auto-destruct
$handle->on_read(sub { $handle->push_read(json => sub { cb_read(@_); }) }); # set read callback
$handle->push_write(json => { command => 'sync', state => 'manual' });
$state->{command} = 'prep';
$h->push_write(json => $state);
return;
}
# kill the client, bad ssl auth
$condvar->broadcast;
}
# callback for reading data
sub cb_read {
my ($handle, $data) = @_;
return if (!defined $data);
switch ($data->{command}) {
case "add" {
cb_add($data);
}
case ["done", "fail"] {
$condvar->broadcast;
}
case "open" {
$handle->on_drain(sub { cb_upload(@_); });
cb_upload();
}
case "prep" {
my $filename = $current_filename;
$filename =~ s/^\/.*\///;
my %reply = ( command => "open",
arch => $config{primary},
type => "pkg",
filename => $filename);
$handle->push_write(json => \%reply);
}
case "uploaded" {
cb_add();
}
}
}
sub cb_upload {
if ($current_fh) {
my ($data, $bytes);
$bytes = read $current_fh, $data, 65536;
if ($bytes) {
$bytes = pack "N", $bytes;
$data = $bytes . $data;
$h->push_write($data);
} else {
print "-> sending zero\n";
$bytes = pack "N", $bytes;
undef $h->{on_drain}; # stop drain event
$h->push_write($bytes);
close $current_fh;
undef $current_fh;
}
} else {
print "-> opening $current_filename\n";
open $current_fh, "<$current_filename";
binmode $current_fh if ($state->{command} ne 'fail');
}
}
sub cb_add {
my $data = shift;
# add just uploaded file
if (!defined $data) {
my $filename = $current_filename;
$filename =~ s/^\/.*\///;
my $md5sum = $files{$current_filename};
# query file for extra information
my $info = `tar -xOf $current_filename .PKGINFO 2>&1`;
my ($pkgname) = $info =~ m/pkgname = (.*)\n?/;
my ($pkgver) = $info =~ m/pkgver = (.*)\n?/;
my $pkgrel;
($pkgver, $pkgrel) = split(/-/, $pkgver, 2);
my ($pkgdesc) = $info =~ m/pkgdesc = (.*)\n?/;
# construct message for server
my %reply = ( command => "add",
arch => $config{primary},
pkgbase => $state->{pkgbase},
pkgname => $pkgname,
pkgver => $pkgver,
pkgrel => $pkgrel,
pkgdesc => $pkgdesc,
repo => $state->{repo},
filename => $filename,
md5sum => $md5sum );
# communicate reply
$h->push_write(json => \%reply);
return;
}
# delete successfully uploaded file from our list (or not)
if ($data->{response} eq "OK") {
delete $files{$current_filename};
undef $current_filename;
} elsif ($data->{response} eq "FAIL") {
print " -> failed to upload file, trying again..\n";
}
# start next file uploading or send done
if (!$current_filename && (my ($filename, $md5sum) = each(%files))) {
$current_filename = $filename;
}
if ($current_filename) {
my $filename = $current_filename;
$filename =~ s/^\/.*\///;
my %reply = ( command => "open",
arch => $config{primary},
type => "pkg",
filename => $filename);
$h->push_write(json => \%reply);
} else {
print " -> finished uploading, sending done\n";
$state->{command} = 'done';
$h->push_write(json => $state);
}
}