Run the gitweb.cgi make for the all target
[girocco.git] / Girocco / Project.pm
blob09786ad4e2cf3e35f5a6fbfd6337b7f3bba72bc6
1 package Girocco::Project;
3 use strict;
4 use warnings;
6 BEGIN {
7 use Girocco::CGI;
8 use Girocco::User;
9 use Girocco::Util;
10 use Girocco::ProjPerm;
11 use Girocco::Config;
12 use base ('Girocco::ProjPerm::'.$Girocco::Config::permission_control); # mwahaha
15 our $metadata_fields = {
16 homepage => ['Homepage URL', 'hp', 'text'],
17 shortdesc => ['Short description', 'desc', 'text'],
18 README => ['README (HTML, lt 8kb)', 'README', 'textarea'],
19 notifymail => ['Commit notify - mail to', 'notifymail', 'text'],
20 notifyjson => ['Commit notify - <a href="http://help.github.com/post-receive-hooks/">POST JSON</a> at', 'notifyjson', 'text'],
21 notifycia => ['Commit notify - <a href="http://cia.vc/doc/">CIA project</a> name', 'notifycia', 'text'],
24 sub _mkdir_forkees {
25 my $self = shift;
26 my @pelems = split('/', $self->{name});
27 pop @pelems; # do not create dir for the project itself
28 my $path = $self->{base_path};
29 foreach my $pelem (@pelems) {
30 $path .= "/$pelem";
31 (-d "$path") or mkdir $path or die "mkdir $path: $!";
32 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
36 our %propmap = (
37 url => ':baseurl',
38 email => ':owner',
39 desc => 'description',
40 README => 'README.html',
41 hp => ':homepage',
42 notifymail => '%hooks.mailinglist',
43 notifyjson => '%hooks.jsonurl',
44 notifycia => '%hooks.cianame',
47 sub _update_index {
48 system($Girocco::Config::basedir . '/gitweb/genindex.sh');
51 sub _property_path {
52 my $self = shift;
53 my ($name) = @_;
54 $self->{path}.'/'.$name;
57 sub _property_fget {
58 my $self = shift;
59 my ($name) = @_;
60 my $pname = $propmap{$name};
61 $pname or die "unknown property: $name";
62 if ($pname =~ s/^://) {
63 my $val = `git --git-dir="$self->{path}" config "gitweb.$pname"`;
64 chomp $val;
65 return $val;
66 } elsif ($pname =~ s/^%//) {
67 my $val = `git --git-dir="$self->{path}" config "$pname"`;
68 chomp $val;
69 return $val;
72 open P, $self->_property_path($pname) or return undef;
73 my @value = <P>;
74 close P;
75 my $value = join('', @value); chomp $value;
76 $value;
79 sub _property_fput {
80 my $self = shift;
81 my ($name, $value) = @_;
82 my $pname = $propmap{$name};
83 $pname or die "unknown property: $name";
84 $value ||= '';
85 if ($pname =~ s/^://) {
86 system('git', '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
87 return;
88 } elsif ($pname =~ s/^%//) {
89 system('git', '--git-dir='.$self->{path}, 'config', $pname, $value);
90 return;
93 my $P = lock_file($self->_property_path($pname));
94 $value ne '' and print $P "$value\n";
95 close $P;
96 unlock_file($self->_property_path($pname));
99 sub _properties_load {
100 my $self = shift;
101 foreach my $prop (keys %propmap) {
102 $self->{$prop} = $self->_property_fget($prop);
106 sub _properties_save {
107 my $self = shift;
108 foreach my $prop (keys %propmap) {
109 $self->_property_fput($prop, $self->{$prop});
113 sub _nofetch_path {
114 my $self = shift;
115 $self->_property_path('.nofetch');
118 sub _nofetch {
119 my $self = shift;
120 my ($nofetch) = @_;
121 my $np = $self->_nofetch_path;
122 if ($nofetch) {
123 open X, '>'.$np or die "nofetch failed: $!";
124 close X;
125 } else {
126 unlink $np or die "yesfetch failed: $!";
130 sub _clonelog_path {
131 my $self = shift;
132 $self->_property_path('.clonelog');
135 sub _clonefail_path {
136 my $self = shift;
137 $self->_property_path('.clone_failed');
140 sub _clonep_path {
141 my $self = shift;
142 $self->_property_path('.clone_in_progress');
145 sub _clonep {
146 my $self = shift;
147 my ($nofetch) = @_;
148 my $np = $self->_clonep_path;
149 if ($nofetch) {
150 open X, '>'.$np or die "clonep failed: $!";
151 close X;
152 } else {
153 unlink $np or die "clonef failed: $!";
156 sub _alternates_setup {
157 my $self = shift;
158 return unless $self->{name} =~ m#/#;
159 my $forkee_name = get_forkee_name($self->{name});
160 my $forkee_path = get_forkee_path($self->{name});
161 return unless -d $forkee_path;
162 mkdir $self->{path}.'/refs'; chmod 02775, $self->{path}.'/refs';
163 mkdir $self->{path}.'/objects'; chmod 02775, $self->{path}.'/objects';
164 mkdir $self->{path}.'/objects/info'; chmod 02775, $self->{path}.'/objects/info';
166 # We set up both alternates and http_alternates since we cannot use
167 # relative path in alternates - that doesn't work recursively.
169 my $filename = $self->{path}.'/objects/info/alternates';
170 open X, '>'.$filename or die "alternates failed: $!";
171 print X "$forkee_path/objects\n";
172 close X;
173 chmod 0664, $filename or warn "cannot chmod $filename: $!";
175 if ($Girocco::Config::httppullurl) {
176 $filename = $self->{path}.'/objects/info/http-alternates';
177 open X, '>'.$filename or die "http-alternates failed: $!";
178 my $upfork = $forkee_name;
179 do { print X "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
180 close X;
181 chmod 0664, $filename or warn "cannot chmod $filename: $!";
184 # The symlink is problematic since git remote prune will traverse it.
185 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
187 # copy refs from parent project
188 # ownership information is changed by a root cronjob
189 system("cp -pR $forkee_path/refs $self->{path}/");
190 # initialize HEAD, hacky version
191 system("cp $forkee_path/HEAD $self->{path}/HEAD");
194 sub _ctags_setup {
195 my $self = shift;
196 mkdir $self->{path}.'/ctags'; chmod 01777, $self->{path}.'/ctags';
199 sub _group_add {
200 my $self = shift;
201 my ($xtra) = @_;
202 $xtra .= join(',', @{$self->{users}});
203 filedb_atomic_append(jailed_file('/etc/group'),
204 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
207 sub _group_update {
208 my $self = shift;
209 my $xtra = join(',', @{$self->{users}});
210 filedb_atomic_edit(jailed_file('/etc/group'),
211 sub {
212 $_ = $_[0];
213 chomp;
214 if ($self->{name} eq (split /:/)[0]) {
215 # preserve readonly flag
216 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
217 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
218 } else {
219 return "$_\n";
225 sub _group_remove {
226 my $self = shift;
227 filedb_atomic_edit(jailed_file('/etc/group'),
228 sub {
229 $self->{name} ne (split /:/)[0] and return $_;
234 sub _hook_path {
235 my $self = shift;
236 my ($name) = @_;
237 $self->{path}.'/hooks/'.$name;
240 sub _hook_install {
241 my $self = shift;
242 my ($name) = @_;
243 open SRC, "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
244 open DST, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
245 while (<SRC>) { print DST $_; }
246 close DST;
247 close SRC;
248 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
251 sub _hooks_install {
252 my $self = shift;
253 foreach my $hook ('post-receive', 'update', 'post-update') {
254 $self->_hook_install($hook);
258 # private constructor, do not use
259 sub _new {
260 my $class = shift;
261 my ($name, $base_path, $path) = @_;
262 valid_name($name) or die "refusing to create project with invalid name ($name)!";
263 $path ||= "$base_path/$name.git";
264 my $proj = { name => $name, base_path => $base_path, path => $path };
266 bless $proj, $class;
269 # public constructor #0
270 # creates a virtual project not connected to disk image
271 # you can conjure() it later to disk
272 sub ghost {
273 my $class = shift;
274 my ($name, $mirror) = @_;
275 my $self = $class->_new($name, $Girocco::Config::reporoot);
276 $self->{users} = [];
277 $self->{mirror} = $mirror;
278 $self->{email} = $self->{orig_email} = '';
279 $self;
282 # public constructor #1
283 sub load {
284 my $class = shift;
285 my ($name) = @_;
287 open F, jailed_file("/etc/group") or die "project load failed: $!";
288 while (<F>) {
289 chomp;
290 @_ = split /:+/;
291 next unless (shift eq $name);
293 my $self = $class->_new($name, $Girocco::Config::reporoot);
294 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
296 my $ulist;
297 ($self->{crypt}, $self->{gid}, $ulist) = @_;
298 $ulist ||= '';
299 $self->{users} = [split /,/, $ulist];
300 $self->{HEAD} = $self->get_HEAD;
301 $self->{orig_HEAD} = $self->{HEAD};
302 $self->{orig_users} = [@{$self->{users}}];
303 $self->{mirror} = ! -e $self->_nofetch_path;
304 $self->{clone_in_progress} = -e $self->_clonep_path;
305 $self->{clone_logged} = -e $self->_clonelog_path;
306 $self->{clone_failed} = -e $self->_clonefail_path;
307 $self->{ccrypt} = $self->{crypt};
309 $self->_properties_load;
310 $self->{orig_email} = $self->{email};
311 return $self;
313 close F;
314 undef;
317 # $proj may not be in sane state if this returns false!
318 sub cgi_fill {
319 my $self = shift;
320 my ($gcgi) = @_;
321 my $cgi = $gcgi->cgi;
323 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
324 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
325 if ($pwd or not $self->{crypt}) {
326 $self->{crypt} = scrypt($pwd);
329 if ($pwd2 and $pwd ne $pwd2) {
330 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
333 $self->{cpwd} = $cgi->param('cpwd');
335 if ($Girocco::Config::project_owners eq 'email') {
336 $self->{email} = $gcgi->wparam('email');
337 valid_email($self->{email})
338 or $gcgi->err("Your email sure looks weird...?");
341 $self->{url} = $gcgi->wparam('url');
342 if ($self->{url}) {
343 valid_repo_url($self->{url})
344 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
347 $self->{desc} = $gcgi->wparam('desc');
348 length($self->{desc}) <= 1024
349 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
351 $self->{README} = $gcgi->wparam('README');
352 length($self->{README}) <= 8192
353 or $gcgi->err("README length &gt; 8kb!");
355 $self->{hp} = $gcgi->wparam('hp');
356 if ($self->{hp}) {
357 valid_web_url($self->{hp})
358 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
361 $self->{users} = [grep { Girocco::User::valid_name($_) && Girocco::User::does_exist($_) } $cgi->param('user')];
363 $self->{HEAD} = $cgi->param('HEAD') if $cgi->param('HEAD');
365 # schedule deletion of tags (will be committed by update() after auth)
366 $self->{tags_to_delete} = [$cgi->param('tags')];
368 $self->{notifymail} = $gcgi->wparam('notifymail');
369 if ($self->{notifymail}) {
370 (valid_email_multi($self->{notifymail}) and length($self->{notifymail}) <= 512)
371 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
374 $self->{notifyjson} = $gcgi->wparam('notifyjson');
375 if ($self->{notifyjson}) {
376 valid_web_url($self->{notifyjson})
377 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
380 $self->{notifycia} = $gcgi->wparam('notifycia');
381 if ($self->{notifycia}) {
382 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
383 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
386 not $gcgi->err_check;
389 sub form_defaults {
390 my $self = shift;
392 name => $self->{name},
393 email => $self->{email},
394 url => $self->{url},
395 desc => html_esc($self->{desc}),
396 README => html_esc($self->{README}),
397 hp => $self->{hp},
398 users => $self->{users},
399 notifymail => html_esc($self->{notifymail}),
400 notifyjson => html_esc($self->{notifyjson}),
401 notifycia => html_esc($self->{notifycia}),
405 sub authenticate {
406 my $self = shift;
407 my ($gcgi) = @_;
409 $self->{ccrypt} or die "Can't authenticate against a project with no password";
410 $self->{cpwd} ||= '';
411 unless ($self->{ccrypt} eq crypt($self->{cpwd}, $self->{ccrypt})) {
412 $gcgi->err("Your admin password does not match!");
413 return 0;
415 return 1;
418 sub _setup {
419 my $self = shift;
420 my ($pushers) = @_;
422 $self->_mkdir_forkees;
424 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
425 if ($Girocco::Config::owning_group) {
426 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
427 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
428 chmod(02775, $self->{path}) or die "chmod 2775 $self->{path} failed: $!";
429 } else {
430 chmod(02777, $self->{path}) or die "chmod 2777 $self->{path} failed: $!";
432 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
433 or die "git init $self->{path} failed: $?";
434 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
435 or die "disabling receive.denyNonFastforwards failed: $?";
436 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
437 or die "disabling gc.auto failed: $?";
439 # /info must have right permissions even before the fixup job,
440 # and git init didn't do it for some reason.
441 if ($Girocco::Config::owning_group) {
442 chmod(02775, $self->{path}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
443 } else {
444 chmod(02777, $self->{path}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
447 $self->_properties_save;
448 $self->_alternates_setup;
449 $self->_ctags_setup;
450 $self->_group_remove;
451 $self->_group_add($pushers);
452 $self->_hooks_install;
453 #$self->perm_initialize;
454 $self->_update_index;
457 sub premirror {
458 my $self = shift;
460 $self->_setup(':');
461 $self->_clonep(1);
462 $self->perm_initialize;
465 sub conjure {
466 my $self = shift;
468 $self->_setup;
469 $self->_nofetch(1);
470 $self->perm_initialize;
473 sub clone {
474 my $self = shift;
476 unlink ($self->_clonefail_path()); # Ignore EEXIST error
477 unlink ($self->_clonelog_path()); # Ignore EEXIST error
479 use IO::Socket;
480 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
481 $sock->print("clone ".$self->{name}."\n");
482 # Just ignore reply, we are going to succeed anyway and the I/O
483 # would apparently get quite hairy.
484 $sock->flush();
485 sleep 2; # *cough*
486 $sock->close();
489 sub update {
490 my $self = shift;
492 $self->_properties_save;
493 $self->_group_update;
495 if (exists($self->{tags_to_delete})) {
496 $self->delete_ctag($_) foreach(@{$self->{tags_to_delete}});
499 $self->set_HEAD($self->{HEAD}) unless $self->{orig_HEAD} eq $self->{HEAD};
501 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
502 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
503 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
504 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
506 $self->_update_index if $self->{email} ne $self->{orig_email};
507 $self->{orig_email} = $self->{email};
512 sub update_password {
513 my $self = shift;
514 my ($pwd) = @_;
516 $self->{crypt} = scrypt($pwd);
517 $self->_group_update;
520 # You can explicitly do this just on a ghost() repository too.
521 sub delete {
522 my $self = shift;
524 if (-d $self->{path}) {
525 system('rm', '-rf', $self->{path}) == 0
526 or die "rm -rf $self->{path} failed: $?";
528 $self->_group_remove;
529 $self->_update_index;
532 sub has_forks {
533 my $self = shift;
535 return glob($Girocco::Config::reporoot.'/'.$self->{name}.'/*');
538 sub delete_ctag {
539 my $self = shift;
540 my ($ctag) = @_;
542 # sanity check, disallow filenames starting with . .. or /
543 unlink($self->{path}.'/ctags/'.$ctag) if($ctag !~ m|^(\.\.?)/|);
546 sub get_ctag_names {
547 my $self = shift;
548 my @ctags = ();
549 opendir(my $dh, $self->{path}.'/ctags')
550 or return @ctags;
551 @ctags = grep { -f "$self->{path}/ctags/$_" } readdir($dh);
552 closedir($dh);
553 return @ctags;
556 sub get_heads {
557 my $self = shift;
558 my $fh;
559 open($fh, '-|', "$Girocco::Config::git_bin --git-dir=$self->{path} show-ref --heads") or die "could not get list of heads";
560 my @res;
561 while (<$fh>) {
562 chomp;
563 next if !m#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
564 push @res, $1;
566 close $fh;
567 @res;
570 sub get_HEAD {
571 my $self = shift;
572 my $HEAD = `$Girocco::Config::git_bin --git-dir=$self->{path} symbolic-ref HEAD`;
573 chomp $HEAD;
574 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
575 return $1;
578 sub set_HEAD {
579 my $self = shift;
580 my $newHEAD = shift;
581 # Cursory checks only -- if you want to break your HEAD, be my guest
582 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
583 die "grossly invalid new HEAD: $newHEAD";
585 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', "refs/heads/$newHEAD");
586 die "could not set HEAD" if ($? >> 8);
589 ### static methods
591 sub get_forkee_name {
592 $_ = $_[0];
593 (m#^(.*)/.*?$#)[0]; #
595 sub get_forkee_path {
596 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
597 -d $forkee ? $forkee : '';
600 sub valid_name {
601 $_ = $_[0];
602 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
603 and (not m#\./#)
604 and (not m#/$#)
605 and m#^[a-zA-Z0-9+./_-]+$#;
608 sub does_exist {
609 my ($name) = @_;
610 valid_name($name) or die "tried to query for project with invalid name $name!";
611 (-d $Girocco::Config::reporoot."/$name.git");
614 sub get_full_list {
615 my $class = shift;
616 my @projects;
618 open F, jailed_file("/etc/group") or die "getting project list failed: $!";
619 while (<F>) {
620 chomp;
621 @_ = split /:+/;
622 next if ($_[2] < 65536);
624 push @projects, $_[0];
626 close F;
627 @projects;