Add support for editing project settings
[girocco/mytab.git] / cgi / reguser.cgi
blob8e636a9ca0af865b5343cfb676e6cd43dd393f9f
1 #!/usr/bin/perl
2 # (c) Petr Baudis <pasky@suse.cz>
3 # GPLv2
5 use strict;
6 use warnings;
7 use CGI qw(:standard :escapeHTML -nosticky);
8 use CGI::Util qw(unescape);
9 use CGI::Carp qw(fatalsToBrowser);
11 my $cgi = CGI->new;
13 print $cgi->header(-type=>'text/html', -charset => 'utf-8');
15 print "<html><head><title>Git User Registration</title></head>\n";
16 print "<body><h1>Register User</h1></body>\n";
18 sub add_user {
19 my ($name, $email) = @_;
20 my $uid = 65536;
21 # racy!
22 open F, "/home/repo/j/etc/passwd" or die "passwd failed: $!";
23 while (<F>) {
24 my $auid = (split /:/)[2];
25 $uid = $auid if ($auid > $uid);
27 close F;
28 open F, ">>/home/repo/j/etc/passwd" or die "passwd append failed: $!";
29 print F "$name:x:$uid:$email:/:/bin/git-shell\n";
30 close F;
31 $uid;
34 sub setup_user {
35 my ($name, $email, $keys) = @_;
36 add_user($name, $email);
37 open F, ">/home/repo/j/etc/sshkeys/$name" or die "sshkey failed: $!";
38 print F "$keys\n";
39 close F;
40 chmod 0664, "/home/repo/j/etc/sshkeys/$name";
41 print "<p>";
42 print "User successfuly registered. ";
43 print "You (or whoever knows the project password) can <a href=\"p/editproj.pl\">assign it</a> to a project now (use project name as username, admin password as password). ";
44 print "(One user can have push access to multiple projects and multiple users can have push access to one project.)";
45 print "</p>\n";
46 print "<p>Congratulations!</p>\n";
47 print "</body></html>\n";
50 if ($cgi->param('name')) {
51 # submitted, let's see
52 # FIXME: racy, do a lock
53 my $err = 0;
54 sub err { print "<p style=\"text-color: red\">@_</p>\n"; $err++; }
55 my $name = $cgi->param('name'); $name =~ s/^\s*(.*?)\s*$/$1/;
56 my $email = $cgi->param('email'); $email =~ s/^\s*(.*?)\s*$/$1/;
57 my $keys = $cgi->param('keys');
58 $name =~ /^[a-zA-Z0-9_+-]+$/
59 or err "Name contains invalid characters.";
60 (-e "/home/repo/j/etc/sshkeys/$name")
61 and err "User with that name already exists.";
62 $email =~ /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/
63 or err "Your email sure looks weird...?";
64 length($keys) <= 4096
65 or err "The list of keys is more than 4kb. Do you really need that much?";
66 if ($err) {
67 print "<p>Registration aborted due to $err errors.</p>\n";
68 } else {
69 setup_user($name, $email, $keys);
70 exit;
74 print "<p>Here you can register a user. You need to register a user so that it can be granted push access to project(s). SSH is used for pushing (the git+ssh protocol) and what authenticates you is your SSH key - there is no password (though we recommend that your SSH key is password-protected). You can find your public key in ~/.ssh/id_rsa.pub or ~/.ssh/id_dsa.pub; if you do not have any yet, generate it using the ssh-keygen command. You can paste multiple keys in the box below, if you wish, each on a separate line. Changing the keys later is not implemented yet - if you need to do that, please contact the administrator.</p>\n";
75 print "<p>We won't bother to verify your email contact, but fill in something sensible in your own interest so that we can contact you or confirm your identity shall the need arise.</p>\n";
76 print "<p>By submitting this form, you are confirming that you will push only free software and no content that would violate any law of Czech Republic. Have fun!</p>\n";
77 print "<form method=\"post\">\n";
78 print "<p>Login: <input type=\"text\" name=\"name\" /></p>\n";
79 print "<p>Public SSH key(s): <textarea name=\"keys\" cols=\"80\" rows=\"10\"></textarea></p>\n";
80 print "<p><input type=\"submit\" name=\"y0\" value=\"Register\" /></p>\n";
81 print "</form>\n";
82 print "</body></html>\n";