1 package Girocco
::Project
;
10 use Girocco
::HashUtil
;
11 use Girocco
::ProjPerm
;
13 use base
('Girocco::ProjPerm::'.$Girocco::Config
::permission_control
); # mwahaha
28 require Digest
::SHA
::PurePerl
;
29 Digest
::SHA
::PurePerl
->import(
32 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
33 . "must be available\n";
36 our $metadata_fields = {
37 homepage
=> ['Homepage URL', 'hp', 'text'],
38 shortdesc
=> ['Short description', 'desc', 'text'],
39 README
=> ['<span style="display:inline-block;vertical-align:top">'.
40 'README (HTML, < 8 KiB)<br />leave blank for automatic</span>',
41 'README', 'textarea', 'Enter only “<!-- comments -->” '.
42 'to completely suppress any README'],
43 notifymail
=> ['Commit notify – mail to', 'notifymail', 'text',
44 'comma separated address list'],
45 reverseorder
=> ['Show oldest first', 'reverseorder', 'checkbox',
46 'show new revisions in oldest to newest order (instead of the default newest to oldest older)'.
47 ' in “Commit notify” email when showing new revisions'],
48 summaryonly
=> ['Summaries only', 'summaryonly', 'checkbox',
49 'suppress patch/diff output in “Commit notify” email when showing new revisions'],
50 notifytag
=> ['Tag notify – mail to', 'notifytag', 'text',
51 'comma separated address list – if not empty, tag '.
52 'notifications are sent here INSTEAD of to '.
53 '“Commit notify – mail to” address(es)'],
54 notifyjson
=> ['Commit notify – '.
55 '<a title="'.html_esc
('single field name is “payload”', 1).'" href="'.
56 'https://developer.github.com/v3/activity/events/types/#pushevent'.
57 '">POST JSON</a> at', 'notifyjson', 'text'],
58 notifycia
=> ['Commit notify – <a href="http://cia.vc/doc/">CIA project</a> name',
59 'notifycia', 'text', 'CIA is defunct – this value is ignored'],
64 my @pelems = split('/', $self->{name
});
65 pop @pelems; # do not create dir for the project itself
66 my $path = $self->{base_path
};
67 foreach my $pelem (@pelems) {
69 (-d
"$path") or mkdir $path or die "mkdir $path: $!";
70 chmod 02775, $path; # ok if fails (dir may already exist and be owned by someone else)
74 # With a leading ':' get from project local config replacing ':' with 'gitweb.'
75 # With a leading '%' get from project local config after removing '%'
76 # Otherwise it's a project file name to be loaded
77 # %propmapro entries are loaded but never written
82 desc
=> 'description',
83 README
=> 'README.html',
85 notifymail
=> '%hooks.mailinglist',
86 notifytag
=> '%hooks.announcelist',
87 notifyjson
=> '%hooks.jsonurl',
88 notifycia
=> '%hooks.cianame',
92 lastchange
=> ':lastchange',
93 lastactivity
=> 'info/lastactivity',
95 lastreceive
=> ':lastreceive',
96 lastparentgc
=> ':lastparentgc',
97 lastrefresh
=> ':lastrefresh',
98 creationtime
=> '%girocco.creationtime',
99 reposizek
=> '%girocco.reposizek',
100 origurl
=> ':baseurl',
103 # Projects with any of these names will be disallowed to avoid possible
104 # collisions with cgi script paths or chroot paths
105 # NOTE: names are checked after using lc on them, so all entries MUST be lowercase
106 our %reservedprojectnames = (
107 admin
=> 1, # /admin/ links
108 alternates
=> 1, # .git/objects/info/alternates
109 b
=> 1, # /b/ -> bundle.cgi
110 blog
=> 1, # /blog/ links
111 c
=> 1, # /c/ -> cgit
112 'git-receive-pack' => 1, # smart HTTP
113 'git-upload-archive' => 1, # smart HTTP
114 'git-upload-pack' => 1, # smart HTTP
115 h
=> 1, # /h/ -> html.cgi
116 head
=> 1, # .git/HEAD
117 'http-alternates' => 1, # .git/objects/info/http-alternates
118 info
=> 1, # .git/info
119 objects
=> 1, # .git/objects
120 packs
=> 1, # .git/objects/info/packs
121 r
=> 1, # /r/ -> git http
122 refs
=> 1, # .git/refs
123 w
=> 1, # /w/ -> gitweb
124 wiki
=> 1, # /wiki/ links
125 srv
=> 1, # /srv/git/ -> chroot ssh git repositories
130 system("$Girocco::Config::basedir/gitweb/genindex.sh", $self->{name
});
133 sub _readlocalconfigfile
{
135 my $undefonerr = shift || 0;
136 delete $self->{configfilehash
};
137 my $configfile = get_git
("--git-dir=$self->{path}",
138 'config', '--list', '--local', '--null');
140 defined($configfile) || $undefonerr or $result = 0, $configfile = '';
141 return undef unless defined($configfile);
142 my %config = map({split(/\n/, $_=~/\n/?
$_:"$_\ntrue", 2)} split(/\x00/, $configfile));
143 $self->{configfilehash
} = \
%config;
147 # @_[0]: argument to convert to boolean result (0 or 1)
148 # @_[1]: value to use if argument is undef (default is 0)
151 my ($val, $def) = @_;
152 defined($def) or $def = 0;
153 defined($val) or $val = $def;
156 return 0 if $val eq '' || $val eq 'false' || $val eq 'off' || $val eq 'no' || $val =~ /^0+$/;
163 $self->{path
}.'/'.$name;
169 my $pname = $propmap{$name};
170 $pname = $propmapro{$name} unless $pname;
171 $pname or die "unknown property: $name";
172 if ($pname =~ /^([:%])(.*)$/) {
173 my ($where, $pname) = ($1,lc($2));
174 $self->_readlocalconfigfile
175 unless ref($self->{configfilehash
}) eq 'HASH';
176 my $val = $where eq ':' ?
177 $self->{configfilehash
}->{"gitweb.$pname"} :
178 $self->{configfilehash
}->{$pname};
179 defined($val) or $val = '';
184 open my $p, '<', $self->_property_path($pname) or return undef;
187 my $value = join('', @value); chomp $value;
193 my ($name, $value) = @_;
194 my $pname = $propmap{$name};
195 $pname or die "unknown property: $name";
197 if ($pname =~ s/^://) {
198 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', "gitweb.$pname", $value);
200 } elsif ($pname =~ s/^%//) {
201 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', $pname, $value);
205 my $P = lock_file
($self->_property_path($pname));
207 $value ne '' and print $P "$value\n";
209 unlock_file
($self->_property_path($pname));
212 sub _cleanup_readme
{
214 defined($self->{README
}) or $self->{README
} = '';
215 $self->{README
} =~ s/\r\n?/\n/gs;
216 $self->{README
} =~ s/^\s+//s;
217 $self->{README
} =~ s/\s+$//s;
218 $self->{README
} eq '' or $self->{README
} .= "\n";
223 return 0 unless defined($self->{README
}) && $self->{README
} ne '';
224 my $test = '<html xmlns="http://www.w3.org/1999/xhtml"><body><div>';
225 $test .= $self->{README
};
226 $test .= '</div></body></html>';
227 my ($code, $errors) = capture_command
(2, $test, 'xmllint', '--nonet',
228 '--noout', '--nowarning', '-');
229 return 0 unless $code;
232 for my $line (split(/\n+/, $errors)) {
233 $line = html_esc
($line);
234 $line =~ s/ /\ /gs;
235 ++$cnt, $line = 'README'.$1 if $line =~ /^-(:\d+:.*)$/;
236 push @errs, '<tt>' . $line . '</tt>';
238 return ($cnt, join("<br />\n", @errs));
241 sub _properties_load
{
243 foreach my $prop (keys %propmap) {
244 $self->{$prop} = $self->_property_fget($prop);
246 foreach my $prop (keys %propmapro) {
247 $self->{$prop} = $self->_property_fget($prop);
249 $self->_readlocalconfigfile
250 unless ref($self->{configfilehash
}) eq 'HASH';
251 $self->{statusupdates
} = _boolval
($self->{configfilehash
}->{'gitweb.statusupdates'}, 1);
252 $self->{reverseorder
} = _boolval
($self->{configfilehash
}->{'hooks.reverseorder'}, 0);
253 $self->{summaryonly
} = _boolval
($self->{configfilehash
}->{'hooks.summaryonly'}, 0);
254 delete $self->{auth
};
255 my $val = $self->{configfilehash
}->{'gitweb.repoauth'};
256 defined($val) or $val = '';
258 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
260 if (time < $expire) {
261 $self->{authtype
} = $1;
265 $self->_cleanup_readme;
266 delete $self->{configfilehash
};
269 sub _properties_save
{
272 foreach my $prop (keys %propmap) {
273 $self->_property_fput($prop, $self->{$prop});
275 $self->{statusupdates
} = 1
276 unless defined($self->{statusupdates
}) && $self->{statusupdates
} =~ /^\d+$/;
277 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', '--bool',
278 "gitweb.statusupdates", $self->{statusupdates
});
279 $self->{reverseorder
} = 0
280 unless defined($self->{reverseorder
}) && $self->{reverseorder
} =~ /^\d+$/;
281 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', '--bool',
282 "hooks.reverseorder", $self->{reverseorder
});
283 $self->{summaryonly
} = 0
284 unless defined($self->{summaryonly
}) && $self->{summaryonly
} =~ /^\d+$/;
285 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', '--bool',
286 "hooks.summaryonly", $self->{summaryonly
});
287 if (defined($self->{origurl
}) && defined($self->{url
}) &&
288 $self->{origurl
} ne $self->{url
} && -e
$self->_property_path(".banged")) {
289 if (open($fd, '>', $self->_property_path(".bangagain"))) {
291 chmod(0664, $self->_property_path(".bangagain"));
298 $self->_property_path('.nofetch');
304 my $nf = $self->_nofetch_path;
306 open my $x, '>', $nf or die "nofetch failed: $!";
309 unlink $nf or die "yesfetch failed: $!";
315 $self->_property_path('.gc_in_progress');
320 $self->_property_path('.clonelog');
323 sub _clonefail_path
{
325 $self->_property_path('.clone_failed');
330 $self->_property_path('.clone_in_progress');
336 my $np = $self->_clonep_path;
338 open my $x, '>', $np or die "clonep failed: $!";
341 unlink $np or die "clonef failed: $!";
344 sub _alternates_setup
{
346 return unless $self->{name
} =~ m
#/#;
347 my $forkee_name = get_forkee_name
($self->{name
});
348 my $forkee_path = get_forkee_path
($self->{name
});
349 return unless -d
$forkee_path;
350 mkdir $self->{path
}.'/refs'; chmod 02775, $self->{path
}.'/refs';
351 mkdir $self->{path
}.'/objects'; chmod 02775, $self->{path
}.'/objects';
352 mkdir $self->{path
}.'/objects/info'; chmod 02775, $self->{path
}.'/objects/info';
354 # We set up both alternates and http_alternates since we cannot use
355 # relative path in alternates - that doesn't work recursively.
357 my $filename = $self->{path
}.'/objects/info/alternates';
358 open my $x, '>', $filename or die "alternates failed: $!";
359 print $x "$forkee_path/objects\n";
361 chmod 0664, $filename or warn "cannot chmod $filename: $!";
363 if ($Girocco::Config
::httppullurl
) {
364 $filename = $self->{path
}.'/objects/info/http-alternates';
365 open my $x, '>', $filename or die "http-alternates failed: $!";
366 my $upfork = $forkee_name;
367 do { print $x "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
369 chmod 0664, $filename or warn "cannot chmod $filename: $!";
372 # copy lastactivity from the parent project
373 if (open $x, '<', $forkee_path.'/info/lastactivity') {{
376 last unless $activity;
377 open $x, '>', $self->{path
}.'/info/lastactivity' or last;
381 $self->{'lastactivity'} = $activity;
384 # copy refs from parent project
386 open(SAVEOUT
, ">&STDOUT") || die "couldn't dup STDOUT: $!";
387 my $result = open(STDOUT
, '>', "$self->{path}/packed-refs");
390 open(STDOUT
, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
391 close(SAVEOUT
) or die "couldn't close SAVEOUT: $!";
392 die "could not write fork's packed-refs file: $resultstr";
394 system($Girocco::Config
::git_bin
, "--git-dir=$forkee_path", 'for-each-ref', '--format=%(objectname) %(refname)');
395 $result = close(STDOUT
);
397 open(STDOUT
, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
398 close(SAVEOUT
) or die "couldn't close SAVEOUT: $!";
399 $result or die "could not close fork's packed-refs file: $resultstr";
400 unlink("$self->{path}/.delaygc") if -s
"$self->{path}/packed-refs";
403 my $HEAD = get_git
("--git-dir=$forkee_path", 'symbolic-ref', 'HEAD');
404 defined($HEAD) && $HEAD =~ m
,^refs
/heads
/., or $HEAD = 'refs/heads/master';
406 system($Girocco::Config
::git_bin
, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', $HEAD);
407 chmod 0664, "$self->{path}/packed-refs", "$self->{path}/HEAD";
413 open $fd, '>', "$self->{path}/htmlcache/changed" and close $fd;
416 sub _set_forkchange
{
418 my $changedtoo = shift;
419 return unless $self->{name
} =~ m
#/#;
420 my $forkee_path = get_forkee_path
($self->{name
});
421 return unless -d
$forkee_path;
422 # mark forkee as changed
424 open $fd, '>', $forkee_path.'/htmlcache/changed' and close $fd if $changedtoo;
425 open $fd, '>', $forkee_path.'/htmlcache/forkchange' and close $fd;
426 return if -e
$forkee_path.'/htmlcache/summary.forkchange';
427 open $fd, '>', $forkee_path.'/htmlcache/summary.forkchange' and close $fd;
432 my $perms = $Girocco::Config
::permission_control
eq 'Hooks' ?
02777 : 02775;
433 mkdir $self->{path
}.'/ctags'; chmod $perms, $self->{path
}.'/ctags';
439 $xtra .= join(',', @
{$self->{users
}});
440 filedb_atomic_append
(jailed_file
('/etc/group'),
441 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
446 my $xtra = join(',', @
{$self->{users
}});
447 filedb_atomic_edit
(jailed_file
('/etc/group'),
451 if ($self->{name
} eq (split /:/)[0]) {
452 # preserve readonly flag
453 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
454 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
464 filedb_atomic_edit
(jailed_file
('/etc/group'),
466 $self->{name
} ne (split /:/)[0] and return $_;
474 $self->{path
}.'/hooks/'.$name;
480 my $hooksdir = $self->{path
}.'/hooks';
481 my $oldmask = umask();
482 umask($oldmask & ~0070);
483 -d
$hooksdir or mkdir $hooksdir or
484 die "hooks directory does not exist and unable to create it for project " . $self->{name
} . ": $!";
486 open my $src, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
487 open my $dst, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
488 while (<$src>) { print $dst $_; }
491 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
496 foreach my $hook ('pre-receive', 'post-receive', 'update') {
497 $self->_hook_install($hook);
501 # private constructor, do not use
504 my ($name, $base_path, $path) = @_;
505 does_exist
($name,1) || valid_name
($name) or die "refusing to create project with invalid name ($name)!";
506 $path ||= "$base_path/$name.git";
507 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
512 # public constructor #0
513 # creates a virtual project not connected to disk image
514 # you can conjure() it later to disk
517 my ($name, $mirror) = @_;
518 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
520 $self->{mirror
} = $mirror;
521 $self->{email
} = $self->{orig_email
} = '';
525 # public constructor #1
528 my $name = shift || '';
530 open my $fd, '<', jailed_file
("/etc/group") or die "project load failed: $!";
531 my $r = qr/^\Q$name\E:/;
532 foreach (grep /$r/, <$fd>) {
535 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
536 (-d
$self->{path
} && $self->_readlocalconfigfile(1))
537 or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
540 (undef, $self->{crypt}, $self->{gid
}, $ulist) = split /:/;
542 $self->{users
} = [split /,/, $ulist];
543 $self->{HEAD
} = $self->get_HEAD;
544 $self->{orig_HEAD
} = $self->{HEAD
};
545 $self->{orig_users
} = [@
{$self->{users
}}];
546 $self->{mirror
} = ! -e
$self->_nofetch_path;
547 $self->{gc_in_progress
} = -e
$self->_gcp_path;
548 $self->{clone_in_progress
} = -e
$self->_clonep_path;
549 $self->{clone_logged
} = -e
$self->_clonelog_path;
550 $self->{clone_failed
} = -e
$self->_clonefail_path;
551 $self->{ccrypt
} = $self->{crypt};
553 $self->_properties_load;
554 $self->{orig_email
} = $self->{email
};
555 $self->{loaded
} = 1; # indicates self was loaded from etc/group file
563 # $proj may not be in sane state if this returns false!
564 # fields listed in %$metadata_fields that are NOT also
565 # in @Girocco::Config::project_fields are totally ignored!
569 my $cgi = $gcgi->cgi;
570 my %allowedfields = map({$_ => 1} @Girocco::Config
::project_fields
);
571 my $field_enabled = sub {
572 !exists($metadata_fields->{$_[0]}) || exists($allowedfields{$_[0]})};
574 my $pwd = $cgi->param('pwd');
575 my $pwd2 = $cgi->param('pwd2');
576 # in case passwords are disabled
577 defined($pwd) or $pwd = ''; defined($pwd2) or $pwd = '';
578 if ($Girocco::Config
::project_passwords
and not $self->{crypt} and $pwd eq '' and $pwd2 eq '') {
579 $gcgi->err("Empty passwords are not permitted.");
581 if ($pwd ne '' or not $self->{crypt}) {
582 $self->{crypt} = scrypt_sha1
($pwd);
584 if (($pwd ne '' || $pwd2 ne '') and $pwd ne $pwd2) {
585 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
588 $self->{cpwd
} = $cgi->param('cpwd');
590 my ($forkee,$project) = ($self->{name
} =~ m
#^(.*/)?([^/]+)$#);
591 my $newtype = $forkee ?
'fork' : 'project';
592 length($project) <= 64
593 or $gcgi->err("The $newtype name is longer than 64 characters. Do you really need that much?");
595 if ($Girocco::Config
::project_owners
eq 'email') {
596 $self->{email
} = $gcgi->wparam('email');
597 valid_email
($self->{email
})
598 or $gcgi->err("Your email sure looks weird...?");
599 length($self->{email
}) <= 96
600 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
603 # No setting the url unless we're either new or an existing mirror!
604 $self->{url
} = $gcgi->wparam('url') unless $self->{loaded
} && !$self->{mirror
};
605 # Always validate the url if we're an existing mirror
606 if ((defined($self->{url
}) && $self->{url
} ne '') || ($self->{loaded
} && $self->{mirror
})) {{
607 # Always allow the url to be left unchanged without validation when editing
608 last if $self->{loaded
} && defined($self->{origurl
}) && defined($self->{url
}) && $self->{origurl
} eq $self->{url
};
609 valid_repo_url
($self->{url
})
610 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocols are supported. If the URL contains funny characters, contact me.");
611 if ($Girocco::Config
::restrict_mirror_hosts
) {
612 my $mh = extract_url_hostname
($self->{url
});
614 or $gcgi->err("Invalid URL. Note that only DNS names are allowed, not IP addresses.");
615 !is_our_hostname
($mh)
616 or $gcgi->err("Invalid URL. Mirrors from this host are not allowed, please create a fork instead.");
620 if ($field_enabled->('desc')) {
621 $self->{desc
} = to_utf8
($gcgi->wparam('desc'), 1);
622 length($self->{desc
}) <= 1024
623 or $gcgi->err("<b>Short</b> description length > 1kb!");
626 if ($field_enabled->('README')) {
627 $self->{README
} = to_utf8
($gcgi->wparam('README'), 1);
628 $self->_cleanup_readme;
629 length($self->{README
}) <= 8192
630 or $gcgi->err("README length > 8kb!");
631 my ($cnt, $err) = (0);
632 ($cnt, $err) = $self->_lint_readme if $gcgi->ok && $Girocco::Config
::xmllint_readme
;
633 $gcgi->err($err), $gcgi->{err
} += $cnt-1 if $cnt;
636 if ($field_enabled->('hp')) {
637 $self->{hp
} = $gcgi->wparam('hp');
639 valid_web_url
($self->{hp
})
640 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
644 # No mucking about with users unless we're a push project
645 if (($self->{loaded
} && !$self->{mirror
}) ||
646 (!$self->{loaded
} && (!defined($self->{url
}) || $self->{url
} eq ''))) {
649 foreach my $user ($cgi->multi_param('user')) {
650 if (!exists($users{$user})) {
652 push(@users, $user) if Girocco
::User
::does_exist
($user, 1);
655 $self->{users
} = \
@users;
658 # HEAD can only be set with editproj, NOT regproj (regproj sets it automatically)
659 # It may ALWAYS be set to what it was (even if there's no such refs/heads/...)
661 if (defined($self->{orig_HEAD
}) && $self->{orig_HEAD
} ne '' &&
662 defined($newhead = $cgi->param('HEAD')) && $newhead ne '') {
663 if ($newhead eq $self->{orig_HEAD
} ||
664 get_git
("--git-dir=$self->{path}", 'rev-parse', '--verify', '--quiet', 'refs/heads/'.$newhead)) {
665 $self->{HEAD
} = $newhead;
667 $gcgi->err("Invalid default branch (no such ref)");
671 # schedule deletion of tags (will be committed by update() after auth)
672 $self->{tags_to_delete
} = [$cgi->multi_param('tags')];
674 if ($field_enabled->('notifymail')) {
675 $self->{notifymail
} = $gcgi->wparam('notifymail');
676 if ($self->{notifymail
}) {
677 (valid_email_multi
($self->{notifymail
}) and length($self->{notifymail
}) <= 512)
678 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
680 if ($field_enabled->('reverseorder')) {
681 $self->{reverseorder
} = $gcgi->wparam('reverseorder') || 0;
683 if ($field_enabled->('summaryonly')) {
684 $self->{summaryonly
} = $gcgi->wparam('summaryonly') || 0;
688 if ($field_enabled->('notifytag')) {
689 $self->{notifytag
} = $gcgi->wparam('notifytag');
690 if ($self->{notifytag
}) {
691 (valid_email_multi
($self->{notifytag
}) and length($self->{notifytag
}) <= 512)
692 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
696 if ($field_enabled->('notifyjson')) {
697 $self->{notifyjson
} = $gcgi->wparam('notifyjson');
698 if ($self->{notifyjson
}) {
699 valid_web_url
($self->{notifyjson
})
700 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
704 if ($field_enabled->('notifycia')) {
705 $self->{notifycia
} = $gcgi->wparam('notifycia');
706 if ($self->{notifycia
}) {
707 $self->{notifycia
} =~ /^[a-zA-Z0-9._-]+$/
708 or $gcgi->err("Overly suspicious CIA notify project name. If it's actually valid, don't contact me, CIA is defunct.");
712 if ($cgi->param('setstatusupdates')) {
713 my $val = $gcgi->wparam('statusupdates') || '0';
714 $self->{statusupdates
} = $val ?
1 : 0;
717 not $gcgi->err_check;
723 name
=> $self->{name
},
724 email
=> $self->{email
},
726 desc
=> html_esc
($self->{desc
}),
727 README
=> html_esc
($self->{README
}),
729 users
=> $self->{users
},
730 notifymail
=> html_esc
($self->{notifymail
}),
731 reverseorder
=> html_esc
($self->{reverseorder
}),
732 summaryonly
=> html_esc
($self->{summaryonly
}),
733 notifytag
=> html_esc
($self->{notifytag
}),
734 notifyjson
=> html_esc
($self->{notifyjson
}),
735 notifycia
=> html_esc
($self->{notifycia
}),
739 # return true if $enc_passwd is a match for $plain_passwd
740 my $_check_passwd_match = sub {
741 my $enc_passwd = shift;
742 my $plain_passwd = shift;
743 defined($enc_passwd) or $enc_passwd = '';
744 defined($plain_passwd) or $plain_passwd = '';
745 # $enc_passwd may be crypt or crypt_sha1
746 if ($enc_passwd =~ m
(^\
$sha1\
$(\d
+)\
$([./0-9A-Za-z]{1,64})\$[./0-9A
-Za
-z
]{28}$)) {
747 # It's using sha1-crypt
748 return $enc_passwd eq crypt_sha1
($plain_passwd, $2, -(0+$1));
751 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
759 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
760 defined($self->{cpwd
}) or $self->{cpwd
} = '';
761 unless ($_check_passwd_match->($self->{ccrypt
}, $self->{cpwd
})) {
762 $gcgi->err("Your admin password does not match!");
768 # return true if the password from the file is empty or consists of all the same
769 # character. However, if the project was NOT loaded from the group file
770 # (!self->{loaded}) then the password is never locked.
771 # This function does NOT check $Girocco::Config::project_passwords, the caller
772 # is responsible for doing so if desired. Same for $self->{email}.
773 sub is_password_locked
{
776 $self->{loaded
} or return 0;
777 my $testcrypt = $self->{ccrypt
}; # The value from the group file
778 defined($testcrypt) or $testcrypt = '';
779 $testcrypt ne '' or return 1; # No password at all
780 $testcrypt =~ /^(.)\1*$/ and return 1; # Bogus crypt value
781 return 0; # Not locked
785 use POSIX
qw(strftime);
790 $self->_mkdir_forkees;
793 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
794 if ($Girocco::Config
::owning_group
) {
795 $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
796 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
797 chmod(02775, $self->{path
}) or die "chmod 02775 $self->{path} failed: $!";
799 chmod(02777, $self->{path
}) or die "chmod 02777 $self->{path} failed: $!";
801 $ENV{'GIT_TEMPLATE_DIR'} = $Girocco::Config
::chroot.'/var/empty';
802 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--quiet', '--bare', '--shared='.$self->shared_mode()) == 0
803 or die "git init $self->{path} failed: $?";
804 # we don't need these two, remove them (they will normally be created empty) if they exist
805 rmdir $self->{path
}."/branches";
806 rmdir $self->{path
}."/remotes";
807 -d
$self->{path
}."/info" or mkdir $self->{path
}."/info"
808 or die "info directory does not exist and unable to create it: $!";
809 -d
$self->{path
}."/hooks" or mkdir $self->{path
}."/hooks"
810 or die "hooks directory does not exist and unable to create it: $!";
811 # clean out any kruft that may have come in from the initial template directory
812 foreach my $cleandir (qw(hooks info)) {
813 if (opendir my $hooksdir, $self->{path
}.'/'.$cleandir) {
814 unlink map "$self->{path}/$_", grep { $_ ne '.' && $_ ne '..' } readdir $hooksdir;
818 if ($Girocco::Config
::owning_group
) {
819 chown(-1, $gid, $self->{path
}."/hooks") or die "chgrp $gid $self->{path}/hooks failed: $!";
821 # hooks never world writable
822 chmod 02775, $self->{path
}."/hooks" or die "chmod 02775 $self->{path}/hooks failed: $!";
823 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'core.logAllRefUpdates', 'false') == 0
824 or die "disabling core.logAllRefUpdates failed: $?";
825 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'core.ignoreCase', 'false') == 0
826 or die "disabling core.ignoreCase failed: $?";
827 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'transfer.fsckObjects', 'true') == 0
828 or die "enabling transfer.fsckObjects failed: $?";
829 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastForwards', 'false') == 0
830 or die "disabling receive.denyNonFastForwards failed: $?";
831 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyDeleteCurrent', 'warn') == 0
832 or die "disabling receive.denyDeleteCurrent failed: $?";
833 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
834 or die "disabling gc.auto failed: $?";
835 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.autogc', '0') == 0
836 or die "disabling receive.autogc failed: $?";
837 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
838 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.updateServerInfo', 'true') == 0
839 or die "enabling receive.updateServerInfo failed: $?";
840 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'repack.writeBitmaps', 'true') == 0
841 or die "enabling repack.writeBitmaps failed: $?";
842 $self->{creationtime
} = strftime
("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
843 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'girocco.creationtime', $self->{creationtime
}) == 0
844 or die "setting girocco.creationtime failed: $?";
845 -d
$self->{path
}."/htmlcache" or mkdir $self->{path
}."/htmlcache"
846 or die "htmlcache directory does not exist and unable to create it: $!";
847 -d
$self->{path
}."/bundles" or mkdir $self->{path
}."/bundles"
848 or die "bundles directory does not exist and unable to create it: $!";
849 foreach my $file (qw(info/lastactivity .delaygc)) {
850 if (open $fd, '>', $self->{path
}."/".$file) {
852 chmod 0664, $self->{path
}."/".$file;
856 # /info must have right permissions,
857 # and git init didn't do it for some reason.
858 # config must have correct permissions.
859 # and Git 2.1.0 - 2.2.1 incorrectly add +x for some reason.
860 # also make sure /refs, /objects and /htmlcache are correct too.
861 my ($dmode, $dmodestr, $fmode, $fmodestr);
862 if ($Girocco::Config
::owning_group
) {
863 ($dmode, $dmodestr) = (02775, '02775');
864 ($fmode, $fmodestr) = (0664, '0664');
866 ($dmode, $dmodestr) = (02777, '02777');
867 ($fmode, $fmodestr) = (0666, '0666');
869 foreach my $dir (qw(info refs objects htmlcache bundles)) {
870 chmod($dmode, $self->{path
}."/$dir") or die "chmod $dmodestr $self->{path}/$dir failed: $!";
872 foreach my $file (qw(config)) {
873 chmod($fmode, $self->{path
}."/$file") or die "chmod $fmodestr $self->{path}/$file failed: $!";
875 # these ones are probably not strictly required but are nice to have
876 foreach my $dir (qw(refs/heads refs/tags objects/info objects/pack)) {
877 -d
$self->{path
}."/$dir" or mkdir $self->{path
}."/$dir";
878 chmod($dmode, $self->{path
}."/$dir");
881 $self->_properties_save;
882 $self->_alternates_setup;
884 $self->_group_remove;
885 $self->_group_add($pushers);
886 $self->_hooks_install;
887 $self->_update_index;
889 $self->_set_forkchange(1);
897 $self->perm_initialize;
905 if ($Girocco::Config
::mob
&& $Girocco::Config
::mob
eq "mob") {
906 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name
}) == 0
907 or die "create-personal-mob-area $self->{name} failed";
909 $self->perm_initialize;
915 unlink ($self->_clonefail_path()); # Ignore EEXIST error
916 unlink ($self->_clonelog_path()); # Ignore EEXIST error
919 my $sock = IO
::Socket
::UNIX
->new($Girocco::Config
::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
920 select((select($sock),$|=1)[0]);
921 $sock->print("clone ".$self->{name
}."\n");
922 # Just ignore reply, we are going to succeed anyway and the I/O
923 # would apparently get quite hairy.
932 $self->_properties_save;
933 $self->_group_update;
935 if (exists($self->{tags_to_delete
})) {
936 $self->delete_ctag($_) foreach(@
{$self->{tags_to_delete
}});
939 $self->set_HEAD($self->{HEAD
}) unless $self->{orig_HEAD
} eq $self->{HEAD
};
941 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
942 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
943 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
944 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
946 $self->_update_index if $self->{email
} ne $self->{orig_email
};
947 $self->{orig_email
} = $self->{email
};
953 sub update_password
{
957 $self->{crypt} = scrypt_sha1
($pwd);
958 $self->_group_update;
961 # You can explicitly do this just on a ghost() repository too.
965 if (-d
$self->{path
}) {
966 system('rm', '-rf', $self->{path
}) == 0
967 or die "rm -rf $self->{path} failed: $?";
969 # attempt to clean up any empty fork directories by removing them
970 my @pelems = split('/', $self->{name
});
971 while (@pelems > 1) {
974 rmdir join('/', $Girocco::Config
::reporoot
, @pelems) or last;
976 $self->_group_remove;
977 $self->_update_index;
978 $self->_set_forkchange(1);
983 sub _contains_files
{
985 (-d
$dir) or return 0;
986 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
987 while (my $entry = readdir($dh)) {
988 next if $entry eq '' || $entry eq '.' || $entry eq '..';
989 closedir($dh), return 1
990 if -f
"$dir/$entry" ||
991 -d
"$dir/$entry" && _contains_files
("$dir/$entry");
1000 return _contains_files
($Girocco::Config
::reporoot
.'/'.$self->{name
});
1003 # Returns an array of 0 or more array refs, one for each bundle:
1004 # ->[0]: bundle creation time (seconds since epoch)
1005 # ->[1]: bundle name (e.g. foo-xxxxxxxx.bundle)
1006 # ->[2]: bundle size in bytes
1011 return @
{$self->{bundles
}} if ref($self->{bundles
}) eq 'ARRAY';
1013 if (-d
$self->{path
}.'/bundles') {{
1014 my $prefix = $self->{name
};
1015 $prefix =~ s
|^.*[^/]/([^/]+)$|$1|;
1016 opendir(my $dh, $self->{path
}.'/bundles') or last;
1017 while (my $bfile = readdir($dh)) {
1018 next unless $bfile =~ /^\d{8}_\d{6}-[0-9a-f]{8}$/;
1019 my $ctime = eval {timegm
(
1020 0+substr($bfile,13,2), 0+substr($bfile,11,2), 0+substr($bfile,9,2),
1021 0+substr($bfile,6,2), 0+substr($bfile,4,2)-1, 0+substr($bfile,0,4)-1900)};
1023 open(my $bh, '<', $self->{path
}.'/bundles/'.$bfile) or next;
1026 $f1 = $self->{path
}.'/objects/pack/'.$f1 if $f1 && $f1 !~ m
|^/|;
1027 $f2 = $self->{path
}.'/objects/pack/'.$f2 if $f2 && $f2 !~ m
|^/|;
1029 next unless $f1 && $f2 && $f1 ne $f2;
1032 next unless -e
$f1 && -e
$f2;
1033 my $s1 = -s
$f1 || 0;
1034 my $s2 = -s
$f2 || 0;
1035 next unless $s1 || $s2;
1036 push(@blist, [$ctime, "$prefix-".substr($bfile,16,8).".bundle", $s1+$s2]);
1040 @blist = sort({$b->[0] <=> $a->[0]} @blist);
1043 foreach my $bndl (@blist) {
1044 next if $seen{$bndl->[1]};
1045 $seen{$bndl->[1]} = 1;
1046 push(@result, $bndl);
1048 $self->{bundles
} = \
@result;
1052 sub has_alternates
{
1054 return -s
$self->{path
}.'/objects/info/alternates' ?
1 : 0;
1060 return scalar($self->bundles);
1063 # returns true if any of the notify fields are non-empty
1066 # We do not ckeck notifycia since it's defunct
1067 return $self->{'notifymail'} || $self->{'notifytag'} || $self->{'notifyjson'};
1071 # A project is considered empty if the git repository does not
1072 # have any refs. This means packed-refs does not exist or is
1073 # empty or only has lines starting with '#' AND there are no
1074 # files in the refs subdirectory hierarchy (no matter how deep).
1078 (-d
$self->{path
}) or return 0;
1079 if (-e
$self->{path
}.'/packed-refs') {
1080 open(my $pr, '<', $self->{path
}.'/packed-refs')
1081 or die "open $self->{path}./packed-refs failed: $!";
1083 while (my $ref = <$pr>) {
1084 next if $ref =~ /^#/;
1089 return 0 if $foundref;
1091 (-d
$self->{path
}.'/refs') or return 1;
1092 return !_contains_files
($self->{path
}.'/refs');
1096 # [0]: earliest possible scheduled next gc, undef if lastgc not set
1097 # [1]: approx. latest possible scheduled next gc, undef if lastgc not set
1098 # Both values (if not undef) are seconds since epoch
1099 # Result only considers lastgc and min_gc_interval nothing else
1102 my $lastgcepoch = parse_any_date
($self->{lastgc
});
1103 return (undef, undef) unless defined $lastgcepoch;
1104 return ($lastgcepoch + $Girocco::Config
::min_gc_interval
,
1105 int($lastgcepoch + 1.25 * $Girocco::Config
::min_gc_interval
+
1106 $Girocco::Config
::min_mirror_interval
));
1109 # returns boolean (0 or 1)
1110 # Attempts to determine whether or not a gc (and corresponding build
1111 # of a .bitmap/.bndl file) will actually take place at the next_gc
1112 # time (as returned by next_gc). Whether or not a .bitmap and .bndl
1113 # end up being built depends on whether or not the local object graph
1114 # is complete. In general if has_alternates is true then a .bitmap/.bndl
1115 # is not possible because the object graph will be incomplete,
1116 # but it *is* possible that even though the repository has_alternates,
1117 # it does not actually borrow any objects so a .bitmap/.bndl will build
1118 # in spite of the presence of alternates -- but this is expected to be rare.
1119 # The result is a best guess and false return value is not an absolute
1120 # guarantee that gc will not take place at the next interval, but it probably
1121 # will not if nothing changes in the meantime.
1124 my $lgc = parse_any_date
($self->{lastgc
});
1125 my $lrecv = parse_any_date
($self->{lastreceive
});
1126 my $lpgc = parse_any_date
($self->{lastparentgc
});
1127 return 1 unless defined($lgc) && defined($lrecv);
1128 if ($self->has_alternates) {
1129 return 1 unless defined($lpgc);
1130 return 1 unless $lpgc < $lgc;
1132 return 1 unless $lrecv < $lgc;
1133 # We don't try running any "is_dirty" check, so if somehow the
1134 # repository became dirty without updating lastreceive we might
1135 # incorrectly return false instead of true.
1143 # sanity check, disallow filenames starting with . .. or /
1144 unlink($self->{path
}.'/ctags/'.$ctag) if($ctag !~ m
|^(\
.\
.?
)/|);
1147 sub get_ctag_names
{
1150 opendir(my $dh, $self->{path
}.'/ctags')
1152 @ctags = grep { -f
"$self->{path}/ctags/$_" } readdir($dh);
1154 return sort({lc($a) cmp lc($b)} @ctags);
1160 my $heads = get_git
("--git-dir=$self->{path}", 'for-each-ref', '--format=%(objectname) %(refname)', 'refs/heads');
1161 defined($heads) or $heads = '';
1164 foreach (split(/\n/, $heads)) {
1166 next if !m
#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
1169 push(@res, $self->{orig_HEAD
}) if !@res && defined($self->{orig_HEAD
}) && $self->{orig_HEAD
} ne '';
1175 my $HEAD = get_git
("--git-dir=$self->{path}", 'symbolic-ref', 'HEAD');
1176 defined($HEAD) or $HEAD = '';
1178 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
1184 my $newHEAD = shift;
1185 # Cursory checks only -- if you want to break your HEAD, be my guest
1186 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
1187 die "grossly invalid new HEAD: $newHEAD";
1189 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic
-ref', 'HEAD
', "refs/heads/$newHEAD");
1190 die "could not set HEAD" if ($? >> 8);
1191 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob
'
1192 or system('cp
', '-p
', '-f
', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
1198 $type = 'REPO
' unless $type && $type =~ /^[A-Z]+$/;
1200 $self->{authtype} = $type;
1203 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
1205 my $expire = time + 24 * 3600;
1206 my $propval = "# ${type}AUTH $self->{auth} $expire";
1207 system($Girocco::Config::git_bin, '--git
-dir
='.$self->{path}, 'config
', 'gitweb
.repoauth
', $propval);
1214 delete $self->{auth};
1215 delete $self->{authtype};
1216 system($Girocco::Config::git_bin, '--git
-dir
='.$self->{path}, 'config
', '--unset
', 'gitweb
.repoauth
');
1221 my ($username) = @_;
1223 my $before_count = @{$self->{users}};
1224 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
1225 return @{$self->{users}} != $before_count;
1230 sub get_forkee_name {
1232 (m#^(.*)/.*?$#)[0]; #
1235 sub get_forkee_path {
1236 no warnings; # avoid silly 'unsuccessful
stat on filename with
\n' warning
1237 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git
';
1238 -d $forkee ? $forkee : '';
1241 # Ultimately the full project/fork name could end up being part of a Git ref name
1242 # when a project's forks are combined into one giant repository
for efficiency
.
1243 # That means that the project/fork name must satisfy the Git ref name requirements:
1245 # 1. Characters with an ASCII value less than or equal to 32 are not allowed
1246 # 2. The character with an ASCII value of 0x7F is not allowed
1247 # 3. The characters '~', '^', ':', '\', '*', '?', and '[' are not allowed
1248 # 4. The character '/' is a separator and is not allowed within a name
1249 # 5. The name may not start with '.' or end with '.'
1250 # 6. The name may not end with '.lock'
1251 # 7. The name may not contain the '..' sequence
1252 # 8. The name may not contain the '@{' sequence
1253 # 9. If multiple components are used (separated by '/'), no empty '' components
1255 # We also prohibit a trailing '.git' on any path component and futher restrict
1256 # the allowed characters to alphanumeric and [+._-] where names must start with
1259 sub _valid_name_characters
{
1264 and (not m
#/[+._-]#)
1267 and (not m
#\.git/#i)
1268 and (not m
#\.git$#i)
1269 and (not m
#\.idx/#i)
1270 and (not m
#\.idx$#i)
1271 and (not m
#\.lock/#i)
1272 and (not m
#\.lock$#i)
1273 and (not m
#\.pack/#i)
1274 and (not m
#\.pack$#i)
1275 and (not m
#\.bundle/#i)
1276 and (not m
#\.bundle$#i)
1278 and (not m/^[a-fA-F0-9]{38}$/)
1279 and m
#^[a-zA-Z0-9/+._-]+$#
1280 and !has_reserved_suffix
($_, $_[1], $_[2]);
1284 no warnings
; # avoid silly 'unsuccessful stat on filename with \n' warning
1286 _valid_name_characters
($_) and not exists($reservedprojectnames{lc($_)})
1287 and @
{[m
#/#g]} <= 5 # maximum fork depth is 5
1288 and ((not m
#/#) or -d get_forkee_path($_)); # will also catch ^/
1291 # It's possible that some forks have been kept but the forkee is gone.
1292 # In this case the standard valid_name check is too strict.
1294 no warnings
; # avoid silly 'unsuccessful stat on filename with \n' warning
1295 my ($name, $nodie) = @_;
1297 _valid_name_characters
($name, $Girocco::Config
::reporoot
, ".git")
1298 and ((not $name =~ m
#/#)
1299 or -d get_forkee_path
($name)
1300 or -d
$Girocco::Config
::reporoot
.'/'.get_forkee_name
($name)));
1301 (!$okay && $nodie) and return undef;
1302 !$okay and die "tried to query for project with invalid name $name!";
1303 (-d
$Girocco::Config
::reporoot
."/$name.git");
1307 open my $fd, '<', jailed_file
("/etc/group") or die "getting project list failed: $!";
1308 my @projects = map {/^([^:_\s#][^:\s#]*):[^:]*:\d{5,}:/ ?
$1 : ()} <$fd>;