Config.pm/Project.pm: reserve a few more extensions and names
[girocco.git] / Girocco / Project.pm
blobdd628825c63a0babeaf073913fe5122ccaad2f4c
1 package Girocco::Project;
3 use strict;
4 use warnings;
6 BEGIN {
7 use Girocco::CGI;
8 use Girocco::User;
9 use Girocco::Util;
10 use Girocco::HashUtil;
11 use Girocco::ProjPerm;
12 use Girocco::Config;
13 use base ('Girocco::ProjPerm::'.$Girocco::Config::permission_control); # mwahaha
16 BEGIN {
17 eval {
18 require Digest::SHA;
19 Digest::SHA->import(
20 qw(sha1_hex)
21 );1} ||
22 eval {
23 require Digest::SHA1;
24 Digest::SHA1->import(
25 qw(sha1_hex)
26 );1} ||
27 eval {
28 require Digest::SHA::PurePerl;
29 Digest::SHA::PurePerl->import(
30 qw(sha1_hex)
31 );1} ||
32 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
33 . "must be available\n";
36 our $metadata_fields = {
37 homepage => ['Homepage URL', 'hp', 'text'],
38 shortdesc => ['Short description', 'desc', 'text'],
39 README => ['<span style="display:inline-block;vertical-align:top">'.
40 'README (HTML, &lt; 8 KiB)<br />leave blank for automatic</span>',
41 'README', 'textarea', 'Enter only &#x201c;<!-- comments -->&#x201d; '.
42 'to completely suppress any README'],
43 notifymail => ['Commit notify &#x2013; mail to', 'notifymail', 'text',
44 'comma separated address list'],
45 notifytag => ['Tag notify &#x2013; mail to', 'notifytag', 'text',
46 'comma separated address list &#x2013; if not empty, tag '.
47 'notifications are sent here INSTEAD of to '.
48 '&#x201c;Commit notify &#x2013; mail to&#x201d; address(es)'],
49 notifyjson => ['Commit notify &#x2013; '.
50 '<a title="'.html_esc('single field name is &#x201c;payload&#x201d;', 1).'" href="'.
51 'https://developer.github.com/v3/activity/events/types/#pushevent'.
52 '">POST JSON</a> at', 'notifyjson', 'text'],
53 notifycia => ['Commit notify &#x2013; <a href="http://cia.vc/doc/">CIA project</a> name',
54 'notifycia', 'text', 'CIA is defunct &#x2013; this value is ignored'],
57 sub _mkdir_forkees {
58 my $self = shift;
59 my @pelems = split('/', $self->{name});
60 pop @pelems; # do not create dir for the project itself
61 my $path = $self->{base_path};
62 foreach my $pelem (@pelems) {
63 $path .= "/$pelem";
64 (-d "$path") or mkdir $path or die "mkdir $path: $!";
65 chmod 02775, $path; # ok if fails (dir may already exist and be owned by someone else)
69 # With a leading ':' get from project local config replacing ':' with 'gitweb.'
70 # With a leading '%' get from project local config after removing '%'
71 # Otherwise it's a project file name to be loaded
72 # %propmapro entries are loaded but never written
74 our %propmap = (
75 url => ':baseurl',
76 email => ':owner',
77 desc => 'description',
78 README => 'README.html',
79 hp => ':homepage',
80 notifymail => '%hooks.mailinglist',
81 notifytag => '%hooks.announcelist',
82 notifyjson => '%hooks.jsonurl',
83 notifycia => '%hooks.cianame',
86 our %propmapro = (
87 lastchange => ':lastchange',
88 lastactivity => 'info/lastactivity',
89 lastgc => ':lastgc',
90 lastreceive => ':lastreceive',
91 lastparentgc => ':lastparentgc',
92 lastrefresh => ':lastrefresh',
93 creationtime => '%girocco.creationtime',
94 reposizek => '%girocco.reposizek',
95 origurl => ':baseurl',
98 # Projects with any of these names will be disallowed to avoid possible
99 # collisions with cgi script paths or chroot paths
100 # NOTE: names are checked after using lc on them, so all entries MUST be lowercase
101 our %reservedprojectnames = (
102 admin => 1, # /admin/ links
103 alternates => 1, # .git/objects/info/alternates
104 b => 1, # /b/ -> bundle.cgi
105 blog => 1, # /blog/ links
106 c => 1, # /c/ -> cgit
107 'git-receive-pack' => 1, # smart HTTP
108 'git-upload-archive' => 1, # smart HTTP
109 'git-upload-pack' => 1, # smart HTTP
110 h => 1, # /h/ -> html.cgi
111 head => 1, # .git/HEAD
112 'http-alternates' => 1, # .git/objects/info/http-alternates
113 info => 1, # .git/info
114 objects => 1, # .git/objects
115 packs => 1, # .git/objects/info/packs
116 r => 1, # /r/ -> git http
117 refs => 1, # .git/refs
118 w => 1, # /w/ -> gitweb
119 wiki => 1, # /wiki/ links
120 srv => 1, # /srv/git/ -> chroot ssh git repositories
123 sub _update_index {
124 my $self = shift;
125 system("$Girocco::Config::basedir/gitweb/genindex.sh", $self->{name});
128 sub _readlocalconfigfile {
129 my $self = shift;
130 my $undefonerr = shift || 0;
131 delete $self->{configfilehash};
132 my $configfile = get_git("--git-dir=$self->{path}",
133 'config', '--list', '--local', '--null');
134 my $result = 1;
135 defined($configfile) || $undefonerr or $result = 0, $configfile = '';
136 return undef unless defined($configfile);
137 my %config = map({split(/\n/, $_=~/\n/?$_:"$_\ntrue", 2)} split(/\x00/, $configfile));
138 $self->{configfilehash} = \%config;
139 return $result;
142 # @_[0]: argument to convert to boolean result (0 or 1)
143 # @_[1]: value to use if argument is undef (default is 0)
144 # Returns 0 or 1
145 sub _boolval {
146 my ($val, $def) = @_;
147 defined($def) or $def = 0;
148 defined($val) or $val = $def;
149 $val =~ s/\s+//gs;
150 $val = lc($val);
151 return 0 if $val eq '' || $val eq 'false' || $val eq 'off' || $val =~ /^0+$/;
152 return 1;
155 sub _property_path {
156 my $self = shift;
157 my ($name) = @_;
158 $self->{path}.'/'.$name;
161 sub _property_fget {
162 my $self = shift;
163 my ($name) = @_;
164 my $pname = $propmap{$name};
165 $pname = $propmapro{$name} unless $pname;
166 $pname or die "unknown property: $name";
167 if ($pname =~ /^([:%])(.*)$/) {
168 my ($where, $pname) = ($1,lc($2));
169 $self->_readlocalconfigfile
170 unless ref($self->{configfilehash}) eq 'HASH';
171 my $val = $where eq ':' ?
172 $self->{configfilehash}->{"gitweb.$pname"} :
173 $self->{configfilehash}->{$pname};
174 defined($val) or $val = '';
175 chomp $val;
176 return $val;
179 open my $p, '<', $self->_property_path($pname) or return undef;
180 my @value = <$p>;
181 close $p;
182 my $value = join('', @value); chomp $value;
183 $value;
186 sub _property_fput {
187 my $self = shift;
188 my ($name, $value) = @_;
189 my $pname = $propmap{$name};
190 $pname or die "unknown property: $name";
191 $value ||= '';
192 if ($pname =~ s/^://) {
193 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
194 return;
195 } elsif ($pname =~ s/^%//) {
196 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', $pname, $value);
197 return;
200 my $P = lock_file($self->_property_path($pname));
201 chomp $value;
202 $value ne '' and print $P "$value\n";
203 close $P;
204 unlock_file($self->_property_path($pname));
207 sub _cleanup_readme {
208 my $self = shift;
209 defined($self->{README}) or $self->{README} = '';
210 $self->{README} =~ s/\r\n?/\n/gs;
211 $self->{README} =~ s/^\s+//s;
212 $self->{README} =~ s/\s+$//s;
213 $self->{README} eq '' or $self->{README} .= "\n";
216 sub _lint_readme {
217 my $self = shift;
218 return 0 unless defined($self->{README}) && $self->{README} ne '';
219 my $test = '<html xmlns="http://www.w3.org/1999/xhtml"><body><div>';
220 $test .= $self->{README};
221 $test .= '</div></body></html>';
222 my ($code, $errors) = capture_command(2, $test, 'xmllint', '--nonet',
223 '--noout', '--nowarning', '-');
224 return 0 unless $code;
225 my $cnt = 0;
226 my @errs = ();
227 for my $line (split(/\n+/, $errors)) {
228 $line = html_esc($line);
229 $line =~ s/ /\&#160;/gs;
230 ++$cnt, $line = 'README'.$1 if $line =~ /^-(:\d+:.*)$/;
231 push @errs, '<tt>' . $line . '</tt>';
233 return ($cnt, join("<br />\n", @errs));
236 sub _properties_load {
237 my $self = shift;
238 foreach my $prop (keys %propmap) {
239 $self->{$prop} = $self->_property_fget($prop);
241 foreach my $prop (keys %propmapro) {
242 $self->{$prop} = $self->_property_fget($prop);
244 $self->_readlocalconfigfile
245 unless ref($self->{configfilehash}) eq 'HASH';
246 $self->{statusupdates} = _boolval($self->{configfilehash}->{'gitweb.statusupdates'}, 1);
247 delete $self->{auth};
248 my $val = $self->{configfilehash}->{'gitweb.repoauth'};
249 defined($val) or $val = '';
250 chomp $val;
251 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
252 my $expire = $3;
253 if (time < $expire) {
254 $self->{authtype} = $1;
255 $self->{auth} = $2;
258 $self->_cleanup_readme;
259 delete $self->{configfilehash};
262 sub _properties_save {
263 my $self = shift;
264 my $fd;
265 foreach my $prop (keys %propmap) {
266 $self->_property_fput($prop, $self->{$prop});
268 $self->{statusupdates} = 1
269 unless defined($self->{statusupdates}) && $self->{statusupdates} =~ /^\d+$/;
270 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool',
271 "gitweb.statusupdates", $self->{statusupdates});
272 if (defined($self->{origurl}) && defined($self->{url}) &&
273 $self->{origurl} ne $self->{url} && -e $self->_property_path(".banged")) {
274 if (open($fd, '>', $self->_property_path(".bangagain"))) {
275 close $fd;
276 chmod(0664, $self->_property_path(".bangagain"));
281 sub _nofetch_path {
282 my $self = shift;
283 $self->_property_path('.nofetch');
286 sub _nofetch {
287 my $self = shift;
288 my ($nofetch) = @_;
289 my $nf = $self->_nofetch_path;
290 if ($nofetch) {
291 open my $x, '>', $nf or die "nofetch failed: $!";
292 close $x;
293 } else {
294 unlink $nf or die "yesfetch failed: $!";
298 sub _gcp_path {
299 my $self = shift;
300 $self->_property_path('.gc_in_progress');
303 sub _clonelog_path {
304 my $self = shift;
305 $self->_property_path('.clonelog');
308 sub _clonefail_path {
309 my $self = shift;
310 $self->_property_path('.clone_failed');
313 sub _clonep_path {
314 my $self = shift;
315 $self->_property_path('.clone_in_progress');
318 sub _clonep {
319 my $self = shift;
320 my ($nofetch) = @_;
321 my $np = $self->_clonep_path;
322 if ($nofetch) {
323 open my $x, '>', $np or die "clonep failed: $!";
324 close $x;
325 } else {
326 unlink $np or die "clonef failed: $!";
329 sub _alternates_setup {
330 my $self = shift;
331 return unless $self->{name} =~ m#/#;
332 my $forkee_name = get_forkee_name($self->{name});
333 my $forkee_path = get_forkee_path($self->{name});
334 return unless -d $forkee_path;
335 mkdir $self->{path}.'/refs'; chmod 02775, $self->{path}.'/refs';
336 mkdir $self->{path}.'/objects'; chmod 02775, $self->{path}.'/objects';
337 mkdir $self->{path}.'/objects/info'; chmod 02775, $self->{path}.'/objects/info';
339 # We set up both alternates and http_alternates since we cannot use
340 # relative path in alternates - that doesn't work recursively.
342 my $filename = $self->{path}.'/objects/info/alternates';
343 open my $x, '>', $filename or die "alternates failed: $!";
344 print $x "$forkee_path/objects\n";
345 close $x;
346 chmod 0664, $filename or warn "cannot chmod $filename: $!";
348 if ($Girocco::Config::httppullurl) {
349 $filename = $self->{path}.'/objects/info/http-alternates';
350 open my $x, '>', $filename or die "http-alternates failed: $!";
351 my $upfork = $forkee_name;
352 do { print $x "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
353 close $x;
354 chmod 0664, $filename or warn "cannot chmod $filename: $!";
357 # copy lastactivity from the parent project
358 if (open $x, '<', $forkee_path.'/info/lastactivity') {{
359 my $activity = <$x>;
360 close $x;
361 last unless $activity;
362 open $x, '>', $self->{path}.'/info/lastactivity' or last;
363 print $x $activity;
364 close $x;
365 chomp $activity;
366 $self->{'lastactivity'} = $activity;
369 # copy refs from parent project
370 local *SAVEOUT;
371 open(SAVEOUT, ">&STDOUT") || die "couldn't dup STDOUT: $!";
372 my $result = open(STDOUT, '>', "$self->{path}/packed-refs");
373 my $resultstr = $!;
374 if (!$result) {
375 open(STDOUT, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
376 close(SAVEOUT) or die "couldn't close SAVEOUT: $!";
377 die "could not write fork's packed-refs file: $resultstr";
379 system($Girocco::Config::git_bin, "--git-dir=$forkee_path", 'for-each-ref', '--format=%(objectname) %(refname)');
380 $result = close(STDOUT);
381 $resultstr = $!;
382 open(STDOUT, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
383 close(SAVEOUT) or die "couldn't close SAVEOUT: $!";
384 $result or die "could not close fork's packed-refs file: $resultstr";
385 unlink("$self->{path}/.delaygc") if -s "$self->{path}/packed-refs";
387 # initialize HEAD
388 my $HEAD = get_git("--git-dir=$forkee_path", 'symbolic-ref', 'HEAD');
389 defined($HEAD) && $HEAD =~ m,^refs/heads/., or $HEAD = 'refs/heads/master';
390 chomp $HEAD;
391 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', $HEAD);
392 chmod 0664, "$self->{path}/packed-refs", "$self->{path}/HEAD";
395 sub _set_changed {
396 my $self = shift;
397 my $fd;
398 open $fd, '>', "$self->{path}/htmlcache/changed" and close $fd;
401 sub _set_forkchange {
402 my $self = shift;
403 my $changedtoo = shift;
404 return unless $self->{name} =~ m#/#;
405 my $forkee_path = get_forkee_path($self->{name});
406 return unless -d $forkee_path;
407 # mark forkee as changed
408 my $fd;
409 open $fd, '>', $forkee_path.'/htmlcache/changed' and close $fd if $changedtoo;
410 open $fd, '>', $forkee_path.'/htmlcache/forkchange' and close $fd;
411 return if -e $forkee_path.'/htmlcache/summary.forkchange';
412 open $fd, '>', $forkee_path.'/htmlcache/summary.forkchange' and close $fd;
415 sub _ctags_setup {
416 my $self = shift;
417 my $perms = $Girocco::Config::permission_control eq 'Hooks' ? 02777 : 02775;
418 mkdir $self->{path}.'/ctags'; chmod $perms, $self->{path}.'/ctags';
421 sub _group_add {
422 my $self = shift;
423 my ($xtra) = @_;
424 $xtra .= join(',', @{$self->{users}});
425 filedb_atomic_append(jailed_file('/etc/group'),
426 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
429 sub _group_update {
430 my $self = shift;
431 my $xtra = join(',', @{$self->{users}});
432 filedb_atomic_edit(jailed_file('/etc/group'),
433 sub {
434 $_ = $_[0];
435 chomp;
436 if ($self->{name} eq (split /:/)[0]) {
437 # preserve readonly flag
438 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
439 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
440 } else {
441 return "$_\n";
447 sub _group_remove {
448 my $self = shift;
449 filedb_atomic_edit(jailed_file('/etc/group'),
450 sub {
451 $self->{name} ne (split /:/)[0] and return $_;
456 sub _hook_path {
457 my $self = shift;
458 my ($name) = @_;
459 $self->{path}.'/hooks/'.$name;
462 sub _hook_install {
463 my $self = shift;
464 my ($name) = @_;
465 my $hooksdir = $self->{path}.'/hooks';
466 my $oldmask = umask();
467 umask($oldmask & ~0070);
468 -d $hooksdir or mkdir $hooksdir or
469 die "hooks directory does not exist and unable to create it for project " . $self->{name} . ": $!";
470 umask($oldmask);
471 open my $src, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
472 open my $dst, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
473 while (<$src>) { print $dst $_; }
474 close $dst;
475 close $src;
476 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
479 sub _hooks_install {
480 my $self = shift;
481 foreach my $hook ('pre-receive', 'post-receive', 'update') {
482 $self->_hook_install($hook);
486 # private constructor, do not use
487 sub _new {
488 my $class = shift;
489 my ($name, $base_path, $path) = @_;
490 does_exist($name,1) || valid_name($name) or die "refusing to create project with invalid name ($name)!";
491 $path ||= "$base_path/$name.git";
492 my $proj = { name => $name, base_path => $base_path, path => $path };
494 bless $proj, $class;
497 # public constructor #0
498 # creates a virtual project not connected to disk image
499 # you can conjure() it later to disk
500 sub ghost {
501 my $class = shift;
502 my ($name, $mirror) = @_;
503 my $self = $class->_new($name, $Girocco::Config::reporoot);
504 $self->{users} = [];
505 $self->{mirror} = $mirror;
506 $self->{email} = $self->{orig_email} = '';
507 $self;
510 # public constructor #1
511 sub load {
512 my $class = shift;
513 my $name = shift || '';
515 open my $fd, '<', jailed_file("/etc/group") or die "project load failed: $!";
516 my $r = qr/^\Q$name\E:/;
517 foreach (grep /$r/, <$fd>) {
518 chomp;
520 my $self = $class->_new($name, $Girocco::Config::reporoot);
521 (-d $self->{path} && $self->_readlocalconfigfile(1))
522 or die "invalid path (".$self->{path}.") for project ".$self->{name};
524 my $ulist;
525 (undef, $self->{crypt}, $self->{gid}, $ulist) = split /:/;
526 $ulist ||= '';
527 $self->{users} = [split /,/, $ulist];
528 $self->{HEAD} = $self->get_HEAD;
529 $self->{orig_HEAD} = $self->{HEAD};
530 $self->{orig_users} = [@{$self->{users}}];
531 $self->{mirror} = ! -e $self->_nofetch_path;
532 $self->{gc_in_progress} = -e $self->_gcp_path;
533 $self->{clone_in_progress} = -e $self->_clonep_path;
534 $self->{clone_logged} = -e $self->_clonelog_path;
535 $self->{clone_failed} = -e $self->_clonefail_path;
536 $self->{ccrypt} = $self->{crypt};
538 $self->_properties_load;
539 $self->{orig_email} = $self->{email};
540 $self->{loaded} = 1; # indicates self was loaded from etc/group file
541 close $fd;
542 return $self;
544 close $fd;
545 undef;
548 # $proj may not be in sane state if this returns false!
549 # fields listed in %$metadata_fields that are NOT also
550 # in @Girocco::Config::project_fields are totally ignored!
551 sub cgi_fill {
552 my $self = shift;
553 my ($gcgi) = @_;
554 my $cgi = $gcgi->cgi;
555 my %allowedfields = map({$_ => 1} @Girocco::Config::project_fields);
556 my $field_enabled = sub {
557 !exists($metadata_fields->{$_[0]}) || exists($allowedfields{$_[0]})};
559 my $pwd = $cgi->param('pwd');
560 my $pwd2 = $cgi->param('pwd2');
561 # in case passwords are disabled
562 defined($pwd) or $pwd = ''; defined($pwd2) or $pwd = '';
563 if ($Girocco::Config::project_passwords and not $self->{crypt} and $pwd eq '' and $pwd2 eq '') {
564 $gcgi->err("Empty passwords are not permitted.");
566 if ($pwd ne '' or not $self->{crypt}) {
567 $self->{crypt} = scrypt_sha1($pwd);
569 if (($pwd ne '' || $pwd2 ne '') and $pwd ne $pwd2) {
570 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
573 $self->{cpwd} = $cgi->param('cpwd');
575 my ($forkee,$project) = ($self->{name} =~ m#^(.*/)?([^/]+)$#);
576 my $newtype = $forkee ? 'fork' : 'project';
577 length($project) <= 64
578 or $gcgi->err("The $newtype name is longer than 64 characters. Do you really need that much?");
580 if ($Girocco::Config::project_owners eq 'email') {
581 $self->{email} = $gcgi->wparam('email');
582 valid_email($self->{email})
583 or $gcgi->err("Your email sure looks weird...?");
584 length($self->{email}) <= 96
585 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
588 # No setting the url unless we're either new or an existing mirror!
589 $self->{url} = $gcgi->wparam('url') unless $self->{loaded} && !$self->{mirror};
590 # Always validate the url if we're an existing mirror
591 if ((defined($self->{url}) && $self->{url} ne '') || ($self->{loaded} && $self->{mirror})) {
592 valid_repo_url($self->{url})
593 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocols are supported. If the URL contains funny characters, contact me.");
594 if ($Girocco::Config::restrict_mirror_hosts) {
595 my $mh = extract_url_hostname($self->{url});
596 is_dns_hostname($mh)
597 or $gcgi->err("Invalid URL. Note that only DNS names are allowed, not IP addresses.");
598 !is_our_hostname($mh)
599 or $gcgi->err("Invalid URL. Mirrors from this host are not allowed, please create a fork instead.");
603 if ($field_enabled->('desc')) {
604 $self->{desc} = to_utf8($gcgi->wparam('desc'), 1);
605 length($self->{desc}) <= 1024
606 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
609 if ($field_enabled->('README')) {
610 $self->{README} = to_utf8($gcgi->wparam('README'), 1);
611 $self->_cleanup_readme;
612 length($self->{README}) <= 8192
613 or $gcgi->err("README length &gt; 8kb!");
614 my ($cnt, $err) = (0);
615 ($cnt, $err) = $self->_lint_readme if $gcgi->ok && $Girocco::Config::xmllint_readme;
616 $gcgi->err($err), $gcgi->{err} += $cnt-1 if $cnt;
619 if ($field_enabled->('hp')) {
620 $self->{hp} = $gcgi->wparam('hp');
621 if ($self->{hp}) {
622 valid_web_url($self->{hp})
623 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
627 # No mucking about with users unless we're a push project
628 if (($self->{loaded} && !$self->{mirror}) ||
629 (!$self->{loaded} && (!defined($self->{url}) || $self->{url} eq ''))) {
630 my %users = ();
631 my @users = ();
632 foreach my $user ($cgi->multi_param('user')) {
633 if (!exists($users{$user})) {
634 $users{$user} = 1;
635 push(@users, $user) if Girocco::User::does_exist($user, 1);
638 $self->{users} = \@users;
641 # HEAD can only be set with editproj, NOT regproj (regproj sets it automatically)
642 # It may ALWAYS be set to what it was (even if there's no such refs/heads/...)
643 my $newhead;
644 if (defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '' &&
645 defined($newhead = $cgi->param('HEAD')) && $newhead ne '') {
646 if ($newhead eq $self->{orig_HEAD} ||
647 get_git("--git-dir=$self->{path}", 'rev-parse', '--verify', '--quiet', 'refs/heads/'.$newhead)) {
648 $self->{HEAD} = $newhead;
649 } else {
650 $gcgi->err("Invalid default branch (no such ref)");
654 # schedule deletion of tags (will be committed by update() after auth)
655 $self->{tags_to_delete} = [$cgi->multi_param('tags')];
657 if ($field_enabled->('notifymail')) {
658 $self->{notifymail} = $gcgi->wparam('notifymail');
659 if ($self->{notifymail}) {
660 (valid_email_multi($self->{notifymail}) and length($self->{notifymail}) <= 512)
661 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
665 if ($field_enabled->('notifytag')) {
666 $self->{notifytag} = $gcgi->wparam('notifytag');
667 if ($self->{notifytag}) {
668 (valid_email_multi($self->{notifytag}) and length($self->{notifytag}) <= 512)
669 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
673 if ($field_enabled->('notifyjson')) {
674 $self->{notifyjson} = $gcgi->wparam('notifyjson');
675 if ($self->{notifyjson}) {
676 valid_web_url($self->{notifyjson})
677 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
681 if ($field_enabled->('notifycia')) {
682 $self->{notifycia} = $gcgi->wparam('notifycia');
683 if ($self->{notifycia}) {
684 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
685 or $gcgi->err("Overly suspicious CIA notify project name. If it's actually valid, don't contact me, CIA is defunct.");
689 if ($cgi->param('setstatusupdates')) {
690 my $val = $gcgi->wparam('statusupdates') || '0';
691 $self->{statusupdates} = $val ? 1 : 0;
694 not $gcgi->err_check;
697 sub form_defaults {
698 my $self = shift;
700 name => $self->{name},
701 email => $self->{email},
702 url => $self->{url},
703 desc => html_esc($self->{desc}),
704 README => html_esc($self->{README}),
705 hp => $self->{hp},
706 users => $self->{users},
707 notifymail => html_esc($self->{notifymail}),
708 notifytag => html_esc($self->{notifytag}),
709 notifyjson => html_esc($self->{notifyjson}),
710 notifycia => html_esc($self->{notifycia}),
714 # return true if $enc_passwd is a match for $plain_passwd
715 my $_check_passwd_match = sub {
716 my $enc_passwd = shift;
717 my $plain_passwd = shift;
718 defined($enc_passwd) or $enc_passwd = '';
719 defined($plain_passwd) or $plain_passwd = '';
720 # $enc_passwd may be crypt or crypt_sha1
721 if ($enc_passwd =~ m(^\$sha1\$(\d+)\$([./0-9A-Za-z]{1,64})\$[./0-9A-Za-z]{28}$)) {
722 # It's using sha1-crypt
723 return $enc_passwd eq crypt_sha1($plain_passwd, $2, -(0+$1));
724 } else {
725 # It's using crypt
726 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
730 sub authenticate {
731 my $self = shift;
732 my ($gcgi) = @_;
734 $self->{ccrypt} or die "Can't authenticate against a project with no password";
735 defined($self->{cpwd}) or $self->{cpwd} = '';
736 unless ($_check_passwd_match->($self->{ccrypt}, $self->{cpwd})) {
737 $gcgi->err("Your admin password does not match!");
738 return 0;
740 return 1;
743 # return true if the password from the file is empty or consists of all the same
744 # character. However, if the project was NOT loaded from the group file
745 # (!self->{loaded}) then the password is never locked.
746 # This function does NOT check $Girocco::Config::project_passwords, the caller
747 # is responsible for doing so if desired. Same for $self->{email}.
748 sub is_password_locked {
749 my $self = shift;
751 $self->{loaded} or return 0;
752 my $testcrypt = $self->{ccrypt}; # The value from the group file
753 defined($testcrypt) or $testcrypt = '';
754 $testcrypt ne '' or return 1; # No password at all
755 $testcrypt =~ /^(.)\1*$/ and return 1; # Bogus crypt value
756 return 0; # Not locked
759 sub _setup {
760 use POSIX qw(strftime);
761 my $self = shift;
762 my ($pushers) = @_;
763 my $fd;
765 $self->_mkdir_forkees;
767 my $gid;
768 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
769 if ($Girocco::Config::owning_group) {
770 $gid = scalar(getgrnam($Girocco::Config::owning_group));
771 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
772 chmod(02775, $self->{path}) or die "chmod 02775 $self->{path} failed: $!";
773 } else {
774 chmod(02777, $self->{path}) or die "chmod 02777 $self->{path} failed: $!";
776 $ENV{'GIT_TEMPLATE_DIR'} = $Girocco::Config::chroot.'/var/empty';
777 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--quiet', '--bare', '--shared='.$self->shared_mode()) == 0
778 or die "git init $self->{path} failed: $?";
779 # we don't need these two, remove them (they will normally be created empty) if they exist
780 rmdir $self->{path}."/branches";
781 rmdir $self->{path}."/remotes";
782 -d $self->{path}."/info" or mkdir $self->{path}."/info"
783 or die "info directory does not exist and unable to create it: $!";
784 -d $self->{path}."/hooks" or mkdir $self->{path}."/hooks"
785 or die "hooks directory does not exist and unable to create it: $!";
786 # clean out any kruft that may have come in from the initial template directory
787 foreach my $cleandir (qw(hooks info)) {
788 if (opendir my $hooksdir, $self->{path}.'/'.$cleandir) {
789 unlink map "$self->{path}/$_", grep { $_ ne '.' && $_ ne '..' } readdir $hooksdir;
790 closedir $hooksdir;
793 if ($Girocco::Config::owning_group) {
794 chown(-1, $gid, $self->{path}."/hooks") or die "chgrp $gid $self->{path}/hooks failed: $!";
796 # hooks never world writable
797 chmod 02775, $self->{path}."/hooks" or die "chmod 02775 $self->{path}/hooks failed: $!";
798 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.logallrefupdates', 'false') == 0
799 or die "disabling core.logallrefupdates failed: $?";
800 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'transfer.fsckObjects', 'true') == 0
801 or die "enabling transfer.fsckObjects failed: $?";
802 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
803 or die "disabling receive.denyNonFastforwards failed: $?";
804 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
805 or die "disabling gc.auto failed: $?";
806 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.autogc', '0') == 0
807 or die "disabling receive.autogc failed: $?";
808 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
809 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.updateserverinfo', 'true') == 0
810 or die "enabling receive.updateserverinfo failed: $?";
811 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'repack.writebitmaps', 'true') == 0
812 or die "enabling repack.writebitmaps failed: $?";
813 $self->{creationtime} = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
814 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'girocco.creationtime', $self->{creationtime}) == 0
815 or die "setting girocco.creationtime failed: $?";
816 -d $self->{path}."/htmlcache" or mkdir $self->{path}."/htmlcache"
817 or die "htmlcache directory does not exist and unable to create it: $!";
818 -d $self->{path}."/bundles" or mkdir $self->{path}."/bundles"
819 or die "bundles directory does not exist and unable to create it: $!";
820 foreach my $file (qw(info/lastactivity .delaygc)) {
821 if (open $fd, '>', $self->{path}."/".$file) {
822 close $fd;
823 chmod 0664, $self->{path}."/".$file;
827 # /info must have right permissions,
828 # and git init didn't do it for some reason.
829 # config must have correct permissions.
830 # and Git 2.1.0 - 2.2.1 incorrectly add +x for some reason.
831 # also make sure /refs, /objects and /htmlcache are correct too.
832 my ($dmode, $dmodestr, $fmode, $fmodestr);
833 if ($Girocco::Config::owning_group) {
834 ($dmode, $dmodestr) = (02775, '02775');
835 ($fmode, $fmodestr) = (0664, '0664');
836 } else {
837 ($dmode, $dmodestr) = (02777, '02777');
838 ($fmode, $fmodestr) = (0666, '0666');
840 foreach my $dir (qw(info refs objects htmlcache bundles)) {
841 chmod($dmode, $self->{path}."/$dir") or die "chmod $dmodestr $self->{path}/$dir failed: $!";
843 foreach my $file (qw(config)) {
844 chmod($fmode, $self->{path}."/$file") or die "chmod $fmodestr $self->{path}/$file failed: $!";
846 # these ones are probably not strictly required but are nice to have
847 foreach my $dir (qw(refs/heads refs/tags objects/info objects/pack)) {
848 -d $self->{path}."/$dir" or mkdir $self->{path}."/$dir";
849 chmod($dmode, $self->{path}."/$dir");
852 $self->_properties_save;
853 $self->_alternates_setup;
854 $self->_ctags_setup;
855 $self->_group_remove;
856 $self->_group_add($pushers);
857 $self->_hooks_install;
858 $self->_update_index;
859 $self->_set_changed;
860 $self->_set_forkchange(1);
863 sub premirror {
864 my $self = shift;
866 $self->_setup(':');
867 $self->_clonep(1);
868 $self->perm_initialize;
871 sub conjure {
872 my $self = shift;
874 $self->_setup;
875 $self->_nofetch(1);
876 if ($Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
877 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
878 or die "create-personal-mob-area $self->{name} failed";
880 $self->perm_initialize;
883 sub clone {
884 my $self = shift;
886 unlink ($self->_clonefail_path()); # Ignore EEXIST error
887 unlink ($self->_clonelog_path()); # Ignore EEXIST error
889 use IO::Socket;
890 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
891 select((select($sock),$|=1)[0]);
892 $sock->print("clone ".$self->{name}."\n");
893 # Just ignore reply, we are going to succeed anyway and the I/O
894 # would apparently get quite hairy.
895 $sock->flush();
896 sleep 2; # *cough*
897 $sock->close();
900 sub update {
901 my $self = shift;
903 $self->_properties_save;
904 $self->_group_update;
906 if (exists($self->{tags_to_delete})) {
907 $self->delete_ctag($_) foreach(@{$self->{tags_to_delete}});
910 $self->set_HEAD($self->{HEAD}) unless $self->{orig_HEAD} eq $self->{HEAD};
912 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
913 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
914 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
915 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
917 $self->_update_index if $self->{email} ne $self->{orig_email};
918 $self->{orig_email} = $self->{email};
919 $self->_set_changed;
924 sub update_password {
925 my $self = shift;
926 my ($pwd) = @_;
928 $self->{crypt} = scrypt_sha1($pwd);
929 $self->_group_update;
932 # You can explicitly do this just on a ghost() repository too.
933 sub delete {
934 my $self = shift;
936 if (-d $self->{path}) {
937 system('rm', '-rf', $self->{path}) == 0
938 or die "rm -rf $self->{path} failed: $?";
940 # attempt to clean up any empty fork directories by removing them
941 my @pelems = split('/', $self->{name});
942 while (@pelems > 1) {
943 pop @pelems;
944 # okay to fail
945 rmdir join('/', $Girocco::Config::reporoot, @pelems) or last;
947 $self->_group_remove;
948 $self->_update_index;
949 $self->_set_forkchange(1);
954 sub _contains_files {
955 my $dir = shift;
956 (-d $dir) or return 0;
957 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
958 while (my $entry = readdir($dh)) {
959 next if $entry eq '' || $entry eq '.' || $entry eq '..';
960 closedir($dh), return 1
961 if -f "$dir/$entry" ||
962 -d "$dir/$entry" && _contains_files("$dir/$entry");
964 closedir($dh);
965 return 0;
968 sub has_forks {
969 my $self = shift;
971 return _contains_files($Girocco::Config::reporoot.'/'.$self->{name});
974 # Returns an array of 0 or more array refs, one for each bundle:
975 # ->[0]: bundle creation time (seconds since epoch)
976 # ->[1]: bundle name (e.g. foo-xxxxxxxx.bundle)
977 # ->[2]: bundle size in bytes
978 sub bundles {
979 my $self = shift;
980 use Time::Local;
982 return @{$self->{bundles}} if ref($self->{bundles}) eq 'ARRAY';
983 my @blist = ();
984 if (-d $self->{path}.'/bundles') {{
985 my $prefix = $self->{name};
986 $prefix =~ s|^.*[^/]/([^/]+)$|$1|;
987 opendir(my $dh, $self->{path}.'/bundles') or last;
988 while (my $bfile = readdir($dh)) {
989 next unless $bfile =~ /^\d{8}_\d{6}-[0-9a-f]{8}$/;
990 my $ctime = eval {timegm(
991 0+substr($bfile,13,2), 0+substr($bfile,11,2), 0+substr($bfile,9,2),
992 0+substr($bfile,6,2), 0+substr($bfile,4,2)-1, 0+substr($bfile,0,4)-1900)};
993 next unless $ctime;
994 open(my $bh, '<', $self->{path}.'/bundles/'.$bfile) or next;
995 my $f1 = <$bh>;
996 my $f2 = <$bh>;
997 $f1 = $self->{path}.'/objects/pack/'.$f1 if $f1 && $f1 !~ m|^/|;
998 $f2 = $self->{path}.'/objects/pack/'.$f2 if $f2 && $f2 !~ m|^/|;
999 close($bh);
1000 next unless $f1 && $f2 && $f1 ne $f2;
1001 chomp $f1;
1002 chomp $f2;
1003 next unless -e $f1 && -e $f2;
1004 my $s1 = -s $f1 || 0;
1005 my $s2 = -s $f2 || 0;
1006 next unless $s1 || $s2;
1007 push(@blist, [$ctime, "$prefix-".substr($bfile,16,8).".bundle", $s1+$s2]);
1009 closedir($dh);
1011 @blist = sort({$b->[0] <=> $a->[0]} @blist);
1012 my %seen = ();
1013 my @result = ();
1014 foreach my $bndl (@blist) {
1015 next if $seen{$bndl->[1]};
1016 $seen{$bndl->[1]} = 1;
1017 push(@result, $bndl);
1019 $self->{bundles} = \@result;
1020 return @result;
1023 sub has_alternates {
1024 my $self = shift;
1025 return -s $self->{path}.'/objects/info/alternates' ? 1 : 0;
1028 sub has_bundle {
1029 my $self = shift;
1031 return scalar($self->bundles);
1034 # returns true if any of the notify fields are non-empty
1035 sub has_notify {
1036 my $self = shift;
1037 # We do not ckeck notifycia since it's defunct
1038 return $self->{'notifymail'} || $self->{'notifytag'} || $self->{'notifyjson'};
1041 sub is_empty {
1042 # A project is considered empty if the git repository does not
1043 # have any refs. This means packed-refs does not exist or is
1044 # empty or only has lines starting with '#' AND there are no
1045 # files in the refs subdirectory hierarchy (no matter how deep).
1047 my $self = shift;
1049 (-d $self->{path}) or return 0;
1050 if (-e $self->{path}.'/packed-refs') {
1051 open(my $pr, '<', $self->{path}.'/packed-refs')
1052 or die "open $self->{path}./packed-refs failed: $!";
1053 my $foundref = 0;
1054 while (my $ref = <$pr>) {
1055 next if $ref =~ /^#/;
1056 $foundref = 1;
1057 last;
1059 close($pr);
1060 return 0 if $foundref;
1062 (-d $self->{path}.'/refs') or return 1;
1063 return !_contains_files($self->{path}.'/refs');
1066 # returns array:
1067 # [0]: earliest possible scheduled next gc, undef if lastgc not set
1068 # [1]: approx. latest possible scheduled next gc, undef if lastgc not set
1069 # Both values (if not undef) are seconds since epoch
1070 # Result only considers lastgc and min_gc_interval nothing else
1071 sub next_gc {
1072 my $self = shift;
1073 my $lastgcepoch = parse_any_date($self->{lastgc});
1074 return (undef, undef) unless defined $lastgcepoch;
1075 return ($lastgcepoch + $Girocco::Config::min_gc_interval,
1076 int($lastgcepoch + 1.25 * $Girocco::Config::min_gc_interval +
1077 $Girocco::Config::min_mirror_interval));
1080 # returns boolean (0 or 1)
1081 # Attempts to determine whether or not a gc (and corresponding build
1082 # of a .bitmap/.bndl file) will actually take place at the next_gc
1083 # time (as returned by next_gc). Whether or not a .bitmap and .bndl
1084 # end up being built depends on whether or not the local object graph
1085 # is complete. In general if has_alternates is true then a .bitmap/.bndl
1086 # is not possible because the object graph will be incomplete,
1087 # but it *is* possible that even though the repository has_alternates,
1088 # it does not actually borrow any objects so a .bitmap/.bndl will build
1089 # in spite of the presence of alternates -- but this is expected to be rare.
1090 # The result is a best guess and false return value is not an absolute
1091 # guarantee that gc will not take place at the next interval, but it probably
1092 # will not if nothing changes in the meantime.
1093 sub needs_gc {
1094 my $self = shift;
1095 my $lgc = parse_any_date($self->{lastgc});
1096 my $lrecv = parse_any_date($self->{lastreceive});
1097 my $lpgc = parse_any_date($self->{lastparentgc});
1098 return 1 unless defined($lgc) && defined($lrecv);
1099 if ($self->has_alternates) {
1100 return 1 unless defined($lpgc);
1101 return 1 unless $lpgc < $lgc;
1103 return 1 unless $lrecv < $lgc;
1104 # We don't try running any "is_dirty" check, so if somehow the
1105 # repository became dirty without updating lastreceive we might
1106 # incorrectly return false instead of true.
1107 return 0;
1110 sub delete_ctag {
1111 my $self = shift;
1112 my ($ctag) = @_;
1114 # sanity check, disallow filenames starting with . .. or /
1115 unlink($self->{path}.'/ctags/'.$ctag) if($ctag !~ m|^(\.\.?)/|);
1118 sub get_ctag_names {
1119 my $self = shift;
1120 my @ctags = ();
1121 opendir(my $dh, $self->{path}.'/ctags')
1122 or return @ctags;
1123 @ctags = grep { -f "$self->{path}/ctags/$_" } readdir($dh);
1124 closedir($dh);
1125 return sort({lc($a) cmp lc($b)} @ctags);
1128 sub get_heads {
1129 my $self = shift;
1130 my $fh;
1131 my $heads = get_git("--git-dir=$self->{path}", 'for-each-ref', '--format=%(objectname) %(refname)', 'refs/heads');
1132 defined($heads) or $heads = '';
1133 chomp $heads;
1134 my @res = ();
1135 foreach (split(/\n/, $heads)) {
1136 chomp;
1137 next if !m#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
1138 push @res, $1;
1140 push(@res, $self->{orig_HEAD}) if !@res && defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '';
1141 @res;
1144 sub get_HEAD {
1145 my $self = shift;
1146 my $HEAD = get_git("--git-dir=$self->{path}", 'symbolic-ref', 'HEAD');
1147 defined($HEAD) or $HEAD = '';
1148 chomp $HEAD;
1149 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
1150 return $1;
1153 sub set_HEAD {
1154 my $self = shift;
1155 my $newHEAD = shift;
1156 # Cursory checks only -- if you want to break your HEAD, be my guest
1157 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
1158 die "grossly invalid new HEAD: $newHEAD";
1160 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', "refs/heads/$newHEAD");
1161 die "could not set HEAD" if ($? >> 8);
1162 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob'
1163 or system('cp', '-p', '-f', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
1166 sub gen_auth {
1167 my $self = shift;
1168 my ($type) = @_;
1169 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
1171 $self->{authtype} = $type;
1173 no warnings;
1174 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
1176 my $expire = time + 24 * 3600;
1177 my $propval = "# ${type}AUTH $self->{auth} $expire";
1178 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gitweb.repoauth', $propval);
1179 $self->{auth};
1182 sub del_auth {
1183 my $self = shift;
1185 delete $self->{auth};
1186 delete $self->{authtype};
1187 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--unset', 'gitweb.repoauth');
1190 sub remove_user {
1191 my $self = shift;
1192 my ($username) = @_;
1194 my $before_count = @{$self->{users}};
1195 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
1196 return @{$self->{users}} != $before_count;
1199 ### static methods
1201 sub get_forkee_name {
1202 local $_ = $_[0];
1203 (m#^(.*)/.*?$#)[0]; #
1206 sub get_forkee_path {
1207 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1208 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
1209 -d $forkee ? $forkee : '';
1212 # Ultimately the full project/fork name could end up being part of a Git ref name
1213 # when a project's forks are combined into one giant repository for efficiency.
1214 # That means that the project/fork name must satisfy the Git ref name requirements:
1216 # 1. Characters with an ASCII value less than or equal to 32 are not allowed
1217 # 2. The character with an ASCII value of 0x7F is not allowed
1218 # 3. The characters '~', '^', ':', '\', '*', '?', and '[' are not allowed
1219 # 4. The character '/' is a separator and is not allowed within a name
1220 # 5. The name may not start with '.' or end with '.'
1221 # 6. The name may not end with '.lock'
1222 # 7. The name may not contain the '..' sequence
1223 # 8. The name may not contain the '@{' sequence
1224 # 9. If multiple components are used (separated by '/'), no empty '' components
1226 # We also prohibit a trailing '.git' on any path component and futher restrict
1227 # the allowed characters to alphanumeric and [+._-] where names must start with
1228 # an alphanumeric.
1230 sub _valid_name_characters {
1231 local $_ = $_[0];
1232 (not m#^[/+._-]#)
1233 and (not m#//#)
1234 and (not m#\.\.#)
1235 and (not m#/[+._-]#)
1236 and (not m#\./#)
1237 and (not m#\.$#)
1238 and (not m#\.git/#i)
1239 and (not m#\.git$#i)
1240 and (not m#\.idx/#i)
1241 and (not m#\.idx$#i)
1242 and (not m#\.lock/#i)
1243 and (not m#\.lock$#i)
1244 and (not m#\.pack/#i)
1245 and (not m#\.pack$#i)
1246 and (not m#\.bundle/#i)
1247 and (not m#\.bundle$#i)
1248 and (not m#/$#)
1249 and (not m/^[a-fA-F0-9]{38}$/)
1250 and m#^[a-zA-Z0-9/+._-]+$#
1251 and !has_reserved_suffix($_, $_[1], $_[2]);
1254 sub valid_name {
1255 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1256 local $_ = $_[0];
1257 _valid_name_characters($_) and not exists($reservedprojectnames{lc($_)})
1258 and @{[m#/#g]} <= 5 # maximum fork depth is 5
1259 and ((not m#/#) or -d get_forkee_path($_)); # will also catch ^/
1262 # It's possible that some forks have been kept but the forkee is gone.
1263 # In this case the standard valid_name check is too strict.
1264 sub does_exist {
1265 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1266 my ($name, $nodie) = @_;
1267 my $okay = (
1268 _valid_name_characters($name, $Girocco::Config::reporoot, ".git")
1269 and ((not $name =~ m#/#)
1270 or -d get_forkee_path($name)
1271 or -d $Girocco::Config::reporoot.'/'.get_forkee_name($name)));
1272 (!$okay && $nodie) and return undef;
1273 !$okay and die "tried to query for project with invalid name $name!";
1274 (-d $Girocco::Config::reporoot."/$name.git");
1277 sub get_full_list {
1278 open my $fd, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
1279 my @projects = map {/^([^:_\s#][^:\s#]*):[^:]*:\d{5,}:/ ? $1 : ()} <$fd>;
1280 close $fd;
1281 @projects;