1 package Girocco
::Project
;
10 use Girocco
::HashUtil
;
11 use Girocco
::ProjPerm
;
13 use base
('Girocco::ProjPerm::'.$Girocco::Config
::permission_control
); # mwahaha
28 require Digest
::SHA
::PurePerl
;
29 Digest
::SHA
::PurePerl
->import(
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, < 8 KiB)<br />leave blank for automatic</span>',
41 'README', 'textarea', 'Enter only “<!-- comments -->” '.
42 'to completely suppress any README'],
43 notifymail
=> ['Commit notify – mail to', 'notifymail', 'text',
44 'comma separated address list'],
45 notifytag
=> ['Tag notify – mail to', 'notifytag', 'text',
46 'comma separated address list – if not empty, tag '.
47 'notifications are sent here INSTEAD of to '.
48 '“Commit notify – mail to” address(es)'],
49 notifyjson
=> ['Commit notify – '.
50 '<a title="'.html_esc
('single field name is “payload”', 1).'" href="'.
51 'https://developer.github.com/v3/activity/events/types/#pushevent'.
52 '">POST JSON</a> at', 'notifyjson', 'text'],
53 notifycia
=> ['Commit notify – <a href="http://cia.vc/doc/">CIA project</a> name',
54 'notifycia', 'text', 'CIA is defunct – this value is ignored'],
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) {
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
77 desc
=> 'description',
78 README
=> 'README.html',
80 notifymail
=> '%hooks.mailinglist',
81 notifytag
=> '%hooks.announcelist',
82 notifyjson
=> '%hooks.jsonurl',
83 notifycia
=> '%hooks.cianame',
87 lastchange
=> ':lastchange',
88 lastactivity
=> 'info/lastactivity',
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
111 system("$Girocco::Config::basedir/gitweb/genindex.sh", $self->{name
});
114 sub _readlocalconfigfile
{
116 my $undefonerr = shift || 0;
117 delete $self->{configfilehash
};
118 my $configfile = get_git
("--git-dir=$self->{path}",
119 'config', '--list', '--local', '--null');
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;
128 # @_[0]: argument to convert to boolean result (0 or 1)
129 # @_[1]: value to use if argument is undef (default is 0)
132 my ($val, $def) = @_;
133 defined($def) or $def = 0;
134 defined($val) or $val = $def;
137 return 0 if $val eq '' || $val eq 'false' || $val eq 'off' || $val =~ /^0+$/;
144 $self->{path
}.'/'.$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 = '';
165 open my $p, '<', $self->_property_path($pname) or return undef;
168 my $value = join('', @value); chomp $value;
174 my ($name, $value) = @_;
175 my $pname = $propmap{$name};
176 $pname or die "unknown property: $name";
178 if ($pname =~ s/^://) {
179 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', "gitweb.$pname", $value);
181 } elsif ($pname =~ s/^%//) {
182 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', $pname, $value);
186 my $P = lock_file
($self->_property_path($pname));
188 $value ne '' and print $P "$value\n";
190 unlock_file
($self->_property_path($pname));
193 sub _cleanup_readme
{
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";
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;
213 for my $line (split(/\n/, $errors)) {
214 $line = html_esc
($line);
215 $line =~ s/ /\ /gs;
216 if ($line =~ /^-(:\d+:.*)$/) {
217 push @errs, $err if $err;
218 $err = '<tt>README' . $1 . '</tt>';
220 $err .= '<br />' if $err;
221 $err .= '<tt>' . $line . '</tt>';
224 push @errs, $err if $err;
228 sub _properties_load
{
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 = '';
243 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
245 if (time < $expire) {
246 $self->{authtype
} = $1;
250 $self->_cleanup_readme;
251 delete $self->{configfilehash
};
254 sub _properties_save
{
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"))) {
268 chmod(0664, $self->_property_path(".bangagain"));
275 $self->_property_path('.nofetch');
281 my $nf = $self->_nofetch_path;
283 open my $x, '>', $nf or die "nofetch failed: $!";
286 unlink $nf or die "yesfetch failed: $!";
292 $self->_property_path('.gc_in_progress');
297 $self->_property_path('.clonelog');
300 sub _clonefail_path
{
302 $self->_property_path('.clone_failed');
307 $self->_property_path('.clone_in_progress');
313 my $np = $self->_clonep_path;
315 open my $x, '>', $np or die "clonep failed: $!";
318 unlink $np or die "clonef failed: $!";
321 sub _alternates_setup
{
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";
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); #
346 chmod 0664, $filename or warn "cannot chmod $filename: $!";
349 # copy lastactivity from the parent project
350 if (open $x, '<', $forkee_path.'/info/lastactivity') {{
353 last unless $activity;
354 open $x, '>', $self->{path
}.'/info/lastactivity' or last;
358 $self->{'lastactivity'} = $activity;
361 # copy refs from parent project
363 open(SAVEOUT
, ">&STDOUT") || die "couldn't dup STDOUT: $!";
364 my $result = open(STDOUT
, '>', "$self->{path}/packed-refs");
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
);
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";
380 my $HEAD = get_git
("--git-dir=$forkee_path", 'symbolic-ref', 'HEAD');
381 defined($HEAD) && $HEAD =~ m
,^refs
/heads
/., or $HEAD = 'refs/heads/master';
383 system($Girocco::Config
::git_bin
, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', $HEAD);
384 chmod 0664, "$self->{path}/packed-refs", "$self->{path}/HEAD";
390 open $fd, '>', "$self->{path}/htmlcache/changed" and close $fd;
393 sub _set_forkchange
{
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
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;
409 my $perms = $Girocco::Config
::permission_control
eq 'Hooks' ?
02777 : 02775;
410 mkdir $self->{path
}.'/ctags'; chmod $perms, $self->{path
}.'/ctags';
416 $xtra .= join(',', @
{$self->{users
}});
417 filedb_atomic_append
(jailed_file
('/etc/group'),
418 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
423 my $xtra = join(',', @
{$self->{users
}});
424 filedb_atomic_edit
(jailed_file
('/etc/group'),
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";
441 filedb_atomic_edit
(jailed_file
('/etc/group'),
443 $self->{name
} ne (split /:/)[0] and return $_;
451 $self->{path
}.'/hooks/'.$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
} . ": $!";
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 $_; }
468 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
473 foreach my $hook ('pre-receive', 'post-receive', 'update') {
474 $self->_hook_install($hook);
478 # private constructor, do not use
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 };
489 # public constructor #0
490 # creates a virtual project not connected to disk image
491 # you can conjure() it later to disk
494 my ($name, $mirror) = @_;
495 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
497 $self->{mirror
} = $mirror;
498 $self->{email
} = $self->{orig_email
} = '';
502 # public constructor #1
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>) {
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
};
517 (undef, $self->{crypt}, $self->{gid
}, $ulist) = split /:/;
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
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!
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');
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
});
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 > 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 > 8kb!");
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');
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.");
618 foreach my $user ($cgi->param('user')) {
619 if (!exists($users{$user})) {
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;
674 name
=> $self->{name
},
675 email
=> $self->{email
},
677 desc
=> html_esc
($self->{desc
}),
678 README
=> html_esc
($self->{README
}),
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));
700 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
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!");
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
{
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
734 use POSIX
qw(strftime);
739 $self->_mkdir_forkees;
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: $!";
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;
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) {
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');
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;
827 $self->_group_remove;
828 $self->_group_add($pushers);
829 $self->_hooks_install;
830 $self->_update_index;
832 $self->_set_forkchange(1);
840 $self->perm_initialize;
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;
858 unlink ($self->_clonefail_path()); # Ignore EEXIST error
859 unlink ($self->_clonelog_path()); # Ignore EEXIST error
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.
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
};
895 sub update_password
{
899 $self->{crypt} = scrypt_sha1
($pwd);
900 $self->_group_update;
903 # You can explicitly do this just on a ghost() repository too.
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) {
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
{
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");
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
953 return @
{$self->{bundles
}} if ref($self->{bundles
}) eq 'ARRAY';
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)};
965 open(my $bh, '<', $self->{path
}.'/bundles/'.$bfile) or next;
968 $f1 = $self->{path
}.'/objects/pack/'.$f1 if $f1 && $f1 !~ m
|^/|;
969 $f2 = $self->{path
}.'/objects/pack/'.$f2 if $f2 && $f2 !~ m
|^/|;
971 next unless $f1 && $f2 && $f1 ne $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]);
982 @blist = sort({$b->[0] <=> $a->[0]} @blist);
985 foreach my $bndl (@blist) {
986 next if $seen{$bndl->[1]};
987 $seen{$bndl->[1]} = 1;
988 push(@result, $bndl);
990 $self->{bundles
} = \
@result;
996 return -s
$self->{path
}.'/objects/info/alternates' ?
1 : 0;
1002 return scalar($self->bundles);
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).
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: $!";
1018 while (my $ref = <$pr>) {
1019 next if $ref =~ /^#/;
1024 return 0 if $foundref;
1026 (-d
$self->{path
}.'/refs') or return 1;
1027 return !_contains_files
($self->{path
}.'/refs');
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
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.
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.
1078 # sanity check, disallow filenames starting with . .. or /
1079 unlink($self->{path
}.'/ctags/'.$ctag) if($ctag !~ m
|^(\
.\
.?
)/|);
1082 sub get_ctag_names
{
1085 opendir(my $dh, $self->{path
}.'/ctags')
1087 @ctags = grep { -f
"$self->{path}/ctags/$_" } readdir($dh);
1095 my $heads = get_git
("--git-dir=$self->{path}", 'for-each-ref', '--format=%(objectname) %(refname)', 'refs/heads');
1096 defined($heads) or $heads = '';
1099 foreach (split(/\n/, $heads)) {
1101 next if !m
#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
1109 my $HEAD = get_git
("--git-dir=$self->{path}", 'symbolic-ref', 'HEAD');
1110 defined($HEAD) or $HEAD = '';
1112 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
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;
1132 $type = 'REPO
' unless $type && $type =~ /^[A-Z]+$/;
1134 $self->{authtype} = $type;
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);
1148 delete $self->{auth};
1149 delete $self->{authtype};
1150 system($Girocco::Config::git_bin, '--git
-dir
='.$self->{path}, 'config
', '--unset
', 'gitweb
.repoauth
');
1155 my ($username) = @_;
1157 my $before_count = @{$self->{users}};
1158 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
1159 return @{$self->{users}} != $before_count;
1164 sub get_forkee_name {
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
1193 sub _valid_name_characters
{
1198 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)
1208 and m
#^[a-zA-Z0-9/+._-]+$#
1209 and !has_reserved_suffix
($_, $_[1], $_[2]);
1213 no warnings
; # avoid silly 'unsuccessful stat on filename with \n' warning
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.
1223 no warnings
; # avoid silly 'unsuccessful stat on filename with \n' warning
1224 my ($name, $nodie) = @_;
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");
1236 open my $fd, '<', jailed_file
("/etc/group") or die "getting project list failed: $!";
1237 my @projects = map {/^([^:_\s#][^:\s#]*):[^:]*:\d{5,}:/ ?
$1 : ()} <$fd>;