mirroring: add individual foreign vcs mirror control
[girocco.git] / cgi / pwproj.cgi
blob36fb6beabeb661d620cd4a3a83a48f6626fae46b
1 #!/usr/bin/perl
2 # (c) Petr Baudis <pasky@suse.cz>
3 # Portions Copyright (c) Kyle J. McKay <mackyle@gmail.com>
4 # GPLv2
6 use strict;
7 use warnings;
9 use lib ".";
10 use Girocco::CGI;
11 use Girocco::Config;
12 use Girocco::Project;
13 use Girocco::Util;
15 my $gcgi = Girocco::CGI->new('Forgotten Project Password');
16 my $cgi = $gcgi->cgi;
18 unless ($Girocco::Config::project_passwords) {
19 print "<p>I don't manage passwords.</p>";
20 exit;
23 my $name = $cgi->param('name');
25 unless (defined $name) {
26 print "<p>I need the project name as an argument.</p>\n";
27 exit;
30 if (!Girocco::Project::does_exist($name,1) && !Girocco::Project::valid_name($name)) {
31 print "<p>Invalid project name. Go away, sorcerer.</p>\n";
32 exit;
35 if (!Girocco::Project::does_exist($name,1)) {
36 print "<p>Sorry but this project does not exist. Now, how did you <em>get</em> here?!</p>\n";
37 exit;
40 my $proj = Girocco::Project->load($name);
41 $proj or die "not found project $name, that's really weird!";
42 my $escname = $name;
43 $escname =~ s/[+]/%2B/g;
45 my $mail = $proj->{email};
47 my $y0 = $cgi->param('y0') || '';
48 if ($y0 eq 'Send authorization code' && $cgi->request_method eq 'POST') {
49 # submitted
51 my $auth = $proj->gen_auth('PWD');
53 defined(my $MAIL = mailer_pipe '-s',
54 "[$Girocco::Config::name] Password change authorization for project $name", $mail)
55 or die "Cannot spawn mailer: $!";
56 print $MAIL <<EOT;
57 Hello,
59 Somebody asked for a password change authorization code to be sent for
60 project $name on $Girocco::Config::name. Since you are the project admin,
61 you receive the authorization code. If you don't want to actually change
62 the password for project $name, just ignore this e-mail. Otherwise use
63 this code within 24 hours:
65 $auth
67 In case you did not request a password change authorization code, we
68 apologize.
70 Should you run into any problems, please let us know.
72 Have fun!
73 EOT
74 close $MAIL or die "mail $mail for $name died? $!";
76 print <<EOT;
77 <p>The project admin should shortly receive an e-mail containing a project
78 password change authorization code. Please enter this code below to change
79 the password for project $name on $Girocco::Config::name. The code will
80 expire in 24 hours or after you have used it to successfully change the
81 password.</p>
82 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi">
83 <input type="hidden" name="name" value="$name" />
84 <p>Authorization code: <input name="auth" size="50" /></p>
85 <p><input type="submit" name="y0" value="Validate code" /></p>
86 </form>
87 EOT
88 exit;
90 if (($y0 eq 'Validate code' || $y0 eq 'Change password') && $cgi->request_method eq 'POST') {
91 # validation & change
93 $proj->{auth} && $proj->{authtype} && $proj->{authtype} eq 'PWD' or do {
94 print <<EOT;
95 <p>There currently isn't any project password change authorization code on file for
96 project $name. Please <a href="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi?name=$escname"
97 >generate one</a>.</p>
98 EOT
99 exit;
101 my $auth = $gcgi->wparam('auth');
102 if ($auth ne $proj->{auth}) {
103 print <<EOT;
104 <p>Invalid authorization code, please re-enter or
105 <a href="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi?name=$escname"
106 >generate a new one</a>.</p>
107 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi">
108 <input type="hidden" name="name" value="$name" />
109 <p>Authorization code: <input name="auth" size="50" /></p>
110 <p><input type="submit" name="y0" value="Validate code" /></p>
111 </form>
113 exit;
115 if ($y0 eq 'Change password') {
116 # changing password
117 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
118 $pwd ||= ''; $pwd2 ||= '';
119 if ($pwd ne $pwd2) {
120 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
121 } elsif (!$pwd || !$pwd2) {
122 $gcgi->err("Empty passwords are not permitted.");
123 } else {
124 $proj->del_auth;
125 $proj->update_password($pwd);
126 print <<EOT;
127 <p>The project password for project $name has been successfully changed.</p>
128 <p>You may now use the new password to edit the project settings
129 <a href="@{[url_path($Girocco::Config::webadmurl)]}/editproj.cgi?name=$escname"
130 >here</a>.</p>
131 <p>Have a nice day.</p>
133 exit;
136 print <<EOT;
137 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi">
138 <input type="hidden" name="name" value="$name" />
139 <input type="hidden" name="auth" value="$auth" />
140 <table class="form">
141 <tr><td class="formlabel">Project name:</td><td class="formdata">$name.git</td></tr>
142 <tr><td class="formlabel">New admin password (twice):</td><td><input type="password" name="pwd" /><br />
143 <input type="password" name="pwd2" /></td></tr>
144 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="Change password" /></td></tr>
145 </table>
146 </form>
148 exit;
151 if ($cgi->request_method eq 'POST') {
152 print "<p>Invalid data. Go away, sorcerer.</p>\n";
153 exit;
156 print <<EOT;
157 <p>You are trying to make me change the password for project $name. I will send
158 an authorization code to change the password to the project admin &lt;$mail&gt;.</p>
159 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi">
160 <input type="hidden" name="name" value="$name" />
161 <p><input type="submit" name="y0" value="Send authorization code" /></p>
162 </form>