add support for recapthcha on form to reduce spam
[buildfarm-server.git] / cgi-bin / register.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use DBI;
5 use Template;
6 use CGI;
7 use Template;
8 use Captcha::reCAPTCHA;
9
10 use vars qw($dbhost $dbname $dbuser $dbpass $dbport $notifyapp $captcha_pubkey $captcha_privkey);
11
12 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
13 #require "BuildFarmWeb.pl";
14
15 my $dsn="dbi:Pg:dbname=$dbname";
16 $dsn .= ";host=$dbhost" if $dbhost;
17 $dsn .= ";port=$dbport" if $dbport;
18
19 my $header = <<EOS;
20 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
21         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
22 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
23 <head>
24         <meta http-equiv="content-type" content="text/html; charset=utf-8" />
25         <title>PostgreSQL BuildFarm Application</title>
26         <link rel="icon" type="image/png" href="/elephant-icon.png" />
27         <link rel="stylesheet" rev="stylesheet" href="/inc/pgbf.css" charset="utf-8" />
28         <style type="text/css"><!--
29         li#register a { color:rgb(17,45,137); background: url(/inc/b/r.png) no-repeat 100% -20px; } 
30         li#register { background: url(/inc/b/l.png) no-repeat 0% -20px; }
31         --></style>
32 </head>
33 <body class="application">
34 <div id="wrapper">
35 <div id="banner">
36 <a href="/index.html"><img src="/inc/pgbuildfarm-banner.png" alt="PostgreSQL BuildFarm" width="800" height="73" /></a>
37 <div id="nav">
38 <ul>
39     <li id="home"><a href="/index.html" title="PostgreSQL BuildFarm Home">Home</a></li>
40     <li id="status"><a href="/cgi-bin/show_status.pl" title="Current results">Status</a></li>
41     <li id="members"><a href="/cgi-bin/show_members.pl" title="Platforms tested">Members</a></li>
42     <li id="register"><a href="/cgi-bin/register-form.pl" title="Join PostgreSQL BuildFarm">Register</a></li>
43     <li id="pgfoundry"><a href="http://pgfoundry.org/projects/pgbuildfarm/">PGFoundry</a></li>
44     <li id="postgresql.org"><a href="http://www.postgresql.org">PostgreSQL.org</a></li>
45 </ul>
46 </div><!-- nav -->
47 </div><!-- banner -->
48 <div id="main">
49 EOS
50
51 my $footer = <<EOS;
52 </div><!-- main -->
53 <hr />
54 <p style="text-align: center;">
55 Hosting for the PostgreSQL Buildfarm is generously 
56 provided by: 
57 <a href="http://www.commandprompt.com">CommandPrompt, 
58 The PostgreSQL Company</a>
59 </p>
60 </div><!-- wrapper -->
61 </body>
62 </html>
63 EOS
64
65 my $query = new CGI;
66
67 my $params = $query->Vars;
68
69 my ($os, $osv, $comp, $compv, $arch, $email, $owner, $challenge, $response ) = @{$params}{
70         qw(os osv comp compv arch email owner recaptcha_challenge_field recaptcha_response_field)};
71
72 my $captcha = Captcha::reCAPTCHA->new;
73 my $captcha_ok = $captcha->check_answer
74     (
75      $captcha_privkey, 
76      $ENV{'REMOTE_ADDR'},
77      $challenge, $response
78      );
79
80
81 unless ($os && $osv && $comp && $compv && $arch && $email && $owner && $captcha_ok->{is_valid})
82 {
83         print "Content-Type: text/html\n\n",
84         $header,
85         "<p>You need to complete all the form items. <a href=\"/cgi-bin/register-form.pl\">Please try again.</a></p>\n",
86         $footer;
87         exit;
88 }
89
90 # some idiot has a script that tries to talk to me
91 # this should catch and dispose of him
92 if ((grep {/rgergerger|\@pgbuildfarm\.org|Content-Type:|http:|mailto:|href=|None|Unknown/} $os,$osv,$comp,$compv,$arch,$email,$owner)
93     || ($email =~ /john.*\@aol.com/) )
94 {
95     print 
96         "Status: 403 Forbidden - go away idiot\n",
97         "Content-Type: text/plain\n\n";
98     exit;    
99 }
100
101 # count transitions to and from upper case
102 my $trans = 1;
103 my $counttrans = 0;
104 foreach (split "" ,"$os$osv$comp$compv$arch$owner")
105 {
106         if (/[A-Z]/)
107         {
108                 next if $trans;
109                 $trans = 1;
110                 $counttrans++;
111         }
112         else
113         {
114                 next unless $trans;
115                 $trans = 0;
116                 $counttrans++;
117         }
118 }
119
120 # reject junk with too many transitions into/outof upper case
121 if ($counttrans > 20)
122 {
123     print 
124         "Status: 403 Forbidden - go away idiot\n",
125         "Content-Type: text/plain\n\n";
126     exit;   
127 }
128
129
130
131 my $secret = "";
132 my $dummyname=""; # we'll select an animal name when we approve it.
133 foreach (1..8)
134 {
135         # 8 random chars is enough for the dummy name
136         $secret .= substr("0123456789abcdefghijklmnopqrstuvwxyz",int(rand(36)),1);
137         $dummyname .= substr("0123456789abcdef",int(rand(16)),1);
138 }
139 foreach (9..32)
140 {
141         $secret .= substr("0123456789abcdef",int(rand(16)),1);
142 }
143
144 my $db = DBI->connect($dsn,$dbuser,$dbpass);
145
146 my $statement = <<EOS;
147
148   insert into buildsystems 
149     (name, secret, operating_system, os_version, compiler, compiler_version,
150      architecture, status, sys_owner, owner_email)
151   values(?,?,?,?,?,?,?,'pending',?,?)
152
153 EOS
154 ;
155
156 my $sth=$db->prepare($statement);
157 my $rv=$sth->execute($dummyname,$secret,$os,$osv,$comp,$compv,
158                           $arch,$owner,$email);
159 my $err=$db->errstr;
160 print "Content-type: text/html\n\n";
161 print $header
162     , "<h1>PostgreSQL BuildFarm Application received</h1>\n"
163     , "<p>Thank you. You should hear from us shortly.</p>"
164     , $footer;
165
166
167 $sth->finish;
168 $db->disconnect;
169
170
171 use Mail::Send;
172
173 my $msg = new Mail::Send;
174
175 my $me = `id -un`;
176
177 my $host = `hostname`;
178
179 $msg->set('From',"PG Build Farm <$me\@$host>");
180
181 $msg->to(@$notifyapp);
182 $msg->subject('New Buildfarm Application');
183 my $fh = $msg->open;
184 print $fh "\n\nName: $dummyname\n",
185     "OS: $os: $osv\n",
186     "Arch: $arch\n",
187     "Comp: $comp: $compv\n",
188     "Owner: $owner <$email>\n";
189 $fh->close;
190
191
192
193
194
195