-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLandfill.pm
278 lines (244 loc) · 7.78 KB
/
Landfill.pm
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
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Landfill Tools System.
#
# The Initial Developer of the Original Code is Everything Solved, Inc.
# Portions created by the Initial Developers are Copyright (C) 2010 the
# Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Max Kanat-Alexander <[email protected]>
package Landfill;
use strict;
use base qw(Exporter);
our @EXPORT = qw(
bzr_branches
detaint_natural
random_string
trick_taint
trim
validate_install
);
# We want any compile errors to get to the browser, if possible.
BEGIN {
# This makes sure we're in a CGI.
if ($ENV{SERVER_SOFTWARE} && !$ENV{MOD_PERL}) {
require CGI::Carp;
CGI::Carp->import('fatalsToBrowser');
}
}
use CGI qw(-no_xhtml -oldstyle_urls :private_tempfiles :unique_headers -utf8);
use DBI;
use Email::Address;
use File::Basename;
use Template;
$| = 1;
$::SIG{TERM} = 'IGNORE';
$::SIG{PIPE} = 'IGNORE';
$::SIG{__DIE__} = \&CGI::Carp::confess;
use constant BZR_REPO => 'bzr://bzr.mozilla.org/bugzilla';
use constant BZ_USER => '[email protected]';
use constant RC_PUBLIC => '6Lefs7wSAAAAANcNlkyg9ytkuaLK6R6UOVfreBFo';
# We explicitly forbid these as a defense-in-depth against HTML injections.
use constant FORBIDDEN_EMAIL_CHARACTERS => qr/[<>"&\s]/;
use constant TEMPLATE_CONFIG => {
INCLUDE_PATH => ['template'],
PRE_CHOMP => 1,
TRIM => 1,
ENCODING => 'UTF-8',
FILTERS => {
js => sub {
my ($var) = @_;
$var =~ s/([\\\'\"\/])/\\$1/g;
$var =~ s/\n/\\n/g;
$var =~ s/\r/\\r/g;
$var =~ s/\@/\\x40/g; # anti-spam for email addresses
$var =~ s/</\\x3c/g;
return $var;
},
},
};
# Note that this is a raw subroutine, not a method, so $class isn't available.
sub init_page {
(binmode STDOUT, ':utf8');
if (${^TAINT}) {
# Some environment variables are not taint safe
delete @::ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
# Some modules throw undefined errors (notably File::Spec::Win32) if
# PATH is undefined.
$ENV{'PATH'} = '/var/www/html/tools/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin';
}
# Because this function is run live from perl "use" commands of
# other scripts, we're skipping the rest of this function if we get here
# during a perl syntax check (perl -c, like we do during the
# 001compile.t test).
return if $^C;
}
init_page();
sub authorized {
# CLI scripts are always authorized.
if (!$ENV{GATEWAY_INTERFACE}) {
return 1;
}
return ($ENV{AUTH_TYPE} and $ENV{REMOTE_USER}) ? 1 : 0;
}
sub cgi {
my $class = shift;
if (!$class->request_cache->{cgi}) {
my $cgi = new CGI();
$cgi->charset('UTF-8');
$class->request_cache->{cgi} = $cgi;
}
return $class->request_cache->{cgi};
}
sub dbh {
my $class = shift;
return $class->request_cache->{dbh} if $class->request_cache->{dbh};
my %attributes = (
RaiseError => 1,
AutoCommit => 1,
PrintError => 0,
ShowErrorStatement => 1,
TaintIn => 1,
FetchHashKeyName => 'NAME_lc',
mysql_enable_utf8 => 1,
);
my $dsn = 'dbi:mysql:database=tools';
my $db_pass = get_db_pass();
my $dbh = DBI->connect($dsn, 'tools', $db_pass, \%attributes);
$dbh->do("SET NAMES utf8");
$class->request_cache->{dbh} = $dbh;
return $dbh;
}
our $_request_cache = {};
sub request_cache {
if ($ENV{MOD_PERL}) {
require Apache2::RequestUtil;
# Sometimes (for example, during mod_perl.pl), the request
# object isn't available, and we should use $_request_cache instead.
my $request = eval { Apache2::RequestUtil->request };
return $_request_cache if !$request;
return $request->pnotes();
}
return $_request_cache;
}
sub template {
my $class = shift;
$class->request_cache->{template} ||= Template->new(TEMPLATE_CONFIG);
return $class->request_cache->{template};
}
###############
# Subroutines #
###############
sub bzr_branches {
my $branch_file = 'bzr_branches';
my $path = dirname(__FILE__) . "/" . $branch_file;
open(my $fh, '<', $path) or die "$path: $!";
my @branches = <$fh>;
close($fh);
chomp($_) foreach @branches;
return \@branches;
}
sub detaint_natural {
my $match = $_[0] =~ /^(\d+)$/;
$_[0] = $match ? int($1) : undef;
return (defined($_[0]));
}
sub get_db_pass {
my ($pass_file) = @_;
$pass_file ||= 'db_pass';
my $db_pass;
my $path = dirname(__FILE__);
open(my $fh, '<', "$path/$pass_file") or die "$path/$pass_file: $!";
$db_pass = <$fh>;
chomp($db_pass);
close($fh);
return $db_pass;
}
sub random_string {
my $size = shift || 10; # default to 10 chars if nothing specified
return join("", map{ ('0'..'9','a'..'z','A'..'Z')[rand 62] } (1..$size));
}
sub trim {
my ($str) = @_;
if ($str) {
$str =~ s/^\s+//g;
$str =~ s/\s+$//g;
}
return $str;
}
sub trick_taint {
require Carp;
Carp::confess("Undef to trick_taint") unless defined $_[0];
my $match = $_[0] =~ /^(.*)$/s;
$_[0] = $match ? $1 : undef;
return (defined($_[0]));
}
sub validate_install {
my ($params, $opts) = @_;
my $dbh = Landfill->dbh;
my (%values, @errors, @fields);
if ($opts->{for_deletion}) {
@fields = qw(name);
}
else {
@fields = qw(name contact mailto user branch db);
if (!Landfill->authorized) {
$params->{name} = random_string();
$params->{contact} = 'Web User';
$params->{user} = 'webuser';
$params->{db} = 'Mysql';
}
}
foreach my $field (@fields) {
$params->{$field} = '' if !defined $params->{$field};
$values{$field} = trim($params->{$field});
if ($values{$field} eq '') {
push(@errors, "You must specify a $field.");
$values{$field} = '';
}
}
if ($values{name} =~ /^(\w*)$/) {
my $name = $values{name} = $1;
my $exists = $dbh->selectrow_array(
'SELECT 1 FROM installs WHERE name = ?', undef, $name);
if ($exists and $opts->{check_exists}) {
push(@errors, "An installation with the name '$name'"
. " has already been created using this interface.");
}
if (!$opts->{for_deletion} and -e "/var/www/html/$name") {
push(@errors, "An installation with the name '$name'"
. " already exists on the disk.");
}
if ($values{name} =~ /_branch$/ or $values{name} =~ /^tip$/i) {
push(@errors, "Installation names can't end in _branch or"
. " be called 'tip'.");
}
}
else {
push(@errors, "The installation name can only contain letters,"
. " numbers, and underscores.");
}
if ($values{user}) {
getpwnam($values{user})
or push(@errors, "'$values{user}' is not a valid landfill user.");
}
if ($values{mailto}
and ($values{mailto} !~ $Email::Address::mailbox
or $values{mailto} =~ FORBIDDEN_EMAIL_CHARACTERS))
{
push(@errors, "'$values{mailto}' is not a valid email address.");
}
return (\%values, \@errors);
}
1;