taskd.pl: make sure no status messages are skipped
[girocco.git] / Girocco / Project.pm
blob5816ca67b115837d240ecd2e890e55c3eed8159d
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::HashUtil;
11 use Girocco::ProjPerm;
12 use Girocco::Config;
13 use base ('Girocco::ProjPerm::'.$Girocco::Config::permission_control); # mwahaha
16 BEGIN {
17 eval {
18 require Digest::SHA;
19 Digest::SHA->import(
20 qw(sha1_hex)
21 );1} ||
22 eval {
23 require Digest::SHA1;
24 Digest::SHA1->import(
25 qw(sha1_hex)
26 );1} ||
27 eval {
28 require Digest::SHA::PurePerl;
29 Digest::SHA::PurePerl->import(
30 qw(sha1_hex)
31 );1} ||
32 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
33 . "must be available\n";
36 our $metadata_fields = {
37 homepage => ['Homepage URL', 'hp', 'text'],
38 shortdesc => ['Short description', 'desc', 'text'],
39 README => ['<span style="display:inline-block;vertical-align:top">'.
40 'README (HTML, &lt; 8 KiB)<br />leave blank for automatic</span>',
41 'README', 'textarea', 'Enter only &#x201c;<!-- comments -->&#x201d; '.
42 'to completely suppress any README'],
43 notifymail => ['Commit notify &#x2013; mail to', 'notifymail', 'text',
44 'comma separated address list'],
45 notifytag => ['Tag notify &#x2013; mail to', 'notifytag', 'text',
46 'comma separated address list &#x2013; if not empty, tag '.
47 'notifications are sent here INSTEAD of to '.
48 '&#x201c;Commit notify &#x2013; mail to&#x201d; address(es)'],
49 notifyjson => ['Commit notify &#x2013; '.
50 '<a title="'.html_esc('single field name is &#x201c;payload&#x201d;', 1).'" href="'.
51 'https://developer.github.com/v3/activity/events/types/#pushevent'.
52 '">POST JSON</a> at', 'notifyjson', 'text'],
53 notifycia => ['Commit notify &#x2013; <a href="http://cia.vc/doc/">CIA project</a> name',
54 'notifycia', 'text', 'CIA is defunct &#x2013; this value is ignored'],
57 sub _mkdir_forkees {
58 my $self = shift;
59 my @pelems = split('/', $self->{name});
60 pop @pelems; # do not create dir for the project itself
61 my $path = $self->{base_path};
62 foreach my $pelem (@pelems) {
63 $path .= "/$pelem";
64 (-d "$path") or mkdir $path or die "mkdir $path: $!";
65 chmod 02775, $path; # ok if fails (dir may already exist and be owned by someone else)
69 # With a leading ':' get from project local config replacing ':' with 'gitweb.'
70 # With a leading '%' get from project local config after removing '%'
71 # Otherwise it's a project file name to be loaded
72 # %propmapro entries are loaded but never written
74 our %propmap = (
75 url => ':baseurl',
76 email => ':owner',
77 desc => 'description',
78 README => 'README.html',
79 hp => ':homepage',
80 notifymail => '%hooks.mailinglist',
81 notifytag => '%hooks.announcelist',
82 notifyjson => '%hooks.jsonurl',
83 notifycia => '%hooks.cianame',
86 our %propmapro = (
87 lastchange => ':lastchange',
88 lastactivity => 'info/lastactivity',
89 lastgc => ':lastgc',
90 lastreceive => ':lastreceive',
91 lastparentgc => ':lastparentgc',
92 lastrefresh => ':lastrefresh',
93 creationtime => '%girocco.creationtime',
94 reposizek => '%girocco.reposizek',
95 origurl => ':baseurl',
98 # Projects with any of these names will be disallowed to avoid possible
99 # collisions with cgi script paths or chroot paths
100 our %reservedprojectnames = (
101 b => 1, # /b/ -> bundle.cgi
102 c => 1, # /c/ -> cgit
103 h => 1, # /h/ -> html.cgi
104 r => 1, # /r/ -> git http
105 w => 1, # /w/ -> gitweb
106 srv => 1, # /srv/git/ -> chroot ssh git repositories
109 sub _update_index {
110 my $self = shift;
111 system("$Girocco::Config::basedir/gitweb/genindex.sh", $self->{name});
114 sub _readlocalconfigfile {
115 my $self = shift;
116 my $undefonerr = shift || 0;
117 delete $self->{configfilehash};
118 my $configfile = get_git("--git-dir=$self->{path}",
119 'config', '--list', '--local', '--null');
120 my $result = 1;
121 defined($configfile) || $undefonerr or $result = 0, $configfile = '';
122 return undef unless defined($configfile);
123 my %config = map({split(/\n/, $_=~/\n/?$_:"$_\ntrue", 2)} split(/\x00/, $configfile));
124 $self->{configfilehash} = \%config;
125 return $result;
128 # @_[0]: argument to convert to boolean result (0 or 1)
129 # @_[1]: value to use if argument is undef (default is 0)
130 # Returns 0 or 1
131 sub _boolval {
132 my ($val, $def) = @_;
133 defined($def) or $def = 0;
134 defined($val) or $val = $def;
135 $val =~ s/\s+//gs;
136 $val = lc($val);
137 return 0 if $val eq '' || $val eq 'false' || $val eq 'off' || $val =~ /^0+$/;
138 return 1;
141 sub _property_path {
142 my $self = shift;
143 my ($name) = @_;
144 $self->{path}.'/'.$name;
147 sub _property_fget {
148 my $self = shift;
149 my ($name) = @_;
150 my $pname = $propmap{$name};
151 $pname = $propmapro{$name} unless $pname;
152 $pname or die "unknown property: $name";
153 if ($pname =~ /^([:%])(.*)$/) {
154 my ($where, $pname) = ($1,lc($2));
155 $self->_readlocalconfigfile
156 unless ref($self->{configfilehash}) eq 'HASH';
157 my $val = $where eq ':' ?
158 $self->{configfilehash}->{"gitweb.$pname"} :
159 $self->{configfilehash}->{$pname};
160 defined($val) or $val = '';
161 chomp $val;
162 return $val;
165 open my $p, '<', $self->_property_path($pname) or return undef;
166 my @value = <$p>;
167 close $p;
168 my $value = join('', @value); chomp $value;
169 $value;
172 sub _property_fput {
173 my $self = shift;
174 my ($name, $value) = @_;
175 my $pname = $propmap{$name};
176 $pname or die "unknown property: $name";
177 $value ||= '';
178 if ($pname =~ s/^://) {
179 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
180 return;
181 } elsif ($pname =~ s/^%//) {
182 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', $pname, $value);
183 return;
186 my $P = lock_file($self->_property_path($pname));
187 chomp $value;
188 $value ne '' and print $P "$value\n";
189 close $P;
190 unlock_file($self->_property_path($pname));
193 sub _cleanup_readme {
194 my $self = shift;
195 defined($self->{README}) or $self->{README} = '';
196 $self->{README} =~ s/\r\n?/\n/gs;
197 $self->{README} =~ s/^\s+//s;
198 $self->{README} =~ s/\s+$//s;
199 $self->{README} eq '' or $self->{README} .= "\n";
202 sub _lint_readme {
203 my $self = shift;
204 return () unless defined($self->{README}) && $self->{README} ne '';
205 my $test = '<html xmlns="http://www.w3.org/1999/xhtml"><body><div>';
206 $test .= $self->{README};
207 $test .= '</div></body></html>';
208 my ($code, $errors) = capture_command(2, $test, 'xmllint', '--nonet',
209 '--noout', '--nowarning', '-');
210 return () unless $code;
211 my @errs = ();
212 my $err = '';
213 for my $line (split(/\n/, $errors)) {
214 $line = html_esc($line);
215 $line =~ s/ /\&#160;/gs;
216 if ($line =~ /^-(:\d+:.*)$/) {
217 push @errs, $err if $err;
218 $err = '<tt>README' . $1 . '</tt>';
219 } else {
220 $err .= '<br />' if $err;
221 $err .= '<tt>' . $line . '</tt>';
224 push @errs, $err if $err;
225 return @errs;
228 sub _properties_load {
229 my $self = shift;
230 foreach my $prop (keys %propmap) {
231 $self->{$prop} = $self->_property_fget($prop);
233 foreach my $prop (keys %propmapro) {
234 $self->{$prop} = $self->_property_fget($prop);
236 $self->_readlocalconfigfile
237 unless ref($self->{configfilehash}) eq 'HASH';
238 $self->{statusupdates} = _boolval($self->{configfilehash}->{'gitweb.statusupdates'}, 1);
239 delete $self->{auth};
240 my $val = $self->{configfilehash}->{'gitweb.repoauth'};
241 defined($val) or $val = '';
242 chomp $val;
243 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
244 my $expire = $3;
245 if (time < $expire) {
246 $self->{authtype} = $1;
247 $self->{auth} = $2;
250 $self->_cleanup_readme;
251 delete $self->{configfilehash};
254 sub _properties_save {
255 my $self = shift;
256 my $fd;
257 foreach my $prop (keys %propmap) {
258 $self->_property_fput($prop, $self->{$prop});
260 $self->{statusupdates} = 1
261 unless defined($self->{statusupdates}) && $self->{statusupdates} =~ /^\d+$/;
262 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool',
263 "gitweb.statusupdates", $self->{statusupdates});
264 if (defined($self->{origurl}) && defined($self->{url}) &&
265 $self->{origurl} ne $self->{url} && -e $self->_property_path(".banged")) {
266 if (open($fd, '>', $self->_property_path(".bangagain"))) {
267 close $fd;
268 chmod(0664, $self->_property_path(".bangagain"));
273 sub _nofetch_path {
274 my $self = shift;
275 $self->_property_path('.nofetch');
278 sub _nofetch {
279 my $self = shift;
280 my ($nofetch) = @_;
281 my $nf = $self->_nofetch_path;
282 if ($nofetch) {
283 open my $x, '>', $nf or die "nofetch failed: $!";
284 close $x;
285 } else {
286 unlink $nf or die "yesfetch failed: $!";
290 sub _gcp_path {
291 my $self = shift;
292 $self->_property_path('.gc_in_progress');
295 sub _clonelog_path {
296 my $self = shift;
297 $self->_property_path('.clonelog');
300 sub _clonefail_path {
301 my $self = shift;
302 $self->_property_path('.clone_failed');
305 sub _clonep_path {
306 my $self = shift;
307 $self->_property_path('.clone_in_progress');
310 sub _clonep {
311 my $self = shift;
312 my ($nofetch) = @_;
313 my $np = $self->_clonep_path;
314 if ($nofetch) {
315 open my $x, '>', $np or die "clonep failed: $!";
316 close $x;
317 } else {
318 unlink $np or die "clonef failed: $!";
321 sub _alternates_setup {
322 my $self = shift;
323 return unless $self->{name} =~ m#/#;
324 my $forkee_name = get_forkee_name($self->{name});
325 my $forkee_path = get_forkee_path($self->{name});
326 return unless -d $forkee_path;
327 mkdir $self->{path}.'/refs'; chmod 02775, $self->{path}.'/refs';
328 mkdir $self->{path}.'/objects'; chmod 02775, $self->{path}.'/objects';
329 mkdir $self->{path}.'/objects/info'; chmod 02775, $self->{path}.'/objects/info';
331 # We set up both alternates and http_alternates since we cannot use
332 # relative path in alternates - that doesn't work recursively.
334 my $filename = $self->{path}.'/objects/info/alternates';
335 open my $x, '>', $filename or die "alternates failed: $!";
336 print $x "$forkee_path/objects\n";
337 close $x;
338 chmod 0664, $filename or warn "cannot chmod $filename: $!";
340 if ($Girocco::Config::httppullurl) {
341 $filename = $self->{path}.'/objects/info/http-alternates';
342 open my $x, '>', $filename or die "http-alternates failed: $!";
343 my $upfork = $forkee_name;
344 do { print $x "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
345 close $x;
346 chmod 0664, $filename or warn "cannot chmod $filename: $!";
349 # copy lastactivity from the parent project
350 if (open $x, '<', $forkee_path.'/info/lastactivity') {{
351 my $activity = <$x>;
352 close $x;
353 last unless $activity;
354 open $x, '>', $self->{path}.'/info/lastactivity' or last;
355 print $x $activity;
356 close $x;
357 chomp $activity;
358 $self->{'lastactivity'} = $activity;
361 # copy refs from parent project
362 local *SAVEOUT;
363 open(SAVEOUT, ">&STDOUT") || die "couldn't dup STDOUT: $!";
364 my $result = open(STDOUT, '>', "$self->{path}/packed-refs");
365 my $resultstr = $!;
366 if (!$result) {
367 open(STDOUT, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
368 close(SAVEOUT) or die "couldn't close SAVEOUT: $!";
369 die "could not write fork's packed-refs file: $resultstr";
371 system($Girocco::Config::git_bin, "--git-dir=$forkee_path", 'for-each-ref', '--format=%(objectname) %(refname)');
372 $result = close(STDOUT);
373 $resultstr = $!;
374 open(STDOUT, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
375 close(SAVEOUT) or die "couldn't close SAVEOUT: $!";
376 $result or die "could not close fork's packed-refs file: $resultstr";
377 unlink("$self->{path}/.delaygc") if -s "$self->{path}/packed-refs";
379 # initialize HEAD
380 my $HEAD = get_git("--git-dir=$forkee_path", 'symbolic-ref', 'HEAD');
381 defined($HEAD) && $HEAD =~ m,^refs/heads/., or $HEAD = 'refs/heads/master';
382 chomp $HEAD;
383 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', $HEAD);
384 chmod 0664, "$self->{path}/packed-refs", "$self->{path}/HEAD";
387 sub _set_changed {
388 my $self = shift;
389 my $fd;
390 open $fd, '>', "$self->{path}/htmlcache/changed" and close $fd;
393 sub _set_forkchange {
394 my $self = shift;
395 my $changedtoo = shift;
396 return unless $self->{name} =~ m#/#;
397 my $forkee_path = get_forkee_path($self->{name});
398 return unless -d $forkee_path;
399 # mark forkee as changed
400 my $fd;
401 open $fd, '>', $forkee_path.'/htmlcache/changed' and close $fd if $changedtoo;
402 open $fd, '>', $forkee_path.'/htmlcache/forkchange' and close $fd;
403 return if -e $forkee_path.'/htmlcache/summary.forkchange';
404 open $fd, '>', $forkee_path.'/htmlcache/summary.forkchange' and close $fd;
407 sub _ctags_setup {
408 my $self = shift;
409 my $perms = $Girocco::Config::permission_control eq 'Hooks' ? 02777 : 02775;
410 mkdir $self->{path}.'/ctags'; chmod $perms, $self->{path}.'/ctags';
413 sub _group_add {
414 my $self = shift;
415 my ($xtra) = @_;
416 $xtra .= join(',', @{$self->{users}});
417 filedb_atomic_append(jailed_file('/etc/group'),
418 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
421 sub _group_update {
422 my $self = shift;
423 my $xtra = join(',', @{$self->{users}});
424 filedb_atomic_edit(jailed_file('/etc/group'),
425 sub {
426 $_ = $_[0];
427 chomp;
428 if ($self->{name} eq (split /:/)[0]) {
429 # preserve readonly flag
430 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
431 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
432 } else {
433 return "$_\n";
439 sub _group_remove {
440 my $self = shift;
441 filedb_atomic_edit(jailed_file('/etc/group'),
442 sub {
443 $self->{name} ne (split /:/)[0] and return $_;
448 sub _hook_path {
449 my $self = shift;
450 my ($name) = @_;
451 $self->{path}.'/hooks/'.$name;
454 sub _hook_install {
455 my $self = shift;
456 my ($name) = @_;
457 my $hooksdir = $self->{path}.'/hooks';
458 my $oldmask = umask();
459 umask($oldmask & ~0070);
460 -d $hooksdir or mkdir $hooksdir or
461 die "hooks directory does not exist and unable to create it for project " . $self->{name} . ": $!";
462 umask($oldmask);
463 open my $src, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
464 open my $dst, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
465 while (<$src>) { print $dst $_; }
466 close $dst;
467 close $src;
468 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
471 sub _hooks_install {
472 my $self = shift;
473 foreach my $hook ('pre-receive', 'post-receive', 'update') {
474 $self->_hook_install($hook);
478 # private constructor, do not use
479 sub _new {
480 my $class = shift;
481 my ($name, $base_path, $path) = @_;
482 does_exist($name,1) || valid_name($name) or die "refusing to create project with invalid name ($name)!";
483 $path ||= "$base_path/$name.git";
484 my $proj = { name => $name, base_path => $base_path, path => $path };
486 bless $proj, $class;
489 # public constructor #0
490 # creates a virtual project not connected to disk image
491 # you can conjure() it later to disk
492 sub ghost {
493 my $class = shift;
494 my ($name, $mirror) = @_;
495 my $self = $class->_new($name, $Girocco::Config::reporoot);
496 $self->{users} = [];
497 $self->{mirror} = $mirror;
498 $self->{email} = $self->{orig_email} = '';
499 $self;
502 # public constructor #1
503 sub load {
504 my $class = shift;
505 my $name = shift || '';
507 open my $fd, '<', jailed_file("/etc/group") or die "project load failed: $!";
508 my $r = qr/^\Q$name\E:/;
509 foreach (grep /$r/, <$fd>) {
510 chomp;
512 my $self = $class->_new($name, $Girocco::Config::reporoot);
513 (-d $self->{path} && $self->_readlocalconfigfile(1))
514 or die "invalid path (".$self->{path}.") for project ".$self->{name};
516 my $ulist;
517 (undef, $self->{crypt}, $self->{gid}, $ulist) = split /:/;
518 $ulist ||= '';
519 $self->{users} = [split /,/, $ulist];
520 $self->{HEAD} = $self->get_HEAD;
521 $self->{orig_HEAD} = $self->{HEAD};
522 $self->{orig_users} = [@{$self->{users}}];
523 $self->{mirror} = ! -e $self->_nofetch_path;
524 $self->{gc_in_progress} = -e $self->_gcp_path;
525 $self->{clone_in_progress} = -e $self->_clonep_path;
526 $self->{clone_logged} = -e $self->_clonelog_path;
527 $self->{clone_failed} = -e $self->_clonefail_path;
528 $self->{ccrypt} = $self->{crypt};
530 $self->_properties_load;
531 $self->{orig_email} = $self->{email};
532 $self->{loaded} = 1; # indicates self was loaded from etc/group file
533 close $fd;
534 return $self;
536 close $fd;
537 undef;
540 # $proj may not be in sane state if this returns false!
541 # fields listed in %$metadata_fields that are NOT also
542 # in @Girocco::Config::project_fields are totally ignored!
543 sub cgi_fill {
544 my $self = shift;
545 my ($gcgi) = @_;
546 my $cgi = $gcgi->cgi;
547 my %allowedfields = map({$_ => 1} @Girocco::Config::project_fields);
548 my $field_enabled = sub {
549 !exists($metadata_fields->{$_[0]}) || exists($allowedfields{$_[0]})};
551 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
552 # in case passwords are disabled
553 defined($pwd) or $pwd = ''; defined($pwd2) or $pwd = '';
554 if ($Girocco::Config::project_passwords and not $self->{crypt} and $pwd eq '' and $pwd2 eq '') {
555 $gcgi->err("Empty passwords are not permitted.");
557 if ($pwd ne '' or not $self->{crypt}) {
558 $self->{crypt} = scrypt_sha1($pwd);
560 if (($pwd ne '' || $pwd2 ne '') and $pwd ne $pwd2) {
561 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
564 $self->{cpwd} = $cgi->param('cpwd');
566 my ($forkee,$project) = ($self->{name} =~ m#^(.*/)?([^/]+)$#);
567 my $newtype = $forkee ? 'fork' : 'project';
568 length($project) <= 64
569 or $gcgi->err("The $newtype name is longer than 64 characters. Do you really need that much?");
571 if ($Girocco::Config::project_owners eq 'email') {
572 $self->{email} = $gcgi->wparam('email');
573 valid_email($self->{email})
574 or $gcgi->err("Your email sure looks weird...?");
575 length($self->{email}) <= 96
576 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
579 $self->{url} = $gcgi->wparam('url');
580 if ($self->{url}) {
581 valid_repo_url($self->{url})
582 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocols are supported. If the URL contains funny characters, contact me.");
583 if ($Girocco::Config::restrict_mirror_hosts) {
584 my $mh = extract_url_hostname($self->{url});
585 is_dns_hostname($mh)
586 or $gcgi->err("Invalid URL. Note that only DNS names are allowed, not IP addresses.");
587 !is_our_hostname($mh)
588 or $gcgi->err("Invalid URL. Mirrors from this host are not allowed, please create a fork instead.");
592 if ($field_enabled->('desc')) {
593 $self->{desc} = to_utf8($gcgi->wparam('desc'), 1);
594 length($self->{desc}) <= 1024
595 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
598 if ($field_enabled->('README')) {
599 $self->{README} = to_utf8($gcgi->wparam('README'), 1);
600 $self->_cleanup_readme;
601 length($self->{README}) <= 8192
602 or $gcgi->err("README length &gt; 8kb!");
603 my @errs = ();
604 @errs = $self->_lint_readme if $gcgi->ok && $Girocco::Config::xmllint_readme;
605 map {$gcgi->err($_); ()} @errs;
608 if ($field_enabled->('hp')) {
609 $self->{hp} = $gcgi->wparam('hp');
610 if ($self->{hp}) {
611 valid_web_url($self->{hp})
612 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
616 my %users = ();
617 my @users = ();
618 foreach my $user ($cgi->param('user')) {
619 if (!exists($users{$user})) {
620 $users{$user} = 1;
621 push(@users, $user) if Girocco::User::does_exist($user, 1);
624 $self->{users} = \@users;
626 $self->{HEAD} = $cgi->param('HEAD') if $cgi->param('HEAD');
628 # schedule deletion of tags (will be committed by update() after auth)
629 $self->{tags_to_delete} = [$cgi->param('tags')];
631 if ($field_enabled->('notifymail')) {
632 $self->{notifymail} = $gcgi->wparam('notifymail');
633 if ($self->{notifymail}) {
634 (valid_email_multi($self->{notifymail}) and length($self->{notifymail}) <= 512)
635 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
639 if ($field_enabled->('notifytag')) {
640 $self->{notifytag} = $gcgi->wparam('notifytag');
641 if ($self->{notifytag}) {
642 (valid_email_multi($self->{notifytag}) and length($self->{notifytag}) <= 512)
643 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
647 if ($field_enabled->('notifyjson')) {
648 $self->{notifyjson} = $gcgi->wparam('notifyjson');
649 if ($self->{notifyjson}) {
650 valid_web_url($self->{notifyjson})
651 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
655 if ($field_enabled->('notifycia')) {
656 $self->{notifycia} = $gcgi->wparam('notifycia');
657 if ($self->{notifycia}) {
658 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
659 or $gcgi->err("Overly suspicious CIA notify project name. If it's actually valid, don't contact me, CIA is defunct.");
663 if ($cgi->param('setstatusupdates')) {
664 my $val = $gcgi->wparam('statusupdates') || '0';
665 $self->{statusupdates} = $val ? 1 : 0;
668 not $gcgi->err_check;
671 sub form_defaults {
672 my $self = shift;
674 name => $self->{name},
675 email => $self->{email},
676 url => $self->{url},
677 desc => html_esc($self->{desc}),
678 README => html_esc($self->{README}),
679 hp => $self->{hp},
680 users => $self->{users},
681 notifymail => html_esc($self->{notifymail}),
682 notifytag => html_esc($self->{notifytag}),
683 notifyjson => html_esc($self->{notifyjson}),
684 notifycia => html_esc($self->{notifycia}),
688 # return true if $enc_passwd is a match for $plain_passwd
689 my $_check_passwd_match = sub {
690 my $enc_passwd = shift;
691 my $plain_passwd = shift;
692 defined($enc_passwd) or $enc_passwd = '';
693 defined($plain_passwd) or $plain_passwd = '';
694 # $enc_passwd may be crypt or crypt_sha1
695 if ($enc_passwd =~ m(^\$sha1\$(\d+)\$([./0-9A-Za-z]{1,64})\$[./0-9A-Za-z]{28}$)) {
696 # It's using sha1-crypt
697 return $enc_passwd eq crypt_sha1($plain_passwd, $2, -(0+$1));
698 } else {
699 # It's using crypt
700 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
704 sub authenticate {
705 my $self = shift;
706 my ($gcgi) = @_;
708 $self->{ccrypt} or die "Can't authenticate against a project with no password";
709 defined($self->{cpwd}) or $self->{cpwd} = '';
710 unless ($_check_passwd_match->($self->{ccrypt}, $self->{cpwd})) {
711 $gcgi->err("Your admin password does not match!");
712 return 0;
714 return 1;
717 # return true if the password from the file is empty or consists of all the same
718 # character. However, if the project was NOT loaded from the group file
719 # (!self->{loaded}) then the password is never locked.
720 # This function does NOT check $Girocco::Config::project_passwords, the caller
721 # is responsible for doing so if desired. Same for $self->{email}.
722 sub is_password_locked {
723 my $self = shift;
725 $self->{loaded} or return 0;
726 my $testcrypt = $self->{ccrypt}; # The value from the group file
727 defined($testcrypt) or $testcrypt = '';
728 $testcrypt ne '' or return 1; # No password at all
729 $testcrypt =~ /^(.)\1*$/ and return 1; # Bogus crypt value
730 return 0; # Not locked
733 sub _setup {
734 use POSIX qw(strftime);
735 my $self = shift;
736 my ($pushers) = @_;
737 my $fd;
739 $self->_mkdir_forkees;
741 my $gid;
742 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
743 if ($Girocco::Config::owning_group) {
744 $gid = scalar(getgrnam($Girocco::Config::owning_group));
745 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
746 chmod(02775, $self->{path}) or die "chmod 02775 $self->{path} failed: $!";
747 } else {
748 chmod(02777, $self->{path}) or die "chmod 02777 $self->{path} failed: $!";
750 $ENV{'GIT_TEMPLATE_DIR'} = $Girocco::Config::chroot.'/var/empty';
751 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--quiet', '--bare', '--shared='.$self->shared_mode()) == 0
752 or die "git init $self->{path} failed: $?";
753 # we don't need these two, remove them (they will normally be created empty) if they exist
754 rmdir $self->{path}."/branches";
755 rmdir $self->{path}."/remotes";
756 -d $self->{path}."/info" or mkdir $self->{path}."/info"
757 or die "info directory does not exist and unable to create it: $!";
758 -d $self->{path}."/hooks" or mkdir $self->{path}."/hooks"
759 or die "hooks directory does not exist and unable to create it: $!";
760 # clean out any kruft that may have come in from the initial template directory
761 foreach my $cleandir (qw(hooks info)) {
762 if (opendir my $hooksdir, $self->{path}.'/'.$cleandir) {
763 unlink map "$self->{path}/$_", grep { $_ ne '.' && $_ ne '..' } readdir $hooksdir;
764 closedir $hooksdir;
767 if ($Girocco::Config::owning_group) {
768 chown(-1, $gid, $self->{path}."/hooks") or die "chgrp $gid $self->{path}/hooks failed: $!";
770 # hooks never world writable
771 chmod 02775, $self->{path}."/hooks" or die "chmod 02775 $self->{path}/hooks failed: $!";
772 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.logallrefupdates', 'false') == 0
773 or die "disabling core.logallrefupdates failed: $?";
774 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
775 or die "disabling receive.denyNonFastforwards failed: $?";
776 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
777 or die "disabling gc.auto failed: $?";
778 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.autogc', '0') == 0
779 or die "disabling receive.autogc failed: $?";
780 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
781 $self->{creationtime} = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
782 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'girocco.creationtime', $self->{creationtime}) == 0
783 or die "setting girocco.creationtime failed: $?";
784 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.updateserverinfo', 'true') == 0
785 or die "enabling receive.updateserverinfo failed: $?";
786 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'repack.writebitmaps', 'true') == 0
787 or die "enabling repack.writebitmaps failed: $?";
788 -d $self->{path}."/htmlcache" or mkdir $self->{path}."/htmlcache"
789 or die "htmlcache directory does not exist and unable to create it: $!";
790 -d $self->{path}."/bundles" or mkdir $self->{path}."/bundles"
791 or die "bundles directory does not exist and unable to create it: $!";
792 foreach my $file (qw(info/lastactivity .delaygc)) {
793 if (open $fd, '>', $self->{path}."/".$file) {
794 close $fd;
795 chmod 0664, $self->{path}."/".$file;
799 # /info must have right permissions,
800 # and git init didn't do it for some reason.
801 # config must have correct permissions.
802 # and Git 2.1.0 - 2.2.1 incorrectly add +x for some reason.
803 # also make sure /refs, /objects and /htmlcache are correct too.
804 my ($dmode, $dmodestr, $fmode, $fmodestr);
805 if ($Girocco::Config::owning_group) {
806 ($dmode, $dmodestr) = (02775, '02775');
807 ($fmode, $fmodestr) = (0664, '0664');
808 } else {
809 ($dmode, $dmodestr) = (02777, '02777');
810 ($fmode, $fmodestr) = (0666, '0666');
812 foreach my $dir (qw(info refs objects htmlcache bundles)) {
813 chmod($dmode, $self->{path}."/$dir") or die "chmod $dmodestr $self->{path}/$dir failed: $!";
815 foreach my $file (qw(config)) {
816 chmod($fmode, $self->{path}."/$file") or die "chmod $fmodestr $self->{path}/$file failed: $!";
818 # these ones are probably not strictly required but are nice to have
819 foreach my $dir (qw(refs/heads refs/tags objects/info objects/pack)) {
820 -d $self->{path}."/$dir" or mkdir $self->{path}."/$dir";
821 chmod($dmode, $self->{path}."/$dir");
824 $self->_properties_save;
825 $self->_alternates_setup;
826 $self->_ctags_setup;
827 $self->_group_remove;
828 $self->_group_add($pushers);
829 $self->_hooks_install;
830 $self->_update_index;
831 $self->_set_changed;
832 $self->_set_forkchange(1);
835 sub premirror {
836 my $self = shift;
838 $self->_setup(':');
839 $self->_clonep(1);
840 $self->perm_initialize;
843 sub conjure {
844 my $self = shift;
846 $self->_setup;
847 $self->_nofetch(1);
848 if ($Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
849 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
850 or die "create-personal-mob-area $self->{name} failed";
852 $self->perm_initialize;
855 sub clone {
856 my $self = shift;
858 unlink ($self->_clonefail_path()); # Ignore EEXIST error
859 unlink ($self->_clonelog_path()); # Ignore EEXIST error
861 use IO::Socket;
862 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
863 $sock->print("clone ".$self->{name}."\n");
864 # Just ignore reply, we are going to succeed anyway and the I/O
865 # would apparently get quite hairy.
866 $sock->flush();
867 sleep 2; # *cough*
868 $sock->close();
871 sub update {
872 my $self = shift;
874 $self->_properties_save;
875 $self->_group_update;
877 if (exists($self->{tags_to_delete})) {
878 $self->delete_ctag($_) foreach(@{$self->{tags_to_delete}});
881 $self->set_HEAD($self->{HEAD}) unless $self->{orig_HEAD} eq $self->{HEAD};
883 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
884 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
885 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
886 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
888 $self->_update_index if $self->{email} ne $self->{orig_email};
889 $self->{orig_email} = $self->{email};
890 $self->_set_changed;
895 sub update_password {
896 my $self = shift;
897 my ($pwd) = @_;
899 $self->{crypt} = scrypt_sha1($pwd);
900 $self->_group_update;
903 # You can explicitly do this just on a ghost() repository too.
904 sub delete {
905 my $self = shift;
907 if (-d $self->{path}) {
908 system('rm', '-rf', $self->{path}) == 0
909 or die "rm -rf $self->{path} failed: $?";
911 # attempt to clean up any empty fork directories by removing them
912 my @pelems = split('/', $self->{name});
913 while (@pelems > 1) {
914 pop @pelems;
915 # okay to fail
916 rmdir join('/', $Girocco::Config::reporoot, @pelems) or last;
918 $self->_group_remove;
919 $self->_update_index;
920 $self->_set_forkchange(1);
925 sub _contains_files {
926 my $dir = shift;
927 (-d $dir) or return 0;
928 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
929 while (my $entry = readdir($dh)) {
930 next if $entry eq '' || $entry eq '.' || $entry eq '..';
931 closedir($dh), return 1
932 if -f "$dir/$entry" ||
933 -d "$dir/$entry" && _contains_files("$dir/$entry");
935 closedir($dh);
936 return 0;
939 sub has_forks {
940 my $self = shift;
942 return _contains_files($Girocco::Config::reporoot.'/'.$self->{name});
945 # Returns an array of 0 or more array refs, one for each bundle:
946 # ->[0]: bundle creation time (seconds since epoch)
947 # ->[1]: bundle name (e.g. foo-xxxxxxxx.bundle)
948 # ->[2]: bundle size in bytes
949 sub bundles {
950 my $self = shift;
951 use Time::Local;
953 return @{$self->{bundles}} if ref($self->{bundles}) eq 'ARRAY';
954 my @blist = ();
955 if (-d $self->{path}.'/bundles') {{
956 my $prefix = $self->{name};
957 $prefix =~ s|^.*[^/]/([^/]+)$|$1|;
958 opendir(my $dh, $self->{path}.'/bundles') or last;
959 while (my $bfile = readdir($dh)) {
960 next unless $bfile =~ /^\d{8}_\d{6}-[0-9a-f]{8}$/;
961 my $ctime = eval {timegm(
962 0+substr($bfile,13,2), 0+substr($bfile,11,2), 0+substr($bfile,9,2),
963 0+substr($bfile,6,2), 0+substr($bfile,4,2)-1, 0+substr($bfile,0,4)-1900)};
964 next unless $ctime;
965 open(my $bh, '<', $self->{path}.'/bundles/'.$bfile) or next;
966 my $f1 = <$bh>;
967 my $f2 = <$bh>;
968 $f1 = $self->{path}.'/objects/pack/'.$f1 if $f1 && $f1 !~ m|^/|;
969 $f2 = $self->{path}.'/objects/pack/'.$f2 if $f2 && $f2 !~ m|^/|;
970 close($bh);
971 next unless $f1 && $f2 && $f1 ne $f2;
972 chomp $f1;
973 chomp $f2;
974 next unless -e $f1 && -e $f2;
975 my $s1 = -s $f1 || 0;
976 my $s2 = -s $f2 || 0;
977 next unless $s1 || $s2;
978 push(@blist, [$ctime, "$prefix-".substr($bfile,16,8).".bundle", $s1+$s2]);
980 closedir($dh);
982 @blist = sort({$b->[0] <=> $a->[0]} @blist);
983 my %seen = ();
984 my @result = ();
985 foreach my $bndl (@blist) {
986 next if $seen{$bndl->[1]};
987 $seen{$bndl->[1]} = 1;
988 push(@result, $bndl);
990 $self->{bundles} = \@result;
991 return @result;
994 sub has_alternates {
995 my $self = shift;
996 return -s $self->{path}.'/objects/info/alternates' ? 1 : 0;
999 sub has_bundle {
1000 my $self = shift;
1002 return scalar($self->bundles);
1005 sub is_empty {
1006 # A project is considered empty if the git repository does not
1007 # have any refs. This means packed-refs does not exist or is
1008 # empty or only has lines starting with '#' AND there are no
1009 # files in the refs subdirectory hierarchy (no matter how deep).
1011 my $self = shift;
1013 (-d $self->{path}) or return 0;
1014 if (-e $self->{path}.'/packed-refs') {
1015 open(my $pr, '<', $self->{path}.'/packed-refs')
1016 or die "open $self->{path}./packed-refs failed: $!";
1017 my $foundref = 0;
1018 while (my $ref = <$pr>) {
1019 next if $ref =~ /^#/;
1020 $foundref = 1;
1021 last;
1023 close($pr);
1024 return 0 if $foundref;
1026 (-d $self->{path}.'/refs') or return 1;
1027 return !_contains_files($self->{path}.'/refs');
1030 # returns array:
1031 # [0]: earliest possible scheduled next gc, undef if lastgc not set
1032 # [1]: approx. latest possible scheduled next gc, undef if lastgc not set
1033 # Both values (if not undef) are seconds since epoch
1034 # Result only considers lastgc and min_gc_interval nothing else
1035 sub next_gc {
1036 my $self = shift;
1037 my $lastgcepoch = parse_any_date($self->{lastgc});
1038 return (undef, undef) unless defined $lastgcepoch;
1039 return ($lastgcepoch + $Girocco::Config::min_gc_interval,
1040 int($lastgcepoch + 1.25 * $Girocco::Config::min_gc_interval +
1041 $Girocco::Config::min_mirror_interval));
1044 # returns boolean (0 or 1)
1045 # Attempts to determine whether or not a gc (and corresponding build
1046 # of a .bitmap/.bndl file) will actually take place at the next_gc
1047 # time (as returned by next_gc). Whether or not a .bitmap and .bndl
1048 # end up being built depends on whether or not the local object graph
1049 # is complete. In general if has_alternates is true then a .bitmap/.bndl
1050 # is not possible because the object graph will be incomplete,
1051 # but it *is* possible that even though the repository has_alternates,
1052 # it does not actually borrow any objects so a .bitmap/.bndl will build
1053 # in spite of the presence of alternates -- but this is expected to be rare.
1054 # The result is a best guess and false return value is not an absolute
1055 # guarantee that gc will not take place at the next interval, but it probably
1056 # will not if nothing changes in the meantime.
1057 sub needs_gc {
1058 my $self = shift;
1059 my $lgc = parse_any_date($self->{lastgc});
1060 my $lrecv = parse_any_date($self->{lastreceive});
1061 my $lpgc = parse_any_date($self->{lastparentgc});
1062 return 1 unless defined($lgc) && defined($lrecv);
1063 if ($self->has_alternates) {
1064 return 1 unless defined($lpgc);
1065 return 1 unless $lpgc < $lgc;
1067 return 1 unless $lrecv < $lgc;
1068 # We don't try running any "is_dirty" check, so if somehow the
1069 # repository became dirty without updating lastreceive we might
1070 # incorrectly return false instead of true.
1071 return 0;
1074 sub delete_ctag {
1075 my $self = shift;
1076 my ($ctag) = @_;
1078 # sanity check, disallow filenames starting with . .. or /
1079 unlink($self->{path}.'/ctags/'.$ctag) if($ctag !~ m|^(\.\.?)/|);
1082 sub get_ctag_names {
1083 my $self = shift;
1084 my @ctags = ();
1085 opendir(my $dh, $self->{path}.'/ctags')
1086 or return @ctags;
1087 @ctags = grep { -f "$self->{path}/ctags/$_" } readdir($dh);
1088 closedir($dh);
1089 return @ctags;
1092 sub get_heads {
1093 my $self = shift;
1094 my $fh;
1095 my $heads = get_git("--git-dir=$self->{path}", 'for-each-ref', '--format=%(objectname) %(refname)', 'refs/heads');
1096 defined($heads) or $heads = '';
1097 chomp $heads;
1098 my @res = ();
1099 foreach (split(/\n/, $heads)) {
1100 chomp;
1101 next if !m#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
1102 push @res, $1;
1104 @res;
1107 sub get_HEAD {
1108 my $self = shift;
1109 my $HEAD = get_git("--git-dir=$self->{path}", 'symbolic-ref', 'HEAD');
1110 defined($HEAD) or $HEAD = '';
1111 chomp $HEAD;
1112 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
1113 return $1;
1116 sub set_HEAD {
1117 my $self = shift;
1118 my $newHEAD = shift;
1119 # Cursory checks only -- if you want to break your HEAD, be my guest
1120 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
1121 die "grossly invalid new HEAD: $newHEAD";
1123 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', "refs/heads/$newHEAD");
1124 die "could not set HEAD" if ($? >> 8);
1125 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob'
1126 or system('cp', '-p', '-f', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
1129 sub gen_auth {
1130 my $self = shift;
1131 my ($type) = @_;
1132 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
1134 $self->{authtype} = $type;
1136 no warnings;
1137 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
1139 my $expire = time + 24 * 3600;
1140 my $propval = "# ${type}AUTH $self->{auth} $expire";
1141 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gitweb.repoauth', $propval);
1142 $self->{auth};
1145 sub del_auth {
1146 my $self = shift;
1148 delete $self->{auth};
1149 delete $self->{authtype};
1150 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--unset', 'gitweb.repoauth');
1153 sub remove_user {
1154 my $self = shift;
1155 my ($username) = @_;
1157 my $before_count = @{$self->{users}};
1158 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
1159 return @{$self->{users}} != $before_count;
1162 ### static methods
1164 sub get_forkee_name {
1165 local $_ = $_[0];
1166 (m#^(.*)/.*?$#)[0]; #
1169 sub get_forkee_path {
1170 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1171 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
1172 -d $forkee ? $forkee : '';
1175 # Ultimately the full project/fork name could end up being part of a Git ref name
1176 # when a project's forks are combined into one giant repository for efficiency.
1177 # That means that the project/fork name must satisfy the Git ref name requirements:
1179 # 1. Characters with an ASCII value less than or equal to 32 are not allowed
1180 # 2. The character with an ASCII value of 0x7F is not allowed
1181 # 3. The characters '~', '^', ':', '\', '*', '?', and '[' are not allowed
1182 # 4. The character '/' is a separator and is not allowed within a name
1183 # 5. The name may not start with '.' or end with '.'
1184 # 6. The name may not end with '.lock'
1185 # 7. The name may not contain the '..' sequence
1186 # 8. The name may not contain the '@{' sequence
1187 # 9. If multiple components are used (separated by '/'), no empty '' components
1189 # We also prohibit a trailing '.git' on any path component and futher restrict
1190 # the allowed characters to alphanumeric and [+._-] where names must start with
1191 # an alphanumeric.
1193 sub _valid_name_characters {
1194 local $_ = $_[0];
1195 (not m#^[/+._-]#)
1196 and (not m#//#)
1197 and (not m#\.\.#)
1198 and (not m#/[+._-]#)
1199 and (not m#\./#)
1200 and (not m#\.$#)
1201 and (not m#\.git/#i)
1202 and (not m#\.git$#i)
1203 and (not m#\.lock/#i)
1204 and (not m#\.lock$#i)
1205 and (not m#\.bundle/#i)
1206 and (not m#\.bundle$#i)
1207 and (not m#/$#)
1208 and m#^[a-zA-Z0-9/+._-]+$#
1209 and !has_reserved_suffix($_, $_[1], $_[2]);
1212 sub valid_name {
1213 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1214 local $_ = $_[0];
1215 _valid_name_characters($_) and not exists($reservedprojectnames{lc($_)})
1216 and @{[m#/#g]} <= 5 # maximum fork depth is 5
1217 and ((not m#/#) or -d get_forkee_path($_)); # will also catch ^/
1220 # It's possible that some forks have been kept but the forkee is gone.
1221 # In this case the standard valid_name check is too strict.
1222 sub does_exist {
1223 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1224 my ($name, $nodie) = @_;
1225 my $okay = (
1226 _valid_name_characters($name, $Girocco::Config::reporoot, ".git")
1227 and ((not $name =~ m#/#)
1228 or -d get_forkee_path($name)
1229 or -d $Girocco::Config::reporoot.'/'.get_forkee_name($name)));
1230 (!$okay && $nodie) and return undef;
1231 !$okay and die "tried to query for project with invalid name $name!";
1232 (-d $Girocco::Config::reporoot."/$name.git");
1235 sub get_full_list {
1236 open my $fd, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
1237 my @projects = map {/^([^:_\s#][^:\s#]*):[^:]*:\d{5,}:/ ? $1 : ()} <$fd>;
1238 close $fd;
1239 @projects;