edituser.cgi: Fix display:none usage
[girocco.git] / cgi / edituser.cgi
blobf8c6e6f2020af698949b33490e872961828380a3
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 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="40" /></p>));
36 print <<EOT;
38 <form method="post">
39 <input type="hidden" name="name" value="$name">
40 $authtag
41 $fields<p><input type="submit" value="$submit" /></p>
42 EOT
45 if ($cgi->param('name')) {
46 # submitted, let's see
47 # FIXME: racy, do a lock
48 my $name = $gcgi->wparam('name');
49 (Girocco::User::valid_name($name)
50 and Girocco::User::does_exist($name))
51 or $gcgi->err("Username is not registered.");
53 $gcgi->err_check and exit;
55 my $user = Girocco::User->load($name) or
56 die "Failed loading user $name - but this can't really happen here";
58 if (!$cgi->param('auth')) {
59 my $auth = $user->gen_auth;
61 # Send auth mail
62 open(MAIL, '|-', '/usr/bin/mail', '-s', "[$Girocco::Config::name] Account update authorization", $user->{email}) or
63 die "Sorry, could not send authorization code: $!";
64 print MAIL <<EOT;
65 Hello,
67 you have requested an authorization code to be sent to you for updating your
68 account's SSH keys. If you don't want to actually update your SSH keys, just
69 ignore this e-mail. Otherwise, use this code within 24 hours:
71 $auth
73 Should you run into any problems, please let us know.
75 Have fun!
76 EOT
77 close MAIL;
79 print "<p>You should shortly receive an e-mail containing an authorization code.
80 Please enter this code below to update your SSH keys.
81 The code will expire in 24 hours or after you have used it.</p>";
82 _auth_form($name, "'Login'");
83 exit;
84 } else {
85 $user->{auth} or
86 die("There currently isn't any authorization code filed under your account. Please <a href=\"edituser.cgi\">generate one</a>.");
88 my $fields = '';
89 my $keys = $cgi->param('keys') || '';
90 if ($keys) {
91 $fields = "<p>Public SSH key(s): <textarea wrap=\"off\" name=\"keys\" cols=\"80\" rows=\"5\">$keys</textarea></p>\n";
94 my $auth = $gcgi->wparam('auth');
95 if ($auth ne $user->{auth}) {
96 print '<p>Invalid authorization code, please re-enter or <a href="edituser.cgi">generate a new one</a>.</p>';
97 _auth_form($name, "'Login'", $fields);
98 exit;
101 # Auth valid, keys given -> save
102 if ($keys) {
103 $user->keys_fill($gcgi);
104 $user->del_auth;
105 $user->keys_save;
106 print "<p>Your SSH keys have been updated.</p>";
107 exit;
110 # Otherwise pre-fill keys
111 $keys = $user->{keys};
112 $fields = "<p>Public SSH key(s): <textarea name=\"keys\" cols=\"80\" rows=\"5\">$keys</textarea></p>\n";
114 print "<p>Authorization code validated (for now).</p>
115 <p>You can paste multiple keys in the box below, each on a separate line.
116 Paste each key <em>including</em> the <tt>ssh-</tt>whatever prefix and email-like postfix.</p>\n";
117 _auth_form($name, "Update keys", $fields, $auth);
118 exit;
123 print <<EOT;
124 <p>Here you can update the public SSH keys associated with your user account.
125 These keys are required for you to push to projects.</p>
126 <p>SSH is used for pushing (the <tt>ssh</tt> protocol), your SSH key authenticates you -
127 there is no password (though we recommend that your SSH key is password-protected;
128 use <code>ssh-agent</code> to help your fingers).
129 You can find your public key in <tt>~/.ssh/id_rsa.pub</tt> or <tt>~/.ssh/id_dsa.pub</tt>.
130 If you do not have any yet, generate it using the <code>ssh-keygen</code> command.</p>
132 <p>Please enter your username below;
133 we will send you an email with an authorization code
134 and further instructions.</p>
136 <form method="post">
137 <table class="form">
138 <tr><td class="formlabel">Login:</td><td><input type="text" name="name" /></td></tr>
139 <tr style="display:none"><td class="formlabel">Anti-captcha (leave empty!):</td><td><input type="text" name="mail" /></td></tr>
140 <tr><td class="formlabel"></td><td><input type="submit" value="Send authorization code" /></td></tr>
141 </table>
142 </form>