svn clones: fix prefix computation
[girocco.git] / cgi / edituser.cgi
blob5b6d80bd26913cd3de3cfd3f4355912a7bd7019a
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 $y0 = $cgi->param('y0') || '';
47 if ($cgi->param('name') && $y0 && $cgi->request_method eq 'POST') {
48 # submitted, let's see
49 # FIXME: racy, do a lock
50 my $name = $gcgi->wparam('name');
51 (Girocco::User::valid_name($name)
52 and Girocco::User::does_exist($name))
53 or $gcgi->err("Username is not registered.");
55 $gcgi->err_check and exit;
57 my $user;
58 ($user = Girocco::User->load($name)) && valid_email($user->{email})
59 or $gcgi->err("Username may not be updated.");
61 $gcgi->err_check and exit;
63 if (!$cgi->param('auth')) {
64 if ($y0 ne 'Send authorization code') {
65 print "<p>Invalid data. Go away, sorcerer.</p>\n";
66 exit;
69 my $auth = $user->gen_auth;
71 # Send auth mail
72 open(MAIL, '|-', '/usr/bin/mail', '-s', "[$Girocco::Config::name] Account update authorization", $user->{email}) or
73 die "Sorry, could not send authorization code: $!";
74 print MAIL <<EOT;
75 Hello,
77 you have requested an authorization code to be sent to you for updating
78 your account's email and/or SSH keys. If you don't want to actually update
79 your email or SSH keys, just ignore this e-mail. Otherwise, use this code
80 within 24 hours:
82 $auth
84 Should you run into any problems, please let us know.
86 Have fun!
87 EOT
88 close MAIL;
90 print "<p>You should shortly receive an e-mail containing an authorization code.
91 Please enter this code below to update your SSH keys.
92 The code will expire in 24 hours or after you have used it.</p>";
93 _auth_form($name, "'Login'");
94 exit;
95 } else {
96 $user->{auth} && $user->{authtype} ne 'DEL' or do {
97 print "<p>There currently isn't any authorization code filed under your account. ".
98 "Please <a href=\"@{[url_path($Girocco::Config::webadmurl)]}/edituser.cgi\">generate one</a>.</p>";
99 exit;
102 my $fields = '';
103 my $email = $cgi->param('email');
104 my $keys = $cgi->param('keys');
106 my $auth = $gcgi->wparam('auth');
107 if ($auth ne $user->{auth}) {
108 print "<p>Invalid authorization code, please re-enter or ".
109 "<a href=\"@{[url_path($Girocco::Config::webadmurl)]}/edituser.cgi\">generate a new one</a>.</p>";
110 _auth_form($name, "'Login'");
111 exit;
114 if (defined($email) && defined($keys)) {
115 if ($y0 ne 'Update') {
116 print "<p>Invalid data. Go away, sorcerer.</p>\n";
117 exit;
120 # Auth valid, keys given -> save
121 if (($email eq $user->{email} || $user->update_email($gcgi, $email)) && $user->keys_fill($gcgi)) {
122 $user->del_auth;
123 $user->keys_save;
124 print "<p>Your Email &amp; SSH keys have been updated.</p>";
125 my $keylist = $user->keys_html_list;
126 if ($keylist) {
127 print <<EOT;
129 <div id="keys"><p>The following keys have been registered for user $name as
130 shown below along with their <tt>ssh-keygen -l</tt> fingerprint:</p>
131 $keylist</div>
134 exit;
136 $y0 = "'Login'";
137 } else {
138 # Otherwise pre-fill fields
139 $email = $user->{email};
140 $keys = $user->{keys};
143 if ($y0 ne "'Login'") {
144 print "<p>Invalid data. Go away, sorcerer.</p>\n";
145 exit;
148 my $httpspara = '';
149 $httpspara = <<EOT if $Girocco::Config::httpspushurl;
150 <p>Please be sure to include at least one RSA key (starts with the <tt>ssh-rsa</tt> prefix) in
151 order to enable HTTPS pushing. <sup><a href="@{[url_path($Girocco::Config::htmlurl)]}/httpspush.html">(learn more)</a></sup><br />
152 X.509 (e.g. OpenSSL) format public keys can be converted to SSH .pub format with the
153 <a href="http://repo.or.cz/w/ezcert.git/blob/master:/ConvertPubKey">ConvertPubKey</a> utility thus obviating the
154 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>
156 my $emailval = CGI::escapeHTML($email);
157 my $keysval = CGI::escapeHTML($keys);
158 print <<EOT;
159 <p>Authorization code validated (for now).</p>
160 <p>SSH (the <tt>ssh</tt> protocol) or HTTPS is used for pushing, your SSH key authenticates you -
161 there is no password (though we recommend that your SSH key is password-protected;
162 use <code>ssh-agent</code> to help your fingers).
163 You can find your public key in <tt>~/.ssh/id_rsa.pub</tt> or <tt>~/.ssh/id_dsa.pub</tt>.
164 If you do not have any yet, generate it using the <code>ssh-keygen</code> command.</p>
165 <p>You can paste multiple keys in the box below, each on a separate line.
166 Paste each key <em>including</em> the <tt>ssh-</tt>whatever prefix and email-like postfix.</p>
167 $httpspara<form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/edituser.cgi">
168 <input type="hidden" name="name" value="$name" />
169 <input type="hidden" name="auth" value="$auth" />
170 <table class="form">
171 <tr><td class="formlabel">Login:</td><td class="formdata">$name</td></tr>
172 <tr><td class="formlabel">Email:</td><td><input type="text" name="email" value="$emailval"/></td></tr>
173 <tr><td class="formlabel">Public SSH key(s):</td><td><textarea wrap="off" name="keys" rows="5" cols="80">$keysval</textarea></td></tr>
174 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="Update" /></td></tr>
175 </table>
177 my $keylist = $user->keys_html_list;
178 if ($keylist) {
179 print <<EOT;
181 <div id="keys"><p>The following keys are currently registered for user $name as
182 shown below along with their <tt>ssh-keygen -l</tt> fingerprint:</p>
183 $keylist</div>
186 exit;
191 my $blurb1 = '';
192 $blurb1 = 'SSH (the <tt>ssh</tt> protocol)'
193 if $Girocco::Config::pushurl && !$Girocco::Config::httpspushurl;
194 $blurb1 = 'HTTPS (the <tt>https</tt> protocol)'
195 if !$Girocco::Config::pushurl && $Girocco::Config::httpspushurl;
196 $blurb1 = 'SSH (the <tt>ssh</tt> protocol) or HTTPS (the <tt>https</tt> protocol)'
197 if $Girocco::Config::pushurl && $Girocco::Config::httpspushurl;
198 my $blurb2 = '';
199 $blurb2 = ' and download https push user authentication certificate(s)'
200 if $Girocco::Config::httpspushurl;
201 print <<EOT;
202 <p>Here you may update the email and public SSH key(s) associated with your user account$blurb2.
203 You may <a href="@{[url_path($Girocco::Config::webadmurl)]}/deluser.cgi">request an authorization
204 code in order to remove your user account from this site</a>.</p>
205 <p>The public SSH key(s) are required for you to push to projects.
206 $blurb1 is used for pushing, your SSH key authenticates you -
207 there is no password (though we recommend that your SSH key is password-protected;
208 use <code>ssh-agent</code> to help your fingers).
209 You can find your public key in <tt>~/.ssh/id_rsa.pub</tt> or <tt>~/.ssh/id_dsa.pub</tt>.
210 If you do not have any yet, generate it using the <code>ssh-keygen</code> command.</p>
212 <p>Please enter your username below;
213 we will send you an email with an authorization code
214 and further instructions.</p>
216 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/edituser.cgi">
217 <table class="form">
218 <tr><td class="formlabel">Login:</td><td><input type="text" name="name" /></td></tr>
219 <tr style="display:none"><td class="formlabel">Anti-captcha (leave empty!):</td><td><input type="text" name="mail" /></td></tr>
220 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="Send authorization code" /></td></tr>
221 </table>
222 </form>