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
=> ['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'],
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) {
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)
60 desc
=> 'description',
61 README
=> 'README.html',
63 notifymail
=> '%hooks.mailinglist',
64 notifytag
=> '%hooks.announcelist',
65 notifyjson
=> '%hooks.jsonurl',
66 notifycia
=> '%hooks.cianame',
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
76 our %reservedprojectnames = (
84 system($Girocco::Config
::basedir
. '/gitweb/genindex.sh');
90 $self->{path
}.'/'.$name;
96 my $pname = $propmap{$name};
97 $pname = $propmapro{$name} unless $pname;
98 $pname or die "unknown property: $name";
99 if ($pname =~ s/^://) {
100 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "gitweb.$pname"`;
103 } elsif ($pname =~ s/^%//) {
104 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "$pname"`;
109 open P
, '<', $self->_property_path($pname) or return undef;
112 my $value = join('', @value); chomp $value;
118 my ($name, $value) = @_;
119 my $pname = $propmap{$name};
120 $pname or die "unknown property: $name";
122 if ($pname =~ s/^://) {
123 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', "gitweb.$pname", $value);
125 } elsif ($pname =~ s/^%//) {
126 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', $pname, $value);
130 my $P = lock_file
($self->_property_path($pname));
131 $value ne '' and print $P "$value\n";
133 unlock_file
($self->_property_path($pname));
136 sub _properties_load
{
138 foreach my $prop (keys %propmap) {
139 $self->{$prop} = $self->_property_fget($prop);
141 foreach my $prop (keys %propmapro) {
142 $self->{$prop} = $self->_property_fget($prop);
144 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config --bool "gitweb.statusupdates" 2>/dev/null`;
146 $val = ($val eq 'false') ?
0 : 1;
147 $self->{statusupdates
} = $val;
148 delete $self->{auth
};
149 $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "gitweb.repoauth"`;
151 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
153 if (time < $expire) {
154 $self->{authtype
} = $1;
160 sub _properties_save
{
162 foreach my $prop (keys %propmap) {
163 $self->_property_fput($prop, $self->{$prop});
165 $self->{statusupdates
} = 1
166 unless defined($self->{statusupdates
}) && $self->{statusupdates
} =~ /^\d+$/;
167 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', '--bool',
168 "gitweb.statusupdates", $self->{statusupdates
});
173 $self->_property_path('.nofetch');
179 my $nf = $self->_nofetch_path;
181 open X
, '>', $nf or die "nofetch failed: $!";
184 unlink $nf or die "yesfetch failed: $!";
190 $self->_property_path('.clonelog');
193 sub _clonefail_path
{
195 $self->_property_path('.clone_failed');
200 $self->_property_path('.clone_in_progress');
206 my $np = $self->_clonep_path;
208 open X
, '>', $np or die "clonep failed: $!";
211 unlink $np or die "clonef failed: $!";
214 sub _alternates_setup
{
216 return unless $self->{name
} =~ m
#/#;
217 my $forkee_name = get_forkee_name
($self->{name
});
218 my $forkee_path = get_forkee_path
($self->{name
});
219 return unless -d
$forkee_path;
220 mkdir $self->{path
}.'/refs'; chmod 02775, $self->{path
}.'/refs';
221 mkdir $self->{path
}.'/objects'; chmod 02775, $self->{path
}.'/objects';
222 mkdir $self->{path
}.'/objects/info'; chmod 02775, $self->{path
}.'/objects/info';
224 # We set up both alternates and http_alternates since we cannot use
225 # relative path in alternates - that doesn't work recursively.
227 my $filename = $self->{path
}.'/objects/info/alternates';
228 open X
, '>', $filename or die "alternates failed: $!";
229 print X
"$forkee_path/objects\n";
231 chmod 0664, $filename or warn "cannot chmod $filename: $!";
233 if ($Girocco::Config
::httppullurl
) {
234 $filename = $self->{path
}.'/objects/info/http-alternates';
235 open X
, '>', $filename or die "http-alternates failed: $!";
236 my $upfork = $forkee_name;
237 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
239 chmod 0664, $filename or warn "cannot chmod $filename: $!";
242 # The symlink is problematic since git remote prune will traverse it.
243 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
245 # copy refs from parent project
246 # ownership information is changed by a root cronjob
247 system("cp -pR $forkee_path/refs $self->{path}/");
248 # initialize HEAD, hacky version
249 system("cp $forkee_path/HEAD $self->{path}/HEAD");
254 my $perms = $Girocco::Config
::permission_control
eq 'Hooks' ?
02777 : 02775;
255 mkdir $self->{path
}.'/ctags'; chmod $perms, $self->{path
}.'/ctags';
261 $xtra .= join(',', @
{$self->{users
}});
262 filedb_atomic_append
(jailed_file
('/etc/group'),
263 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
268 my $xtra = join(',', @
{$self->{users
}});
269 filedb_atomic_edit
(jailed_file
('/etc/group'),
273 if ($self->{name
} eq (split /:/)[0]) {
274 # preserve readonly flag
275 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
276 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
286 filedb_atomic_edit
(jailed_file
('/etc/group'),
288 $self->{name
} ne (split /:/)[0] and return $_;
296 $self->{path
}.'/hooks/'.$name;
302 open SRC
, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
303 open DST
, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
304 while (<SRC
>) { print DST
$_; }
307 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
312 foreach my $hook ('pre-receive', 'post-receive', 'update', 'post-update') {
313 $self->_hook_install($hook);
317 # private constructor, do not use
320 my ($name, $base_path, $path) = @_;
321 does_exist
($name,1) || valid_name
($name) or die "refusing to create project with invalid name ($name)!";
322 $path ||= "$base_path/$name.git";
323 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
328 # public constructor #0
329 # creates a virtual project not connected to disk image
330 # you can conjure() it later to disk
333 my ($name, $mirror) = @_;
334 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
336 $self->{mirror
} = $mirror;
337 $self->{email
} = $self->{orig_email
} = '';
341 # public constructor #1
344 my $name = shift || '';
346 open F
, '<', jailed_file
("/etc/group") or die "project load failed: $!";
350 next unless (shift eq $name);
352 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
353 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
356 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
358 $self->{users
} = [split /,/, $ulist];
359 $self->{HEAD
} = $self->get_HEAD;
360 $self->{orig_HEAD
} = $self->{HEAD
};
361 $self->{orig_users
} = [@
{$self->{users
}}];
362 $self->{mirror
} = ! -e
$self->_nofetch_path;
363 $self->{clone_in_progress
} = -e
$self->_clonep_path;
364 $self->{clone_logged
} = -e
$self->_clonelog_path;
365 $self->{clone_failed
} = -e
$self->_clonefail_path;
366 $self->{ccrypt
} = $self->{crypt};
368 $self->_properties_load;
369 $self->{orig_email
} = $self->{email
};
376 # $proj may not be in sane state if this returns false!
380 my $cgi = $gcgi->cgi;
382 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
383 # in case passwords are disabled
384 defined($pwd) or $pwd = ''; defined($pwd2) or $pwd = '';
385 if ($Girocco::Config
::project_passwords
and not $self->{crypt} and $pwd eq '' and $pwd2 eq '') {
386 $gcgi->err("Empty passwords are not permitted.");
388 if ($pwd ne '' or not $self->{crypt}) {
389 $self->{crypt} = scrypt_sha1
($pwd);
391 if (($pwd ne '' || $pwd2 ne '') and $pwd ne $pwd2) {
392 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
395 $self->{cpwd
} = $cgi->param('cpwd');
397 my ($forkee,$project) = ($self->{name
} =~ m
#^(.*/)?([^/]+)$#);
398 my $newtype = $forkee ?
'fork' : 'project';
399 length($project) <= 64
400 or $gcgi->err("The $newtype name is longer than 64 characters. Do you really need that much?");
402 if ($Girocco::Config
::project_owners
eq 'email') {
403 $self->{email
} = $gcgi->wparam('email');
404 valid_email
($self->{email
})
405 or $gcgi->err("Your email sure looks weird...?");
406 length($self->{email
}) <= 96
407 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
410 $self->{url
} = $gcgi->wparam('url');
412 valid_repo_url
($self->{url
})
413 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
416 $self->{desc
} = $gcgi->wparam('desc');
417 length($self->{desc
}) <= 1024
418 or $gcgi->err("<b>Short</b> description length > 1kb!");
420 $self->{README
} = $gcgi->wparam('README');
421 length($self->{README
}) <= 8192
422 or $gcgi->err("README length > 8kb!");
424 $self->{hp
} = $gcgi->wparam('hp');
426 valid_web_url
($self->{hp
})
427 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
430 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) && Girocco
::User
::does_exist
($_) } $cgi->param('user')];
432 $self->{HEAD
} = $cgi->param('HEAD') if $cgi->param('HEAD');
434 # schedule deletion of tags (will be committed by update() after auth)
435 $self->{tags_to_delete
} = [$cgi->param('tags')];
437 $self->{notifymail
} = $gcgi->wparam('notifymail');
438 if ($self->{notifymail
}) {
439 (valid_email_multi
($self->{notifymail
}) and length($self->{notifymail
}) <= 512)
440 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
443 $self->{notifyjson
} = $gcgi->wparam('notifyjson');
444 if ($self->{notifyjson
}) {
445 valid_web_url
($self->{notifyjson
})
446 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
449 $self->{notifycia
} = $gcgi->wparam('notifycia');
450 if ($self->{notifycia
}) {
451 $self->{notifycia
} =~ /^[a-zA-Z0-9._-]+$/
452 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
455 if ($cgi->param('setstatusupdates')) {
456 my $val = $gcgi->wparam('statusupdates') || '0';
457 $self->{statusupdates
} = $val ?
1 : 0;
460 not $gcgi->err_check;
466 name
=> $self->{name
},
467 email
=> $self->{email
},
469 desc
=> html_esc
($self->{desc
}),
470 README
=> html_esc
($self->{README
}),
472 users
=> $self->{users
},
473 notifymail
=> html_esc
($self->{notifymail
}),
474 notifyjson
=> html_esc
($self->{notifyjson
}),
475 notifycia
=> html_esc
($self->{notifycia
}),
479 # return true if $enc_passwd is a match for $plain_passwd
480 my $_check_passwd_match = sub {
481 my $enc_passwd = shift;
482 my $plain_passwd = shift;
483 defined($enc_passwd) or $enc_passwd = '';
484 defined($plain_passwd) or $plain_passwd = '';
485 # $enc_passwd may be crypt or crypt_sha1
486 if ($enc_passwd =~ m
(^\
$sha1\
$(\d
+)\
$([./0-9A-Za-z]{1,64})\$[./0-9A
-Za
-z
]{28}$)) {
487 # It's using sha1-crypt
488 return $enc_passwd eq crypt_sha1
($plain_passwd, $2, -(0+$1));
491 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
499 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
500 defined($self->{cpwd
}) or $self->{cpwd
} = '';
501 unless ($_check_passwd_match->($self->{ccrypt
}, $self->{cpwd
})) {
502 $gcgi->err("Your admin password does not match!");
512 $self->_mkdir_forkees;
514 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
515 if ($Girocco::Config
::owning_group
) {
516 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
517 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
518 chmod(02775, $self->{path
}) or die "chmod 02775 $self->{path} failed: $!";
520 chmod(02777, $self->{path
}) or die "chmod 02777 $self->{path} failed: $!";
522 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
523 or die "git init $self->{path} failed: $?";
524 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
525 or die "disabling receive.denyNonFastforwards failed: $?";
526 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
527 or die "disabling gc.auto failed: $?";
529 # /info must have right permissions,
530 # and git init didn't do it for some reason.
531 if ($Girocco::Config
::owning_group
) {
532 chmod(02775, $self->{path
}."/info") or die "chmod 02775 $self->{path}/info failed: $!";
534 chmod(02777, $self->{path
}."/info") or die "chmod 02777 $self->{path}/info failed: $!";
537 $self->_properties_save;
538 $self->_alternates_setup;
540 $self->_group_remove;
541 $self->_group_add($pushers);
542 $self->_hooks_install;
543 #$self->perm_initialize;
544 $self->_update_index;
552 $self->perm_initialize;
560 if ($Girocco::Config
::mob
&& $Girocco::Config
::mob
eq "mob") {
561 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name
}) == 0
562 or die "create-personal-mob-area $self->{name} failed";
564 $self->perm_initialize;
570 unlink ($self->_clonefail_path()); # Ignore EEXIST error
571 unlink ($self->_clonelog_path()); # Ignore EEXIST error
574 my $sock = IO
::Socket
::UNIX
->new($Girocco::Config
::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
575 $sock->print("clone ".$self->{name
}."\n");
576 # Just ignore reply, we are going to succeed anyway and the I/O
577 # would apparently get quite hairy.
586 $self->_properties_save;
587 $self->_group_update;
589 if (exists($self->{tags_to_delete
})) {
590 $self->delete_ctag($_) foreach(@
{$self->{tags_to_delete
}});
593 $self->set_HEAD($self->{HEAD
}) unless $self->{orig_HEAD
} eq $self->{HEAD
};
595 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
596 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
597 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
598 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
600 $self->_update_index if $self->{email
} ne $self->{orig_email
};
601 $self->{orig_email
} = $self->{email
};
606 sub update_password
{
610 $self->{crypt} = scrypt_sha1
($pwd);
611 $self->_group_update;
614 # You can explicitly do this just on a ghost() repository too.
618 if (-d
$self->{path
}) {
619 system('rm', '-rf', $self->{path
}) == 0
620 or die "rm -rf $self->{path} failed: $?";
622 # attempt to clean up any empty fork directories by removing them
623 my @pelems = split('/', $self->{name
});
624 while (@pelems > 1) {
627 rmdir join('/', $Girocco::Config
::reporoot
, @pelems) or last;
629 $self->_group_remove;
630 $self->_update_index;
633 sub _contains_files
{
635 (-d
$dir) or return 0;
636 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
637 while (my $entry = readdir($dh)) {
638 next if $entry eq '' || $entry eq '.' || $entry eq '..';
639 closedir($dh), return 1
640 if -f
"$dir/$entry" ||
641 -d
"$dir/$entry" && _contains_files
("$dir/$entry");
650 return _contains_files
($Girocco::Config
::reporoot
.'/'.$self->{name
});
654 # A project is considered empty if the git repository does not
655 # have any refs. This means packed-refs does not exist or is
656 # empty or only has lines starting with '#' AND there are no
657 # files in the refs subdirectory hierarchy (no matter how deep).
661 (-d
$self->{path
}) or return 0;
662 if (-e
$self->{path
}.'/packed-refs') {
663 open(my $pr, '<', $self->{path
}.'/packed-refs')
664 or die "open $self->{path}./packed-refs failed: $!";
666 while (my $ref = <$pr>) {
667 next if $ref =~ /^#/;
672 return 0 if $foundref;
674 (-d
$self->{path
}.'/refs') or return 1;
675 return !_contains_files
($self->{path
}.'/refs');
682 # sanity check, disallow filenames starting with . .. or /
683 unlink($self->{path
}.'/ctags/'.$ctag) if($ctag !~ m
|^(\
.\
.?
)/|);
689 opendir(my $dh, $self->{path
}.'/ctags')
691 @ctags = grep { -f
"$self->{path}/ctags/$_" } readdir($dh);
699 open($fh, '-|', "$Girocco::Config::git_bin --git-dir=$self->{path} show-ref --heads") or die "could not get list of heads";
703 next if !m
#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
712 my $HEAD = `$Girocco::Config::git_bin --git-dir=$self->{path} symbolic-ref HEAD`;
714 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
721 # Cursory checks only -- if you want to break your HEAD, be my guest
722 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
723 die "grossly invalid new HEAD: $newHEAD";
725 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic
-ref', 'HEAD
', "refs/heads/$newHEAD");
726 die "could not set HEAD" if ($? >> 8);
727 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob
'
728 or system('cp
', '-p
', '-f
', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
734 $type = 'REPO
' unless $type && $type =~ /^[A-Z]+$/;
736 $self->{authtype} = $type;
739 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
741 my $expire = time + 24 * 3600;
742 my $propval = "# ${type}AUTH $self->{auth} $expire";
743 system($Girocco::Config::git_bin, '--git
-dir
='.$self->{path}, 'config
', 'gitweb
.repoauth
', $propval);
750 delete $self->{auth};
751 delete $self->{authtype};
752 system($Girocco::Config::git_bin, '--git
-dir
='.$self->{path}, 'config
', '--unset
', 'gitweb
.repoauth
');
759 my $before_count = @{$self->{users}};
760 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
761 return @{$self->{users}} != $before_count;
766 sub get_forkee_name {
768 (m#^(.*)/.*?$#)[0]; #
771 sub get_forkee_path {
772 no warnings; # avoid silly 'unsuccessful
stat on filename with
\n' warning
773 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git
';
774 -d $forkee ? $forkee : '';
777 sub _valid_name_characters {
783 and m#^[a-zA-Z0-9+./_-]+$#;
787 no warnings; # avoid silly 'unsuccessful
stat on filename with
\n' warning
789 _valid_name_characters($_) and not exists($reservedprojectnames{$_})
790 and ((not m#/#) or -d get_forkee_path($_)); # will also catch ^/
793 # It's possible that some forks have been kept but the forkee is gone
.
794 # In this case the standard valid_name check is too strict.
796 no warnings
; # avoid silly 'unsuccessful stat on filename with \n' warning
797 my ($name, $nodie) = @_;
799 _valid_name_characters
($name)
800 and ((not $name =~ m
#/#)
801 or -d get_forkee_path
($name)
802 or -d
$Girocco::Config
::reporoot
.'/'.get_forkee_name
($name)));
803 (!$okay && $nodie) and return undef;
804 !$okay and die "tried to query for project with invalid name $name!";
805 (-d
$Girocco::Config
::reporoot
."/$name.git");
812 open F
, '<', jailed_file
("/etc/group") or die "getting project list failed: $!";
816 next if ($_[2] < 65536);
818 push @projects, $_[0];