mob.html: personal mob updates do NOT generate CIA/JSON notifications
[girocco.git] / Girocco / Project.pm
blobb93e87df65201bd2c304f5c64a0ac23b1dc6a732
1 package Girocco::Project;
3 use strict;
4 use warnings;
6 BEGIN {
7 use Girocco::CGI;
8 use Girocco::User;
9 use Girocco::Util;
10 use Girocco::HashUtil;
11 use Girocco::ProjPerm;
12 use Girocco::Config;
13 use base ('Girocco::ProjPerm::'.$Girocco::Config::permission_control); # mwahaha
16 BEGIN {
17 eval {
18 require Digest::SHA;
19 Digest::SHA->import(
20 qw(sha1_hex)
21 );1} ||
22 eval {
23 require Digest::SHA1;
24 Digest::SHA1->import(
25 qw(sha1_hex)
26 );1} ||
27 eval {
28 require Digest::SHA::PurePerl;
29 Digest::SHA::PurePerl->import(
30 qw(sha1_hex)
31 );1} ||
32 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
33 . "must be available\n";
36 our $metadata_fields = {
37 cleanmirror => ['Mirror refs', 'cleanmirror', 'placeholder'],
38 homepage => ['Homepage URL', 'hp', 'text'],
39 shortdesc => ['Short description', 'desc', 'text'],
40 README => ['<span style="display:inline-block;vertical-align:top">'.
41 'README (HTML, &lt; 8 KiB)<br />leave blank for automatic</span>',
42 'README', 'textarea', 'Enter only &#x201c;<!-- comments -->&#x201d; '.
43 'to completely suppress any README'],
44 notifymail => ['Commit notify &#x2013; mail to', 'notifymail', 'text',
45 'comma separated address list'],
46 reverseorder => ['Show oldest first', 'reverseorder', 'checkbox',
47 'show new revisions in oldest to newest order (instead of the default newest to oldest older)'.
48 ' in &#x201c;Commit notify&#x201d; email when showing new revisions'],
49 summaryonly => ['Summaries only', 'summaryonly', 'checkbox',
50 'suppress patch/diff output in &#x201c;Commit notify&#x201d; email when showing new revisions'],
51 notifytag => ['Tag notify &#x2013; mail to', 'notifytag', 'text',
52 'comma separated address list &#x2013; if not empty, tag '.
53 'notifications are sent here INSTEAD of to '.
54 '&#x201c;Commit notify &#x2013; mail to&#x201d; address(es)'],
55 notifyjson => ['Commit notify &#x2013; '.
56 '<a title="'.html_esc('single field name is &#x201c;payload&#x201d;', 1).'" href="'.
57 'https://developer.github.com/v3/activity/events/types/#pushevent'.
58 '">POST JSON</a> at', 'notifyjson', 'text'],
59 notifycia => ['Commit notify &#x2013; <a href="http://cia.vc/doc/">CIA project</a> name',
60 'notifycia', 'text', 'CIA is defunct &#x2013; this value is ignored'],
63 sub _mkdir_forkees {
64 my $self = shift;
65 my @pelems = split('/', $self->{name});
66 pop @pelems; # do not create dir for the project itself
67 my $path = $self->{base_path};
68 foreach my $pelem (@pelems) {
69 $path .= "/$pelem";
70 (-d "$path") or mkdir $path or die "mkdir $path: $!";
71 chmod 02775, $path; # ok if fails (dir may already exist and be owned by someone else)
75 # With a leading ':' get from project local config replacing ':' with 'gitweb.'
76 # With a leading '%' get from project local config after removing '%'
77 # Otherwise it's a project file name to be loaded
78 # %propmapro entries are loaded but never written
80 our %propmap = (
81 url => ':baseurl',
82 email => ':owner',
83 desc => 'description',
84 README => 'README.html',
85 hp => ':homepage',
86 notifymail => '%hooks.mailinglist',
87 notifytag => '%hooks.announcelist',
88 notifyjson => '%hooks.jsonurl',
89 notifycia => '%hooks.cianame',
92 our %propmapro = (
93 lastchange => ':lastchange',
94 lastactivity => 'info/lastactivity',
95 lastgc => ':lastgc',
96 lastreceive => ':lastreceive',
97 lastparentgc => ':lastparentgc',
98 lastrefresh => ':lastrefresh',
99 creationtime => '%girocco.creationtime',
100 reposizek => '%girocco.reposizek',
101 origurl => ':baseurl',
104 # Projects with any of these names will be disallowed to avoid possible
105 # collisions with cgi script paths or chroot paths
106 # NOTE: names are checked after using lc on them, so all entries MUST be lowercase
107 our %reservedprojectnames = (
108 admin => 1, # /admin/ links
109 alternates => 1, # .git/objects/info/alternates
110 b => 1, # /b/ -> bundle.cgi
111 blog => 1, # /blog/ links
112 c => 1, # /c/ -> cgit
113 'git-receive-pack' => 1, # smart HTTP
114 'git-upload-archive' => 1, # smart HTTP
115 'git-upload-pack' => 1, # smart HTTP
116 h => 1, # /h/ -> html.cgi
117 head => 1, # .git/HEAD
118 'http-alternates' => 1, # .git/objects/info/http-alternates
119 info => 1, # .git/info
120 objects => 1, # .git/objects
121 packs => 1, # .git/objects/info/packs
122 r => 1, # /r/ -> git http
123 refs => 1, # .git/refs
124 w => 1, # /w/ -> gitweb
125 wiki => 1, # /wiki/ links
126 srv => 1, # /srv/git/ -> chroot ssh git repositories
129 sub _update_index {
130 my $self = shift;
131 system("$Girocco::Config::basedir/gitweb/genindex.sh", $self->{name});
134 sub _readlocalconfigfile {
135 my $self = shift;
136 my $undefonerr = shift || 0;
137 delete $self->{configfilehash};
138 my $configfile = get_git("--git-dir=$self->{path}",
139 'config', '--list', '--local', '--null');
140 my $result = 1;
141 defined($configfile) || $undefonerr or $result = 0, $configfile = '';
142 return undef unless defined($configfile);
143 my %config = map({split(/\n/, $_=~/\n/?$_:"$_\ntrue", 2)} split(/\x00/, $configfile));
144 $self->{configfilehash} = \%config;
145 return $result;
148 # @_[0]: argument to convert to boolean result (0 or 1)
149 # @_[1]: value to use if argument is undef (default is 0)
150 # Returns 0 or 1
151 sub _boolval {
152 my ($val, $def) = @_;
153 defined($def) or $def = 0;
154 defined($val) or $val = $def;
155 $val =~ s/\s+//gs;
156 $val = lc($val);
157 return 0 if $val eq '' || $val eq 'false' || $val eq 'off' || $val eq 'no' || $val =~ /^0+$/;
158 return 1;
161 sub _property_path {
162 my $self = shift;
163 my ($name) = @_;
164 $self->{path}.'/'.$name;
167 sub _property_fget {
168 my $self = shift;
169 my ($name) = @_;
170 my $pname = $propmap{$name};
171 $pname = $propmapro{$name} unless $pname;
172 $pname or die "unknown property: $name";
173 if ($pname =~ /^([:%])(.*)$/) {
174 my ($where, $pname) = ($1,lc($2));
175 $self->_readlocalconfigfile
176 unless ref($self->{configfilehash}) eq 'HASH';
177 my $val = $where eq ':' ?
178 $self->{configfilehash}->{"gitweb.$pname"} :
179 $self->{configfilehash}->{$pname};
180 defined($val) or $val = '';
181 chomp $val;
182 return $val;
185 open my $p, '<', $self->_property_path($pname) or return undef;
186 my @value = <$p>;
187 close $p;
188 my $value = join('', @value); chomp $value;
189 $value;
192 sub _property_fput {
193 my $self = shift;
194 my ($name, $value) = @_;
195 my $pname = $propmap{$name};
196 $pname or die "unknown property: $name";
197 $value ||= '';
198 if ($pname =~ s/^://) {
199 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
200 return;
201 } elsif ($pname =~ s/^%//) {
202 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', $pname, $value);
203 return;
206 my $P = lock_file($self->_property_path($pname));
207 chomp $value;
208 $value ne '' and print $P "$value\n";
209 close $P;
210 unlock_file($self->_property_path($pname));
213 sub _cleanup_readme {
214 my $self = shift;
215 defined($self->{README}) or $self->{README} = '';
216 $self->{README} =~ s/\r\n?/\n/gs;
217 $self->{README} =~ s/^\s+//s;
218 $self->{README} =~ s/\s+$//s;
219 $self->{README} eq '' or $self->{README} .= "\n";
222 sub _lint_readme {
223 my $self = shift;
224 return 0 unless defined($self->{README}) && $self->{README} ne '';
225 my $test = '<html xmlns="http://www.w3.org/1999/xhtml"><body><div>';
226 $test .= $self->{README};
227 $test .= '</div></body></html>';
228 my ($code, $errors) = capture_command(2, $test, 'xmllint', '--nonet',
229 '--noout', '--nowarning', '-');
230 return 0 unless $code;
231 my $cnt = 0;
232 my @errs = ();
233 for my $line (split(/\n+/, $errors)) {
234 $line = html_esc($line);
235 $line =~ s/ /\&#160;/gs;
236 ++$cnt, $line = 'README'.$1 if $line =~ /^-(:\d+:.*)$/;
237 push @errs, '<tt>' . $line . '</tt>';
239 return ($cnt, join("<br />\n", @errs));
242 sub _properties_load {
243 my $self = shift;
244 foreach my $prop (keys %propmap) {
245 $self->{$prop} = $self->_property_fget($prop);
247 foreach my $prop (keys %propmapro) {
248 $self->{$prop} = $self->_property_fget($prop);
250 $self->_readlocalconfigfile
251 unless ref($self->{configfilehash}) eq 'HASH';
252 $self->{cleanmirror} = _boolval($self->{configfilehash}->{'girocco.cleanmirror'}, 0);
253 $self->{statusupdates} = _boolval($self->{configfilehash}->{'gitweb.statusupdates'}, 1);
254 $self->{reverseorder} = _boolval($self->{configfilehash}->{'hooks.reverseorder'}, 0);
255 $self->{summaryonly} = _boolval($self->{configfilehash}->{'hooks.summaryonly'}, 0);
256 delete $self->{auth};
257 my $val = $self->{configfilehash}->{'gitweb.repoauth'};
258 defined($val) or $val = '';
259 chomp $val;
260 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
261 my $expire = $3;
262 if (time < $expire) {
263 $self->{authtype} = $1;
264 $self->{auth} = $2;
267 $self->_cleanup_readme;
268 delete $self->{configfilehash};
271 sub _properties_save {
272 my $self = shift;
273 my $fd;
274 foreach my $prop (keys %propmap) {
275 $self->_property_fput($prop, $self->{$prop});
277 $self->{cleanmirror} = 0
278 unless defined($self->{cleanmirror}) && $self->{cleanmirror} =~ /^\d+$/;
279 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool',
280 "girocco.cleanmirror", $self->{cleanmirror});
281 $self->{statusupdates} = 1
282 unless defined($self->{statusupdates}) && $self->{statusupdates} =~ /^\d+$/;
283 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool',
284 "gitweb.statusupdates", $self->{statusupdates});
285 $self->{reverseorder} = 0
286 unless defined($self->{reverseorder}) && $self->{reverseorder} =~ /^\d+$/;
287 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool',
288 "hooks.reverseorder", $self->{reverseorder});
289 $self->{summaryonly} = 0
290 unless defined($self->{summaryonly}) && $self->{summaryonly} =~ /^\d+$/;
291 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool',
292 "hooks.summaryonly", $self->{summaryonly});
293 if (defined($self->{origurl}) && defined($self->{url}) &&
294 $self->{origurl} ne $self->{url} && -e $self->_property_path(".banged")) {
295 if (open($fd, '>', $self->_property_path(".bangagain"))) {
296 close $fd;
297 chmod(0664, $self->_property_path(".bangagain"));
302 sub _nofetch_path {
303 my $self = shift;
304 $self->_property_path('.nofetch');
307 sub _nofetch {
308 my $self = shift;
309 my ($nofetch) = @_;
310 my $nf = $self->_nofetch_path;
311 if ($nofetch) {
312 open my $x, '>', $nf or die "nofetch failed: $!";
313 close $x;
314 } else {
315 unlink $nf or die "yesfetch failed: $!";
319 sub _gcp_path {
320 my $self = shift;
321 $self->_property_path('.gc_in_progress');
324 sub _clonelog_path {
325 my $self = shift;
326 $self->_property_path('.clonelog');
329 sub _clonefail_path {
330 my $self = shift;
331 $self->_property_path('.clone_failed');
334 sub _clonep_path {
335 my $self = shift;
336 $self->_property_path('.clone_in_progress');
339 sub _clonep {
340 my $self = shift;
341 my ($nofetch) = @_;
342 my $np = $self->_clonep_path;
343 if ($nofetch) {
344 open my $x, '>', $np or die "clonep failed: $!";
345 close $x;
346 } else {
347 unlink $np or die "clonef failed: $!";
350 sub _alternates_setup {
351 my $self = shift;
352 return unless $self->{name} =~ m#/#;
353 my $forkee_name = get_forkee_name($self->{name});
354 my $forkee_path = get_forkee_path($self->{name});
355 return unless -d $forkee_path;
356 mkdir $self->{path}.'/refs'; chmod 02775, $self->{path}.'/refs';
357 mkdir $self->{path}.'/objects'; chmod 02775, $self->{path}.'/objects';
358 mkdir $self->{path}.'/objects/info'; chmod 02775, $self->{path}.'/objects/info';
360 # We set up both alternates and http_alternates since we cannot use
361 # relative path in alternates - that doesn't work recursively.
363 my $filename = $self->{path}.'/objects/info/alternates';
364 open my $x, '>', $filename or die "alternates failed: $!";
365 print $x "$forkee_path/objects\n";
366 close $x;
367 chmod 0664, $filename or warn "cannot chmod $filename: $!";
369 if ($Girocco::Config::httppullurl) {
370 $filename = $self->{path}.'/objects/info/http-alternates';
371 open my $x, '>', $filename or die "http-alternates failed: $!";
372 my $upfork = $forkee_name;
373 do { print $x "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
374 close $x;
375 chmod 0664, $filename or warn "cannot chmod $filename: $!";
378 # copy lastactivity from the parent project
379 if (open $x, '<', $forkee_path.'/info/lastactivity') {{
380 my $activity = <$x>;
381 close $x;
382 last unless $activity;
383 open $x, '>', $self->{path}.'/info/lastactivity' or last;
384 print $x $activity;
385 close $x;
386 chomp $activity;
387 $self->{'lastactivity'} = $activity;
390 # copy refs from parent project
391 local *SAVEOUT;
392 open(SAVEOUT, ">&STDOUT") || die "couldn't dup STDOUT: $!";
393 my $result = open(STDOUT, '>', "$self->{path}/packed-refs");
394 my $resultstr = $!;
395 if (!$result) {
396 open(STDOUT, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
397 close(SAVEOUT) or die "couldn't close SAVEOUT: $!";
398 die "could not write fork's packed-refs file: $resultstr";
400 system($Girocco::Config::git_bin, "--git-dir=$forkee_path", 'for-each-ref', '--format=%(objectname) %(refname)');
401 $result = close(STDOUT);
402 $resultstr = $!;
403 open(STDOUT, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
404 close(SAVEOUT) or die "couldn't close SAVEOUT: $!";
405 $result or die "could not close fork's packed-refs file: $resultstr";
406 unlink("$self->{path}/.delaygc") if -s "$self->{path}/packed-refs";
408 # initialize HEAD
409 my $HEAD = get_git("--git-dir=$forkee_path", 'symbolic-ref', 'HEAD');
410 defined($HEAD) && $HEAD =~ m,^refs/heads/., or $HEAD = 'refs/heads/master';
411 chomp $HEAD;
412 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', $HEAD);
413 chmod 0664, "$self->{path}/packed-refs", "$self->{path}/HEAD";
416 sub _set_changed {
417 my $self = shift;
418 my $fd;
419 open $fd, '>', "$self->{path}/htmlcache/changed" and close $fd;
422 sub _set_forkchange {
423 my $self = shift;
424 my $changedtoo = shift;
425 return unless $self->{name} =~ m#/#;
426 my $forkee_path = get_forkee_path($self->{name});
427 return unless -d $forkee_path;
428 # mark forkee as changed
429 my $fd;
430 open $fd, '>', $forkee_path.'/htmlcache/changed' and close $fd if $changedtoo;
431 open $fd, '>', $forkee_path.'/htmlcache/forkchange' and close $fd;
432 return if -e $forkee_path.'/htmlcache/summary.forkchange';
433 open $fd, '>', $forkee_path.'/htmlcache/summary.forkchange' and close $fd;
436 sub _ctags_setup {
437 my $self = shift;
438 my $perms = $Girocco::Config::permission_control eq 'Hooks' ? 02777 : 02775;
439 mkdir $self->{path}.'/ctags'; chmod $perms, $self->{path}.'/ctags';
442 sub _group_add {
443 my $self = shift;
444 my ($xtra) = @_;
445 $xtra .= join(',', @{$self->{users}});
446 filedb_atomic_append(jailed_file('/etc/group'),
447 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
450 sub _group_update {
451 my $self = shift;
452 my $xtra = join(',', @{$self->{users}});
453 filedb_atomic_edit(jailed_file('/etc/group'),
454 sub {
455 $_ = $_[0];
456 chomp;
457 if ($self->{name} eq (split /:/)[0]) {
458 # preserve readonly flag
459 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
460 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
461 } else {
462 return "$_\n";
468 sub _group_remove {
469 my $self = shift;
470 filedb_atomic_edit(jailed_file('/etc/group'),
471 sub {
472 $self->{name} ne (split /:/)[0] and return $_;
477 sub _hook_path {
478 my $self = shift;
479 my ($name) = @_;
480 $self->{path}.'/hooks/'.$name;
483 sub _hook_install {
484 my $self = shift;
485 my ($name) = @_;
486 my $hooksdir = $self->{path}.'/hooks';
487 my $oldmask = umask();
488 umask($oldmask & ~0070);
489 -d $hooksdir or mkdir $hooksdir or
490 die "hooks directory does not exist and unable to create it for project " . $self->{name} . ": $!";
491 umask($oldmask);
492 open my $src, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
493 open my $dst, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
494 while (<$src>) { print $dst $_; }
495 close $dst;
496 close $src;
497 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
500 sub _hooks_install {
501 my $self = shift;
502 foreach my $hook ('pre-receive', 'post-receive', 'update') {
503 $self->_hook_install($hook);
507 # private constructor, do not use
508 sub _new {
509 my $class = shift;
510 my ($name, $base_path, $path) = @_;
511 does_exist($name,1) || valid_name($name) or die "refusing to create project with invalid name ($name)!";
512 $path ||= "$base_path/$name.git";
513 my $proj = { name => $name, base_path => $base_path, path => $path };
515 bless $proj, $class;
518 # public constructor #0
519 # creates a virtual project not connected to disk image
520 # you can conjure() it later to disk
521 sub ghost {
522 my $class = shift;
523 my ($name, $mirror) = @_;
524 my $self = $class->_new($name, $Girocco::Config::reporoot);
525 $self->{users} = [];
526 $self->{mirror} = $mirror;
527 $self->{email} = $self->{orig_email} = '';
528 $self;
531 # public constructor #1
532 sub load {
533 my $class = shift;
534 my $name = shift || '';
536 open my $fd, '<', jailed_file("/etc/group") or die "project load failed: $!";
537 my $r = qr/^\Q$name\E:/;
538 foreach (grep /$r/, <$fd>) {
539 chomp;
541 my $self = $class->_new($name, $Girocco::Config::reporoot);
542 (-d $self->{path} && $self->_readlocalconfigfile(1))
543 or die "invalid path (".$self->{path}.") for project ".$self->{name};
545 my $ulist;
546 (undef, $self->{crypt}, $self->{gid}, $ulist) = split /:/;
547 $ulist ||= '';
548 $self->{users} = [split /,/, $ulist];
549 $self->{HEAD} = $self->get_HEAD;
550 $self->{orig_HEAD} = $self->{HEAD};
551 $self->{orig_users} = [@{$self->{users}}];
552 $self->{mirror} = ! -e $self->_nofetch_path;
553 $self->{gc_in_progress} = -e $self->_gcp_path;
554 $self->{clone_in_progress} = -e $self->_clonep_path;
555 $self->{clone_logged} = -e $self->_clonelog_path;
556 $self->{clone_failed} = -e $self->_clonefail_path;
557 $self->{ccrypt} = $self->{crypt};
559 $self->_properties_load;
560 $self->{orig_email} = $self->{email};
561 $self->{loaded} = 1; # indicates self was loaded from etc/group file
562 close $fd;
563 return $self;
565 close $fd;
566 undef;
569 # $proj may not be in sane state if this returns false!
570 # fields listed in %$metadata_fields that are NOT also
571 # in @Girocco::Config::project_fields are totally ignored!
572 sub cgi_fill {
573 my $self = shift;
574 my ($gcgi) = @_;
575 my $cgi = $gcgi->cgi;
576 my %allowedfields = map({$_ => 1} @Girocco::Config::project_fields);
577 my $field_enabled = sub {
578 !exists($metadata_fields->{$_[0]}) || exists($allowedfields{$_[0]})};
580 my $pwd = $cgi->param('pwd');
581 my $pwd2 = $cgi->param('pwd2');
582 # in case passwords are disabled
583 defined($pwd) or $pwd = ''; defined($pwd2) or $pwd = '';
584 if ($Girocco::Config::project_passwords and not $self->{crypt} and $pwd eq '' and $pwd2 eq '') {
585 $gcgi->err("Empty passwords are not permitted.");
587 if ($pwd ne '' or not $self->{crypt}) {
588 $self->{crypt} = scrypt_sha1($pwd);
590 if (($pwd ne '' || $pwd2 ne '') and $pwd ne $pwd2) {
591 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
594 $self->{cpwd} = $cgi->param('cpwd');
596 my ($forkee,$project) = ($self->{name} =~ m#^(.*/)?([^/]+)$#);
597 my $newtype = $forkee ? 'fork' : 'project';
598 length($project) <= 64
599 or $gcgi->err("The $newtype name is longer than 64 characters. Do you really need that much?");
601 if ($Girocco::Config::project_owners eq 'email') {
602 $self->{email} = $gcgi->wparam('email');
603 valid_email($self->{email})
604 or $gcgi->err("Your email sure looks weird...?");
605 length($self->{email}) <= 96
606 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
609 # No setting the url unless we're either new or an existing mirror!
610 unless ($self->{loaded} && !$self->{mirror}) {
611 $self->{url} = $gcgi->wparam('url') ;
612 if ($field_enabled->('cleanmirror')) {
613 $self->{cleanmirror} = $gcgi->wparam('cleanmirror') || 0;
616 # Always validate the url if we're an existing mirror
617 if ((defined($self->{url}) && $self->{url} ne '') || ($self->{loaded} && $self->{mirror})) {{
618 # Always allow the url to be left unchanged without validation when editing
619 last if $self->{loaded} && defined($self->{origurl}) && defined($self->{url}) && $self->{origurl} eq $self->{url};
620 valid_repo_url($self->{url})
621 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocols are supported. If the URL contains funny characters, contact me.");
622 if ($Girocco::Config::restrict_mirror_hosts) {
623 my $mh = extract_url_hostname($self->{url});
624 is_dns_hostname($mh)
625 or $gcgi->err("Invalid URL. Note that only DNS names are allowed, not IP addresses.");
626 !is_our_hostname($mh)
627 or $gcgi->err("Invalid URL. Mirrors from this host are not allowed, please create a fork instead.");
631 if ($field_enabled->('desc')) {
632 $self->{desc} = to_utf8($gcgi->wparam('desc'), 1);
633 length($self->{desc}) <= 1024
634 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
637 if ($field_enabled->('README')) {
638 $self->{README} = to_utf8($gcgi->wparam('README'), 1);
639 $self->_cleanup_readme;
640 length($self->{README}) <= 8192
641 or $gcgi->err("README length &gt; 8kb!");
642 my ($cnt, $err) = (0);
643 ($cnt, $err) = $self->_lint_readme if $gcgi->ok && $Girocco::Config::xmllint_readme;
644 $gcgi->err($err), $gcgi->{err} += $cnt-1 if $cnt;
647 if ($field_enabled->('hp')) {
648 $self->{hp} = $gcgi->wparam('hp');
649 if ($self->{hp}) {
650 valid_web_url($self->{hp})
651 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
655 # No mucking about with users unless we're a push project
656 if (($self->{loaded} && !$self->{mirror}) ||
657 (!$self->{loaded} && (!defined($self->{url}) || $self->{url} eq ''))) {
658 my %users = ();
659 my @users = ();
660 foreach my $user ($cgi->multi_param('user')) {
661 if (!exists($users{$user})) {
662 $users{$user} = 1;
663 push(@users, $user) if Girocco::User::does_exist($user, 1);
666 $self->{users} = \@users;
669 # HEAD can only be set with editproj, NOT regproj (regproj sets it automatically)
670 # It may ALWAYS be set to what it was (even if there's no such refs/heads/...)
671 my $newhead;
672 if (defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '' &&
673 defined($newhead = $cgi->param('HEAD')) && $newhead ne '') {
674 if ($newhead eq $self->{orig_HEAD} ||
675 get_git("--git-dir=$self->{path}", 'rev-parse', '--verify', '--quiet', 'refs/heads/'.$newhead)) {
676 $self->{HEAD} = $newhead;
677 } else {
678 $gcgi->err("Invalid default branch (no such ref)");
682 # schedule deletion of tags (will be committed by update() after auth)
683 $self->{tags_to_delete} = [$cgi->multi_param('tags')];
685 if ($field_enabled->('notifymail')) {
686 $self->{notifymail} = $gcgi->wparam('notifymail');
687 if ($self->{notifymail}) {
688 (valid_email_multi($self->{notifymail}) and length($self->{notifymail}) <= 512)
689 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
691 if ($field_enabled->('reverseorder')) {
692 $self->{reverseorder} = $gcgi->wparam('reverseorder') || 0;
694 if ($field_enabled->('summaryonly')) {
695 $self->{summaryonly} = $gcgi->wparam('summaryonly') || 0;
699 if ($field_enabled->('notifytag')) {
700 $self->{notifytag} = $gcgi->wparam('notifytag');
701 if ($self->{notifytag}) {
702 (valid_email_multi($self->{notifytag}) and length($self->{notifytag}) <= 512)
703 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
707 if ($field_enabled->('notifyjson')) {
708 $self->{notifyjson} = $gcgi->wparam('notifyjson');
709 if ($self->{notifyjson}) {
710 valid_web_url($self->{notifyjson})
711 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
715 if ($field_enabled->('notifycia')) {
716 $self->{notifycia} = $gcgi->wparam('notifycia');
717 if ($self->{notifycia}) {
718 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
719 or $gcgi->err("Overly suspicious CIA notify project name. If it's actually valid, don't contact me, CIA is defunct.");
723 if ($cgi->param('setstatusupdates')) {
724 my $val = $gcgi->wparam('statusupdates') || '0';
725 $self->{statusupdates} = $val ? 1 : 0;
728 not $gcgi->err_check;
731 sub form_defaults {
732 my $self = shift;
734 name => $self->{name},
735 email => $self->{email},
736 url => $self->{url},
737 cleanmirror => $self->{cleanmirror},
738 desc => html_esc($self->{desc}),
739 README => html_esc($self->{README}),
740 hp => $self->{hp},
741 users => $self->{users},
742 notifymail => html_esc($self->{notifymail}),
743 reverseorder => $self->{reverseorder},
744 summaryonly => $self->{summaryonly},
745 notifytag => html_esc($self->{notifytag}),
746 notifyjson => html_esc($self->{notifyjson}),
747 notifycia => html_esc($self->{notifycia}),
751 # return true if $enc_passwd is a match for $plain_passwd
752 my $_check_passwd_match = sub {
753 my $enc_passwd = shift;
754 my $plain_passwd = shift;
755 defined($enc_passwd) or $enc_passwd = '';
756 defined($plain_passwd) or $plain_passwd = '';
757 # $enc_passwd may be crypt or crypt_sha1
758 if ($enc_passwd =~ m(^\$sha1\$(\d+)\$([./0-9A-Za-z]{1,64})\$[./0-9A-Za-z]{28}$)) {
759 # It's using sha1-crypt
760 return $enc_passwd eq crypt_sha1($plain_passwd, $2, -(0+$1));
761 } else {
762 # It's using crypt
763 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
767 sub authenticate {
768 my $self = shift;
769 my ($gcgi) = @_;
771 $self->{ccrypt} or die "Can't authenticate against a project with no password";
772 defined($self->{cpwd}) or $self->{cpwd} = '';
773 unless ($_check_passwd_match->($self->{ccrypt}, $self->{cpwd})) {
774 $gcgi->err("Your admin password does not match!");
775 return 0;
777 return 1;
780 # return true if the password from the file is empty or consists of all the same
781 # character. However, if the project was NOT loaded from the group file
782 # (!self->{loaded}) then the password is never locked.
783 # This function does NOT check $Girocco::Config::project_passwords, the caller
784 # is responsible for doing so if desired. Same for $self->{email}.
785 sub is_password_locked {
786 my $self = shift;
788 $self->{loaded} or return 0;
789 my $testcrypt = $self->{ccrypt}; # The value from the group file
790 defined($testcrypt) or $testcrypt = '';
791 $testcrypt ne '' or return 1; # No password at all
792 $testcrypt =~ /^(.)\1*$/ and return 1; # Bogus crypt value
793 return 0; # Not locked
796 sub _setup {
797 use POSIX qw(strftime);
798 my $self = shift;
799 my ($pushers) = @_;
800 my $fd;
802 $self->_mkdir_forkees;
804 my $gid;
805 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
806 if ($Girocco::Config::owning_group) {
807 $gid = scalar(getgrnam($Girocco::Config::owning_group));
808 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
809 chmod(02775, $self->{path}) or die "chmod 02775 $self->{path} failed: $!";
810 } else {
811 chmod(02777, $self->{path}) or die "chmod 02777 $self->{path} failed: $!";
813 $ENV{'GIT_TEMPLATE_DIR'} = $Girocco::Config::chroot.'/var/empty';
814 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--quiet', '--bare', '--shared='.$self->shared_mode()) == 0
815 or die "git init $self->{path} failed: $?";
816 # we don't need these two, remove them (they will normally be created empty) if they exist
817 rmdir $self->{path}."/branches";
818 rmdir $self->{path}."/remotes";
819 -d $self->{path}."/info" or mkdir $self->{path}."/info"
820 or die "info directory does not exist and unable to create it: $!";
821 -d $self->{path}."/hooks" or mkdir $self->{path}."/hooks"
822 or die "hooks directory does not exist and unable to create it: $!";
823 # clean out any kruft that may have come in from the initial template directory
824 foreach my $cleandir (qw(hooks info)) {
825 if (opendir my $hooksdir, $self->{path}.'/'.$cleandir) {
826 unlink map "$self->{path}/$_", grep { $_ ne '.' && $_ ne '..' } readdir $hooksdir;
827 closedir $hooksdir;
830 if ($Girocco::Config::owning_group) {
831 chown(-1, $gid, $self->{path}."/hooks") or die "chgrp $gid $self->{path}/hooks failed: $!";
833 # hooks never world writable
834 chmod 02775, $self->{path}."/hooks" or die "chmod 02775 $self->{path}/hooks failed: $!";
835 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.logAllRefUpdates', 'false') == 0
836 or die "disabling core.logAllRefUpdates failed: $?";
837 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.ignoreCase', 'false') == 0
838 or die "disabling core.ignoreCase failed: $?";
839 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'transfer.fsckObjects', 'true') == 0
840 or die "enabling transfer.fsckObjects failed: $?";
841 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastForwards', 'false') == 0
842 or die "disabling receive.denyNonFastForwards failed: $?";
843 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyDeleteCurrent', 'warn') == 0
844 or die "disabling receive.denyDeleteCurrent failed: $?";
845 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
846 or die "disabling gc.auto failed: $?";
847 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.autogc', '0') == 0
848 or die "disabling receive.autogc failed: $?";
849 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
850 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.updateServerInfo', 'true') == 0
851 or die "enabling receive.updateServerInfo failed: $?";
852 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'repack.writeBitmaps', 'true') == 0
853 or die "enabling repack.writeBitmaps failed: $?";
854 $self->{creationtime} = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
855 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'girocco.creationtime', $self->{creationtime}) == 0
856 or die "setting girocco.creationtime failed: $?";
857 -d $self->{path}."/htmlcache" or mkdir $self->{path}."/htmlcache"
858 or die "htmlcache directory does not exist and unable to create it: $!";
859 -d $self->{path}."/bundles" or mkdir $self->{path}."/bundles"
860 or die "bundles directory does not exist and unable to create it: $!";
861 -d $self->{path}."/reflogs" or mkdir $self->{path}."/reflogs"
862 or die "reflogs directory does not exist and unable to create it: $!";
863 foreach my $file (qw(info/lastactivity .delaygc)) {
864 if (open $fd, '>', $self->{path}."/".$file) {
865 close $fd;
866 chmod 0664, $self->{path}."/".$file;
870 # /info must have right permissions,
871 # and git init didn't do it for some reason.
872 # config must have correct permissions.
873 # and Git 2.1.0 - 2.2.1 incorrectly add +x for some reason.
874 # also make sure /refs, /objects and /htmlcache are correct too.
875 my ($dmode, $dmodestr, $fmode, $fmodestr);
876 if ($Girocco::Config::owning_group) {
877 ($dmode, $dmodestr) = (02775, '02775');
878 ($fmode, $fmodestr) = (0664, '0664');
879 } else {
880 ($dmode, $dmodestr) = (02777, '02777');
881 ($fmode, $fmodestr) = (0666, '0666');
883 foreach my $dir (qw(info refs objects htmlcache bundles reflogs)) {
884 chmod($dmode, $self->{path}."/$dir") or die "chmod $dmodestr $self->{path}/$dir failed: $!";
886 foreach my $file (qw(config)) {
887 chmod($fmode, $self->{path}."/$file") or die "chmod $fmodestr $self->{path}/$file failed: $!";
889 # these ones are probably not strictly required but are nice to have
890 foreach my $dir (qw(refs/heads refs/tags objects/info objects/pack)) {
891 -d $self->{path}."/$dir" or mkdir $self->{path}."/$dir";
892 chmod($dmode, $self->{path}."/$dir");
895 $self->_properties_save;
896 $self->_alternates_setup;
897 $self->_ctags_setup;
898 $self->_group_remove;
899 $self->_group_add($pushers);
900 $self->_hooks_install;
901 $self->_update_index;
902 $self->_set_changed;
903 $self->_set_forkchange(1);
906 sub premirror {
907 my $self = shift;
909 $self->_setup(':');
910 $self->_clonep(1);
911 $self->perm_initialize;
914 sub conjure {
915 my $self = shift;
917 $self->_setup;
918 $self->_nofetch(1);
919 if ($Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
920 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
921 or die "create-personal-mob-area $self->{name} failed";
923 $self->perm_initialize;
926 sub clone {
927 my $self = shift;
929 unlink ($self->_clonefail_path()); # Ignore EEXIST error
930 unlink ($self->_clonelog_path()); # Ignore EEXIST error
932 use IO::Socket;
933 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
934 select((select($sock),$|=1)[0]);
935 $sock->print("clone ".$self->{name}."\n");
936 # Just ignore reply, we are going to succeed anyway and the I/O
937 # would apparently get quite hairy.
938 $sock->flush();
939 sleep 2; # *cough*
940 $sock->close();
943 sub update {
944 my $self = shift;
946 $self->_properties_save;
947 $self->_group_update;
949 if (exists($self->{tags_to_delete})) {
950 $self->delete_ctag($_) foreach(@{$self->{tags_to_delete}});
953 $self->set_HEAD($self->{HEAD}) unless $self->{orig_HEAD} eq $self->{HEAD};
955 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
956 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
957 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
958 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
960 $self->_update_index if $self->{email} ne $self->{orig_email};
961 $self->{orig_email} = $self->{email};
962 $self->_set_changed;
967 sub update_password {
968 my $self = shift;
969 my ($pwd) = @_;
971 $self->{crypt} = scrypt_sha1($pwd);
972 $self->_group_update;
975 # You can explicitly do this just on a ghost() repository too.
976 sub delete {
977 my $self = shift;
979 if (-d $self->{path}) {
980 system('rm', '-rf', $self->{path}) == 0
981 or die "rm -rf $self->{path} failed: $?";
983 # attempt to clean up any empty fork directories by removing them
984 my @pelems = split('/', $self->{name});
985 while (@pelems > 1) {
986 pop @pelems;
987 # okay to fail
988 rmdir join('/', $Girocco::Config::reporoot, @pelems) or last;
990 $self->_group_remove;
991 $self->_update_index;
992 $self->_set_forkchange(1);
997 sub _contains_files {
998 my $dir = shift;
999 (-d $dir) or return 0;
1000 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
1001 while (my $entry = readdir($dh)) {
1002 next if $entry eq '' || $entry eq '.' || $entry eq '..';
1003 closedir($dh), return 1
1004 if -f "$dir/$entry" ||
1005 -d "$dir/$entry" && _contains_files("$dir/$entry");
1007 closedir($dh);
1008 return 0;
1011 sub has_forks {
1012 my $self = shift;
1014 return _contains_files($Girocco::Config::reporoot.'/'.$self->{name});
1017 # Returns an array of 0 or more array refs, one for each bundle:
1018 # ->[0]: bundle creation time (seconds since epoch)
1019 # ->[1]: bundle name (e.g. foo-xxxxxxxx.bundle)
1020 # ->[2]: bundle size in bytes
1021 sub bundles {
1022 my $self = shift;
1023 use Time::Local;
1025 return @{$self->{bundles}} if ref($self->{bundles}) eq 'ARRAY';
1026 my @blist = ();
1027 if (-d $self->{path}.'/bundles') {{
1028 my $prefix = $self->{name};
1029 $prefix =~ s|^.*[^/]/([^/]+)$|$1|;
1030 opendir(my $dh, $self->{path}.'/bundles') or last;
1031 while (my $bfile = readdir($dh)) {
1032 next unless $bfile =~ /^\d{8}_\d{6}-[0-9a-f]{8}$/;
1033 my $ctime = eval {timegm(
1034 0+substr($bfile,13,2), 0+substr($bfile,11,2), 0+substr($bfile,9,2),
1035 0+substr($bfile,6,2), 0+substr($bfile,4,2)-1, 0+substr($bfile,0,4)-1900)};
1036 next unless $ctime;
1037 open(my $bh, '<', $self->{path}.'/bundles/'.$bfile) or next;
1038 my $f1 = <$bh>;
1039 my $f2 = <$bh>;
1040 $f1 = $self->{path}.'/objects/pack/'.$f1 if $f1 && $f1 !~ m|^/|;
1041 $f2 = $self->{path}.'/objects/pack/'.$f2 if $f2 && $f2 !~ m|^/|;
1042 close($bh);
1043 next unless $f1 && $f2 && $f1 ne $f2;
1044 chomp $f1;
1045 chomp $f2;
1046 next unless -e $f1 && -e $f2;
1047 my $s1 = -s $f1 || 0;
1048 my $s2 = -s $f2 || 0;
1049 next unless $s1 || $s2;
1050 push(@blist, [$ctime, "$prefix-".substr($bfile,16,8).".bundle", $s1+$s2]);
1052 closedir($dh);
1054 @blist = sort({$b->[0] <=> $a->[0]} @blist);
1055 my %seen = ();
1056 my @result = ();
1057 foreach my $bndl (@blist) {
1058 next if $seen{$bndl->[1]};
1059 $seen{$bndl->[1]} = 1;
1060 push(@result, $bndl);
1062 $self->{bundles} = \@result;
1063 return @result;
1066 sub has_alternates {
1067 my $self = shift;
1068 return -s $self->{path}.'/objects/info/alternates' ? 1 : 0;
1071 sub has_bundle {
1072 my $self = shift;
1074 return scalar($self->bundles);
1077 # returns true if any of the notify fields are non-empty
1078 sub has_notify {
1079 my $self = shift;
1080 # We do not ckeck notifycia since it's defunct
1081 return $self->{'notifymail'} || $self->{'notifytag'} || $self->{'notifyjson'};
1084 sub is_empty {
1085 # A project is considered empty if the git repository does not
1086 # have any refs. This means packed-refs does not exist or is
1087 # empty or only has lines starting with '#' AND there are no
1088 # files in the refs subdirectory hierarchy (no matter how deep).
1090 my $self = shift;
1092 (-d $self->{path}) or return 0;
1093 if (-e $self->{path}.'/packed-refs') {
1094 open(my $pr, '<', $self->{path}.'/packed-refs')
1095 or die "open $self->{path}./packed-refs failed: $!";
1096 my $foundref = 0;
1097 while (my $ref = <$pr>) {
1098 next if $ref =~ /^#/;
1099 $foundref = 1;
1100 last;
1102 close($pr);
1103 return 0 if $foundref;
1105 (-d $self->{path}.'/refs') or return 1;
1106 return !_contains_files($self->{path}.'/refs');
1109 # returns array:
1110 # [0]: earliest possible scheduled next gc, undef if lastgc not set
1111 # [1]: approx. latest possible scheduled next gc, undef if lastgc not set
1112 # Both values (if not undef) are seconds since epoch
1113 # Result only considers lastgc and min_gc_interval nothing else
1114 sub next_gc {
1115 my $self = shift;
1116 my $lastgcepoch = parse_any_date($self->{lastgc});
1117 return (undef, undef) unless defined $lastgcepoch;
1118 return ($lastgcepoch + $Girocco::Config::min_gc_interval,
1119 int($lastgcepoch + 1.25 * $Girocco::Config::min_gc_interval +
1120 $Girocco::Config::min_mirror_interval));
1123 # returns boolean (0 or 1)
1124 # Attempts to determine whether or not a gc (and corresponding build
1125 # of a .bitmap/.bndl file) will actually take place at the next_gc
1126 # time (as returned by next_gc). Whether or not a .bitmap and .bndl
1127 # end up being built depends on whether or not the local object graph
1128 # is complete. In general if has_alternates is true then a .bitmap/.bndl
1129 # is not possible because the object graph will be incomplete,
1130 # but it *is* possible that even though the repository has_alternates,
1131 # it does not actually borrow any objects so a .bitmap/.bndl will build
1132 # in spite of the presence of alternates -- but this is expected to be rare.
1133 # The result is a best guess and false return value is not an absolute
1134 # guarantee that gc will not take place at the next interval, but it probably
1135 # will not if nothing changes in the meantime.
1136 sub needs_gc {
1137 my $self = shift;
1138 my $lgc = parse_any_date($self->{lastgc});
1139 my $lrecv = parse_any_date($self->{lastreceive});
1140 my $lpgc = parse_any_date($self->{lastparentgc});
1141 return 1 unless defined($lgc) && defined($lrecv);
1142 if ($self->has_alternates) {
1143 return 1 unless defined($lpgc);
1144 return 1 unless $lpgc < $lgc;
1146 return 1 unless $lrecv < $lgc;
1147 # We don't try running any "is_dirty" check, so if somehow the
1148 # repository became dirty without updating lastreceive we might
1149 # incorrectly return false instead of true.
1150 return 0;
1153 sub delete_ctag {
1154 my $self = shift;
1155 my ($ctag) = @_;
1157 # sanity check, disallow filenames starting with . .. or /
1158 unlink($self->{path}.'/ctags/'.$ctag) if($ctag !~ m|^(\.\.?)/|);
1161 sub get_ctag_names {
1162 my $self = shift;
1163 my @ctags = ();
1164 opendir(my $dh, $self->{path}.'/ctags')
1165 or return @ctags;
1166 @ctags = grep { -f "$self->{path}/ctags/$_" } readdir($dh);
1167 closedir($dh);
1168 return sort({lc($a) cmp lc($b)} @ctags);
1171 sub get_heads {
1172 my $self = shift;
1173 my $fh;
1174 my $heads = get_git("--git-dir=$self->{path}", 'for-each-ref', '--format=%(objectname) %(refname)', 'refs/heads');
1175 defined($heads) or $heads = '';
1176 chomp $heads;
1177 my @res = ();
1178 foreach (split(/\n/, $heads)) {
1179 chomp;
1180 next if !m#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
1181 push @res, $1;
1183 push(@res, $self->{orig_HEAD}) if !@res && defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '';
1184 @res;
1187 sub get_HEAD {
1188 my $self = shift;
1189 my $HEAD = get_git("--git-dir=$self->{path}", 'symbolic-ref', 'HEAD');
1190 defined($HEAD) or $HEAD = '';
1191 chomp $HEAD;
1192 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
1193 return $1;
1196 sub set_HEAD {
1197 my $self = shift;
1198 my $newHEAD = shift;
1199 # Cursory checks only -- if you want to break your HEAD, be my guest
1200 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
1201 die "grossly invalid new HEAD: $newHEAD";
1203 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', "refs/heads/$newHEAD");
1204 die "could not set HEAD" if ($? >> 8);
1205 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob'
1206 or system('cp', '-p', '-f', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
1209 sub gen_auth {
1210 my $self = shift;
1211 my ($type) = @_;
1212 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
1214 $self->{authtype} = $type;
1216 no warnings;
1217 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
1219 my $expire = time + 24 * 3600;
1220 my $propval = "# ${type}AUTH $self->{auth} $expire";
1221 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gitweb.repoauth', $propval);
1222 $self->{auth};
1225 sub del_auth {
1226 my $self = shift;
1228 delete $self->{auth};
1229 delete $self->{authtype};
1230 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--unset', 'gitweb.repoauth');
1233 sub remove_user {
1234 my $self = shift;
1235 my ($username) = @_;
1237 my $before_count = @{$self->{users}};
1238 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
1239 return @{$self->{users}} != $before_count;
1242 ### static methods
1244 sub get_forkee_name {
1245 local $_ = $_[0];
1246 (m#^(.*)/.*?$#)[0]; #
1249 sub get_forkee_path {
1250 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1251 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
1252 -d $forkee ? $forkee : '';
1255 # Ultimately the full project/fork name could end up being part of a Git ref name
1256 # when a project's forks are combined into one giant repository for efficiency.
1257 # That means that the project/fork name must satisfy the Git ref name requirements:
1259 # 1. Characters with an ASCII value less than or equal to 32 are not allowed
1260 # 2. The character with an ASCII value of 0x7F is not allowed
1261 # 3. The characters '~', '^', ':', '\', '*', '?', and '[' are not allowed
1262 # 4. The character '/' is a separator and is not allowed within a name
1263 # 5. The name may not start with '.' or end with '.'
1264 # 6. The name may not end with '.lock'
1265 # 7. The name may not contain the '..' sequence
1266 # 8. The name may not contain the '@{' sequence
1267 # 9. If multiple components are used (separated by '/'), no empty '' components
1269 # We also prohibit a trailing '.git' on any path component and futher restrict
1270 # the allowed characters to alphanumeric and [+._-] where names must start with
1271 # an alphanumeric.
1273 sub _valid_name_characters {
1274 local $_ = $_[0];
1275 (not m#^[/+._-]#)
1276 and (not m#//#)
1277 and (not m#\.\.#)
1278 and (not m#/[+._-]#)
1279 and (not m#\./#)
1280 and (not m#\.$#)
1281 and (not m#\.git/#i)
1282 and (not m#\.git$#i)
1283 and (not m#\.idx/#i)
1284 and (not m#\.idx$#i)
1285 and (not m#\.lock/#i)
1286 and (not m#\.lock$#i)
1287 and (not m#\.pack/#i)
1288 and (not m#\.pack$#i)
1289 and (not m#\.bundle/#i)
1290 and (not m#\.bundle$#i)
1291 and (not m#/$#)
1292 and (not m/^[a-fA-F0-9]{38}$/)
1293 and m#^[a-zA-Z0-9/+._-]+$#
1294 and !has_reserved_suffix($_, $_[1], $_[2]);
1297 sub valid_name {
1298 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1299 local $_ = $_[0];
1300 _valid_name_characters($_) and not exists($reservedprojectnames{lc($_)})
1301 and @{[m#/#g]} <= 5 # maximum fork depth is 5
1302 and ((not m#/#) or -d get_forkee_path($_)); # will also catch ^/
1305 # It's possible that some forks have been kept but the forkee is gone.
1306 # In this case the standard valid_name check is too strict.
1307 sub does_exist {
1308 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1309 my ($name, $nodie) = @_;
1310 my $okay = (
1311 _valid_name_characters($name, $Girocco::Config::reporoot, ".git")
1312 and ((not $name =~ m#/#)
1313 or -d get_forkee_path($name)
1314 or -d $Girocco::Config::reporoot.'/'.get_forkee_name($name)));
1315 (!$okay && $nodie) and return undef;
1316 !$okay and die "tried to query for project with invalid name $name!";
1317 (-d $Girocco::Config::reporoot."/$name.git");
1320 sub get_full_list {
1321 open my $fd, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
1322 my @projects = map {/^([^:_\s#][^:\s#]*):[^:]*:\d{5,}:/ ? $1 : ()} <$fd>;
1323 close $fd;
1324 @projects;