Set up refs/forkee in new forks
[girocco.git] / cgi / Git / RepoCGI.pm
blob48a245f94414a9bc6439368912df3c5dd4742525
1 package Git::RepoCGI;
3 use strict;
4 use warnings;
7 ### Administrativa
9 BEGIN {
10 our $VERSION = '0.1';
11 our @ISA = qw(Exporter);
12 our @EXPORT = qw(scrypt html_esc jailed_file
13 lock_file unlock_file
14 filedb_atomic_append filedb_atomic_edit
15 proj_get_forkee_name proj_get_forkee_path
16 valid_proj_name valid_user_name valid_email valid_repo_url valid_web_url);
18 use CGI qw(:standard :escapeHTML -nosticky);
19 use CGI::Util qw(unescape);
20 use CGI::Carp qw(fatalsToBrowser);
24 ### RepoCGI object
26 sub new {
27 my $class = shift;
28 my ($heading) = @_;
29 my $repo = {};
31 $repo->{cgi} = CGI->new;
33 print $repo->{cgi}->header(-type=>'text/html', -charset => 'utf-8');
35 print <<EOT;
36 <?xml version="1.0" encoding="utf-8"?>
37 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
38 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
40 <head>
41 <title>repo.or.cz :: $heading</title>
42 <link rel="stylesheet" type="text/css" href="/gitweb.css"/>
43 <link rel="shortcut icon" href="/git-favicon.png" type="image/png"/>
44 </head>
46 <body>
48 <div class="page_header">
49 <a href="http://git.or.cz/" title="Git homepage"><img src="/git-logo.png" width="72" height="27" alt="git" style="float:right; border-width:0px;"/></a>
50 <a href="/">repo.or.cz</a> / administration / $heading
51 </div>
53 EOT
55 bless $repo, $class;
58 sub DESTROY {
59 my $self = shift;
60 my $cgi = $self->cgi;
61 my $cginame = $cgi->url(-absolute => 1);
62 $cginame =~ s#^/m/##;
63 if ($cginame =~ /^[a-zA-Z0-9_.\/-]+\.cgi$/) {
64 print <<EOT;
65 <div align="right">
66 <a href="http://repo.or.cz/w/repo.git?a=blob;f=cgi/$cginame">(view source)</a>
67 </div>
68 EOT
70 print <<EOT;
71 </body>
72 </html>
73 EOT
76 sub cgi {
77 my $self = shift;
78 $self->{cgi};
81 sub err {
82 my $self = shift;
83 print "<p style=\"text-color: red\">@_</p>\n";
84 $self->{err}++;
87 sub err_check {
88 my $self = shift;
89 my $err = $self->{err};
90 $err and print "<p>Operation aborted due to $err errors.</p>\n";
91 $err;
94 sub wparam {
95 my $self = shift;
96 my ($param) = @_;
97 my $val = $self->{cgi}->param($param);
98 $val =~ s/^\s*(.*?)\s*$/$1/;
99 $val;
103 ### Random utility functions
105 sub scrypt {
106 my ($pwd) = @_;
107 crypt($pwd, join ('', ('.', '/', 2..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
110 sub html_esc {
111 my ($str) = @_;
112 $str =~ s/&/&amp;/g;
113 $str =~ s/</&lt;/g; $str =~ s/>/&gt;/g;
114 $str =~ s/"/&quot;/g;
115 $str;
118 sub jailed_file {
119 my ($filename) = @_;
120 "/home/repo/j/$filename";
123 sub lock_file {
124 my ($path) = @_;
126 $path .= '.lock';
128 use Errno qw(EEXIST);
129 use Fcntl qw(O_WRONLY O_CREAT O_EXCL);
130 use IO::Handle;
131 my $handle = new IO::Handle;
133 unless (sysopen($handle, $path, O_WRONLY|O_CREAT|O_EXCL)) {
134 my $cnt = 0;
135 while (not sysopen($handle, $path, O_WRONLY|O_CREAT|O_EXCL)) {
136 ($! == EEXIST) or die "$path open failed: $!";
137 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
138 sleep(1);
141 # XXX: filedb-specific
142 chmod 0664, $path or die "$path g+w failed: $!";
144 $handle;
147 sub unlock_file {
148 my ($path) = @_;
150 rename "$path.lock", $path or die "$path unlock failed: $!";
153 sub filedb_atomic_append {
154 my ($file, $line) = @_;
155 my $id = 65536;
157 open my $src, $file or die "$file open for reading failed: $!";
158 my $dst = lock_file($file);
160 while (<$src>) {
161 my $aid = (split /:/)[2];
162 $id = $aid + 1 if ($aid >= $id);
164 print $dst $_ or die "$file(l) write failed: $!";
167 $line =~ s/\\i/$id/g;
168 print $dst "$line\n" or die "$file(l) write failed: $!";
170 close $dst or die "$file(l) close failed: $!";
171 close $src;
173 unlock_file($file);
175 $id;
178 sub filedb_atomic_edit {
179 my ($file, $fn) = @_;
181 open my $src, $file or die "$file open for reading failed: $!";
182 my $dst = lock_file($file);
184 while (<$src>) {
185 print $dst $fn->($_) or die "$file(l) write failed: $!";
188 close $dst or die "$file(l) close failed: $!";
189 close $src;
191 unlock_file($file);
194 sub proj_get_forkee_name {
195 $_ = $_[0];
196 (m#^(.*)/.*?$#)[0];
198 sub proj_get_forkee_path {
199 my $forkee = '/srv/git/'.proj_get_forkee_name($_[0]).'.git';
200 -d $forkee ? $forkee : '';
202 sub valid_proj_name {
203 $_ = $_[0];
204 (not m#/# or -d proj_get_forkee_path($_)) # will also catch ^/
205 and (not m#\./#)
206 and m#^[a-zA-Z0-9+./_-]+$#;
208 sub valid_user_name {
209 $_ = $_[0];
210 /^[a-zA-Z0-9+._-]+$/;
212 sub valid_email {
213 $_ = $_[0];
214 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/;
216 sub valid_web_url {
217 $_ = $_[0];
218 /^http:\/\/[a-zA-Z0-9-.]+(\/[_\%a-zA-Z0-9.\/~-]*)?(#[a-zA-Z0-9._-]+)?$/;
220 sub valid_repo_url {
221 $_ = $_[0];
222 /^http:\/\/[a-zA-Z0-9-.]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/ or
223 /^git:\/\/[a-zA-Z0-9-.]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/;
227 ### Project object
229 package Git::RepoCGI::Project;
231 BEGIN { use Git::RepoCGI; }
233 sub _mkdir_forkees {
234 my $self = shift;
235 my @pelems = split('/', $self->{name});
236 pop @pelems; # do not create dir for the project itself
237 my $path = $self->{base_path};
238 foreach my $pelem (@pelems) {
239 $path .= "/$pelem";
240 (-d "$path") or mkdir $path or die "mkdir $path: $!";
241 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
245 our %propmap = (
246 url => 'base_url',
247 email => 'owner',
248 desc => 'description',
249 README => 'README.html',
250 hp => 'homepage',
253 sub _property_path {
254 my $self = shift;
255 my ($name) = @_;
256 $self->{path}.'/'.$name;
259 sub _property_fget {
260 my $self = shift;
261 my ($name) = @_;
262 my $value;
263 $propmap{$name} or die "unknown property: $name";
264 open P, $self->_property_path($propmap{$name}) or return undef;
265 chomp($value = <P>);
266 close P;
267 $value;
270 sub _property_fput {
271 my $self = shift;
272 my ($name, $value) = @_;
273 $propmap{$name} or die "unknown property: $name";
275 my $P = lock_file($self->_property_path($propmap{$name}));
276 $value ne '' and print $P "$value\n";
277 close $P;
278 unlock_file($self->_property_path($propmap{$name}));
281 sub _properties_load {
282 my $self = shift;
283 foreach my $prop (keys %propmap) {
284 $self->{$prop} = $self->_property_fget($prop);
288 sub _properties_save {
289 my $self = shift;
290 foreach my $prop (keys %propmap) {
291 $self->_property_fput($prop, $self->{$prop});
295 sub _nofetch_path {
296 my $self = shift;
297 $self->_property_path('.nofetch');
300 sub _nofetch {
301 my $self = shift;
302 my ($nofetch) = @_;
303 my $np = $self->_nofetch_path;
304 if ($nofetch) {
305 open X, '>'.$np or die "nofetch failed: $!";
306 close X;
307 } else {
308 unlink $np or die "yesfetch failed: $!";
312 sub _alternates_setup {
313 my $self = shift;
314 return unless $self->{name} =~ m#/#;
315 my $forkee_name = proj_get_forkee_name($self->{name});
316 my $forkee_path = proj_get_forkee_path($self->{name});
317 return unless -d $forkee_path;
318 mkdir $self->{path}.'/refs'; chmod 0775, $self->{path}.'/refs';
319 mkdir $self->{path}.'/objects'; chmod 0775, $self->{path}.'/objects';
320 mkdir $self->{path}.'/objects/info'; chmod 0775, $self->{path}.'/objects/info';
321 my $filename = $self->{path}.'/objects/info/alternates';
322 open X, '>'.$filename or die "alternates failed: $!";
323 # We use relative URL here so that http-fetch groks it automagically.
324 $forkee_name =~ s#^.*/##; # Only the last element of the path matters, again.
325 print X "../../../$forkee_name.git/objects\n";
326 close X;
327 chmod 0664, $filename or warn "cannot chmod $filename: $!";
328 symlink "../../../$forkee_name.git/refs", $self->{path}.'/refs/forkee';
331 sub _group_add {
332 my $self = shift;
333 my ($xtra) = @_;
334 $xtra .= join(',', @{$self->{users}});
335 filedb_atomic_append(jailed_file('/etc/group'),
336 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
339 sub _group_update {
340 my $self = shift;
341 my $xtra = join(',', @{$self->{users}});
342 filedb_atomic_edit(jailed_file('/etc/group'),
343 sub {
344 $_ = $_[0];
345 chomp;
346 if ($self->{name} eq (split /:/)[0]) {
347 # preserve readonly flag
348 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
349 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
350 } else {
351 return "$_\n";
357 sub _group_remove {
358 my $self = shift;
359 filedb_atomic_edit(jailed_file('/etc/group'),
360 sub {
361 $self->{name} ne (split /:/)[0] and return $_;
366 sub _hook_path {
367 my $self = shift;
368 my ($name) = @_;
369 $self->{path}.'/hooks/'.$name;
372 sub _hook_install {
373 my $self = shift;
374 my ($name) = @_;
375 open SRC, "/home/repo/repomgr/$name-hook" or die "cannot open hook $name: $!";
376 open DST, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
377 while (<SRC>) { print DST $_; }
378 close DST;
379 close SRC;
380 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
383 sub _hooks_install {
384 my $self = shift;
385 foreach my $hook ('update') {
386 $self->_hook_install($hook);
390 # private constructor, do not use
391 sub _new {
392 my $class = shift;
393 my ($name, $base_path, $path) = @_;
394 valid_proj_name($name) or die "refusing to create project with invalid name ($name)!";
395 $path ||= "$base_path/$name.git";
396 my $proj = { name => $name, base_path => $base_path, path => $path };
398 bless $proj, $class;
401 # public constructor #0
402 # creates a virtual project not connected to disk image
403 # you can conjure() it later to disk
404 sub ghost {
405 my $class = shift;
406 my ($name, $mirror) = @_;
407 my $self = $class->_new($name, $mirror ? "/home/repo/repodata/to-clone" : "/srv/git",
408 $mirror ? "/home/repo/repodata/to-clone/$name" : "/srv/git/$name.git");
409 $self->{users} = [];
410 $self->{mirror} = $mirror;
411 $self;
414 # public constructor #1
415 sub load {
416 my $class = shift;
417 my ($name) = @_;
419 open F, jailed_file("/etc/group") or die "project load failed: $!";
420 while (<F>) {
421 chomp;
422 @_ = split /:+/;
423 next unless (shift eq $name);
425 my $self = $class->_new($name, "/srv/git");
426 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
428 my $ulist;
429 ($self->{crypt}, $self->{gid}, $ulist) = @_;
430 $ulist ||= '';
431 $self->{users} = [split /,/, $ulist];
432 $self->{mirror} = ! -e $self->_nofetch_path;
434 $self->_properties_load;
435 return $self;
437 close F;
438 undef;
441 # $proj may not be in sane state if this returns false!
442 sub cgi_fill {
443 my $self = shift;
444 my ($repo) = @_;
445 my $cgi = $repo->cgi;
447 my $pwd = $cgi->param('pwd');
448 if ($pwd ne '' or not $self->{crypt}) {
449 $self->{crypt} = scrypt($pwd);
452 $self->{email} = $repo->wparam('email');
453 valid_email($self->{email})
454 or $repo->err("Your email sure looks weird...?");
456 $self->{url} = $repo->wparam('url');
457 if ($self->{url}) {
458 valid_repo_url($self->{url})
459 or $repo->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
462 $self->{desc} = $repo->wparam('desc');
463 length($self->{desc}) <= 1024
464 or $repo->err("<b>Short</b> description length > 1kb!");
466 $self->{README} = $repo->wparam('README');
467 length($self->{README}) <= 8192
468 or $repo->err("README length > 8kb!");
470 $self->{hp} = $repo->wparam('hp');
471 if ($self->{hp}) {
472 valid_web_url($self->{hp})
473 or $repo->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
476 # FIXME: Permit only existing users
477 $self->{users} = [grep { valid_user_name($_) } $cgi->param('user')];
479 not $repo->err_check;
482 sub form_defaults {
483 my $self = shift;
485 name => $self->{name},
486 email => $self->{email},
487 url => $self->{url},
488 desc => html_esc($self->{desc}),
489 README => html_esc($self->{README}),
490 hp => $self->{hp},
491 users => $self->{users},
495 sub premirror {
496 my $self = shift;
498 $self->_mkdir_forkees;
499 mkdir $self->{path} or die "mkdir failed: $!";
500 chmod 0775, $self->{path} or die "chmod failed: $!";
501 $self->_properties_save;
502 $self->_alternates_setup;
503 $self->_group_add(':');
506 sub conjure {
507 my $self = shift;
509 $self->_mkdir_forkees;
510 system('cg-admin-setuprepo', '-g', 'repo', $self->{path}) == 0
511 or die "cg-admin-setuprepo failed: $?";
512 $self->_nofetch(1);
513 $self->_properties_save;
514 $self->_alternates_setup;
515 $self->_group_add;
516 $self->_hooks_install;
519 sub update {
520 my $self = shift;
522 $self->_properties_save;
523 $self->_group_update;
526 # You can explicitly do this just on a ghost() repository too.
527 sub delete {
528 my $self = shift;
530 if (-d $self->{path}) {
531 system('rm', '-r', $self->{path}) == 0
532 or die "rm -r failed: $?";
534 $self->_group_remove;
537 # static method
538 sub does_exist {
539 my ($name) = @_;
540 valid_proj_name($name) or die "tried to query for project with invalid name $name!";
541 (available($name)
542 or -d "/home/repo/repodata/cloning/$name"
543 or -d "/home/repo/repodata/to-clone/$name");
545 sub available {
546 my ($name) = @_;
547 valid_proj_name($name) or die "tried to query for project with invalid name $name!";
548 (-d "/srv/git/$name.git");
552 ### User object
554 package Git::RepoCGI::User;
556 BEGIN { use Git::RepoCGI; }
558 sub _passwd_add {
559 my $self = shift;
560 filedb_atomic_append(jailed_file('/etc/passwd'),
561 join(':', $self->{name}, 'x', '\i', 65534, $self->{email}, '/', '/bin/git-shell'));
564 sub _sshkey_path {
565 my $self = shift;
566 '/etc/sshkeys/'.$self->{name};
569 sub _sshkey_save {
570 my $self = shift;
571 open F, ">".jailed_file($self->_sshkey_path) or die "sshkey failed: $!";
572 print F $self->{keys}."\n";
573 close F;
574 chmod 0664, jailed_file($self->_sshkey_path);
577 # private constructor, do not use
578 sub _new {
579 my $class = shift;
580 my ($name) = @_;
581 valid_user_name($name) or die "refusing to create user with invalid name ($name)!";
582 my $proj = { name => $name };
584 bless $proj, $class;
587 # public constructor #0
588 # creates a virtual user not connected to disk record
589 # you can conjure() it later to disk
590 sub ghost {
591 my $class = shift;
592 my ($name) = @_;
593 my $self = $class->_new($name);
594 $self;
597 # $user may not be in sane state if this returns false!
598 sub cgi_fill {
599 my $self = shift;
600 my ($repo) = @_;
601 my $cgi = $repo->cgi;
603 $self->{name} = $repo->wparam('name');
604 valid_user_name($self->{name})
605 or $repo->err("Name contains invalid characters.");
607 $self->{email} = $repo->wparam('email');
608 valid_email($self->{email})
609 or $repo->err("Your email sure looks weird...?");
611 $self->{keys} = $cgi->param('keys');
612 length($self->{keys}) <= 4096
613 or $repo->err("The list of keys is more than 4kb. Do you really need that much?");
615 not $repo->err_check;
618 sub conjure {
619 my $self = shift;
621 $self->_passwd_add;
622 $self->_sshkey_save;
625 # static method
626 sub does_exist {
627 my ($name) = @_;
628 valid_user_name($name) or die "tried to query for user with invalid name $name!";
629 (-e jailed_file("/etc/sshkeys/$name"));
631 sub available {
632 does_exist(@_);