admins.html: add lightweight irc web client link
[girocco.git] / Girocco / Project.pm
blobf3f97dba7cd0025f1b46fe9c33b09a53e5a0ae2d
1 package Girocco::Project;
3 use strict;
4 use warnings;
6 BEGIN {
7 use Girocco::Config;
8 use Girocco::CGI;
9 use Girocco::User;
10 use Girocco::Util;
11 use Girocco::HashUtil;
12 use Girocco::Notify;
13 use Girocco::ProjPerm;
14 use base ('Girocco::ProjPerm::'.$Girocco::Config::permission_control); # mwahaha
17 BEGIN {
18 eval {
19 require Digest::SHA;
20 Digest::SHA->import(
21 qw(sha1_hex)
22 );1} ||
23 eval {
24 require Digest::SHA1;
25 Digest::SHA1->import(
26 qw(sha1_hex)
27 );1} ||
28 eval {
29 require Digest::SHA::PurePerl;
30 Digest::SHA::PurePerl->import(
31 qw(sha1_hex)
32 );1} ||
33 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
34 . "must be available\n";
37 # NOTE: Each value may be either an ARRAY ref of a single element
38 # OR an ARRAY ref containing 1 or more ARRAY refs thus allowing
39 # a set of fields to be associated with a name (e.g. 'notifyjson')
40 our $metadata_fields = {
41 cleanmirror => ['Mirror refs', 'cleanmirror', 'placeholder'],
42 homepage => ['Homepage URL', 'hp', 'text'],
43 shortdesc => ['Short description', 'desc', 'text'],
44 README => [
45 ['README Format', 'rmtype', 'select', 'Format of README data entered here', \&_rmtype_choices],
46 ['<span style="display:inline-block;vertical-align:top">'.
47 'README (&lt; 8 KiB)<br />leave blank for automatic</span>',
48 'README', 'textarea', 'Enter only &#x201c;<!-- comments -->&#x201d; '.
49 'to completely suppress any README']],
50 notifymail => ['Commit notify &#x2013; mail to', 'notifymail', 'text',
51 'comma separated address list'],
52 reverseorder => ['Show oldest first', 'reverseorder', 'checkbox',
53 'show new revisions in oldest to newest order (instead of the default newest to oldest older)'.
54 ' in &#x201c;Commit notify&#x201d; email when showing new revisions'],
55 summaryonly => ['Summaries only', 'summaryonly', 'checkbox',
56 'suppress patch/diff output in &#x201c;Commit notify&#x201d; email when showing new revisions'],
57 notifytag => ['Tag notify &#x2013; mail to', 'notifytag', 'text',
58 'comma separated address list &#x2013; if not empty, tag '.
59 'notifications are sent here INSTEAD of to '.
60 '&#x201c;Commit notify &#x2013; mail to&#x201d; address(es)'],
61 notifyjson => [
62 ['Commit notify &#x2013; '.
63 '<a title="'.html_esc('single field name is &#x201c;payload&#x201d;', 1).'" href="'.
64 'https://docs.github.com/developers/webhooks-and-events/webhook-events-and-payloads#push'.
65 '">POST JSON</a> at', 'notifyjson', 'text'],
66 ['JSON Content-Type', 'jsontype', 'select', 'JSON POST Content-Type', \&_json_choices],
67 ['JSON Secret', 'jsonsecret', 'text', 'secret used to compute JSON POST signatures']],
68 notifycia => ['Commit notify &#x2013; <a href="http://cia.vc/doc/">CIA project</a> name',
69 'notifycia', 'text', 'CIA is defunct &#x2013; this value is ignored'],
72 sub _rmtype_choices {
73 return ('Markdown', 'Plain Text', 'HTML');
76 sub _json_choices {
77 return ('application/x-www-form-urlencoded', 'application/json');
80 sub _mkdir_forkees {
81 my $self = shift;
82 my @pelems = split('/', $self->{name});
83 pop @pelems; # do not create dir for the project itself
84 my $path = $self->{base_path};
85 foreach my $pelem (@pelems) {
86 $path .= "/$pelem";
87 (-d "$path") or mkdir $path or die "mkdir $path: $!";
88 chmod 02775, $path; # ok if fails (dir may already exist and be owned by someone else)
92 # With a leading ':' get from project local config replacing ':' with 'gitweb.'
93 # With a leading '%' get from project local config after removing '%'
94 # With a leading '!' get boolean from project local config after removing '!' (prefixed with 'gitweb.' if no '.')
95 # With a leading [:%!] a trailing ':defval' may be added to select the default value to use if unset
96 # Otherwise it's a project file name to be loaded
97 # %propmapro entries are loaded but never written
98 # %propmapromirror entries are loaded only for mirrors but never written
100 our %propmap = (
101 url => ':baseurl',
102 email => ':owner',
103 desc => 'description',
104 README => 'README.html',
105 READMEDATA => 'README.dat',
106 rmtype => '%girocco.readmetype',
107 hp => ':homepage',
108 notifymail => '%hooks.mailinglist',
109 notifytag => '%hooks.announcelist',
110 notifyjson => '%hooks.jsonurl',
111 jsontype => '%hooks.jsontype',
112 jsonsecret => '%hooks.jsonsecret',
113 notifycia => '%hooks.cianame',
114 cleanmirror => '!girocco.cleanmirror',
115 statusupdates => '!statusupdates:1',
116 reverseorder => '!hooks.reverseorder',
117 summaryonly => '!hooks.summaryonly',
120 our %propmapro = (
121 lastchange => ':lastchange',
122 lastactivity => 'info/lastactivity',
123 lastgc => ':lastgc',
124 lastreceive => ':lastreceive',
125 lastparentgc => ':lastparentgc',
126 lastrefresh => ':lastrefresh',
127 creationtime => '%girocco.creationtime',
128 reposizek => '%girocco.reposizek',
129 notifyhook => '%girocco.notifyhook:undef',
130 origurl => ':baseurl',
133 our %propmapromirror = (
134 bangcount => '%girocco.bang.count',
135 bangfirstfail => '%girocco.bang.firstfail',
136 bangmessagesent => '!girocco.bang.messagesent',
137 showpush => '!showpush',
140 # Projects with any of these names will be disallowed to avoid possible
141 # collisions with cgi script paths or chroot paths
142 # NOTE: names are checked after using lc on them, so all entries MUST be lowercase
143 our %reservedprojectnames = (
144 admin => 1, # /admin/ links
145 alternates => 1, # .git/objects/info/alternates
146 b => 1, # /b/ -> bundle.cgi
147 blog => 1, # /blog/ links
148 c => 1, # /c/ -> cgit
149 'git-receive-pack' => 1, # smart HTTP
150 'git-upload-archive' => 1, # smart HTTP
151 'git-upload-pack' => 1, # smart HTTP
152 h => 1, # /h/ -> html.cgi
153 head => 1, # .git/HEAD
154 'http-alternates' => 1, # .git/objects/info/http-alternates
155 info => 1, # .git/info
156 objects => 1, # .git/objects
157 packs => 1, # .git/objects/info/packs
158 r => 1, # /r/ -> git http
159 refs => 1, # .git/refs
160 w => 1, # /w/ -> gitweb
161 wiki => 1, # /wiki/ links
162 srv => 1, # /srv/git/ -> chroot ssh git repositories
165 sub _update_index {
166 my $self = shift;
167 system("$Girocco::Config::basedir/gitweb/genindex.sh", $self->{name});
170 sub _readlocalconfigfile {
171 my $self = shift;
172 my $undefonerr = shift || 0;
173 delete $self->{configfilehash};
174 my $confighash = read_config_file_hash($self->{path} . "/config");
175 my $result = 1;
176 defined($confighash) || $undefonerr or $result = 0, $confighash = {};
177 return undef unless defined($confighash);
178 $self->{configfilehash} = $confighash;
179 return $result;
182 # @_[0]: argument to convert to boolean result (0 or 1)
183 # @_[1]: value to use if argument is undef (default is 0)
184 # Returns 0 or 1
185 sub _boolval {
186 my ($val, $def) = @_;
187 defined($def) or $def = 0;
188 defined($val) or $val = $def;
189 $val =~ s/\s+//gs;
190 $val = lc($val);
191 return 0 if $val eq '' || $val eq 'false' || $val eq 'off' || $val eq 'no' || $val =~ /^[-+]?0+$/;
192 return 1;
195 sub _property_path {
196 my $self = shift;
197 my ($name) = @_;
198 $self->{path}.'/'.$name;
201 sub _property_fget {
202 my $self = shift;
203 my ($name, $nodef) = @_;
204 my $pname = $propmap{$name};
205 $pname = $propmapro{$name} unless $pname;
206 $pname = $propmapromirror{$name} unless $pname;
207 $pname or die "unknown property: $name";
208 if ($pname =~ /^([:%!])([^:]+)(:.*)?$/) {
209 my ($where, $pname, $defval) = ($1, lc($2), substr(($3||":"),1));
210 $defval = undef if $defval eq "undef";
211 $defval = '' if $nodef;
212 $self->_readlocalconfigfile
213 unless ref($self->{configfilehash}) eq 'HASH';
214 $pname = "gitweb." . $pname if $where eq ':' or $where eq '!' && $pname !~ /[.]/;
215 my $val = $self->{configfilehash}->{$pname};
216 defined($val) or $val = $defval;
217 chomp $val if defined($val);
218 $val = _boolval($val, $defval) if $where eq '!';
219 return $nodef && !exists($self->{configfilehash}->{$pname}) ? undef : $val;
222 open my $p, '<', $self->_property_path($pname) or return undef;
223 my @value = <$p>;
224 close $p;
225 my $value = join('', @value); chomp $value;
226 $value;
229 sub _prop_is_same {
230 my $self = shift;
231 my ($name, $value) = @_;
232 my $existing = $self->_property_fget($name, 1);
233 defined($value) or $value = '';
234 return defined($existing) && $existing eq $value;
237 sub _property_fput {
238 my $self = shift;
239 my ($name, $value, $nosetsame) = @_;
240 my $pname = $propmap{$name};
241 $pname or die "unknown property: $name";
242 my $defval = '';
243 ($pname, $defval) = ($1, substr(($2||":"),1)) if $pname =~ /^([:%!][^:]+)(:.*)?$/;
244 defined($value) or $value = $defval;
245 if ($pname =~ s/^://) {
246 return if $nosetsame && $self->_prop_is_same($name, $value);
247 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
248 return;
249 } elsif ($pname =~ s/^%//) {
250 return if $nosetsame && $self->_prop_is_same($name, $value);
251 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', $pname, $value);
252 return;
253 } elsif ($pname =~ s/^!//) {
254 $pname = "gitweb." . $pname unless $pname =~ /[.]/;
255 $value = _boolval($value, $defval);
256 return if $nosetsame && $self->_prop_is_same($name, $value);
257 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool', $pname, $value);
258 return;
261 my $propfile = $self->_property_path($pname);
262 my $P = lock_file($propfile);
263 chomp $value;
264 $value ne '' and $value .= "\n";
265 if ($nosetsame && -e $propfile && -f _) {
266 if (open my $fd, '<', $propfile) {
267 my $contents = undef;
268 {local $/ = undef; $contents = <$fd>;}
269 close $fd;
270 if (defined($contents) && $contents eq $value) {
271 close $P;
272 unlock_file($propfile, 1);
273 return;
277 $value ne '' and print $P $value;
278 close $P;
279 unlock_file($propfile);
282 sub _cleanup_datetime {
283 my $self = shift;
284 my $k = shift;
285 defined($k) && $k ne "" or return;
286 local $_ = $self->{$k}; defined($_) or $_ = '';
287 {use bytes; s/[\x00-\x08\x0e-\x1f\x7f]+//gs;}
288 s/\s+/ /gs;
289 s/^\s+//s;
290 s/\s+$//s;
291 my $ts = parse_any_date($_);
292 defined($ts) or $_ = '';
293 /^(.*)$/ and $self->{$k} = $1;
296 sub _cleanup_description {
297 my $self = shift;
298 local $_ = $self->{desc}; defined($_) or $_ = '';
299 {use bytes; s/[\x00-\x08\x0e-\x1f\x7f]+//gs;}
300 s/\r\n?/\n/gs;
301 s/^\s+//s;
302 s/\s+$//s;
303 /\A(.*)$/m and $self->{desc} = $1;
306 sub _cleanup_readme_str {
307 local $_ = shift; defined($_) or $_ = '';
308 {use bytes; s/[\x00-\x08\x0e-\x1f\x7f]+//gs;}
309 s/\r\n?/\n/gs;
310 s/^\s+//s;
311 s/\s+$//s;
312 $_ eq '' or $_ .= "\n";
313 /^(.*)$/s and $_ = $1;
314 return $_;
317 sub _cleanup_readme {
318 my $self = shift;
319 $self->{README} = _cleanup_readme_str($self->{README});
320 $self->{READMEDATA} = _cleanup_readme_str($self->{READMEDATA});
323 my %rmtypes;
324 BEGIN { %rmtypes = (
325 default => 'Markdown',
327 'text/markdown' => 'Markdown',
328 'text/x-markdown' => 'Markdown',
329 markdown => 'Markdown',
330 md => 'Markdown',
332 'text/plain' => 'Plain Text',
333 'plain text' => 'Plain Text',
334 'plain-text' => 'Plain Text',
335 plain_text => 'Plain Text',
336 plaintext => 'Plain Text',
337 plain => 'Plain Text',
338 text => 'Plain Text',
339 txt => 'Plain Text',
341 'text/html' => 'HTML',
342 html => 'HTML',
343 htm => 'HTML'
346 # if $_[1] is true, return "" instead of default for unknown type
347 sub _normalize_rmtype {
348 my $rmtype = shift;
349 my $nodefault = shift;
350 defined($rmtype) and $rmtype = $rmtypes{lc($rmtype)};
351 defined($rmtype) && $rmtype ne '' or
352 $rmtype = $nodefault ? "" : $rmtypes{'default'};
353 return $rmtype;
356 sub _lint_readme {
357 my $self = shift;
358 my $htmlfrag = shift;
359 defined($htmlfrag) or $htmlfrag = 1;
360 defined($self->{READMEDATA}) or $self->{READMEDATA} = '';
361 $self->{rmtype} = _normalize_rmtype($self->{rmtype});
362 # Empty always just becomes empty with no errors
363 if ($self->{READMEDATA} eq '') {
364 $self->{README} = '';
365 return 0;
367 if ($self->{rmtype} eq 'Plain Text') {
368 # Never any errors with this one
369 my $esc = $self->{READMEDATA};
370 $esc =~ s/&/&amp;/gs;
371 $esc =~ s/</&lt;/gs;
372 $self->{README} = '<pre class="plaintext">' . $esc . '</pre>';
373 return 0;
375 eval { require Markdown; 1 } or do {
376 return (1, "README: Markdown/HTML formats temporarily unavailable");
378 my @errs = ();
379 if ($self->{rmtype} eq 'HTML') {
380 # Legacy HTML support but now sanitized with possible auto-fixup
381 my $output;
382 eval { $output = Markdown::ProcessRaw($self->{READMEDATA},
383 sanitize => 1, xmlcheck => 2, htmlauto => 0,
384 stripcomments => 3); 1; } or
385 do {
386 @errs = split(/\n+/, $@);
387 @errs or push(@errs, 'Invalid HTML');
389 # Make one more attempt with htmlauto in case
390 # that's good enough to fix up the HTML and make it valid
391 eval { $output = Markdown::ProcessRaw($self->{READMEDATA},
392 sanitize => 1, xmlcheck => 2, htmlauto => 1,
393 stripcomments => 3); 1; } and
394 do {
395 @errs = ();
398 !@errs and $self->{README} = (chomp($output),$output);
399 } else {
400 # Markdown format
401 my $output;
402 my $gwbase = url_path($Girocco::Config::gitweburl,1).$self->{name}.'.git';
403 my %mdopts = (
404 sanitize => 1,
405 xmlcheck => 2,
406 yamlmode => 1,
407 yamlvis => 0,
408 keepabs => 1,
409 stripcomments => 3,
410 base_prefix => $self->{name}.'.git',
411 url_prefix => $gwbase . '/blob/HEAD:',
412 img_prefix => $gwbase . '/blob_plain/HEAD:'
414 eval { $output = Markdown::Markdown($self->{READMEDATA}, %mdopts); 1; } or
415 do {
416 @errs = split(/\n+/, $@);
417 @errs or push(@errs, 'Invalid Markdown');
419 !@errs and $self->{README} = (chomp($output),$output);
421 return 0 unless @errs;
422 my @fmterrs = ();
423 foreach (@errs) {
424 $_ = html_esc($_) if $htmlfrag;
425 s/ /\&#160;/gs if $htmlfrag;
426 if ($htmlfrag) {
427 push @fmterrs, 'README: <tt>' . $_ . '</tt>';
428 } else {
429 push @fmterrs, 'README: ' . $_ . "\n";
432 if ($htmlfrag) {
433 return (scalar(@fmterrs), join("<br />\n", @fmterrs));
434 } else {
435 return (scalar(@fmterrs), join("", @fmterrs));
439 sub _properties_load {
440 my $self = shift;
441 my $setprop = sub {
442 my $propval = $self->_property_fget($_);
443 defined($propval) and $self->{$_} = $propval;
445 &$setprop foreach keys %propmap;
446 &$setprop foreach keys %propmapro;
447 do {&$setprop foreach keys %propmapromirror} if $self->{mirror};
448 $self->_readlocalconfigfile
449 unless ref($self->{configfilehash}) eq 'HASH';
450 delete $self->{auth};
451 my $val = $self->{configfilehash}->{'gitweb.repoauth'};
452 defined($val) or $val = '';
453 chomp $val;
454 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
455 my $expire = $3;
456 if (time < $expire) {
457 $self->{authtype} = $1;
458 $self->{auth} = $2;
461 if ($Girocco::Config::autogchack && ($self->{mirror} || $Girocco::Config::autogchack ne "mirror")) {
462 if (defined($self->{configfilehash}->{'girocco.autogchack'})) {
463 $self->{autogchack} = _boolval($self->{configfilehash}->{'girocco.autogchack'});
466 defined($self->{jsontype}) or $self->{jsontype} = "";
467 $self->{jsontype} = lc($self->{jsontype});
468 $self->{jsontype} eq 'application/x-www-form-urlencoded' || $self->{jsontype} eq 'application/json' or
469 $self->{jsontype} = 'application/x-www-form-urlencoded';
470 defined($self->{jsonsecret}) or $self->{jsonsecret} = "";
471 $self->_cleanup_datetime('lastactivity');
472 $self->_cleanup_description;
473 $self->_cleanup_readme;
474 $self->{rmtype} = _normalize_rmtype($self->{rmtype}, 1);
475 if (!$self->{rmtype}) {
476 # default type is "Markdown" unless README ne '' but READMEDATA eq ''
477 if ($self->{README} ne '' && $self->{READMEDATA} eq '') {
478 # This was the only previously understood format
479 $self->{rmtype} = 'HTML';
480 $self->{READMEDATA} = $self->{README};
481 } else {
482 $self->{rmtype} = _normalize_rmtype(""); # get default type
485 delete $self->{configfilehash};
488 sub _set_bangagain {
489 my $self = shift;
490 my $fd;
491 if ($self->{mirror} && defined($self->{origurl}) && $self->{url} &&
492 $self->{origurl} ne $self->{url} && -e $self->_banged_path) {
493 if (open($fd, '>', $self->_bangagain_path)) {
494 close $fd;
495 chmod(0664, $self->_bangagain_path);
500 sub _properties_save {
501 my $self = shift;
502 delete $self->{configfilehash};
503 foreach my $prop (keys %propmap) {
504 $self->_property_fput($prop, $self->{$prop}, 1);
506 $self->_set_bangagain;
509 sub _nofetch_path {
510 my $self = shift;
511 $self->_property_path('.nofetch');
514 sub _nofetch {
515 my $self = shift;
516 my ($nofetch) = @_;
517 my $nf = $self->_nofetch_path;
518 if ($nofetch) {
519 open my $x, '>', $nf or die "nofetch failed: $!";
520 close $x;
521 } else {
522 ! -e $nf or unlink $nf or die "yesfetch failed: $!";
526 sub _banged_path {
527 my $self = shift;
528 $self->_property_path('.banged');
531 sub _bangagain_path {
532 my $self = shift;
533 $self->_property_path('.bangagain');
536 sub _gcp_path {
537 my $self = shift;
538 $self->_property_path('.gc_in_progress');
541 sub _clonelog_path {
542 my $self = shift;
543 $self->_property_path('.clonelog');
546 sub _clonefail_path {
547 my $self = shift;
548 $self->_property_path('.clone_failed');
551 sub _clonep_path {
552 my $self = shift;
553 $self->_property_path('.clone_in_progress');
556 sub _clonep {
557 my $self = shift;
558 my ($nofetch) = @_;
559 my $np = $self->_clonep_path;
560 if ($nofetch) {
561 open my $x, '>', $np or die "clonep failed: $!";
562 close $x;
563 } else {
564 unlink $np or die "clonef failed: $!";
567 sub _alternates_setup {
568 use POSIX qw(:fcntl_h);
569 my $self = shift;
570 return unless $self->{name} =~ m#/#;
571 my $forkee_name = get_forkee_name($self->{name});
572 my $forkee_path = get_forkee_path($self->{name});
573 return unless -d $forkee_path;
574 mkdir $self->{path}.'/refs'; chmod 02775, $self->{path}.'/refs';
575 mkdir $self->{path}.'/objects'; chmod 02775, $self->{path}.'/objects';
576 mkdir $self->{path}.'/objects/info'; chmod 02775, $self->{path}.'/objects/info';
577 mkdir $self->{path}.'/objects/pack'; chmod 02775, $self->{path}.'/objects/pack';
579 # If somehow either our objects/pack or the prospective alternate's pack
580 # directory does not exist decline to set up any alternates
581 my $altpath = "$forkee_path/objects";
582 -d $self->{path}.'/objects/pack' && -d $altpath.'/pack' or return;
584 # If our objects/pack and the prospective alternate's pack directory
585 # do not share the same device then decline to set up any alternates
586 my ($selfdev) = stat($self->{path}.'/objects/pack');
587 my ($altdev) = stat($altpath.'/pack');
588 defined($selfdev) && defined($altdev) && $selfdev ne "" && $altdev ne "" && $selfdev == $altdev or return;
590 # We set up both alternates and http_alternates since we cannot use
591 # relative path in alternates - that doesn't work recursively.
593 my $filename = $self->{path}.'/objects/info/alternates';
594 open my $x, '>', $filename or die "alternates failed: $!";
595 print $x "$altpath\n";
596 close $x;
597 chmod 0664, $filename or warn "cannot chmod $filename: $!";
599 if ($Girocco::Config::httppullurl) {
600 $filename = $self->{path}.'/objects/info/http-alternates';
601 open my $x, '>', $filename or die "http-alternates failed: $!";
602 my $upfork = $forkee_name;
603 do { print $x "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
604 close $x;
605 chmod 0664, $filename or warn "cannot chmod $filename: $!";
608 # copy lastactivity from the parent project
609 if (open $x, '<', $forkee_path.'/info/lastactivity') {{
610 my $activity = <$x>;
611 close $x;
612 last unless $activity;
613 open $x, '>', $self->{path}.'/info/lastactivity' or last;
614 print $x $activity;
615 close $x;
616 chomp $activity;
617 $self->{'lastactivity'} = $activity;
620 # copy refs from parent project
621 my $dupout;
622 open $dupout, '>&1' or
623 die "could not dup STDOUT_FILENO: $!";
624 my $packedrefsfd = POSIX::open("$self->{path}/packed-refs", O_WRONLY|O_TRUNC|O_CREAT, 0664);
625 defined($packedrefsfd) && $packedrefsfd >= 0 or die "could not open fork's packed-refs file for writing: $!";
626 POSIX::dup2($packedrefsfd, 1) or
627 die "could not dup2 STDOUT_FILENO: $!";
628 POSIX::close($packedrefsfd);
629 my $result = system($Girocco::Config::git_bin, "--git-dir=$forkee_path", 'for-each-ref', '--format=%(objectname) %(refname)');
630 my $resultstr = $!;
631 POSIX::dup2(fileno($dupout), 1);
632 close($dupout);
633 $result == 0 or die "could not create fork's packed-refs file data: $resultstr";
634 unlink("$self->{path}/.delaygc") if -s "$self->{path}/packed-refs";
636 # initialize HEAD
637 my $HEAD = get_git("--git-dir=$forkee_path", 'symbolic-ref', 'HEAD');
638 defined($HEAD) && $HEAD =~ m,^refs/heads/., or $HEAD = 'refs/heads/master';
639 chomp $HEAD;
640 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', $HEAD);
641 chmod 0664, "$self->{path}/packed-refs", "$self->{path}/HEAD";
644 sub _set_changed {
645 my $self = shift;
646 my $fd;
647 open $fd, '>', "$self->{path}/htmlcache/changed" and close $fd;
650 sub _set_forkchange {
651 my $self = shift;
652 my $changedtoo = shift;
653 return unless $self->{name} =~ m#/#;
654 my $forkee_path = get_forkee_path($self->{name});
655 return unless -d $forkee_path;
656 # mark forkee as changed
657 my $fd;
658 open $fd, '>', $forkee_path.'/htmlcache/changed' and close $fd if $changedtoo;
659 open $fd, '>', $forkee_path.'/htmlcache/forkchange' and close $fd;
660 return if -e $forkee_path.'/htmlcache/summary.forkchange';
661 open $fd, '>', $forkee_path.'/htmlcache/summary.forkchange' and close $fd;
664 sub _ctags_setup {
665 my $self = shift;
666 my $perms = $Girocco::Config::permission_control eq 'Hooks' ? 02777 : 02775;
667 mkdir $self->{path}.'/ctags'; chmod $perms, $self->{path}.'/ctags';
670 sub _group_add {
671 my $self = shift;
672 my ($xtra) = @_;
673 $xtra .= join(',', @{$self->{users}});
674 my $crypt = $self->{crypt};
675 defined($crypt) or $crypt = 'unknown';
676 filedb_atomic_append(jailed_file('/etc/group'),
677 join(':', $self->{name}, $crypt, '\i', $xtra));
680 sub _group_update {
681 my $self = shift;
682 my $xtra = join(',', @{$self->{users}});
683 filedb_atomic_edit(jailed_file('/etc/group'),
684 sub {
685 $_ = $_[0];
686 chomp;
687 if ($self->{name} eq (split /:/)[0]) {
688 # preserve readonly flag
689 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
690 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
691 } else {
692 return "$_\n";
698 sub _group_remove {
699 my $self = shift;
700 filedb_atomic_edit(jailed_file('/etc/group'),
701 sub {
702 $self->{name} ne (split /:/)[0] and return $_;
707 sub _hook_path {
708 my $self = shift;
709 my ($name) = @_;
710 $self->{path}.'/hooks/'.$name;
713 sub _hook_install {
714 my $self = shift;
715 my ($name) = @_;
716 my $hooksdir = $self->{path}.'/hooks';
717 my $oldmask = umask();
718 umask($oldmask & ~0070);
719 -d $hooksdir or mkdir $hooksdir or
720 die "hooks directory does not exist and unable to create it for project " . $self->{name} . ": $!";
721 umask($oldmask);
722 my $globalhooks = $Girocco::Config::reporoot . "/_global/hooks";
723 -f "$globalhooks/$name" && -r _ or die "cannot find hook $name: $!";
724 ! -e $self->_hook_path($name) || unlink($self->_hook_path($name)) && ! -e $self->_hook_path($name) or
725 die "hooks directory contains unremovable pre-existing hook $name: $!";
726 symlink("$globalhooks/$name", $self->_hook_path($name)) or
727 die "cannot create hook $name symlink: $!";
730 sub _hooks_install {
731 my $self = shift;
732 foreach my $hook ('pre-auto-gc', 'pre-receive', 'post-commit', 'post-receive', 'update') {
733 $self->_hook_install($hook);
737 # private constructor, do not use
738 sub _new {
739 my $class = shift;
740 my ($name, $base_path, $path, $orphan, $optp) = @_;
741 does_exist(\$name,1) || valid_name(\$name, $orphan, $optp) or die "refusing to create project with invalid name ($name)!";
742 $path ||= "$base_path/$name.git";
743 my $proj = { name => $name, base_path => $base_path, path => $path };
745 bless $proj, $class;
748 # public constructor #0
749 # creates a virtual project not connected to disk image
750 # you can conjure() it later to disk
751 sub ghost {
752 my $class = shift;
753 my ($name, $mirror, $orphan, $optp) = @_;
754 my $self = $class->_new($name, $Girocco::Config::reporoot, undef, $orphan, $optp);
755 $self->{users} = [];
756 $self->{mirror} = $mirror;
757 $self->{email} = $self->{orig_email} = '';
758 $self->{notifyjson} = $self->{orig_notifyjson} = '';
759 $self;
762 # public constructor #1
763 sub load {
764 my $class = shift;
765 my $name = shift || '';
767 open my $fd, '<', jailed_file("/etc/group") or die "project load failed: $!";
768 my $r = qr/^\Q$name\E:/;
769 foreach (grep /$r/, <$fd>) {
770 chomp;
772 my $self = $class->_new($name, $Girocco::Config::reporoot);
773 (-d $self->{path} && $self->_readlocalconfigfile(1))
774 or die "invalid path (".$self->{path}.") for project ".$self->{name};
776 my (undef, $crypt, $gid, $ulist) = split /:/;
777 $gid =~ /^(\d+)$/ or next; $self->{gid} = $1;
778 { use bytes;
779 $crypt =~ /^([^:\x00-\x1F\x7F-\xFF]*)$/ or next; $self->{crypt} = $1; }
780 defined($ulist) or $ulist = '';
781 $ulist =~ /^((?:[a-zA-Z0-9][a-zA-Z0-9+._-]*(?:,[a-zA-Z0-9][a-zA-Z0-9+._-]*)*)?)$/ or next;
782 $self->{users} = [split /,/, $1];
783 $self->{HEAD} = $self->get_HEAD;
784 $self->{orig_HEAD} = $self->{HEAD};
785 $self->{orig_users} = [@{$self->{users}}];
786 $self->{mirror} = ! -e $self->_nofetch_path;
787 $self->{banged} = -e $self->_banged_path if $self->{mirror};
788 $self->{gc_in_progress} = -e $self->_gcp_path;
789 $self->{clone_in_progress} = -e $self->_clonep_path;
790 $self->{clone_logged} = -e $self->_clonelog_path;
791 $self->{clone_failed} = -e $self->_clonefail_path;
792 $self->{ccrypt} = $self->{crypt};
794 $self->_properties_load;
795 $self->{orig_email} = $self->{email};
796 $self->{orig_notifyjson} = $self->{notifyjson};
797 $self->{loaded} = 1; # indicates self was loaded from etc/group file
798 close $fd;
799 return $self;
801 close $fd;
802 undef;
805 # $proj may not be in sane state if this returns false!
806 # fields listed in %$metadata_fields that are NOT also
807 # in @Girocco::Config::project_fields are totally ignored!
808 sub cgi_fill {
809 my $self = shift;
810 my ($gcgi, $silent) = @_;
811 my $cgi = $gcgi->cgi;
812 my %allowedfields = map({$_ => 1} @Girocco::Config::project_fields);
813 my $field_enabled = sub {
814 defined($cgi->param($_[0])) &&
815 (!exists($metadata_fields->{$_[0]}) || exists($allowedfields{$_[0]}))};
817 my $pwd = $cgi->param('pwd');
818 my $pwd2 = $cgi->param('pwd2');
819 # in case passwords are disabled
820 defined($pwd) or $pwd = ''; defined($pwd2) or $pwd2 = '';
821 if ($Girocco::Config::project_passwords and not $self->{crypt} and $pwd eq '' and $pwd2 eq '') {
822 $gcgi->err("Empty passwords are not permitted.");
824 if ($pwd ne '' or not $self->{crypt}) {
825 $self->{crypt} = scrypt_sha1($pwd);
827 if (($pwd ne '' || $pwd2 ne '') and $pwd ne $pwd2) {
828 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
831 $self->{cpwd} = $cgi->param('cpwd');
833 my ($forkee,$project) = ($self->{name} =~ m#^(.*/)?([^/]+)$#);
834 my $newtype = $forkee ? 'fork' : 'project';
835 length($project) <= 64
836 or $gcgi->err("The $newtype name is longer than 64 characters. Do you really need that much?");
838 if ($Girocco::Config::project_owners eq 'email') {
839 $self->{email} = $gcgi->wparam('email');
840 valid_email($self->{email})
841 or $gcgi->err("Your email sure looks weird...?");
842 length($self->{email}) <= 96
843 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
846 # No setting the url unless we're either new or an existing mirror!
847 unless ($self->{loaded} && !$self->{mirror}) {
848 $self->{url} = $gcgi->wparam('url') ;
849 if ($field_enabled->('cleanmirror')) {
850 $self->{cleanmirror} = $gcgi->wparam('cleanmirror') || 0;
853 # Always validate the url if we're an existing mirror
854 if ((defined($self->{url}) && $self->{url} ne '') || ($self->{loaded} && $self->{mirror})) {{
855 # Always allow the url to be left unchanged without validation when editing
856 last if $self->{loaded} && defined($self->{origurl}) && defined($self->{url}) && $self->{origurl} eq $self->{url};
857 valid_repo_url($self->{url})
858 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocols are supported. If the URL contains funny characters, contact me.");
859 if ($Girocco::Config::restrict_mirror_hosts) {
860 my $mh = extract_url_hostname($self->{url});
861 is_dns_hostname($mh)
862 or $gcgi->err("Invalid URL. Note that only DNS names are allowed, not IP addresses.");
863 !is_our_hostname($mh)
864 or $gcgi->err("Invalid URL. Mirrors from this host are not allowed, please create a fork instead.");
868 if ($field_enabled->('desc')) {
869 $self->{desc} = to_utf8($gcgi->wparam('desc'), 1);
870 length($self->{desc}) <= 1024
871 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
874 if ($field_enabled->('README')) {
875 $self->{rmtype} = $rmtypes{lc($gcgi->wparam('rmtype')||'')};
876 if (!$self->{rmtype}) {
877 $gcgi->err("Invalid README Format. Must be 'Markdown', 'Plain Text' or 'HTML'.");
878 $self->{rmtype} = 'Markdown';
880 $self->{READMEDATA} = _cleanup_readme_str(to_utf8($gcgi->wparam('README'), 1));
881 length($self->{READMEDATA}) <= 8192
882 or $gcgi->err("README length &gt; 8kb!");
883 my ($cnt, $err) = (0);
884 ($cnt, $err) = $self->_lint_readme if $gcgi->ok;
885 $gcgi->err($err), $gcgi->{err} += $cnt-1 if $cnt;
888 if ($field_enabled->('hp')) {
889 $self->{hp} = $gcgi->wparam('hp');
890 if ($self->{hp}) {
891 valid_web_url($self->{hp})
892 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
896 # No mucking about with users unless we're a push project
897 if (($self->{loaded} && !$self->{mirror}) ||
898 (!$self->{loaded} && (!defined($self->{url}) || $self->{url} eq ''))) {
899 my %users = ();
900 my @users = ();
901 foreach my $user ($cgi->multi_param('user')) {
902 if (!exists($users{$user})) {
903 $users{$user} = 1;
904 push(@users, $user) if Girocco::User::does_exist($user, 1);
907 $self->{users} = \@users;
910 # HEAD can only be set with editproj, NOT regproj (regproj sets it automatically)
911 # It may ALWAYS be set to what it was (even if there's no such refs/heads/...)
912 my $newhead;
913 if (defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '' &&
914 defined($newhead = $cgi->param('HEAD')) && $newhead ne '') {
915 if ($newhead eq $self->{orig_HEAD} ||
916 get_git("--git-dir=$self->{path}", 'rev-parse', '--verify', '--quiet', 'refs/heads/'.$newhead)) {
917 $self->{HEAD} = $newhead;
918 } else {
919 $gcgi->err("Invalid default branch (no such ref)");
923 # schedule deletion of tags (will be committed by update() after auth)
924 $self->{tags_to_delete} = [$cgi->multi_param('tags')];
926 if ($field_enabled->('notifymail')) {
927 my $newaddrs = clean_email_multi($gcgi->wparam('notifymail'));
928 if ($newaddrs eq "" or (valid_email_multi($newaddrs) and length($newaddrs) <= 512)) {
929 $self->{notifymail} = $newaddrs;
930 } else {
931 $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
933 if ($field_enabled->('reverseorder')) {
934 $self->{reverseorder} = $gcgi->wparam('reverseorder') || 0;
936 if ($field_enabled->('summaryonly')) {
937 $self->{summaryonly} = $gcgi->wparam('summaryonly') || 0;
941 if ($field_enabled->('notifytag')) {
942 my $newaddrs = clean_email_multi($gcgi->wparam('notifytag'));
943 if ($newaddrs eq "" or (valid_email_multi($newaddrs) and length($newaddrs) <= 512)) {
944 $self->{notifytag} = $newaddrs;
945 } else {
946 $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
950 if ($field_enabled->('notifyjson')) {
951 $self->{notifyjson} = $gcgi->wparam('notifyjson');
952 if ($self->{notifyjson}) {
953 valid_web_url($self->{notifyjson})
954 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
956 $self->{jsontype} = $gcgi->wparam('jsontype');
957 defined($self->{jsontype}) && $self->{jsontype} ne "" or
958 $self->{jsontype} = 'application/x-www-form-urlencoded';
959 $self->{jsontype} = lc($self->{jsontype});
960 unless ($self->{jsontype} eq 'application/x-www-form-urlencoded' ||
961 $self->{jsontype} eq 'application/json') {
962 if ($self->{notifyjson}) {
963 $gcgi->err("Invalid JSON Content-Type. Must be 'application/x-www-form-urlencoded' or 'application/json'.");
964 } else {
965 $self->{jsontype} = 'application/x-www-form-urlencoded';
968 $self->{jsonsecret} = $gcgi->wparam('jsonsecret');
969 my $origurl = $self->{orig_notifyjson};
970 defined($origurl) or $origurl = '';
971 $self->{notifyjson} eq $origurl ||
972 $self->{notifyjson} eq "" ||
973 Girocco::Notify::json_test_post($self) or
974 $gcgi->err("JSON notify URL failed POST test.");
977 if ($field_enabled->('notifycia')) {
978 $self->{notifycia} = $gcgi->wparam('notifycia');
979 if ($self->{notifycia}) {
980 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
981 or $gcgi->err("Overly suspicious CIA notify project name. If it's actually valid, don't contact me, CIA is defunct.");
985 if ($cgi->param('setstatusupdates')) {
986 my $val = $gcgi->wparam('statusupdates') || '0';
987 $self->{statusupdates} = $val ? 1 : 0;
990 $silent ? $gcgi->ok : !$gcgi->err_check;
993 sub form_defaults {
994 my $self = shift;
996 name => $self->{name},
997 email => $self->{email},
998 url => $self->{url},
999 cleanmirror => $self->{cleanmirror},
1000 desc => html_esc($self->{desc}),
1001 README => html_esc($self->{READMEDATA}),
1002 rmtype => $self->{rmtype},
1003 hp => $self->{hp},
1004 users => $self->{users},
1005 notifymail => html_esc($self->{notifymail}),
1006 reverseorder => $self->{reverseorder},
1007 summaryonly => $self->{summaryonly},
1008 notifytag => html_esc($self->{notifytag}),
1009 notifyjson => html_esc($self->{notifyjson}),
1010 jsontype => html_esc($self->{jsontype}),
1011 jsonsecret => html_esc($self->{jsonsecret}),
1012 notifycia => html_esc($self->{notifycia}),
1014 __project__ => $self
1018 # return true if $enc_passwd is a match for $plain_passwd
1019 my $_check_passwd_match = sub {
1020 my $enc_passwd = shift;
1021 my $plain_passwd = shift;
1022 defined($enc_passwd) or $enc_passwd = '';
1023 defined($plain_passwd) or $plain_passwd = '';
1024 # $enc_passwd may be crypt or crypt_sha1
1025 if ($enc_passwd =~ m(^\$sha1\$(\d+)\$([./0-9A-Za-z]{1,64})\$[./0-9A-Za-z]{28}$)) {
1026 # It's using sha1-crypt
1027 return $enc_passwd eq crypt_sha1($plain_passwd, $2, -(0+$1));
1028 } else {
1029 # It's using crypt
1030 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
1034 sub authenticate {
1035 my $self = shift;
1036 my ($gcgi) = @_;
1038 $self->{ccrypt} or die "Can't authenticate against a project with no password";
1039 defined($self->{cpwd}) or $self->{cpwd} = '';
1040 unless ($_check_passwd_match->($self->{ccrypt}, $self->{cpwd})) {
1041 $gcgi->err("Your admin password does not match!");
1042 return 0;
1044 return 1;
1047 # return true if the password from the file is empty or consists of all the same
1048 # character. However, if the project was NOT loaded from the group file
1049 # (!self->{loaded}) then the password is never locked.
1050 # This function does NOT check $Girocco::Config::project_passwords, the caller
1051 # is responsible for doing so if desired. Same for $self->{email}.
1052 sub is_password_locked {
1053 my $self = shift;
1055 $self->{loaded} or return 0;
1056 my $testcrypt = $self->{ccrypt}; # The value from the group file
1057 defined($testcrypt) or $testcrypt = '';
1058 $testcrypt ne '' or return 1; # No password at all
1059 $testcrypt =~ /^(.)\1*$/ and return 1; # Bogus crypt value
1060 return 0; # Not locked
1063 sub _setup {
1064 use POSIX qw(strftime);
1065 my $self = shift;
1066 my ($pushers) = @_;
1067 my $fd;
1069 defined($self->{path}) && $self->{path} ne "" or die "invalid setup call";
1070 $self->_mkdir_forkees unless $self->{adopt};
1072 my $gid;
1073 $self->{adopt} || mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
1074 -d $self->{path} or die "unable to setup nonexistent $self->{path}";
1075 if ($Girocco::Config::owning_group) {
1076 $gid = scalar(getgrnam($Girocco::Config::owning_group));
1077 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
1078 chmod(02775, $self->{path}) or die "chmod 02775 $self->{path} failed: $!";
1079 } else {
1080 chmod(02777, $self->{path}) or die "chmod 02777 $self->{path} failed: $!";
1082 delete $ENV{GIT_OBJECT_DIRECTORY};
1083 delete $ENV{GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME};
1084 $ENV{'GIT_TEMPLATE_DIR'} = $Girocco::Config::chroot.'/var/empty';
1085 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--quiet', '--bare', '--shared='.$self->shared_mode()) == 0
1086 or die "git init $self->{path} failed: $?";
1087 # we don't need these two, remove them (they will normally be created empty) if they exist
1088 rmdir $self->{path}."/branches";
1089 rmdir $self->{path}."/remotes";
1090 -d $self->{path}."/info" or mkdir $self->{path}."/info"
1091 or die "info directory does not exist and unable to create it: $!";
1092 -d $self->{path}."/hooks" or mkdir $self->{path}."/hooks"
1093 or die "hooks directory does not exist and unable to create it: $!";
1094 # clean out any kruft that may have come in from the initial template directory
1095 foreach my $cleandir (qw(hooks info)) {
1096 if (opendir my $hooksdir, $self->{path}.'/'.$cleandir) {
1097 unlink map "$self->{path}/$_", grep { $_ ne '.' && $_ ne '..' } readdir $hooksdir;
1098 closedir $hooksdir;
1101 if ($Girocco::Config::owning_group) {
1102 chown(-1, $gid, $self->{path}."/hooks") or die "chgrp $gid $self->{path}/hooks failed: $!";
1104 # hooks never world writable
1105 chmod 02775, $self->{path}."/hooks" or die "chmod 02775 $self->{path}/hooks failed: $!";
1106 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.compression', '5') == 0
1107 or die "setting core.compression failed: $?";
1108 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.logAllRefUpdates', 'false') == 0
1109 or die "disabling core.logAllRefUpdates failed: $?";
1110 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.ignoreCase', 'false') == 0
1111 or die "disabling core.ignoreCase failed: $?";
1112 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.hooksPath',
1113 ($Girocco::Config::localhooks ? $self->{path}."/hooks" : $Girocco::Config::reporoot . "/_global/hooks")) == 0
1114 or die "setting core.hooksPath failed: $?";
1115 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'extensions.preciousObjects', 'true') == 0
1116 or die "setting extensions.preciousObjects failed: $?";
1117 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'transfer.fsckObjects', 'true') == 0
1118 or die "enabling transfer.fsckObjects failed: $?";
1119 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'transfer.unpackLimit', '1') == 0
1120 or die "setting transfer.unpackLimit failed: $?";
1121 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.fsckObjects', 'true') == 0
1122 or die "enabling receive.fsckObjects failed: $?";
1123 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastForwards', 'false') == 0
1124 or die "disabling receive.denyNonFastForwards failed: $?";
1125 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyDeleteCurrent', 'warn') == 0
1126 or die "disabling receive.denyDeleteCurrent failed: $?";
1127 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.autogc', '0') == 0
1128 or die "disabling receive.autogc failed: $?";
1129 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
1130 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.updateServerInfo', 'true') == 0
1131 or die "enabling receive.updateServerInfo failed: $?";
1132 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'repack.writeBitmaps', 'true') == 0
1133 or die "enabling repack.writeBitmaps failed: $?";
1134 $self->{creationtime} = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
1135 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'girocco.creationtime', $self->{creationtime}) == 0
1136 or die "setting girocco.creationtime failed: $?";
1137 -d $self->{path}."/htmlcache" or mkdir $self->{path}."/htmlcache"
1138 or die "htmlcache directory does not exist and unable to create it: $!";
1139 -d $self->{path}."/bundles" or mkdir $self->{path}."/bundles"
1140 or die "bundles directory does not exist and unable to create it: $!";
1141 -d $self->{path}."/reflogs" or mkdir $self->{path}."/reflogs"
1142 or die "reflogs directory does not exist and unable to create it: $!";
1143 foreach my $file (qw(info/lastactivity .delaygc)) {
1144 if (open $fd, '>', $self->{path}."/".$file) {
1145 close $fd;
1146 chmod 0664, $self->{path}."/".$file;
1150 # /info must have right permissions,
1151 # and git init didn't do it for some reason.
1152 # config must have correct permissions.
1153 # and Git 2.1.0 - 2.2.1 incorrectly add +x for some reason.
1154 # also make sure /refs, /objects and /htmlcache are correct too.
1155 my ($dmode, $dmodestr, $fmode, $fmodestr);
1156 if ($Girocco::Config::owning_group) {
1157 ($dmode, $dmodestr) = (02775, '02775');
1158 ($fmode, $fmodestr) = (0664, '0664');
1159 } else {
1160 ($dmode, $dmodestr) = (02777, '02777');
1161 ($fmode, $fmodestr) = (0666, '0666');
1163 foreach my $dir (qw(info refs objects htmlcache bundles reflogs)) {
1164 chmod($dmode, $self->{path}."/$dir") or die "chmod $dmodestr $self->{path}/$dir failed: $!";
1166 foreach my $file (qw(config)) {
1167 chmod($fmode, $self->{path}."/$file") or die "chmod $fmodestr $self->{path}/$file failed: $!";
1169 # these ones are probably not strictly required but are nice to have
1170 foreach my $dir (qw(refs/heads refs/tags objects/info objects/pack)) {
1171 -d $self->{path}."/$dir" or mkdir $self->{path}."/$dir";
1172 chmod($dmode, $self->{path}."/$dir");
1174 if ($Girocco::Config::owning_group && defined($gid) && $Girocco::Config::htmlcache_owning_group) {
1175 my $htmlgid = scalar(getgrnam($Girocco::Config::htmlcache_owning_group));
1176 if (defined($htmlgid) && $htmlgid ne $gid) {
1177 chown(-1, $htmlgid, $self->{path}."/htmlcache") or die "chgrp $htmlgid $self->{path}/htmlcache failed: $!";
1178 chmod($dmode, $self->{path}."/htmlcache") or die "chmod $dmodestr $self->{path}/htmlcache failed: $!";
1181 if ($Girocco::Config::owning_group && defined($gid) && $Girocco::Config::ctags_owning_group) {
1182 my $ctagsgid = scalar(getgrnam($Girocco::Config::ctags_owning_group));
1183 if (defined($ctagsgid) && $ctagsgid ne $gid) {
1184 chown(-1, $ctagsgid, $self->{path}."/ctags") or die "chgrp $ctagsgid $self->{path}/ctags failed: $!";
1185 chmod($dmode, $self->{path}."/ctags") or die "chmod $dmodestr $self->{path}/ctags failed: $!";
1189 $self->_properties_save;
1190 $self->_alternates_setup unless $self->{noalternates} || $self->{adopt};
1191 $self->_ctags_setup;
1192 $self->_group_remove;
1193 $self->_group_add($pushers);
1194 $self->_hooks_install;
1195 $self->_update_index;
1196 $self->_set_changed;
1197 $self->_set_forkchange(1);
1200 sub premirror {
1201 my $self = shift;
1203 delete $self->{adopt};
1204 $self->_setup(':');
1205 $self->_clonep(1);
1206 if ($Girocco::Config::autogchack) {
1207 system("$Girocco::Config::basedir/jobd/maintain-auto-gc-hack.sh", $self->{name}) == 0
1208 or die "maintain-auto-gc-hack.sh $self->{name} failed";
1210 $self->perm_initialize;
1213 sub conjure {
1214 my $self = shift;
1216 delete $self->{adopt};
1217 $self->_setup;
1218 $self->_nofetch(1);
1219 if ($Girocco::Config::autogchack && $Girocco::Config::autogchack ne "mirror") {
1220 system("$Girocco::Config::basedir/jobd/maintain-auto-gc-hack.sh", $self->{name}) == 0
1221 or die "maintain-auto-gc-hack.sh $self->{name} failed";
1223 if ($Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
1224 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
1225 or die "create-personal-mob-area $self->{name} failed";
1227 $self->perm_initialize;
1228 # set initial HEAD if it's not already set to the configured initial branch name
1229 my $def_head = $self->{HEAD};
1230 defined($def_head) && $def_head ne "" or $def_head = $Girocco::Config::initial_branch;
1231 defined($def_head) or $def_head = "";
1232 $def_head ne "" && valid_branch_name($def_head) or
1233 $def_head = "master";
1234 my $cur_head = read_HEAD_ref($self->{path});
1235 defined($cur_head) && $cur_head =~ m{^refs/heads/\Q$def_head\E$} or
1236 $self->set_HEAD($def_head);
1237 $self->{HEAD} = $def_head;
1238 if (defined($Girocco::Config::empty_commit_message)) {
1239 system("$Girocco::Config::basedir/bin/create-initial-empty-commit", $self->{name}) == 0
1240 or die "create-initial-empty-commit $self->{name} failed";
1245 sub clone {
1246 my $self = shift;
1248 unlink ($self->_clonefail_path()); # Ignore EEXIST error
1249 unlink ($self->_clonelog_path()); # Ignore EEXIST error
1251 use IO::Socket;
1252 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
1253 select((select($sock),$|=1)[0]);
1254 $sock->print("clone ".$self->{name}."\n");
1255 # Just ignore reply, we are going to succeed anyway and the I/O
1256 # would apparently get quite hairy.
1257 $sock->flush();
1258 sleep 2; # *cough*
1259 $sock->close();
1262 # call this after ghost instead of conjure or premirror+clone to adopt a pre-existing Git dir
1263 # ghost must be called with the proper value of $mirror for the to-be-adopted project
1264 # for mirrors the $proj->{url} needs to be set and for push projects the $proj->{users} array ref
1265 sub adopt {
1266 my $self = shift;
1267 my $name = $self->{name};
1269 # Sanity check first
1270 defined($name) && $name ne "" && !$self->{loaded} or return undef;
1271 does_exist($name, 1) or return undef;
1272 defined($self->{path}) && $self->{path} eq $Girocco::Config::reporoot."/$name.git" or return undef;
1273 defined($self->{base_path}) && $self->{base_path} eq $Girocco::Config::reporoot or return undef;
1274 defined($self->{email}) && defined($self->{orig_email}) && defined($self->{mirror}) && ref($self->{users}) eq 'ARRAY'
1275 or return undef;
1276 !defined(Girocco::Project->load($name)) or return undef;
1277 is_git_dir($self->{path}) or return undef;
1278 my $config = read_config_file_hash($self->{path}."/config");
1279 defined($config) && _boolval($config->{"core.bare"}) or die "refusing to adopt non-bare repository";
1280 defined(read_HEAD_symref($self->{path})) or die "refusing to adopt non-symref HEAD repository";
1282 # Adopt the project by creating a new $chroot/etc/group entry and setting up anything that's missing
1283 $self->_nofetch(!$self->{mirror});
1284 $self->{adopt} = 1;
1285 $self->_setup($self->{mirror} ? ":" : "");
1286 delete $self->{adopt};
1287 if ($Girocco::Config::autogchack && ($self->{mirror} || $Girocco::Config::autogchack ne "mirror")) {
1288 system("$Girocco::Config::basedir/jobd/maintain-auto-gc-hack.sh", $self->{name}) == 0
1289 or die "maintain-auto-gc-hack.sh $self->{name} failed";
1291 if (!$self->{mirror} && $Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
1292 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
1293 or die "create-personal-mob-area $self->{name} failed";
1295 my $result = $self->perm_initialize;
1296 unlink("$self->{path}/.delaygc") unless $self->is_empty(1);
1297 # Pick up any pre-existing settings
1298 my $p = Girocco::Project->load($name);
1299 %$self = %$p if defined($p) && $p->{loaded};
1300 $result;
1303 sub _update_users {
1304 my $self = shift;
1306 $self->_group_update;
1307 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
1308 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
1309 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
1310 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
1313 sub update {
1314 my $self = shift;
1316 $self->_properties_save;
1317 $self->_update_users;
1319 if (exists($self->{tags_to_delete})) {
1320 $self->delete_ctag($_) foreach(@{$self->{tags_to_delete}});
1323 $self->set_HEAD($self->{HEAD}) unless $self->{orig_HEAD} eq $self->{HEAD};
1325 $self->_update_index if $self->{email} ne $self->{orig_email};
1326 $self->{orig_email} = $self->{email};
1327 $self->_set_changed;
1332 sub update_password {
1333 my $self = shift;
1334 my ($pwd) = @_;
1336 $self->{crypt} = scrypt_sha1($pwd);
1337 $self->_group_update;
1340 # You can explicitly do this just on a ghost() repository too.
1341 sub delete {
1342 my $self = shift;
1344 if (-d $self->{path}) {
1345 system('rm', '-rf', $self->{path}) == 0
1346 or die "rm -rf $self->{path} failed: $?";
1348 # attempt to clean up any empty fork directories by removing them
1349 my @pelems = split('/', $self->{name});
1350 while (@pelems > 1) {
1351 pop @pelems;
1352 # okay to fail
1353 rmdir join('/', $Girocco::Config::reporoot, @pelems) or last;
1355 $self->_group_remove;
1356 $self->_update_index;
1357 $self->_set_forkchange(1);
1362 # If the project's directory actually exists archive it before deleting it
1363 # Return full path to archived project ("" if none)
1364 sub archive_and_delete {
1365 my $self = shift;
1367 unless (-d $self->{path}) {
1368 $self->delete;
1369 return "";
1372 # archive the project before deletion
1373 use POSIX qw(strftime);
1374 my $destdir = $self->{base_path};
1375 $destdir =~ s,(?<=[^/])/+$,,;
1376 $destdir .= "/_recyclebin/";
1377 $destdir .= $1 if $self->{name} =~ m,^(.*/)[^/]+$,;
1378 my $destbase = $self->{name};
1379 $destbase = $1 if $destbase =~ m,^.*/([^/]+)$,;
1380 my $oldmask = umask();
1381 umask($oldmask & ~0070);
1382 system('mkdir', '-p', $destdir) == 0 && -d $destdir
1383 or die "mkdir -p \"$destdir\" failed: $?";
1384 umask($oldmask);
1385 my $suffix = '';
1386 if (-e "$destdir$destbase.git") {
1387 $suffix = 1;
1388 while (-e "$destdir$destbase~$suffix.git") {
1389 ++$suffix;
1390 last if $suffix >= 10000; # don't get too carried away
1392 $suffix = '~'.$suffix;
1394 not -e "$destdir$destbase$suffix.git"
1395 or die "Unable to compute suitable archive path";
1396 system('mv', $self->{path}, "$destdir$destbase$suffix.git") == 0
1397 or die "mv \"$self->{path}\" \"$destdir$destbase$suffix.git\" failed: $?";
1398 if (!$self->{mirror} && @{$self->{users}}) {
1399 # Remember the user list at recycle time
1400 system($Girocco::Config::git_bin, '--git-dir='.$destdir.$destbase.$suffix.".git",
1401 'config', 'girocco.recycleusers', join(",", @{$self->{users}}));
1403 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
1404 my $recycletime = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
1405 # We ought to do something if this fails, but the project has already been moved
1406 # so there's really nothing to be done at this point.
1407 system($Girocco::Config::git_bin, '--git-dir='.$destdir.$destbase.$suffix.".git",
1408 'config', 'girocco.recycletime', $recycletime);
1410 $self->delete;
1411 return $destdir.$destbase.$suffix.".git";
1414 sub _contains_files {
1415 my $dir = shift;
1416 (-d $dir) or return 0;
1417 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
1418 while (my $entry = readdir($dh)) {
1419 next if $entry eq '' || $entry eq '.' || $entry eq '..';
1420 closedir($dh), return 1
1421 if -f "$dir/$entry" ||
1422 -d "$dir/$entry" && _contains_files("$dir/$entry");
1424 closedir($dh);
1425 return 0;
1428 sub has_forks {
1429 my $self = shift;
1431 return _contains_files($Girocco::Config::reporoot.'/'.$self->{name});
1434 # Returns an array of 0 or more array refs, one for each bundle:
1435 # ->[0]: bundle creation time (seconds since epoch)
1436 # ->[1]: bundle name (e.g. foo-xxxxxxxx.bundle)
1437 # ->[2]: bundle size in bytes
1438 sub bundles {
1439 my $self = shift;
1440 use Time::Local;
1442 return @{$self->{bundles}} if ref($self->{bundles}) eq 'ARRAY';
1443 my @blist = ();
1444 if (-d $self->{path}.'/bundles') {{
1445 my $prefix = $self->{name};
1446 $prefix =~ s|^.*[^/]/([^/]+)$|$1|;
1447 opendir(my $dh, $self->{path}.'/bundles') or last;
1448 while (my $bfile = readdir($dh)) {
1449 next unless $bfile =~ /^\d{8}_\d{6}-[0-9a-f]{8}$/;
1450 my $ctime = eval {timegm(
1451 0+substr($bfile,13,2), 0+substr($bfile,11,2), 0+substr($bfile,9,2),
1452 0+substr($bfile,6,2), 0+substr($bfile,4,2)-1, 0+substr($bfile,0,4))};
1453 next unless $ctime;
1454 open(my $bh, '<', $self->{path}.'/bundles/'.$bfile) or next;
1455 my $f1 = <$bh>;
1456 my $f2 = <$bh>;
1457 $f1 = $self->{path}.'/objects/pack/'.$f1 if $f1 && $f1 !~ m|^/|;
1458 $f2 = $self->{path}.'/objects/pack/'.$f2 if $f2 && $f2 !~ m|^/|;
1459 close($bh);
1460 next unless $f1 && $f2 && $f1 ne $f2;
1461 chomp $f1;
1462 chomp $f2;
1463 next unless -e $f1 && -e $f2;
1464 my $s1 = -s $f1 || 0;
1465 my $s2 = -s $f2 || 0;
1466 next unless $s1 || $s2;
1467 push(@blist, [$ctime, "$prefix-".substr($bfile,16,8).".bundle", $s1+$s2]);
1469 closedir($dh);
1471 @blist = sort({$b->[0] <=> $a->[0]} @blist);
1472 my %seen = ();
1473 my @result = ();
1474 foreach my $bndl (@blist) {
1475 next if $seen{$bndl->[1]};
1476 $seen{$bndl->[1]} = 1;
1477 push(@result, $bndl);
1479 $self->{bundles} = \@result;
1480 return @result;
1483 sub has_alternates {
1484 my $self = shift;
1485 my $af = $self->{path}.'/objects/info/alternates';
1486 -f $af && -s _ or return 0;
1487 my $nb = 0;
1488 open my $fh, '<', $af or return 1;
1489 while (my $line = <$fh>) {
1490 next if $line =~ /^$/ || $line =~ /^#/;
1491 $nb = 1;
1492 last;
1494 close $fh;
1495 return $nb;
1498 sub has_bundle {
1499 my $self = shift;
1501 return scalar($self->bundles);
1504 sub _has_notifyhook {
1505 my $self = shift;
1506 my $val = $Girocco::Config::default_notifyhook;
1507 defined($self->{'notifyhook'}) and $val = $self->{'notifyhook'};
1508 return (defined($val) && $val ne "") ? $val : undef;
1511 # returns true if any of the notify fields are non-empty
1512 sub has_notify {
1513 my $self = shift;
1514 # We do not ckeck notifycia since it's defunct
1515 return
1516 $self->{'notifymail'} || $self->{'notifytag'} ||
1517 $self->{'notifyjson'} ||
1518 !!$self->_has_notifyhook;
1521 sub is_empty {
1522 # A project is considered empty if the git repository does not
1523 # have any refs. This means packed-refs does not exist or is
1524 # empty or only has lines starting with '#' AND there are no
1525 # files in the refs subdirectory hierarchy (no matter how deep).
1526 # As a "kludge" to accomodate the $Girocco::Config::empty_commit_message
1527 # setting, if the packed-refs file does not exist or contains no
1528 # refs, and the .delaygc file exists and the .allowgc file does not
1529 # then the project will be considered empty unless it's a mirror.
1530 # But if a true value is passed as the first argument, disable this kludge.
1532 my $self = shift;
1533 my $nokludge = shift;
1535 (-d $self->{path}) or return 0;
1536 if (-e $self->{path}.'/packed-refs') {
1537 open(my $pr, '<', $self->{path}.'/packed-refs')
1538 or die "open $self->{path}./packed-refs failed: $!";
1539 my $foundref = 0;
1540 while (my $ref = <$pr>) {
1541 next if $ref =~ /^#/;
1542 $foundref = 1;
1543 last;
1545 close($pr);
1546 return 0 if $foundref;
1548 (-d $self->{path}.'/refs') or return 1;
1549 # Begin kludge to accomodate $empty_commit_message setting
1550 if (!$nokludge && !$self->{mirror} && -e $self->{path}.'/.delaygc' && ! -e $self->{path}.'/.allowgc') {
1551 # Sure looks like an empty push project
1552 # The non-kludge is to make sure there's only exactly one ref, HEAD is a
1553 # symbolic-ref to that ref and the commit has no parents and an empty tree
1554 # while still requiring the !mirror, .delaygc && !.allowgc conditions too.
1555 return 1;
1557 return !_contains_files($self->{path}.'/refs');
1560 # returns array:
1561 # [0]: earliest possible scheduled next gc, undef if lastgc not set
1562 # [1]: approx. latest possible scheduled next gc, undef if lastgc not set
1563 # Both values (if not undef) are seconds since epoch
1564 # Result only considers lastgc and min_gc_interval nothing else
1565 sub next_gc {
1566 my $self = shift;
1567 my $lastgcepoch = parse_any_date($self->{lastgc});
1568 return (undef, undef) unless defined $lastgcepoch;
1569 return ($lastgcepoch + $Girocco::Config::min_gc_interval,
1570 int($lastgcepoch + 1.25 * $Girocco::Config::min_gc_interval +
1571 $Girocco::Config::min_mirror_interval));
1574 # returns boolean (0 or 1)
1575 # Attempts to determine whether or not a gc (and corresponding build
1576 # of a .bitmap/.bndl file) will actually take place at the next_gc
1577 # time (as returned by next_gc). Whether or not a .bitmap and .bndl
1578 # end up being built depends on whether or not the local object graph
1579 # is complete. In general if has_alternates is true then a .bitmap/.bndl
1580 # is not possible because the object graph will be incomplete,
1581 # but it *is* possible that even though the repository has_alternates,
1582 # it does not actually borrow any objects so a .bitmap/.bndl will build
1583 # in spite of the presence of alternates -- but this is expected to be rare.
1584 # The result is a best guess and false return value is not an absolute
1585 # guarantee that gc will not take place at the next interval, but it probably
1586 # will not if nothing changes in the meantime.
1587 sub needs_gc {
1588 my $self = shift;
1589 my $lgc = parse_any_date($self->{lastgc});
1590 my $lrecv = parse_any_date($self->{lastreceive});
1591 my $lpgc = parse_any_date($self->{lastparentgc});
1592 return 1 unless defined($lgc) && defined($lrecv);
1593 if ($self->has_alternates) {
1594 return 1 unless defined($lpgc);
1595 return 1 unless $lpgc < $lgc;
1597 return 1 unless $lrecv < $lgc;
1598 # We don't try running any "is_dirty" check, so if somehow the
1599 # repository became dirty without updating lastreceive we might
1600 # incorrectly return false instead of true.
1601 return 0;
1604 sub delete_ctag {
1605 my $self = shift;
1606 my ($ctag) = @_;
1608 # sanity check, disallow filenames starting with . .. or /
1609 unlink($self->{path}.'/ctags/'.$ctag)
1610 unless !defined($ctag) || $ctag =~ m|^/| || $ctag =~ m{(?:^|/)(?:\.\.?)(?:/|$)};
1613 # returns new tag count value on success (will always be >= 1) otherwise undef
1614 sub add_ctag {
1615 my $self = shift;
1616 my $ctag = valid_tag(shift);
1617 my $nochanged = shift;
1619 # sanity check, disallow filenames starting with . .. or /
1620 return undef if !defined($ctag) || $ctag =~ m|^/| || $ctag =~ m{(?:^|/)(?:\.\.?)(?:/|$)};
1622 my $val = 0;
1623 my $ct;
1624 if (open $ct, '<', $self->{path}."/ctags/$ctag") {
1625 my $count = <$ct>;
1626 close $ct;
1627 defined $count or $count = '';
1628 chomp $count;
1629 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
1631 ++$val;
1632 my $oldmask = umask();
1633 umask($oldmask & ~0060);
1634 open $ct, '>', $self->{path}."/ctags/$ctag" and print $ct $val."\n" and close $ct;
1635 $self->_set_changed unless $nochanged;
1636 $self->_set_forkchange unless $nochanged;
1637 umask($oldmask);
1638 return $val;
1641 sub get_ctag_names {
1642 my $self = shift;
1643 my @ctags = ();
1644 opendir(my $dh, $self->{path}.'/ctags')
1645 or return @ctags;
1646 @ctags = grep { -f "$self->{path}/ctags/$_" } readdir($dh);
1647 closedir($dh);
1648 return sort({lc($a) cmp lc($b)} @ctags);
1651 sub get_heads {
1652 my $self = shift;
1653 my $fh;
1654 my $heads = get_git("--git-dir=$self->{path}", 'for-each-ref', '--format=%(objectname) %(refname)', 'refs/heads');
1655 defined($heads) or $heads = '';
1656 chomp $heads;
1657 my @res = ();
1658 foreach (split(/\n/, $heads)) {
1659 chomp;
1660 next if !m#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
1661 push @res, $1;
1663 push(@res, $self->{orig_HEAD}) if !@res && defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '';
1664 @res;
1667 sub get_HEAD {
1668 my $self = shift;
1669 my $HEAD = read_HEAD_ref($self->{path});
1670 defined($HEAD) && $HEAD ne "" or die "could not get HEAD";
1671 return $1 if $HEAD =~ m{^refs/heads/(.+)$};
1672 return "[other]" if $HEAD =~ m{^refs/};
1673 return "[detached]" if $HEAD =~ m{^[0-9a-fA-F]{4,}$};
1674 return "[invalid]";
1677 sub set_HEAD {
1678 my $self = shift;
1679 my $newHEAD = shift;
1680 # Cursory checks only -- if you want to break your HEAD, be my guest
1681 valid_branch_name($newHEAD) or
1682 die "grossly invalid new HEAD: $newHEAD";
1683 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', "refs/heads/$newHEAD");
1684 die "could not set HEAD" if ($? >> 8);
1685 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob'
1686 or system('cp', '-p', '-f', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
1689 sub gen_auth {
1690 my $self = shift;
1691 my ($type) = @_;
1692 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
1694 $self->{authtype} = $type;
1696 no warnings;
1697 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
1699 my $expire = time + 24 * 3600;
1700 my $propval = "# ${type}AUTH $self->{auth} $expire";
1701 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gitweb.repoauth', $propval);
1702 $self->{auth};
1705 sub del_auth {
1706 my $self = shift;
1708 delete $self->{auth};
1709 delete $self->{authtype};
1710 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--unset', 'gitweb.repoauth');
1713 sub remove_user {
1714 my $self = shift;
1715 my ($username) = @_;
1717 my $before_count = @{$self->{users}};
1718 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
1719 return @{$self->{users}} != $before_count;
1722 ### static methods
1724 sub get_forkee_name {
1725 local $_ = $_[0];
1726 (m#^(.*)/.*?$#)[0]; #
1729 sub get_forkee_path {
1730 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1731 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
1732 -d $forkee ? $forkee : '';
1735 # Ultimately the full project/fork name could end up being part of a Git ref name
1736 # when a project's forks are combined into one giant repository for efficiency.
1737 # That means that the project/fork name must satisfy the Git ref name requirements:
1739 # 1. Characters with an ASCII value less than or equal to 32 are not allowed
1740 # 2. The character with an ASCII value of 0x7F is not allowed
1741 # 3. The characters '~', '^', ':', '\', '*', '?', and '[' are not allowed
1742 # 4. The character '/' is a separator and is not allowed within a name
1743 # 5. The name may not start with '.' or end with '.'
1744 # 6. The name may not end with '.lock'
1745 # 7. The name may not contain the '..' sequence
1746 # 8. The name may not contain the '@{' sequence
1747 # 9. If multiple components are used (separated by '/'), no empty '' components
1749 # We also prohibit a trailing '.git' on any path component and futher restrict
1750 # the allowed characters to alphanumeric and [+._-] where names must start with
1751 # an alphanumeric.
1753 sub _valid_name_characters {
1754 local $_ = $_[0];
1755 (not m#^[/+._-]#)
1756 and (not m#//#)
1757 and (not m#\.\.#)
1758 and (not m#/[+._-]#)
1759 and (not m#\./#)
1760 and (not m#\.$#)
1761 and (not m#\.git/#i)
1762 and (not m#\.git$#i)
1763 and (not m#\.idx/#i)
1764 and (not m#\.idx$#i)
1765 and (not m#\.lock/#i)
1766 and (not m#\.lock$#i)
1767 and (not m#\.pack/#i)
1768 and (not m#\.pack$#i)
1769 and (not m#\.bundle/#i)
1770 and (not m#\.bundle$#i)
1771 and (not m#/$#)
1772 and (not m/^[a-fA-F0-9]{38}$/)
1773 and m#^[a-zA-Z0-9/+._-]+$#
1774 and !has_reserved_suffix($_, $_[1], $_[2]);
1777 # $_[0] => prospective project name (WITHOUT trailing .git)
1778 # $_[1] => true to allow orphans (i.e. two-or-more-level-deep projects without a parent)
1779 # (the directory in which the orphan will be created must, however, already exist)
1780 # $_[2] => true to allow orphans w/o needed directory if $_[1] also true (like mkdir -p)
1781 # If $_[0] is a SCALAR ref, ${$_[0]} contains the name and will be untainted on success.
1782 sub valid_name {
1783 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1784 local $_ = $_[0];
1785 my $mayberef_name = $_;
1786 ref($_) eq 'SCALAR' and $_ = $$_;
1787 my $rv = (
1788 _valid_name_characters($_) and not exists($reservedprojectnames{lc($_)})
1789 and @{[m#/#g]} <= 5 # maximum fork depth is 5
1790 and ((not m#/#) or -d get_forkee_path($_) or ($_[1] and ($_[2] or -d $Girocco::Config::reporoot.'/'.get_forkee_name($_))))
1791 and (! -f $Girocco::Config::reporoot."/$_.git")
1793 $rv && ref($mayberef_name) eq 'SCALAR' && m|^(.+)$| and $$mayberef_name = $1;
1794 return $rv;
1797 # It's possible that some forks have been kept but the forkee is gone.
1798 # In this case the standard valid_name check is too strict.
1799 # If $_[0] is a SCALAR ref, ${$_[0]} contains the name and will be untainted on success.
1800 sub does_exist {
1801 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1802 my ($mayberef_name, $nodie) = @_;
1803 my $name = ref($mayberef_name) eq 'SCALAR' ? $$mayberef_name : $mayberef_name;
1804 my $okay = (
1805 _valid_name_characters($name, $Girocco::Config::reporoot, ".git")
1806 and ((not $name =~ m#/#)
1807 or -d get_forkee_path($name)
1808 or -d $Girocco::Config::reporoot.'/'.get_forkee_name($name)));
1809 (!$okay && $nodie) and return undef;
1810 !$okay and die "tried to query for project with invalid name $name!";
1811 -d $Girocco::Config::reporoot."/$name.git" or return undef;
1812 ref($mayberef_name) eq 'SCALAR' && $name =~ m|^(.+)$| and $$mayberef_name = $1;
1813 return 1;
1816 # returns array of names only taken strictly from the group file
1817 # only entries with non-invalid names and gid >= 10000 are returned
1818 # a "non-invalid" name is 1 or more non-whitespace or '#' chars, NOT starting with '_'
1819 sub get_full_list {
1820 open my $fd, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
1821 my @projects = map {/^([^:_\s#][^:\s#]*):[^:]*:[1-9]\d{4,}:/ ? $1 : ()} <$fd>;
1822 close $fd;
1823 @projects;
1826 # returns array of array refs containing all fields (at least 3) of each entry from group file
1827 # only entries with non-invalid names and gid >= 10000 are returned
1828 # a "non-invalid" name is 1 or more non-whitespace or '#' chars, NOT starting with '_'
1829 # using
1830 # join(':',@{<array_ref>})
1831 # will recover the _exact_ original line from the group file for that entry
1832 sub get_full_list_extended {
1833 open my $fd, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
1834 my @projects = map {chomp; /^([^:_\s#][^:\s#]*:[^:]*:[1-9]\d{4,}:.*)$/ ? [split(/:/,$1,-1)] : ()} <$fd>;
1835 close $fd;
1836 @projects;