update-all-config: new utility to update projects' config
[girocco.git] / cgi / edituser.cgi
blob065f17c07efd1188961b80cf6bd7bd1289175a99
1 #!/usr/bin/perl
2 # (c) Petr Baudis <pasky@suse.cz>
3 # (c) Jan Krueger <jk@jk.gs>
4 # GPLv2
6 use strict;
7 use warnings;
9 use lib ".";
10 use Girocco::CGI;
11 use Girocco::Config;
12 use Girocco::User;
13 use Girocco::Util;
15 my $gcgi = Girocco::CGI->new('User Email & SSH Key Update');
16 my $cgi = $gcgi->cgi;
18 unless ($Girocco::Config::manage_users) {
19 print "<p>I don't manage users.</p>";
20 exit;
23 if ($cgi->param('mail')) {
24 print "<p>Go away, bot.</p>";
25 exit;
28 sub _auth_form {
29 my $name = shift;
30 my $submit = shift;
31 my $fields = shift;
32 $fields = '' if (!$fields);
33 my $auth = shift;
34 my $authtag = ($auth ? qq(<input type="hidden" name="auth" value="$auth" />) :
35 qq(<p>Authorization code: <input name="auth" size="50" /></p>));
36 print <<EOT;
38 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/edituser.cgi">
39 <input type="hidden" name="name" value="$name" />
40 $authtag
41 $fields<p><input type="submit" name="y0" value="$submit" /></p>
42 </form>
43 EOT
46 my $savefail = 0;
47 my $y0 = $cgi->param('y0') || '';
48 if ($cgi->param('name') && $y0 && $cgi->request_method eq 'POST') {
49 # submitted, let's see
50 # FIXME: racy, do a lock
51 my $name = $gcgi->wparam('name');
52 (Girocco::User::valid_name($name)
53 and Girocco::User::does_exist($name))
54 or $gcgi->err("Username is not registered.");
56 $gcgi->err_check and exit;
58 my $user;
59 ($user = Girocco::User->load($name)) && valid_email($user->{email})
60 or $gcgi->err("Username may not be updated.");
62 $gcgi->err_check and exit;
64 if (!$cgi->param('auth')) {
65 if ($y0 ne 'Send authorization code') {
66 print "<p>Invalid data. Go away, sorcerer.</p>\n";
67 exit;
70 valid_email($user->{email}) or die "Sorry, this user cannot be changed.";
72 my $auth = $user->gen_auth;
74 # Send auth mail
75 defined(my $MAIL = mailer_pipe '-s', "[$Girocco::Config::name] Account update authorization", $user->{email}) or
76 die "Sorry, could not send authorization code: $!";
77 print $MAIL <<EOT;
78 Hello,
80 You have requested an authorization code to be sent to you for updating
81 your account's email and/or SSH keys. If you don't want to actually update
82 your email or SSH keys, just ignore this e-mail. Otherwise, use this code
83 within 24 hours:
85 $auth
87 Should you run into any problems, please let us know.
89 Have fun!
90 EOT
91 close $MAIL;
93 print "<p>You should shortly receive an e-mail containing an authorization code.
94 Please enter this code below to update your SSH keys.
95 The code will expire in 24 hours or after you have used it.</p>";
96 _auth_form($name, "'Login'");
97 exit;
98 } else {
99 $user->{auth} && $user->{authtype} ne 'DEL' or do {
100 print "<p>There currently isn't any authorization code filed under your account. ".
101 "Please <a href=\"@{[url_path($Girocco::Config::webadmurl)]}/edituser.cgi\">generate one</a>.</p>";
102 exit;
105 my $fields = '';
106 my $email = $cgi->param('email');
107 my $keys = $cgi->param('keys');
109 my $auth = $gcgi->wparam('auth');
110 if ($auth ne $user->{auth}) {
111 print "<p>Invalid authorization code, please re-enter or ".
112 "<a href=\"@{[url_path($Girocco::Config::webadmurl)]}/edituser.cgi\">generate a new one</a>.</p>";
113 _auth_form($name, "'Login'");
114 exit;
117 if (defined($email) && defined($keys)) {
118 if ($y0 ne 'Update') {
119 print "<p>Invalid data. Go away, sorcerer.</p>\n";
120 exit;
123 # Auth valid, keys given -> save
124 if (($email eq $user->{email} || $user->update_email($gcgi, $email)) && $user->keys_fill($gcgi)) {
125 $user->del_auth;
126 $user->keys_save;
127 print "<p>Your Email &amp; SSH keys have been updated.</p>";
128 my $keylist = $user->keys_html_list;
129 if ($keylist) {
130 print <<EOT;
132 <div id="keys"><p>The following keys have been registered for user $name as
133 shown below along with their <tt>ssh-keygen -l</tt> fingerprint:</p>
134 $keylist</div>
137 exit;
139 $y0 = "'Login'";
140 $savefail = 1;
141 } else {
142 # Otherwise pre-fill fields
143 $email = $user->{email};
144 $keys = $user->{keys}."\n";
147 if ($y0 ne "'Login'") {
148 print "<p>Invalid data. Go away, sorcerer.</p>\n";
149 exit;
152 my $httpspara = '';
153 $httpspara = <<EOT if $Girocco::Config::httpspushurl;
154 <p>Please be sure to include at least one RSA key (starts with the <tt>ssh-rsa</tt> prefix) in
155 order to enable HTTPS pushing. <sup><a href="@{[url_path($Girocco::Config::htmlurl)]}/httpspush.html">(learn more)</a></sup><br />
156 An X.509 (e.g. OpenSSL) format public key can be converted to SSH .pub format with the
157 <a href="http://repo.or.cz/w/ezcert.git/blob/master:/ConvertPubKey">ConvertPubKey</a> utility thus obviating the
158 need for OpenSSH if all pushing is to be done using HTTPS (see the example in the TIPS section of the <tt>ConvertPubKey -h</tt> output).</p>
160 my $emailval = CGI::escapeHTML($email);
161 my $keysval = CGI::escapeHTML($keys);
162 my $blurb = '';
163 $blurb = 'SSH (the <tt>ssh</tt> protocol)'
164 if $Girocco::Config::pushurl && !$Girocco::Config::httpspushurl;
165 $blurb = 'HTTPS (the <tt>https</tt> protocol)'
166 if !$Girocco::Config::pushurl && $Girocco::Config::httpspushurl;
167 $blurb = 'SSH (the <tt>ssh</tt> protocol) or HTTPS (the <tt>https</tt> protocol)'
168 if $Girocco::Config::pushurl && $Girocco::Config::httpspushurl;
169 my $dsablurb = '';
170 $dsablurb = ' or <tt>~/.ssh/id_dsa.pub</tt>' unless $Girocco::Config::disable_dsa;
171 print <<EOT;
172 <p>Authorization code validated (for now).</p>
173 <p>$blurb is used for pushing, your SSH key authenticates you -
174 there is no password (though we recommend that your SSH key is password-protected;
175 use <code>ssh-agent</code> to help your fingers).
176 You can find your public key in <tt>~/.ssh/id_rsa.pub</tt>$dsablurb.
177 If you do not have any yet, generate it using the <code>ssh-keygen</code> command.</p>
178 <p>You can paste multiple keys in the box below, each on a separate line.
179 Paste each key <em>including</em> the <tt>ssh-</tt>whatever prefix and email-like postfix.</p>
180 $httpspara<form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/edituser.cgi">
181 <input type="hidden" name="name" value="$name" />
182 <input type="hidden" name="auth" value="$auth" />
183 <table class="form">
184 <tr><td class="formlabel">Login:</td><td class="formdata">$name</td></tr>
185 <tr><td class="formlabel">Email:</td><td><input type="text" name="email" value="$emailval"/></td></tr>
186 <tr><td class="formlabel">Public SSH key(s):</td><td><textarea wrap="off" name="keys" rows="5" cols="80">$keysval</textarea></td></tr>
187 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="Update" /></td></tr>
188 </table>
190 my $keylist = $savefail ? '' : $user->keys_html_list;
191 if ($keylist) {
192 print <<EOT;
194 <div id="keys"><p>The following keys are currently registered for user $name as
195 shown below along with their <tt>ssh-keygen -l</tt> fingerprint:</p>
196 $keylist</div>
199 exit;
204 my $blurb1 = '';
205 $blurb1 = 'SSH (the <tt>ssh</tt> protocol)'
206 if $Girocco::Config::pushurl && !$Girocco::Config::httpspushurl;
207 $blurb1 = 'HTTPS (the <tt>https</tt> protocol)'
208 if !$Girocco::Config::pushurl && $Girocco::Config::httpspushurl;
209 $blurb1 = 'SSH (the <tt>ssh</tt> protocol) or HTTPS (the <tt>https</tt> protocol)'
210 if $Girocco::Config::pushurl && $Girocco::Config::httpspushurl;
211 my $blurb2 = '';
212 $blurb2 = ' and download https push user authentication certificate(s)'
213 if $Girocco::Config::httpspushurl;
214 my $dsablurb = '';
215 $dsablurb = ' or <tt>~/.ssh/id_dsa.pub</tt>' unless $Girocco::Config::disable_dsa;
216 print <<EOT;
217 <p>Here you may update the email and public SSH key(s) associated with your user account$blurb2.
218 You may <a href="@{[url_path($Girocco::Config::webadmurl)]}/deluser.cgi">request an authorization
219 code in order to remove your user account from this site</a>.</p>
220 <p>If you do not already have a user account you may
221 <a href="@{[url_path($Girocco::Config::webadmurl)]}/reguser.cgi">register user</a> instead.</p>
222 <p>The public SSH key(s) are required for you to push to projects.
223 $blurb1 is used for pushing, your SSH key authenticates you -
224 there is no password (though we recommend that your SSH key is password-protected;
225 use <code>ssh-agent</code> to help your fingers).
226 You can find your public key in <tt>~/.ssh/id_rsa.pub</tt>$dsablurb.
227 If you do not have any yet, generate it using the <code>ssh-keygen</code> command.</p>
229 <p>Please enter your username below;
230 we will send you an email with an authorization code
231 and further instructions.</p>
233 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/edituser.cgi">
234 <table class="form">
235 <tr><td class="formlabel">Login:</td><td><input type="text" name="name" /></td></tr>
236 <tr style="display:none"><td class="formlabel">Anti-captcha (leave empty!):</td><td><input type="text" name="mail" /></td></tr>
237 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="Send authorization code" /></td></tr>
238 </table>
239 </form>