http-alternates, not http_alternates
[girocco/mytab.git] / cgi / Git / RepoCGI.pm
blobf986b88db72ec3f0dbc3f28e1f078bb1da328a90
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);
22 $ENV{PATH} = '/home/pasky/bin:'.$ENV{PATH};
26 ### RepoCGI object
28 sub new {
29 my $class = shift;
30 my ($heading) = @_;
31 my $repo = {};
33 $repo->{cgi} = CGI->new;
35 print $repo->{cgi}->header(-type=>'text/html', -charset => 'utf-8');
37 print <<EOT;
38 <?xml version="1.0" encoding="utf-8"?>
39 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
40 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
42 <head>
43 <title>repo.or.cz :: $heading</title>
44 <link rel="stylesheet" type="text/css" href="/gitweb.css"/>
45 <link rel="shortcut icon" href="/git-favicon.png" type="image/png"/>
46 </head>
48 <body>
50 <div class="page_header">
51 <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>
52 <a href="/">repo.or.cz</a> / administration / $heading
53 </div>
55 EOT
57 bless $repo, $class;
60 sub DESTROY {
61 my $self = shift;
62 my $cgi = $self->cgi;
63 my $cginame = $cgi->url(-absolute => 1);
64 $cginame =~ s#^/m/##;
65 if ($cginame =~ /^[a-zA-Z0-9_.\/-]+\.cgi$/) {
66 print <<EOT;
67 <div align="right">
68 <a href="http://repo.or.cz/w/repo.git?a=blob;f=cgi/$cginame">(view source)</a>
69 </div>
70 EOT
72 print <<EOT;
73 </body>
74 </html>
75 EOT
78 sub cgi {
79 my $self = shift;
80 $self->{cgi};
83 sub err {
84 my $self = shift;
85 print "<p style=\"text-color: red\">@_</p>\n";
86 $self->{err}++;
89 sub err_check {
90 my $self = shift;
91 my $err = $self->{err};
92 $err and print "<p>Operation aborted due to $err errors.</p>\n";
93 $err;
96 sub wparam {
97 my $self = shift;
98 my ($param) = @_;
99 my $val = $self->{cgi}->param($param);
100 $val =~ s/^\s*(.*?)\s*$/$1/;
101 $val;
105 ### Random utility functions
107 sub scrypt {
108 my ($pwd) = @_;
109 crypt($pwd, join ('', ('.', '/', 2..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
112 sub html_esc {
113 my ($str) = @_;
114 $str =~ s/&/&amp;/g;
115 $str =~ s/</&lt;/g; $str =~ s/>/&gt;/g;
116 $str =~ s/"/&quot;/g;
117 $str;
120 sub jailed_file {
121 my ($filename) = @_;
122 "/home/repo/j/$filename";
125 sub lock_file {
126 my ($path) = @_;
128 $path .= '.lock';
130 use Errno qw(EEXIST);
131 use Fcntl qw(O_WRONLY O_CREAT O_EXCL);
132 use IO::Handle;
133 my $handle = new IO::Handle;
135 unless (sysopen($handle, $path, O_WRONLY|O_CREAT|O_EXCL)) {
136 my $cnt = 0;
137 while (not sysopen($handle, $path, O_WRONLY|O_CREAT|O_EXCL)) {
138 ($! == EEXIST) or die "$path open failed: $!";
139 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
140 sleep(1);
143 # XXX: filedb-specific
144 chmod 0664, $path or die "$path g+w failed: $!";
146 $handle;
149 sub unlock_file {
150 my ($path) = @_;
152 rename "$path.lock", $path or die "$path unlock failed: $!";
155 sub filedb_atomic_append {
156 my ($file, $line) = @_;
157 my $id = 65536;
159 open my $src, $file or die "$file open for reading failed: $!";
160 my $dst = lock_file($file);
162 while (<$src>) {
163 my $aid = (split /:/)[2];
164 $id = $aid + 1 if ($aid >= $id);
166 print $dst $_ or die "$file(l) write failed: $!";
169 $line =~ s/\\i/$id/g;
170 print $dst "$line\n" or die "$file(l) write failed: $!";
172 close $dst or die "$file(l) close failed: $!";
173 close $src;
175 unlock_file($file);
177 $id;
180 sub filedb_atomic_edit {
181 my ($file, $fn) = @_;
183 open my $src, $file or die "$file open for reading failed: $!";
184 my $dst = lock_file($file);
186 while (<$src>) {
187 print $dst $fn->($_) or die "$file(l) write failed: $!";
190 close $dst or die "$file(l) close failed: $!";
191 close $src;
193 unlock_file($file);
196 sub proj_get_forkee_name {
197 $_ = $_[0];
198 (m#^(.*)/.*?$#)[0];
200 sub proj_get_forkee_path {
201 my $forkee = '/srv/git/'.proj_get_forkee_name($_[0]).'.git';
202 -d $forkee ? $forkee : '';
204 sub valid_proj_name {
205 $_ = $_[0];
206 (not m#/# or -d proj_get_forkee_path($_)) # will also catch ^/
207 and (not m#\./#)
208 and (not m#/$#)
209 and m#^[a-zA-Z0-9+./_-]+$#;
211 sub valid_user_name {
212 $_ = $_[0];
213 /^[a-zA-Z0-9+._-]+$/;
215 sub valid_email {
216 $_ = $_[0];
217 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/;
219 sub valid_web_url {
220 $_ = $_[0];
221 /^http:\/\/[a-zA-Z0-9-.]+(\/[_\%a-zA-Z0-9.\/~-]*)?(#[a-zA-Z0-9._-]+)?$/;
223 sub valid_repo_url {
224 $_ = $_[0];
225 /^http:\/\/[a-zA-Z0-9-.]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/ or
226 /^git:\/\/[a-zA-Z0-9-.]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/;
230 ### Project object
232 package Git::RepoCGI::Project;
234 BEGIN { use Git::RepoCGI; }
236 sub _mkdir_forkees {
237 my $self = shift;
238 my @pelems = split('/', $self->{name});
239 pop @pelems; # do not create dir for the project itself
240 my $path = $self->{base_path};
241 foreach my $pelem (@pelems) {
242 $path .= "/$pelem";
243 (-d "$path") or mkdir $path or die "mkdir $path: $!";
244 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
248 our %propmap = (
249 url => 'base_url',
250 email => 'owner',
251 desc => 'description',
252 README => 'README.html',
253 hp => 'homepage',
256 sub _property_path {
257 my $self = shift;
258 my ($name) = @_;
259 $self->{path}.'/'.$name;
262 sub _property_fget {
263 my $self = shift;
264 my ($name) = @_;
265 my $value;
266 $propmap{$name} or die "unknown property: $name";
267 open P, $self->_property_path($propmap{$name}) or return undef;
268 chomp($value = <P>);
269 close P;
270 $value;
273 sub _property_fput {
274 my $self = shift;
275 my ($name, $value) = @_;
276 $propmap{$name} or die "unknown property: $name";
278 my $P = lock_file($self->_property_path($propmap{$name}));
279 $value ne '' and print $P "$value\n";
280 close $P;
281 unlock_file($self->_property_path($propmap{$name}));
284 sub _properties_load {
285 my $self = shift;
286 foreach my $prop (keys %propmap) {
287 $self->{$prop} = $self->_property_fget($prop);
291 sub _properties_save {
292 my $self = shift;
293 foreach my $prop (keys %propmap) {
294 $self->_property_fput($prop, $self->{$prop});
298 sub _nofetch_path {
299 my $self = shift;
300 $self->_property_path('.nofetch');
303 sub _nofetch {
304 my $self = shift;
305 my ($nofetch) = @_;
306 my $np = $self->_nofetch_path;
307 if ($nofetch) {
308 open X, '>'.$np or die "nofetch failed: $!";
309 close X;
310 } else {
311 unlink $np or die "yesfetch failed: $!";
315 sub _alternates_setup {
316 my $self = shift;
317 return unless $self->{name} =~ m#/#;
318 my $forkee_name = proj_get_forkee_name($self->{name});
319 my $forkee_path = proj_get_forkee_path($self->{name});
320 return unless -d $forkee_path;
321 mkdir $self->{path}.'/refs'; chmod 0775, $self->{path}.'/refs';
322 mkdir $self->{path}.'/objects'; chmod 0775, $self->{path}.'/objects';
323 mkdir $self->{path}.'/objects/info'; chmod 0775, $self->{path}.'/objects/info';
325 # We set up both alternates and http_alternates since we cannot use
326 # relative path in alternates - that doesn't work recursively.
328 my $filename = $self->{path}.'/objects/info/alternates';
329 open X, '>'.$filename or die "alternates failed: $!";
330 print X "$forkee_path/objects\n";
331 close X;
332 chmod 0664, $filename or warn "cannot chmod $filename: $!";
334 my $filename = $self->{path}.'/objects/info/http-alternates';
335 open X, '>'.$filename or die "http-alternates failed: $!";
336 print X "/r/$forkee_name.git\n";
337 close X;
338 chmod 0664, $filename or warn "cannot chmod $filename: $!";
340 symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
343 sub _group_add {
344 my $self = shift;
345 my ($xtra) = @_;
346 $xtra .= join(',', @{$self->{users}});
347 filedb_atomic_append(jailed_file('/etc/group'),
348 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
351 sub _group_update {
352 my $self = shift;
353 my $xtra = join(',', @{$self->{users}});
354 filedb_atomic_edit(jailed_file('/etc/group'),
355 sub {
356 $_ = $_[0];
357 chomp;
358 if ($self->{name} eq (split /:/)[0]) {
359 # preserve readonly flag
360 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
361 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
362 } else {
363 return "$_\n";
369 sub _group_remove {
370 my $self = shift;
371 filedb_atomic_edit(jailed_file('/etc/group'),
372 sub {
373 $self->{name} ne (split /:/)[0] and return $_;
378 sub _hook_path {
379 my $self = shift;
380 my ($name) = @_;
381 $self->{path}.'/hooks/'.$name;
384 sub _hook_install {
385 my $self = shift;
386 my ($name) = @_;
387 open SRC, "/home/repo/repomgr/$name-hook" or die "cannot open hook $name: $!";
388 open DST, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
389 while (<SRC>) { print DST $_; }
390 close DST;
391 close SRC;
392 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
395 sub _hooks_install {
396 my $self = shift;
397 foreach my $hook ('update') {
398 $self->_hook_install($hook);
402 # private constructor, do not use
403 sub _new {
404 my $class = shift;
405 my ($name, $base_path, $path) = @_;
406 valid_proj_name($name) or die "refusing to create project with invalid name ($name)!";
407 $path ||= "$base_path/$name.git";
408 my $proj = { name => $name, base_path => $base_path, path => $path };
410 bless $proj, $class;
413 # public constructor #0
414 # creates a virtual project not connected to disk image
415 # you can conjure() it later to disk
416 sub ghost {
417 my $class = shift;
418 my ($name, $mirror) = @_;
419 my $self = $class->_new($name, $mirror ? "/home/repo/repodata/to-clone" : "/srv/git",
420 $mirror ? "/home/repo/repodata/to-clone/$name" : "/srv/git/$name.git");
421 $self->{users} = [];
422 $self->{mirror} = $mirror;
423 $self;
426 # public constructor #1
427 sub load {
428 my $class = shift;
429 my ($name) = @_;
431 open F, jailed_file("/etc/group") or die "project load failed: $!";
432 while (<F>) {
433 chomp;
434 @_ = split /:+/;
435 next unless (shift eq $name);
437 my $self = $class->_new($name, "/srv/git");
438 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
440 my $ulist;
441 ($self->{crypt}, $self->{gid}, $ulist) = @_;
442 $ulist ||= '';
443 $self->{users} = [split /,/, $ulist];
444 $self->{mirror} = ! -e $self->_nofetch_path;
446 $self->_properties_load;
447 return $self;
449 close F;
450 undef;
453 # $proj may not be in sane state if this returns false!
454 sub cgi_fill {
455 my $self = shift;
456 my ($repo) = @_;
457 my $cgi = $repo->cgi;
459 my $pwd = $cgi->param('pwd');
460 if ($pwd ne '' or not $self->{crypt}) {
461 $self->{crypt} = scrypt($pwd);
464 $self->{email} = $repo->wparam('email');
465 valid_email($self->{email})
466 or $repo->err("Your email sure looks weird...?");
468 $self->{url} = $repo->wparam('url');
469 if ($self->{url}) {
470 valid_repo_url($self->{url})
471 or $repo->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
474 $self->{desc} = $repo->wparam('desc');
475 length($self->{desc}) <= 1024
476 or $repo->err("<b>Short</b> description length > 1kb!");
478 $self->{README} = $repo->wparam('README');
479 length($self->{README}) <= 8192
480 or $repo->err("README length > 8kb!");
482 $self->{hp} = $repo->wparam('hp');
483 if ($self->{hp}) {
484 valid_web_url($self->{hp})
485 or $repo->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
488 # FIXME: Permit only existing users
489 $self->{users} = [grep { valid_user_name($_) } $cgi->param('user')];
491 not $repo->err_check;
494 sub form_defaults {
495 my $self = shift;
497 name => $self->{name},
498 email => $self->{email},
499 url => $self->{url},
500 desc => html_esc($self->{desc}),
501 README => html_esc($self->{README}),
502 hp => $self->{hp},
503 users => $self->{users},
507 sub premirror {
508 my $self = shift;
510 $self->_mkdir_forkees;
511 mkdir $self->{path} or die "mkdir failed: $!";
512 chmod 0775, $self->{path} or die "chmod failed: $!";
513 $self->_properties_save;
514 $self->_alternates_setup;
515 $self->_group_add(':');
518 sub conjure {
519 my $self = shift;
521 $self->_mkdir_forkees;
522 system('cg-admin-setuprepo', '-g', 'repo', $self->{path}) == 0
523 or die "cg-admin-setuprepo failed: $?";
524 system('git', '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false');
525 $self->_nofetch(1);
526 $self->_properties_save;
527 $self->_alternates_setup;
528 $self->_group_add;
529 $self->_hooks_install;
532 sub update {
533 my $self = shift;
535 $self->_properties_save;
536 $self->_group_update;
539 # You can explicitly do this just on a ghost() repository too.
540 sub delete {
541 my $self = shift;
543 if (-d $self->{path}) {
544 system('rm', '-r', $self->{path}) == 0
545 or die "rm -r failed: $?";
547 $self->_group_remove;
550 # static method
551 sub does_exist {
552 my ($name) = @_;
553 valid_proj_name($name) or die "tried to query for project with invalid name $name!";
554 (available($name)
555 or -d "/home/repo/repodata/cloning/$name"
556 or -d "/home/repo/repodata/to-clone/$name");
558 sub available {
559 my ($name) = @_;
560 valid_proj_name($name) or die "tried to query for project with invalid name $name!";
561 (-d "/srv/git/$name.git");
565 ### User object
567 package Git::RepoCGI::User;
569 BEGIN { use Git::RepoCGI; }
571 sub _passwd_add {
572 my $self = shift;
573 filedb_atomic_append(jailed_file('/etc/passwd'),
574 join(':', $self->{name}, 'x', '\i', 65534, $self->{email}, '/', '/bin/git-shell'));
577 sub _sshkey_path {
578 my $self = shift;
579 '/etc/sshkeys/'.$self->{name};
582 sub _sshkey_save {
583 my $self = shift;
584 open F, ">".jailed_file($self->_sshkey_path) or die "sshkey failed: $!";
585 print F $self->{keys}."\n";
586 close F;
587 chmod 0664, jailed_file($self->_sshkey_path);
590 # private constructor, do not use
591 sub _new {
592 my $class = shift;
593 my ($name) = @_;
594 valid_user_name($name) or die "refusing to create user with invalid name ($name)!";
595 my $proj = { name => $name };
597 bless $proj, $class;
600 # public constructor #0
601 # creates a virtual user not connected to disk record
602 # you can conjure() it later to disk
603 sub ghost {
604 my $class = shift;
605 my ($name) = @_;
606 my $self = $class->_new($name);
607 $self;
610 # $user may not be in sane state if this returns false!
611 sub cgi_fill {
612 my $self = shift;
613 my ($repo) = @_;
614 my $cgi = $repo->cgi;
616 $self->{name} = $repo->wparam('name');
617 valid_user_name($self->{name})
618 or $repo->err("Name contains invalid characters.");
620 $self->{email} = $repo->wparam('email');
621 valid_email($self->{email})
622 or $repo->err("Your email sure looks weird...?");
624 $self->{keys} = $cgi->param('keys');
625 length($self->{keys}) <= 4096
626 or $repo->err("The list of keys is more than 4kb. Do you really need that much?");
628 not $repo->err_check;
631 sub conjure {
632 my $self = shift;
634 $self->_passwd_add;
635 $self->_sshkey_save;
638 # static method
639 sub does_exist {
640 my ($name) = @_;
641 valid_user_name($name) or die "tried to query for user with invalid name $name!";
642 (-e jailed_file("/etc/sshkeys/$name"));
644 sub available {
645 does_exist(@_);