Project.pm: reserve the srv project name
[girocco.git] / Girocco / Project.pm
blob2c5cd59ad2690b8c209925e4d57c89609f08ab41
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 => ['README (HTML, lt 8kb)', 'README', 'textarea'],
40 notifymail => ['Commit notify - mail to', 'notifymail', 'text'],
41 notifyjson => ['Commit notify - <a href="http://help.github.com/post-receive-hooks/">POST JSON</a> at', 'notifyjson', 'text'],
42 notifycia => ['Commit notify - <a href="http://cia.vc/doc/">CIA project</a> name', 'notifycia', 'text'],
45 sub _mkdir_forkees {
46 my $self = shift;
47 my @pelems = split('/', $self->{name});
48 pop @pelems; # do not create dir for the project itself
49 my $path = $self->{base_path};
50 foreach my $pelem (@pelems) {
51 $path .= "/$pelem";
52 (-d "$path") or mkdir $path or die "mkdir $path: $!";
53 chmod 02775, $path; # ok if fails (dir may already exist and be owned by someone else)
57 our %propmap = (
58 url => ':baseurl',
59 email => ':owner',
60 desc => 'description',
61 README => 'README.html',
62 hp => ':homepage',
63 notifymail => '%hooks.mailinglist',
64 notifytag => '%hooks.announcelist',
65 notifyjson => '%hooks.jsonurl',
66 notifycia => '%hooks.cianame',
69 our %propmapro = (
70 lastchange => ':lastchange',
71 lastactivity => 'info/lastactivity',
74 # Projects with any of these names will be disallowed to avoid possible
75 # collisions with cgi script paths or chroot paths
76 our %reservedprojectnames = (
77 c => 1, # /c/ -> cgit
78 h => 1, # /h/ -> html.cgi
79 r => 1, # /r/ -> git http
80 w => 1, # /w/ -> gitweb
81 srv => 1, # /srv/git/ -> chroot ssh git repositories
84 sub _update_index {
85 system($Girocco::Config::basedir . '/gitweb/genindex.sh');
88 sub _property_path {
89 my $self = shift;
90 my ($name) = @_;
91 $self->{path}.'/'.$name;
94 sub _property_fget {
95 my $self = shift;
96 my ($name) = @_;
97 my $pname = $propmap{$name};
98 $pname = $propmapro{$name} unless $pname;
99 $pname or die "unknown property: $name";
100 if ($pname =~ s/^://) {
101 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "gitweb.$pname"`;
102 chomp $val;
103 return $val;
104 } elsif ($pname =~ s/^%//) {
105 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "$pname"`;
106 chomp $val;
107 return $val;
110 open P, '<', $self->_property_path($pname) or return undef;
111 my @value = <P>;
112 close P;
113 my $value = join('', @value); chomp $value;
114 $value;
117 sub _property_fput {
118 my $self = shift;
119 my ($name, $value) = @_;
120 my $pname = $propmap{$name};
121 $pname or die "unknown property: $name";
122 $value ||= '';
123 if ($pname =~ s/^://) {
124 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
125 return;
126 } elsif ($pname =~ s/^%//) {
127 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', $pname, $value);
128 return;
131 my $P = lock_file($self->_property_path($pname));
132 $value ne '' and print $P "$value\n";
133 close $P;
134 unlock_file($self->_property_path($pname));
137 sub _properties_load {
138 my $self = shift;
139 foreach my $prop (keys %propmap) {
140 $self->{$prop} = $self->_property_fget($prop);
142 foreach my $prop (keys %propmapro) {
143 $self->{$prop} = $self->_property_fget($prop);
145 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config --bool "gitweb.statusupdates" 2>/dev/null`;
146 chomp $val;
147 $val = ($val eq 'false') ? 0 : 1;
148 $self->{statusupdates} = $val;
149 delete $self->{auth};
150 $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "gitweb.repoauth"`;
151 chomp $val;
152 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
153 my $expire = $3;
154 if (time < $expire) {
155 $self->{authtype} = $1;
156 $self->{auth} = $2;
161 sub _properties_save {
162 my $self = shift;
163 foreach my $prop (keys %propmap) {
164 $self->_property_fput($prop, $self->{$prop});
166 $self->{statusupdates} = 1
167 unless defined($self->{statusupdates}) && $self->{statusupdates} =~ /^\d+$/;
168 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool',
169 "gitweb.statusupdates", $self->{statusupdates});
172 sub _nofetch_path {
173 my $self = shift;
174 $self->_property_path('.nofetch');
177 sub _nofetch {
178 my $self = shift;
179 my ($nofetch) = @_;
180 my $nf = $self->_nofetch_path;
181 if ($nofetch) {
182 open X, '>', $nf or die "nofetch failed: $!";
183 close X;
184 } else {
185 unlink $nf or die "yesfetch failed: $!";
189 sub _clonelog_path {
190 my $self = shift;
191 $self->_property_path('.clonelog');
194 sub _clonefail_path {
195 my $self = shift;
196 $self->_property_path('.clone_failed');
199 sub _clonep_path {
200 my $self = shift;
201 $self->_property_path('.clone_in_progress');
204 sub _clonep {
205 my $self = shift;
206 my ($nofetch) = @_;
207 my $np = $self->_clonep_path;
208 if ($nofetch) {
209 open X, '>', $np or die "clonep failed: $!";
210 close X;
211 } else {
212 unlink $np or die "clonef failed: $!";
215 sub _alternates_setup {
216 my $self = shift;
217 return unless $self->{name} =~ m#/#;
218 my $forkee_name = get_forkee_name($self->{name});
219 my $forkee_path = get_forkee_path($self->{name});
220 return unless -d $forkee_path;
221 mkdir $self->{path}.'/refs'; chmod 02775, $self->{path}.'/refs';
222 mkdir $self->{path}.'/objects'; chmod 02775, $self->{path}.'/objects';
223 mkdir $self->{path}.'/objects/info'; chmod 02775, $self->{path}.'/objects/info';
225 # We set up both alternates and http_alternates since we cannot use
226 # relative path in alternates - that doesn't work recursively.
228 my $filename = $self->{path}.'/objects/info/alternates';
229 open X, '>', $filename or die "alternates failed: $!";
230 print X "$forkee_path/objects\n";
231 close X;
232 chmod 0664, $filename or warn "cannot chmod $filename: $!";
234 if ($Girocco::Config::httppullurl) {
235 $filename = $self->{path}.'/objects/info/http-alternates';
236 open X, '>', $filename or die "http-alternates failed: $!";
237 my $upfork = $forkee_name;
238 do { print X "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
239 close X;
240 chmod 0664, $filename or warn "cannot chmod $filename: $!";
243 # The symlink is problematic since git remote prune will traverse it.
244 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
246 # copy refs from parent project
247 # ownership information is changed by a root cronjob
248 system("cp -pR $forkee_path/refs $self->{path}/");
249 # initialize HEAD, hacky version
250 system("cp $forkee_path/HEAD $self->{path}/HEAD");
253 sub _ctags_setup {
254 my $self = shift;
255 my $perms = $Girocco::Config::permission_control eq 'Hooks' ? 02777 : 02775;
256 mkdir $self->{path}.'/ctags'; chmod $perms, $self->{path}.'/ctags';
259 sub _group_add {
260 my $self = shift;
261 my ($xtra) = @_;
262 $xtra .= join(',', @{$self->{users}});
263 filedb_atomic_append(jailed_file('/etc/group'),
264 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
267 sub _group_update {
268 my $self = shift;
269 my $xtra = join(',', @{$self->{users}});
270 filedb_atomic_edit(jailed_file('/etc/group'),
271 sub {
272 $_ = $_[0];
273 chomp;
274 if ($self->{name} eq (split /:/)[0]) {
275 # preserve readonly flag
276 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
277 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
278 } else {
279 return "$_\n";
285 sub _group_remove {
286 my $self = shift;
287 filedb_atomic_edit(jailed_file('/etc/group'),
288 sub {
289 $self->{name} ne (split /:/)[0] and return $_;
294 sub _hook_path {
295 my $self = shift;
296 my ($name) = @_;
297 $self->{path}.'/hooks/'.$name;
300 sub _hook_install {
301 my $self = shift;
302 my ($name) = @_;
303 open SRC, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
304 open DST, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
305 while (<SRC>) { print DST $_; }
306 close DST;
307 close SRC;
308 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
311 sub _hooks_install {
312 my $self = shift;
313 foreach my $hook ('pre-receive', 'post-receive', 'update', 'post-update') {
314 $self->_hook_install($hook);
318 # private constructor, do not use
319 sub _new {
320 my $class = shift;
321 my ($name, $base_path, $path) = @_;
322 does_exist($name,1) || valid_name($name) or die "refusing to create project with invalid name ($name)!";
323 $path ||= "$base_path/$name.git";
324 my $proj = { name => $name, base_path => $base_path, path => $path };
326 bless $proj, $class;
329 # public constructor #0
330 # creates a virtual project not connected to disk image
331 # you can conjure() it later to disk
332 sub ghost {
333 my $class = shift;
334 my ($name, $mirror) = @_;
335 my $self = $class->_new($name, $Girocco::Config::reporoot);
336 $self->{users} = [];
337 $self->{mirror} = $mirror;
338 $self->{email} = $self->{orig_email} = '';
339 $self;
342 # public constructor #1
343 sub load {
344 my $class = shift;
345 my $name = shift || '';
347 open F, '<', jailed_file("/etc/group") or die "project load failed: $!";
348 while (<F>) {
349 chomp;
350 @_ = split /:+/;
351 next unless (shift eq $name);
353 my $self = $class->_new($name, $Girocco::Config::reporoot);
354 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
356 my $ulist;
357 ($self->{crypt}, $self->{gid}, $ulist) = @_;
358 $ulist ||= '';
359 $self->{users} = [split /,/, $ulist];
360 $self->{HEAD} = $self->get_HEAD;
361 $self->{orig_HEAD} = $self->{HEAD};
362 $self->{orig_users} = [@{$self->{users}}];
363 $self->{mirror} = ! -e $self->_nofetch_path;
364 $self->{clone_in_progress} = -e $self->_clonep_path;
365 $self->{clone_logged} = -e $self->_clonelog_path;
366 $self->{clone_failed} = -e $self->_clonefail_path;
367 $self->{ccrypt} = $self->{crypt};
369 $self->_properties_load;
370 $self->{orig_email} = $self->{email};
371 return $self;
373 close F;
374 undef;
377 # $proj may not be in sane state if this returns false!
378 sub cgi_fill {
379 my $self = shift;
380 my ($gcgi) = @_;
381 my $cgi = $gcgi->cgi;
383 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
384 # in case passwords are disabled
385 defined($pwd) or $pwd = ''; defined($pwd2) or $pwd = '';
386 if ($Girocco::Config::project_passwords and not $self->{crypt} and $pwd eq '' and $pwd2 eq '') {
387 $gcgi->err("Empty passwords are not permitted.");
389 if ($pwd ne '' or not $self->{crypt}) {
390 $self->{crypt} = scrypt_sha1($pwd);
392 if (($pwd ne '' || $pwd2 ne '') and $pwd ne $pwd2) {
393 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
396 $self->{cpwd} = $cgi->param('cpwd');
398 my ($forkee,$project) = ($self->{name} =~ m#^(.*/)?([^/]+)$#);
399 my $newtype = $forkee ? 'fork' : 'project';
400 length($project) <= 64
401 or $gcgi->err("The $newtype name is longer than 64 characters. Do you really need that much?");
403 if ($Girocco::Config::project_owners eq 'email') {
404 $self->{email} = $gcgi->wparam('email');
405 valid_email($self->{email})
406 or $gcgi->err("Your email sure looks weird...?");
407 length($self->{email}) <= 96
408 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
411 $self->{url} = $gcgi->wparam('url');
412 if ($self->{url}) {
413 valid_repo_url($self->{url})
414 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
417 $self->{desc} = $gcgi->wparam('desc');
418 length($self->{desc}) <= 1024
419 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
421 $self->{README} = $gcgi->wparam('README');
422 length($self->{README}) <= 8192
423 or $gcgi->err("README length &gt; 8kb!");
425 $self->{hp} = $gcgi->wparam('hp');
426 if ($self->{hp}) {
427 valid_web_url($self->{hp})
428 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
431 $self->{users} = [grep { Girocco::User::valid_name($_) && Girocco::User::does_exist($_) } $cgi->param('user')];
433 $self->{HEAD} = $cgi->param('HEAD') if $cgi->param('HEAD');
435 # schedule deletion of tags (will be committed by update() after auth)
436 $self->{tags_to_delete} = [$cgi->param('tags')];
438 $self->{notifymail} = $gcgi->wparam('notifymail');
439 if ($self->{notifymail}) {
440 (valid_email_multi($self->{notifymail}) and length($self->{notifymail}) <= 512)
441 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
444 $self->{notifyjson} = $gcgi->wparam('notifyjson');
445 if ($self->{notifyjson}) {
446 valid_web_url($self->{notifyjson})
447 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
450 $self->{notifycia} = $gcgi->wparam('notifycia');
451 if ($self->{notifycia}) {
452 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
453 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
456 if ($cgi->param('setstatusupdates')) {
457 my $val = $gcgi->wparam('statusupdates') || '0';
458 $self->{statusupdates} = $val ? 1 : 0;
461 not $gcgi->err_check;
464 sub form_defaults {
465 my $self = shift;
467 name => $self->{name},
468 email => $self->{email},
469 url => $self->{url},
470 desc => html_esc($self->{desc}),
471 README => html_esc($self->{README}),
472 hp => $self->{hp},
473 users => $self->{users},
474 notifymail => html_esc($self->{notifymail}),
475 notifyjson => html_esc($self->{notifyjson}),
476 notifycia => html_esc($self->{notifycia}),
480 # return true if $enc_passwd is a match for $plain_passwd
481 my $_check_passwd_match = sub {
482 my $enc_passwd = shift;
483 my $plain_passwd = shift;
484 defined($enc_passwd) or $enc_passwd = '';
485 defined($plain_passwd) or $plain_passwd = '';
486 # $enc_passwd may be crypt or crypt_sha1
487 if ($enc_passwd =~ m(^\$sha1\$(\d+)\$([./0-9A-Za-z]{1,64})\$[./0-9A-Za-z]{28}$)) {
488 # It's using sha1-crypt
489 return $enc_passwd eq crypt_sha1($plain_passwd, $2, -(0+$1));
490 } else {
491 # It's using crypt
492 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
496 sub authenticate {
497 my $self = shift;
498 my ($gcgi) = @_;
500 $self->{ccrypt} or die "Can't authenticate against a project with no password";
501 defined($self->{cpwd}) or $self->{cpwd} = '';
502 unless ($_check_passwd_match->($self->{ccrypt}, $self->{cpwd})) {
503 $gcgi->err("Your admin password does not match!");
504 return 0;
506 return 1;
509 sub _setup {
510 my $self = shift;
511 my ($pushers) = @_;
513 $self->_mkdir_forkees;
515 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
516 if ($Girocco::Config::owning_group) {
517 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
518 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
519 chmod(02775, $self->{path}) or die "chmod 02775 $self->{path} failed: $!";
520 } else {
521 chmod(02777, $self->{path}) or die "chmod 02777 $self->{path} failed: $!";
523 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
524 or die "git init $self->{path} failed: $?";
525 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
526 or die "disabling receive.denyNonFastforwards failed: $?";
527 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
528 or die "disabling gc.auto failed: $?";
530 # /info must have right permissions,
531 # and git init didn't do it for some reason.
532 if ($Girocco::Config::owning_group) {
533 chmod(02775, $self->{path}."/info") or die "chmod 02775 $self->{path}/info failed: $!";
534 } else {
535 chmod(02777, $self->{path}."/info") or die "chmod 02777 $self->{path}/info failed: $!";
538 $self->_properties_save;
539 $self->_alternates_setup;
540 $self->_ctags_setup;
541 $self->_group_remove;
542 $self->_group_add($pushers);
543 $self->_hooks_install;
544 #$self->perm_initialize;
545 $self->_update_index;
548 sub premirror {
549 my $self = shift;
551 $self->_setup(':');
552 $self->_clonep(1);
553 $self->perm_initialize;
556 sub conjure {
557 my $self = shift;
559 $self->_setup;
560 $self->_nofetch(1);
561 if ($Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
562 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
563 or die "create-personal-mob-area $self->{name} failed";
565 $self->perm_initialize;
568 sub clone {
569 my $self = shift;
571 unlink ($self->_clonefail_path()); # Ignore EEXIST error
572 unlink ($self->_clonelog_path()); # Ignore EEXIST error
574 use IO::Socket;
575 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
576 $sock->print("clone ".$self->{name}."\n");
577 # Just ignore reply, we are going to succeed anyway and the I/O
578 # would apparently get quite hairy.
579 $sock->flush();
580 sleep 2; # *cough*
581 $sock->close();
584 sub update {
585 my $self = shift;
587 $self->_properties_save;
588 $self->_group_update;
590 if (exists($self->{tags_to_delete})) {
591 $self->delete_ctag($_) foreach(@{$self->{tags_to_delete}});
594 $self->set_HEAD($self->{HEAD}) unless $self->{orig_HEAD} eq $self->{HEAD};
596 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
597 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
598 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
599 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
601 $self->_update_index if $self->{email} ne $self->{orig_email};
602 $self->{orig_email} = $self->{email};
607 sub update_password {
608 my $self = shift;
609 my ($pwd) = @_;
611 $self->{crypt} = scrypt_sha1($pwd);
612 $self->_group_update;
615 # You can explicitly do this just on a ghost() repository too.
616 sub delete {
617 my $self = shift;
619 if (-d $self->{path}) {
620 system('rm', '-rf', $self->{path}) == 0
621 or die "rm -rf $self->{path} failed: $?";
623 # attempt to clean up any empty fork directories by removing them
624 my @pelems = split('/', $self->{name});
625 while (@pelems > 1) {
626 pop @pelems;
627 # okay to fail
628 rmdir join('/', $Girocco::Config::reporoot, @pelems) or last;
630 $self->_group_remove;
631 $self->_update_index;
634 sub _contains_files {
635 my $dir = shift;
636 (-d $dir) or return 0;
637 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
638 while (my $entry = readdir($dh)) {
639 next if $entry eq '' || $entry eq '.' || $entry eq '..';
640 closedir($dh), return 1
641 if -f "$dir/$entry" ||
642 -d "$dir/$entry" && _contains_files("$dir/$entry");
644 closedir($dh);
645 return 0;
648 sub has_forks {
649 my $self = shift;
651 return _contains_files($Girocco::Config::reporoot.'/'.$self->{name});
654 sub is_empty {
655 # A project is considered empty if the git repository does not
656 # have any refs. This means packed-refs does not exist or is
657 # empty or only has lines starting with '#' AND there are no
658 # files in the refs subdirectory hierarchy (no matter how deep).
660 my $self = shift;
662 (-d $self->{path}) or return 0;
663 if (-e $self->{path}.'/packed-refs') {
664 open(my $pr, '<', $self->{path}.'/packed-refs')
665 or die "open $self->{path}./packed-refs failed: $!";
666 my $foundref = 0;
667 while (my $ref = <$pr>) {
668 next if $ref =~ /^#/;
669 $foundref = 1;
670 last;
672 close($pr);
673 return 0 if $foundref;
675 (-d $self->{path}.'/refs') or return 1;
676 return !_contains_files($self->{path}.'/refs');
679 sub delete_ctag {
680 my $self = shift;
681 my ($ctag) = @_;
683 # sanity check, disallow filenames starting with . .. or /
684 unlink($self->{path}.'/ctags/'.$ctag) if($ctag !~ m|^(\.\.?)/|);
687 sub get_ctag_names {
688 my $self = shift;
689 my @ctags = ();
690 opendir(my $dh, $self->{path}.'/ctags')
691 or return @ctags;
692 @ctags = grep { -f "$self->{path}/ctags/$_" } readdir($dh);
693 closedir($dh);
694 return @ctags;
697 sub get_heads {
698 my $self = shift;
699 my $fh;
700 open($fh, '-|', "$Girocco::Config::git_bin --git-dir=$self->{path} show-ref --heads") or die "could not get list of heads";
701 my @res;
702 while (<$fh>) {
703 chomp;
704 next if !m#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
705 push @res, $1;
707 close $fh;
708 @res;
711 sub get_HEAD {
712 my $self = shift;
713 my $HEAD = `$Girocco::Config::git_bin --git-dir=$self->{path} symbolic-ref HEAD`;
714 chomp $HEAD;
715 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
716 return $1;
719 sub set_HEAD {
720 my $self = shift;
721 my $newHEAD = shift;
722 # Cursory checks only -- if you want to break your HEAD, be my guest
723 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
724 die "grossly invalid new HEAD: $newHEAD";
726 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', "refs/heads/$newHEAD");
727 die "could not set HEAD" if ($? >> 8);
728 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob'
729 or system('cp', '-p', '-f', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
732 sub gen_auth {
733 my $self = shift;
734 my ($type) = @_;
735 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
737 $self->{authtype} = $type;
739 no warnings;
740 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
742 my $expire = time + 24 * 3600;
743 my $propval = "# ${type}AUTH $self->{auth} $expire";
744 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gitweb.repoauth', $propval);
745 $self->{auth};
748 sub del_auth {
749 my $self = shift;
751 delete $self->{auth};
752 delete $self->{authtype};
753 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--unset', 'gitweb.repoauth');
756 sub remove_user {
757 my $self = shift;
758 my ($username) = @_;
760 my $before_count = @{$self->{users}};
761 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
762 return @{$self->{users}} != $before_count;
765 ### static methods
767 sub get_forkee_name {
768 local $_ = $_[0];
769 (m#^(.*)/.*?$#)[0]; #
772 sub get_forkee_path {
773 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
774 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
775 -d $forkee ? $forkee : '';
778 sub _valid_name_characters {
779 local $_ = $_[0];
780 (not m#^/#)
781 and (not m#//#)
782 and (not m#\./#)
783 and (not m#/$#)
784 and m#^[a-zA-Z0-9+./_-]+$#;
787 sub valid_name {
788 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
789 local $_ = $_[0];
790 _valid_name_characters($_) and not exists($reservedprojectnames{$_})
791 and ((not m#/#) or -d get_forkee_path($_)); # will also catch ^/
794 # It's possible that some forks have been kept but the forkee is gone.
795 # In this case the standard valid_name check is too strict.
796 sub does_exist {
797 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
798 my ($name, $nodie) = @_;
799 my $okay = (
800 _valid_name_characters($name)
801 and ((not $name =~ m#/#)
802 or -d get_forkee_path($name)
803 or -d $Girocco::Config::reporoot.'/'.get_forkee_name($name)));
804 (!$okay && $nodie) and return undef;
805 !$okay and die "tried to query for project with invalid name $name!";
806 (-d $Girocco::Config::reporoot."/$name.git");
809 sub get_full_list {
810 my $class = shift;
811 my @projects;
813 open F, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
814 while (<F>) {
815 chomp;
816 @_ = split /:+/;
817 next if ($_[2] < 65536);
819 push @projects, $_[0];
821 close F;
822 @projects;