cgi/html.cgi: Introduce simple template engine
[girocco.git] / cgi / edituser.cgi
blob8ab2e7e6e625b63a65a18a0070ef63386555e645
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 if ($cgi->param('mail')) {
19 print "<p>Go away, bot.</p>";
20 exit;
23 sub _auth_form {
24 my $name = shift;
25 my $submit = shift;
26 my $fields = shift;
27 $fields = '' if (!$fields);
28 my $auth = shift;
29 my $authtag = ($auth ? qq(<input type="hidden" name="auth" value="$auth" />) :
30 qq(<p>Authorization code: <input name="auth" size="40" /></p>));
31 print <<EOT;
33 <form method="post">
34 <input type="hidden" name="name" value="$name">
35 $authtag
36 $fields<p><input type="submit" value="$submit" /></p>
37 EOT
40 if ($cgi->param('name')) {
41 # submitted, let's see
42 # FIXME: racy, do a lock
43 my $name = $gcgi->wparam('name');
44 !Girocco::User::valid_name($name)
45 or !Girocco::User::does_exist($name)
46 and $gcgi->err("Username is not registered.");
48 $gcgi->err_check and exit;
50 my $user = Girocco::User->load($name) or
51 die "Failed loading user but this can't really happen here";
53 if (!$cgi->param('auth')) {
54 my $auth = $user->gen_auth;
56 # Send auth mail
57 open(MAIL, '|-', '/usr/bin/mail', '-s', '[repo.or.cz] Account update authorization', $user->{email}) or
58 die "Sorry, could not send authorization code: $!";
59 print MAIL <<EOT;
60 Hello,
62 you have requested that an authorization code be sent to you for updating your
63 account's SSH keys. If you don't want to actually update your SSH keys, just
64 ignore this e-mail. Otherwise, use this code within 24 hours:
66 $auth
68 Should you run into any problems, please let me know.
70 Thanks for using repo.or.cz!
72 Petr Baudis <pasky\@suse.cz>
73 EOT
74 close MAIL;
76 print "<p>You should shortly receive an e-mail containing an authorization code. Please enter this code below to update your SSH keys.
77 The code will expire in 24 hours or after you have used it.</p>";
78 _auth_form($name, "'Login'");
79 exit;
80 } else {
81 $user->{auth} or
82 die("There currently isn't any authorization code filed under your account. Please <a href=\"edituser.cgi\">generate one</a>.");
84 my $fields = '';
85 my $keys = $cgi->param('keys') || '';
86 if ($keys) {
87 $fields = "<p>Public SSH key(s): <textarea name=\"keys\" cols=\"80\" rows=\"5\">$keys</textarea></p>\n";
90 my $auth = $gcgi->wparam('auth');
91 if ($auth ne $user->{auth}) {
92 print '<p>Invalid authorization code, please re-enter or <a href="edituser.cgi">generate a new one</a>.</p>';
93 _auth_form($name, "'Login'", $fields);
94 exit;
97 # Auth valid, keys given -> save
98 if ($keys) {
99 $user->keys_fill($gcgi);
100 $user->del_auth;
101 $user->keys_save;
102 print "<p>Your SSH keys have been updated.</p>";
103 exit;
106 # Otherwise pre-fill keys
107 $keys = $user->{keys};
108 $fields = "<p>Public SSH key(s): <textarea name=\"keys\" cols=\"80\" rows=\"5\">$keys</textarea></p>\n";
110 print "<p>Authorization code validated (for now).</p>
111 <p>You can paste multiple keys in the box below, each on a separate line.
112 Paste each key <em>including</em> the <tt>ssh-</tt>whatever prefix and email-like postfix.</p>\n";
113 _auth_form($name, "Update keys", $fields, $auth);
114 exit;
119 print <<EOT;
120 <p>Here you can update the public SSH keys associated with your user account. These keys are required for you to push to projects.</p>
121 <p>SSH is used for pushing (the <tt>ssh</tt> protocol), your SSH key authenticates you -
122 there is no password (though we recommend that your SSH key is password-protected; use <code>ssh-agent</code> to help your fingers).
123 You can find your public key in <tt>~/.ssh/id_rsa.pub</tt> or <tt>~/.ssh/id_dsa.pub</tt>.
124 If you do not have any yet, generate it using the <code>ssh-keygen</code> command.</p>
126 <p>Please enter your username below so we can send you an authorization code to the e-mail address you gave us when you registered the account.</p>
128 <form method="post">
129 <p>Login: <input type="text" name="name" /></p>
130 <p style="display:none">Anti-captcha (leave empty!): <input type="text" name="mail" /></p>
131 <p><input type="submit" value="Send authorization code" /></p>
132 </form>