upkg-dl-list 6.53 KB
Newer Older
fun's avatar
.  
fun committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/perl
#
# Extract URLs from untrusted Pkgfiles.
#

use strict;
use warnings FATAL => 'all';
use diagnostics;

sub main
{
	my $pkgfile = shift;

	my $content = read_file($pkgfile);
	$content =~ / ^ \s* source=\( (?<sources>.+?) \) /xms;

	my @raw = extract_raw_urls($+{sources}) if $+{sources};
	my @url;
	my %vars;
	eval {
fun's avatar
fun committed
21
22
		@url = map { expand_url($_, \$content, \%vars) }
		    @raw
fun's avatar
.  
fun committed
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
	};

	if ($@) {
		print STDERR "$pkgfile: $@";
		die $@;
	}

	map { print $_, "\n" } @url;
}

sub read_file
{
	my $name = shift;
	open my $f, '<', $name or die "open($name): $!";
	my $c = join '', <$f>;
	close $f;
	return $c;
}

sub extract_raw_urls
{
	my $c = shift;

	# remove comments
	while ($c =~ s/^ (.*\s) \# .* $/ $1 /xgm) { }

fun's avatar
.    
fun committed
49
	# remove \ at the end of line
fun's avatar
.    
fun committed
50
	$c =~ s/\\\n//sg;
fun's avatar
.    
fun committed
51

fun's avatar
.  
fun committed
52
	my @s;
fun's avatar
.    
fun committed
53
54
	while ($c =~ m{ \s* (['"]?) (http | https | ftp) :// ([^\s]+) \1 }xg) {
		push @s, "$2://$3";
fun's avatar
.  
fun committed
55
56
57
58
59
60
61
62
63
64
65
66
	}
	return @s;
}

sub expand_url
{
	my ($raw, $content, $vars) = @_;

	die "Unsupported shell execution: $raw" if $raw =~ /(\$\(|`)/;

	return expand_string($raw, $content, $vars) if not $raw =~ m{^
		(?<pre> .+? [^\$] )
fun's avatar
.    
fun committed
67
		\{ (?<brace> .*? , .*) \}
fun's avatar
.  
fun committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
		(?<post> .* )
	$}x;

	# we have brace expansion
	# {a,b,c} {,a} {,a}
	return map { expand_url($+{pre} . $_ . $+{post}, $content, $vars) }
	    split /,/, $+{brace};
}

sub expand_string
{
	my ($raw, $content, $vars) = @_;
	my @pieces = split /\$/, $raw;
	my @url = shift @pieces;
	for my $p (@pieces) {
		my ($expr, $clear) = ($p =~ /^ { (.+?) } (.*) $/x);
		($expr, $clear) = ($p =~ /^      (\w+)   (.*) $/x) if not $expr;
fun's avatar
fun committed
85

fun's avatar
.  
fun committed
86
		die "Can't handle $raw" if not $expr and not $clear;
fun's avatar
fun committed
87

fun's avatar
.  
fun committed
88
		$clear =~ s/\\([_-])/$1/g if $clear;    # $name\_$var
fun's avatar
.    
fun committed
89
		$clear =~ s/\"([_-])"/$1/g if $clear;    # $name"_"$var
fun's avatar
fun committed
90

fun's avatar
.  
fun committed
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
		push @url, eval_expr($expr, $content, $vars), $clear;
	}
	return join '', @url;
}

sub bash_substring
{
	my ($v, $off, $len) = @_;

	$off = 0 if not defined $off or not length($off);

	return substr($v, $off) if not defined $len or not length($len);
	return substr($v, $off, $len);
}

sub bash_case_modification
{
	my ($v, $how) = @_;
	my %h = (
		'^'  => sub { return ucfirst shift },
		'^^' => sub { return uc shift },
		','  => sub { return lcfirst shift },
		',,' => sub { return lc shift },
	);
	die "unknown case for $v: $how" if not defined $h{$how};
	my $fct = $h{$how};
	return &$fct($v);
}

sub quote_from_bash
{
	my $bash = shift;
	$bash =~ s|\\||g;
	return quotemeta $bash;
}

fun's avatar
.    
fun committed
127
128
129
130
131
132
133
134
sub quote_from_bash_relaxed
{
	my $bash = shift;
	$bash =~ s|\\||g;
	return $bash if $bash =~ /^[A-Za-z_0-9-]+$/;
	return quotemeta $bash;
}

fun's avatar
.  
fun committed
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
sub bash_re_to_perl_re
{
	my ($bash, $greedy) = @_;

	my ($perl, $last) = ('', '');

	while ($bash =~ /(?!\\)(?:[\*\?]|\[0-9\])/pg) {
		$perl .= quote_from_bash ${^PREMATCH} if length ${^PREMATCH};

		$perl .= '.'       if ${^MATCH} eq '?';
		$perl .= '.*'      if ${^MATCH} eq '*' and $greedy;
		$perl .= '.*?'     if ${^MATCH} eq '*' and not $greedy;
		$perl .= ${^MATCH} if ${^MATCH} eq '[0-9]';

		$last = ${^POSTMATCH};
	}

	$perl .= quote_from_bash $last if length $last;
	$perl .= quote_from_bash $bash if length($perl) == 0;

	return $perl;
}

sub bash_substring_removal
{
	my ($v, $what, $where, $greedy) = @_;

	my $remove = bash_re_to_perl_re($what, $greedy);

	$v =~ s/^$remove//       if $where eq 'prefix';
	$v =~ s/$remove$//       if $where eq 'suffix' and $greedy;
	$v =~ s/(.*)$remove$/$1/ if $where eq 'suffix' and not $greedy;

	# version=1.2.3
	# ${version%.*} -> remove=\..*?
	#	s/$remove$//        -> 1
	#	s/(.*?)$remove$/$1/ -> 1
	#	s/(.*)$remove$/$1/  -> 1.2

	return $v;
}

sub bash_search_and_replace
{
	my ($v, $from, $to, $times, $where, $greedy) = @_;

	# ${version/_*}
	# ${version/*_}
	if (not defined $to and $where !~ '^(prefix|suffix)$') {
		$where = 'suffix' if substr($from, -1, 1) eq '*';
		$where = 'prefix' if substr($from, 0,  1) eq '*';
	}

	$from = bash_re_to_perl_re($from, $greedy);

	$from = '^' . $from if $where eq 'prefix';
	$from = $from . '$' if $where eq 'suffix';

fun's avatar
.    
fun committed
193
	$to = quote_from_bash_relaxed $to if length $to;
fun's avatar
.  
fun committed
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
	$to = '' if not defined $to;

	$v =~ s/$from/$to/g if $times eq 'all';
	$v =~ s/$from/$to/  if $times eq 'once';

	return $v;
}

sub eval_expr
{
	my ($e, $content, $vars) = @_;

	return expand_var($e, $content, $vars) if $e =~ /^\w+$/;

	#
	# String::Bash is not safe enough
	#

	if ($e =~ m{^ (?<name> \w+ ) : (?<off> [0-9]* ) : (?<len> [0-9]+ ) $}x)
	{
		#
		# {var:[off]:len}
		#

		my $v = expand_var($+{name}, $content, $vars);
		return bash_substring($v, $+{off}, $+{len});
	} elsif ($e =~ m{^ (?<name> \w+ ) : (?<off> [0-9]+ ) $}x) {
		#
		# {var:off}
		#

		my $v = expand_var($+{name}, $content, $vars);
		return bash_substring($v, $+{off});
	} elsif ($e =~ m{^ (?<name> \w+ ) (?<how> (?: \^{1,2} | ,{1,2} ) ) $}x)
	{
		#
		# {var:^[^]} {var:,[,]}
		#

		my $v = expand_var($+{name}, $content, $vars);
		return bash_case_modification($v, $+{how});
	} elsif (
		$e =~ m{^
			(?<name>  \w+ )
			(?<where> (?: \#{1,2} | \%{1,2} ) )
			(?<what>  [\w\.\?\*\+\\\[\]_-]+ )
			$}x
	    ) {
		#
		# {var#[#]prefix} || {var%[%]suffix}
		#

		my $greedy = length($+{where}) - 1;
		my $where  = 'suffix';
		$where = 'prefix' if substr($+{where}, 0, 1) eq '#';

		my $v = expand_var($+{name}, $content, $vars);
		return bash_substring_removal($v, $+{what}, $where, $greedy);
	} elsif (
fun's avatar
.    
fun committed
253
		$e =~ m{^
fun's avatar
.  
fun committed
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
			(?<name>     \w+ )
			(?<times>    /{1,2} )
			(?<where>    (?: \#{0,2} | \%{0,2} ) )
			(?<from>     [\w\.\?\*\+\\\[\]_-]+ )
			(?: / (?<to> [\w\._-]* ) ){0,1}
			$}x
	    ) {
		#
		# {var/[/][#%]{0,2}from[/[to]]/}
		#

		my $times  = length($+{times}) == 1 ? 'once' : 'all';
		my $where  = 'everywhere';
		my $greedy = 0;

		if (length $+{where}) {
			$greedy = length($+{where}) - 1;
			$where  = 'suffix';
			$where  = 'prefix' if substr($+{where}, 0, 1) eq '#';
		}

		my $v = expand_var($+{name}, $content, $vars);
		return bash_search_and_replace($v, $+{from}, $+{to}, $times,
			$where, $greedy);
	} else {
		die "unsupported expression: $e";
	}
}

fun's avatar
fun committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
sub hack_vars
{
	my ($name, $content, $vars) = @_;

	if (
		$$content =~ / ^ $name =
			\$\(printf \s+ "%i%.2i%.2i%.2i" \s+ \$\{version\/\/\.\/\s\}\) /xm
	    ) {
		my $v = expand_var('version', $content, $vars);
		my @v = split /\./, $v;
		push @v, 0;
		return sprintf "%i%.2i%.2i%.2i", @v;
	}

	return undef;
}

fun's avatar
.  
fun committed
300
301
302
303
304
sub expand_var
{
	my ($name, $content, $vars) = @_;

	if (not exists $vars->{$name}) {
fun's avatar
fun committed
305
306
307
308
309
310
311
312
		my $v = hack_vars(@_);
		if (not $v) {
			$$content =~ / ^ $name = ([^\s]+) /xm
			    or die "unknown variable: $name";
			my $value = $1;
			$value =~ s/^"(.+)"$/$1/;
			$v = expand_string($value, $content, $vars);
		}
fun's avatar
.  
fun committed
313
314
315
316
317
318
319
		$vars->{$name} = $v;
	}

	return $vars->{$name};
}

map { main($_) } @ARGV;