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',
75 system($Girocco::Config
::basedir
. '/gitweb/genindex.sh');
81 $self->{path
}.'/'.$name;
87 my $pname = $propmap{$name};
88 $pname = $propmapro{$name} unless $pname;
89 $pname or die "unknown property: $name";
90 if ($pname =~ s/^://) {
91 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "gitweb.$pname"`;
94 } elsif ($pname =~ s/^%//) {
95 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "$pname"`;
100 open P
, '<', $self->_property_path($pname) or return undef;
103 my $value = join('', @value); chomp $value;
109 my ($name, $value) = @_;
110 my $pname = $propmap{$name};
111 $pname or die "unknown property: $name";
113 if ($pname =~ s/^://) {
114 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', "gitweb.$pname", $value);
116 } elsif ($pname =~ s/^%//) {
117 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', $pname, $value);
121 my $P = lock_file
($self->_property_path($pname));
122 $value ne '' and print $P "$value\n";
124 unlock_file
($self->_property_path($pname));
127 sub _properties_load
{
129 foreach my $prop (keys %propmap) {
130 $self->{$prop} = $self->_property_fget($prop);
132 foreach my $prop (keys %propmapro) {
133 $self->{$prop} = $self->_property_fget($prop);
135 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config --bool "gitweb.statusupdates" 2>/dev/null`;
137 $val = ($val eq 'false') ?
0 : 1;
138 $self->{statusupdates
} = $val;
139 delete $self->{auth
};
140 $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "gitweb.repoauth"`;
142 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
144 if (time < $expire) {
145 $self->{authtype
} = $1;
151 sub _properties_save
{
153 foreach my $prop (keys %propmap) {
154 $self->_property_fput($prop, $self->{$prop});
156 $self->{statusupdates
} = 1
157 unless defined($self->{statusupdates
}) && $self->{statusupdates
} =~ /^\d+$/;
158 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', '--bool',
159 "gitweb.statusupdates", $self->{statusupdates
});
164 $self->_property_path('.nofetch');
170 my $nf = $self->_nofetch_path;
172 open X
, '>', $nf or die "nofetch failed: $!";
175 unlink $nf or die "yesfetch failed: $!";
181 $self->_property_path('.clonelog');
184 sub _clonefail_path
{
186 $self->_property_path('.clone_failed');
191 $self->_property_path('.clone_in_progress');
197 my $np = $self->_clonep_path;
199 open X
, '>', $np or die "clonep failed: $!";
202 unlink $np or die "clonef failed: $!";
205 sub _alternates_setup
{
207 return unless $self->{name
} =~ m
#/#;
208 my $forkee_name = get_forkee_name
($self->{name
});
209 my $forkee_path = get_forkee_path
($self->{name
});
210 return unless -d
$forkee_path;
211 mkdir $self->{path
}.'/refs'; chmod 02775, $self->{path
}.'/refs';
212 mkdir $self->{path
}.'/objects'; chmod 02775, $self->{path
}.'/objects';
213 mkdir $self->{path
}.'/objects/info'; chmod 02775, $self->{path
}.'/objects/info';
215 # We set up both alternates and http_alternates since we cannot use
216 # relative path in alternates - that doesn't work recursively.
218 my $filename = $self->{path
}.'/objects/info/alternates';
219 open X
, '>', $filename or die "alternates failed: $!";
220 print X
"$forkee_path/objects\n";
222 chmod 0664, $filename or warn "cannot chmod $filename: $!";
224 if ($Girocco::Config
::httppullurl
) {
225 $filename = $self->{path
}.'/objects/info/http-alternates';
226 open X
, '>', $filename or die "http-alternates failed: $!";
227 my $upfork = $forkee_name;
228 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
230 chmod 0664, $filename or warn "cannot chmod $filename: $!";
233 # The symlink is problematic since git remote prune will traverse it.
234 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
236 # copy refs from parent project
237 # ownership information is changed by a root cronjob
238 system("cp -pR $forkee_path/refs $self->{path}/");
239 # initialize HEAD, hacky version
240 system("cp $forkee_path/HEAD $self->{path}/HEAD");
245 my $perms = $Girocco::Config
::permission_control
eq 'Hooks' ?
02777 : 02775;
246 mkdir $self->{path
}.'/ctags'; chmod $perms, $self->{path
}.'/ctags';
252 $xtra .= join(',', @
{$self->{users
}});
253 filedb_atomic_append
(jailed_file
('/etc/group'),
254 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
259 my $xtra = join(',', @
{$self->{users
}});
260 filedb_atomic_edit
(jailed_file
('/etc/group'),
264 if ($self->{name
} eq (split /:/)[0]) {
265 # preserve readonly flag
266 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
267 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
277 filedb_atomic_edit
(jailed_file
('/etc/group'),
279 $self->{name
} ne (split /:/)[0] and return $_;
287 $self->{path
}.'/hooks/'.$name;
293 open SRC
, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
294 open DST
, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
295 while (<SRC
>) { print DST
$_; }
298 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
303 foreach my $hook ('post-receive', 'update', 'post-update') {
304 $self->_hook_install($hook);
308 # private constructor, do not use
311 my ($name, $base_path, $path) = @_;
312 does_exist
($name,1) || valid_name
($name) or die "refusing to create project with invalid name ($name)!";
313 $path ||= "$base_path/$name.git";
314 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
319 # public constructor #0
320 # creates a virtual project not connected to disk image
321 # you can conjure() it later to disk
324 my ($name, $mirror) = @_;
325 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
327 $self->{mirror
} = $mirror;
328 $self->{email
} = $self->{orig_email
} = '';
332 # public constructor #1
335 my $name = shift || '';
337 open F
, '<', jailed_file
("/etc/group") or die "project load failed: $!";
341 next unless (shift eq $name);
343 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
344 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
347 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
349 $self->{users
} = [split /,/, $ulist];
350 $self->{HEAD
} = $self->get_HEAD;
351 $self->{orig_HEAD
} = $self->{HEAD
};
352 $self->{orig_users
} = [@
{$self->{users
}}];
353 $self->{mirror
} = ! -e
$self->_nofetch_path;
354 $self->{clone_in_progress
} = -e
$self->_clonep_path;
355 $self->{clone_logged
} = -e
$self->_clonelog_path;
356 $self->{clone_failed
} = -e
$self->_clonefail_path;
357 $self->{ccrypt
} = $self->{crypt};
359 $self->_properties_load;
360 $self->{orig_email
} = $self->{email
};
367 # $proj may not be in sane state if this returns false!
371 my $cgi = $gcgi->cgi;
373 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
374 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
375 if ($pwd or not $self->{crypt}) {
376 $self->{crypt} = scrypt_sha1
($pwd);
379 if ($pwd2 and $pwd ne $pwd2) {
380 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
383 $self->{cpwd
} = $cgi->param('cpwd');
385 if ($Girocco::Config
::project_owners
eq 'email') {
386 $self->{email
} = $gcgi->wparam('email');
387 valid_email
($self->{email
})
388 or $gcgi->err("Your email sure looks weird...?");
391 $self->{url
} = $gcgi->wparam('url');
393 valid_repo_url
($self->{url
})
394 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
397 $self->{desc
} = $gcgi->wparam('desc');
398 length($self->{desc
}) <= 1024
399 or $gcgi->err("<b>Short</b> description length > 1kb!");
401 $self->{README
} = $gcgi->wparam('README');
402 length($self->{README
}) <= 8192
403 or $gcgi->err("README length > 8kb!");
405 $self->{hp
} = $gcgi->wparam('hp');
407 valid_web_url
($self->{hp
})
408 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
411 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) && Girocco
::User
::does_exist
($_) } $cgi->param('user')];
413 $self->{HEAD
} = $cgi->param('HEAD') if $cgi->param('HEAD');
415 # schedule deletion of tags (will be committed by update() after auth)
416 $self->{tags_to_delete
} = [$cgi->param('tags')];
418 $self->{notifymail
} = $gcgi->wparam('notifymail');
419 if ($self->{notifymail
}) {
420 (valid_email_multi
($self->{notifymail
}) and length($self->{notifymail
}) <= 512)
421 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
424 $self->{notifyjson
} = $gcgi->wparam('notifyjson');
425 if ($self->{notifyjson
}) {
426 valid_web_url
($self->{notifyjson
})
427 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
430 $self->{notifycia
} = $gcgi->wparam('notifycia');
431 if ($self->{notifycia
}) {
432 $self->{notifycia
} =~ /^[a-zA-Z0-9._-]+$/
433 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
436 if ($cgi->param('setstatusupdates')) {
437 my $val = $gcgi->wparam('statusupdates') || '0';
438 $self->{statusupdates
} = $val ?
1 : 0;
441 not $gcgi->err_check;
447 name
=> $self->{name
},
448 email
=> $self->{email
},
450 desc
=> html_esc
($self->{desc
}),
451 README
=> html_esc
($self->{README
}),
453 users
=> $self->{users
},
454 notifymail
=> html_esc
($self->{notifymail
}),
455 notifyjson
=> html_esc
($self->{notifyjson
}),
456 notifycia
=> html_esc
($self->{notifycia
}),
460 # return true if $enc_passwd is a match for $plain_passwd
461 my $_check_passwd_match = sub {
462 my $enc_passwd = shift || '';
463 my $plain_passwd = shift || '';
464 # $enc_passwd may be crypt or crypt_sha1
465 if ($enc_passwd =~ m
(^\
$sha1\
$(\d
+)\
$([./0-9A-Za-z]{1,64})\$[./0-9A
-Za
-z
]{28}$)) {
466 # It's using sha1-crypt
467 return $enc_passwd eq crypt_sha1
($plain_passwd, $2, -(0+$1));
470 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
478 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
479 $self->{cpwd
} ||= '';
480 unless ($_check_passwd_match->($self->{ccrypt
}, $self->{cpwd
})) {
481 $gcgi->err("Your admin password does not match!");
491 $self->_mkdir_forkees;
493 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
494 if ($Girocco::Config
::owning_group
) {
495 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
496 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
497 chmod(02775, $self->{path
}) or die "chmod 02775 $self->{path} failed: $!";
499 chmod(02777, $self->{path
}) or die "chmod 02777 $self->{path} failed: $!";
501 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
502 or die "git init $self->{path} failed: $?";
503 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
504 or die "disabling receive.denyNonFastforwards failed: $?";
505 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
506 or die "disabling gc.auto failed: $?";
508 # /info must have right permissions,
509 # and git init didn't do it for some reason.
510 if ($Girocco::Config
::owning_group
) {
511 chmod(02775, $self->{path
}."/info") or die "chmod 02775 $self->{path}/info failed: $!";
513 chmod(02777, $self->{path
}."/info") or die "chmod 02777 $self->{path}/info failed: $!";
516 $self->_properties_save;
517 $self->_alternates_setup;
519 $self->_group_remove;
520 $self->_group_add($pushers);
521 $self->_hooks_install;
522 #$self->perm_initialize;
523 $self->_update_index;
531 $self->perm_initialize;
539 if ($Girocco::Config
::mob
&& $Girocco::Config
::mob
eq "mob") {
540 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name
}) == 0
541 or die "create-personal-mob-area $self->{name} failed";
543 $self->perm_initialize;
549 unlink ($self->_clonefail_path()); # Ignore EEXIST error
550 unlink ($self->_clonelog_path()); # Ignore EEXIST error
553 my $sock = IO
::Socket
::UNIX
->new($Girocco::Config
::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
554 $sock->print("clone ".$self->{name
}."\n");
555 # Just ignore reply, we are going to succeed anyway and the I/O
556 # would apparently get quite hairy.
565 $self->_properties_save;
566 $self->_group_update;
568 if (exists($self->{tags_to_delete
})) {
569 $self->delete_ctag($_) foreach(@
{$self->{tags_to_delete
}});
572 $self->set_HEAD($self->{HEAD
}) unless $self->{orig_HEAD
} eq $self->{HEAD
};
574 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
575 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
576 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
577 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
579 $self->_update_index if $self->{email
} ne $self->{orig_email
};
580 $self->{orig_email
} = $self->{email
};
585 sub update_password
{
589 $self->{crypt} = scrypt_sha1
($pwd);
590 $self->_group_update;
593 # You can explicitly do this just on a ghost() repository too.
597 if (-d
$self->{path
}) {
598 system('rm', '-rf', $self->{path
}) == 0
599 or die "rm -rf $self->{path} failed: $?";
601 # attempt to clean up any empty fork directories by removing them
602 my @pelems = split('/', $self->{name
});
603 while (@pelems > 1) {
606 rmdir join('/', $Girocco::Config
::reporoot
, @pelems) or last;
608 $self->_group_remove;
609 $self->_update_index;
612 sub _contains_files
{
614 (-d
$dir) or return 0;
615 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
616 while (my $entry = readdir($dh)) {
617 next if $entry eq '' || $entry eq '.' || $entry eq '..';
618 closedir($dh), return 1
619 if -f
"$dir/$entry" ||
620 -d
"$dir/$entry" && _contains_files
("$dir/$entry");
629 return _contains_files
($Girocco::Config
::reporoot
.'/'.$self->{name
});
633 # A project is considered empty if the git repository does not
634 # have any refs. This means packed-refs does not exist or is
635 # empty or only has lines starting with '#' AND there are no
636 # files in the refs subdirectory hierarchy (no matter how deep).
640 (-d
$self->{path
}) or return 0;
641 if (-e
$self->{path
}.'/packed-refs') {
642 open(my $pr, '<', $self->{path
}.'/packed-refs')
643 or die "open $self->{path}./packed-refs failed: $!";
645 while (my $ref = <$pr>) {
646 next if $ref =~ /^#/;
651 return 0 if $foundref;
653 (-d
$self->{path
}.'/refs') or return 1;
654 return !_contains_files
($self->{path
}.'/refs');
661 # sanity check, disallow filenames starting with . .. or /
662 unlink($self->{path
}.'/ctags/'.$ctag) if($ctag !~ m
|^(\
.\
.?
)/|);
668 opendir(my $dh, $self->{path
}.'/ctags')
670 @ctags = grep { -f
"$self->{path}/ctags/$_" } readdir($dh);
678 open($fh, '-|', "$Girocco::Config::git_bin --git-dir=$self->{path} show-ref --heads") or die "could not get list of heads";
682 next if !m
#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
691 my $HEAD = `$Girocco::Config::git_bin --git-dir=$self->{path} symbolic-ref HEAD`;
693 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
700 # Cursory checks only -- if you want to break your HEAD, be my guest
701 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
702 die "grossly invalid new HEAD: $newHEAD";
704 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic
-ref', 'HEAD
', "refs/heads/$newHEAD");
705 die "could not set HEAD" if ($? >> 8);
706 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob
'
707 or system('cp
', '-p
', '-f
', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
713 $type = 'REPO
' unless $type && $type =~ /^[A-Z]+$/;
715 $self->{authtype} = $type;
718 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
720 my $expire = time + 24 * 3600;
721 my $propval = "# ${type}AUTH $self->{auth} $expire";
722 system($Girocco::Config::git_bin, '--git
-dir
='.$self->{path}, 'config
', 'gitweb
.repoauth
', $propval);
729 delete $self->{auth};
730 delete $self->{authtype};
731 system($Girocco::Config::git_bin, '--git
-dir
='.$self->{path}, 'config
', '--unset
', 'gitweb
.repoauth
');
738 my $before_count = @{$self->{users}};
739 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
740 return @{$self->{users}} != $before_count;
745 sub get_forkee_name {
747 (m#^(.*)/.*?$#)[0]; #
750 sub get_forkee_path {
751 no warnings; # avoid silly 'unsuccessful
stat on filename with
\n' warning
752 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git
';
753 -d $forkee ? $forkee : '';
756 sub _valid_name_characters {
762 and m#^[a-zA-Z0-9+./_-]+$#;
766 no warnings; # avoid silly 'unsuccessful
stat on filename with
\n' warning
768 _valid_name_characters($_)
769 and ((not m#/#) or -d get_forkee_path($_)); # will also catch ^/
772 # It's possible that some forks have been kept but the forkee is gone
.
773 # In this case the standard valid_name check is too strict.
775 no warnings
; # avoid silly 'unsuccessful stat on filename with \n' warning
776 my ($name, $nodie) = @_;
778 _valid_name_characters
($name)
779 and ((not $name =~ m
#/#)
780 or -d get_forkee_path
($name)
781 or -d
$Girocco::Config
::reporoot
.'/'.get_forkee_name
($name)));
782 (!$okay && $nodie) and return undef;
783 !$okay and die "tried to query for project with invalid name $name!";
784 (-d
$Girocco::Config
::reporoot
."/$name.git");
791 open F
, '<', jailed_file
("/etc/group") or die "getting project list failed: $!";
795 next if ($_[2] < 65536);
797 push @projects, $_[0];