Factor out uid/gid allocation to next_free_id()
[girocco.git] / cgi / regproj.cgi
blobde57e95db3689bb7480468f1a3b0f96a17def847
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('Project Registration');
12 my $cgi = $repo->cgi;
14 sub save_proj_data {
15 my ($path, $url, $email, $desc, $hp) = @_;
16 open F, ">$path/base_url" or die "base_url failed: $!"; print F "$url\n"; close F;
17 open F, ">$path/owner" or die "owner failed: $!"; print F "$email\n"; close F;
18 open F, ">$path/description" or die "desc failed: $!"; print F "$desc\n"; close F;
19 open F, ">$path/homepage" or die "hp failed: $!"; print F "$hp\n"; close F;
22 sub add_group {
23 my ($name, $pwd, $xtra) = @_;
24 # racy!
25 my $gid = next_free_id(jailed_file('/etc/group'));
26 open F, ">>".jailed_file("/etc/group") or die "group append failed: $!";
27 print F "$name:".scrypt($pwd).":$gid:$xtra\n";
28 close F;
29 $gid;
32 sub setup_push {
33 my ($name, $pwd, $email, $url, $desc, $hp) = @_;
34 system("cg-admin-setuprepo -g repo /srv/git/$name.git") == 0 or die "cg-admin-setuprepo failed: $?";
35 open X, ">/srv/git/$name.git/.nofetch" or die "nofetch failed: $!"; close X;
36 save_proj_data("/srv/git/$name.git", $url, $email, $desc, $hp);
37 chmod 0664, map { "/srv/git/$name.git/$_" } qw(base_url owner description homepage);
38 add_group($name, $pwd, '');
39 print <<EOT;
40 <p>
41 Project successfuly set up.
42 You can <a href="p/editproj.pl">assign users</a> now (use project name as username, admin password as password).
43 You need to <a href="reguser.pl">register a user</a> first if you have not done so yet.
44 (One user can have push access to multiple projects and multiple users can have push access to one project.)
45 </p>
46 <p>You may experience permission problems if you try to push right now. If so, that should get fixed automagically in few minutes.</p>
47 <p>Enjoy yourself!</p>
48 EOT
51 sub setup_mirror {
52 my ($name, $pwd, $email, $url, $desc, $hp) = @_;
53 mkdir "/home/repo/repodata/to-clone/$name" or die "mkdir failed: $!";
54 save_proj_data("/home/repo/repodata/to-clone/$name", $url, $email, $desc, $hp);
55 chmod 0775, "/home/repo/repodata/to-clone/$name" or die "chmod failed: $!";
56 add_group($name, $pwd, ':');
57 print "<p>Initiated mirroring. You will be notified by mail about results.</p>\n";
60 if ($cgi->param('name')) {
61 # submitted, let's see
62 # FIXME: racy, do a lock
63 my $name = $repo->wparam('name');
64 my $email = $repo->wparam('email');
65 my $url = $repo->wparam('url');
66 my $desc = $repo->wparam('desc');
67 my $hp = $repo->wparam('hp');
68 my $pwd = $cgi->param('pwd');
69 $name =~ /^[a-zA-Z0-9_+-]+$/
70 or $repo->err "Name contains invalid characters.";
71 (-d "/srv/git/$name.git" or -d "/home/repo/repodata/cloning/$name" or -d "/home/repo/repodata/to-clone/$name")
72 and $repo->err "Project with that name already exists.";
73 if ($url) {
74 $url =~ /^http:\/\/[a-zA-Z0-9-.]+\/[_\%a-zA-Z0-9.\/~-]+$/ or
75 $url =~ /^git:\/\/[a-zA-Z0-9-.]+\/[_\%a-zA-Z0-9.\/~-]+$/
76 or $repo->err "Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.";
77 } else {
78 $cgi->param('mode') eq 'mirror'
79 and $repo->err "Missing URL.";
81 if ($hp) {
82 $hp =~ /^http:\/\/[a-zA-Z0-9-.]+\/[_\%a-zA-Z0-9.\/~-]+$/
83 or $repo->err "Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.";
85 $email =~ /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/
86 or $repo->err "Your email sure looks weird...?";
87 length($desc) <= 1024
88 or $repo->err "<b>Short</b> description length > 1kb!";
89 unless ($repo->err_check) {
90 if ($cgi->param('mode') eq 'mirror') {
91 setup_mirror($name, $pwd, $email, $url, $desc, $hp);
92 } elsif ($cgi->param('mode') eq 'push') {
93 setup_push($name, $pwd, $email, $url, $desc, $hp);
95 exit;
99 print <<EOT;
100 <p>Here you can register a project. It can be hosted in two modes: mirror mode and push mode. In the first mode, we will check another repository at a given URL each hour and mirror any new updates. In the second mode, registered users with appropriate permissions will be able to push to the repository. You currently cannot switch freely between those two modes; if you want to switch an existing project, please contact the administrator.</p>
101 <p>You will need the admin password to adjust project settings later (mirroring URL, list of users allowed to push, project description, ...). Use the project name as the username when asked by the browser.</p>
102 <p>By submitting this form, you are confirming that the repository contains only free software and redistributing it does not violate any law of Czech Republic. Have fun!</p>
103 <form method="post">
104 <p>Project name (w/o the .git suffix): <input type="text" name="name" /></p>
105 <p>Admin password: <input type="password" name="pwd" /></p>
106 <p>E-mail contact: <input type="text" name="email" /></p>
107 <p>Description: <input type="text" name="desc" /></p>
108 <p>Homepage URL: <input type="text" name="hp" /></p>
109 <p>Hosting mode:</p><ul>
110 <li><input type="radio" name="mode" value="mirror" />Mirror mode. Repository URL: <input type="text" name="url" /></li>
111 <li><input type="radio" name="mode" value="push" />Push mode.</li></ul>
112 <p><input type="submit" name="y0" value="Register" /></p>
113 </form>