Factor out CGI submitted data error processing
[girocco/ztw.git] / cgi / reguser.cgi
blobe8c979e45b71572ca51fa3a03d64f9c528a49bec
1 #!/usr/bin/perl
2 # (c) Petr Baudis <pasky@suse.cz>
3 # GPLv2
5 use strict;
6 use warnings;
8 use lib qw(/home/repo/repomgr/cgi);
9 use Git::RepoCGI;
11 my $repo = Git::RepoCGI->new('User Registration');
12 my $cgi = $repo->cgi;
14 sub add_user {
15 my ($name, $email) = @_;
16 my $uid = 65536;
17 # racy!
18 open F, "/home/repo/j/etc/passwd" or die "passwd failed: $!";
19 while (<F>) {
20 my $auid = (split /:/)[2];
21 $uid = $auid + 1 if ($auid >= $uid);
23 close F;
24 open F, ">>/home/repo/j/etc/passwd" or die "passwd append failed: $!";
25 print F "$name:x:$uid:65534:$email:/:/bin/git-shell\n";
26 close F;
27 $uid;
30 sub setup_user {
31 my ($name, $email, $keys) = @_;
32 add_user($name, $email);
33 open F, ">/home/repo/j/etc/sshkeys/$name" or die "sshkey failed: $!";
34 print F "$keys\n";
35 close F;
36 chmod 0664, "/home/repo/j/etc/sshkeys/$name";
37 print <<EOT;
38 <p>
39 User successfuly registered.
40 You (or whoever knows the project password) can <a href="p/editproj.pl">assign it</a> to a project now
41 (use project name as username, admin password as password).
42 (One user can have push access to multiple projects and multiple users can have push access to one project.)
43 </p>
44 <p>Congratulations!</p>
45 EOT
48 if ($cgi->param('name')) {
49 # submitted, let's see
50 # FIXME: racy, do a lock
51 my $name = $cgi->param('name'); $name =~ s/^\s*(.*?)\s*$/$1/;
52 my $email = $cgi->param('email'); $email =~ s/^\s*(.*?)\s*$/$1/;
53 my $keys = $cgi->param('keys');
54 $name =~ /^[a-zA-Z0-9_+-]+$/
55 or $repo->err "Name contains invalid characters.";
56 (-e "/home/repo/j/etc/sshkeys/$name")
57 and $repo->err "User with that name already exists.";
58 $email =~ /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/
59 or $repo->err "Your email sure looks weird...?";
60 length($keys) <= 4096
61 or $repo->err "The list of keys is more than 4kb. Do you really need that much?";
62 unless ($rep->err_check) {
63 setup_user($name, $email, $keys);
64 exit;
68 print <<EOT;
69 <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>
70 <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>
71 <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>
72 <form method="post">
73 <p>Login: <input type="text" name="name" /></p>
74 <p>Email: <input type="text" name="email" /></p>
75 <p>Public SSH key(s): <textarea name="keys" cols="80" rows="10"></textarea></p>
76 <p><input type="submit" name="y0" value="Register" /></p>
77 </form>
78 EOT