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 = `git --git-dir="$self->{path}" config "gitweb.$pname"`;
94 } elsif ($pname =~ s/^%//) {
95 my $val = `git --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('git', '--git-dir='.$self->{path
}, 'config', "gitweb.$pname", $value);
116 } elsif ($pname =~ s/^%//) {
117 system('git', '--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 = `git --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 = `git --git-dir="$self->{path}" config "gitweb.delauth"`;
142 if ($val =~ /^# DELAUTH ([0-9a-f]+) (\d+)/) {
144 $self->{auth
} = $1 unless (time >= $expire);
148 sub _properties_save
{
150 foreach my $prop (keys %propmap) {
151 $self->_property_fput($prop, $self->{$prop});
153 $self->{statusupdates
} = 1
154 unless defined($self->{statusupdates
}) && $self->{statusupdates
} =~ /^\d+$/;
155 system('git', '--git-dir='.$self->{path
}, 'config', '--bool',
156 "gitweb.statusupdates", $self->{statusupdates
});
161 $self->_property_path('.nofetch');
167 my $nf = $self->_nofetch_path;
169 open X
, '>', $nf or die "nofetch failed: $!";
172 unlink $nf or die "yesfetch failed: $!";
178 $self->_property_path('.clonelog');
181 sub _clonefail_path
{
183 $self->_property_path('.clone_failed');
188 $self->_property_path('.clone_in_progress');
194 my $np = $self->_clonep_path;
196 open X
, '>', $np or die "clonep failed: $!";
199 unlink $np or die "clonef failed: $!";
202 sub _alternates_setup
{
204 return unless $self->{name
} =~ m
#/#;
205 my $forkee_name = get_forkee_name
($self->{name
});
206 my $forkee_path = get_forkee_path
($self->{name
});
207 return unless -d
$forkee_path;
208 mkdir $self->{path
}.'/refs'; chmod 02775, $self->{path
}.'/refs';
209 mkdir $self->{path
}.'/objects'; chmod 02775, $self->{path
}.'/objects';
210 mkdir $self->{path
}.'/objects/info'; chmod 02775, $self->{path
}.'/objects/info';
212 # We set up both alternates and http_alternates since we cannot use
213 # relative path in alternates - that doesn't work recursively.
215 my $filename = $self->{path
}.'/objects/info/alternates';
216 open X
, '>', $filename or die "alternates failed: $!";
217 print X
"$forkee_path/objects\n";
219 chmod 0664, $filename or warn "cannot chmod $filename: $!";
221 if ($Girocco::Config
::httppullurl
) {
222 $filename = $self->{path
}.'/objects/info/http-alternates';
223 open X
, '>', $filename or die "http-alternates failed: $!";
224 my $upfork = $forkee_name;
225 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
227 chmod 0664, $filename or warn "cannot chmod $filename: $!";
230 # The symlink is problematic since git remote prune will traverse it.
231 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
233 # copy refs from parent project
234 # ownership information is changed by a root cronjob
235 system("cp -pR $forkee_path/refs $self->{path}/");
236 # initialize HEAD, hacky version
237 system("cp $forkee_path/HEAD $self->{path}/HEAD");
242 my $perms = $Girocco::Config
::permission_control
eq 'Hooks' ?
02777 : 02775;
243 mkdir $self->{path
}.'/ctags'; chmod $perms, $self->{path
}.'/ctags';
249 $xtra .= join(',', @
{$self->{users
}});
250 filedb_atomic_append
(jailed_file
('/etc/group'),
251 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
256 my $xtra = join(',', @
{$self->{users
}});
257 filedb_atomic_edit
(jailed_file
('/etc/group'),
261 if ($self->{name
} eq (split /:/)[0]) {
262 # preserve readonly flag
263 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
264 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
274 filedb_atomic_edit
(jailed_file
('/etc/group'),
276 $self->{name
} ne (split /:/)[0] and return $_;
284 $self->{path
}.'/hooks/'.$name;
290 open SRC
, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
291 open DST
, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
292 while (<SRC
>) { print DST
$_; }
295 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
300 foreach my $hook ('post-receive', 'update', 'post-update') {
301 $self->_hook_install($hook);
305 # private constructor, do not use
308 my ($name, $base_path, $path) = @_;
309 does_exist
($name,1) || valid_name
($name) or die "refusing to create project with invalid name ($name)!";
310 $path ||= "$base_path/$name.git";
311 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
316 # public constructor #0
317 # creates a virtual project not connected to disk image
318 # you can conjure() it later to disk
321 my ($name, $mirror) = @_;
322 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
324 $self->{mirror
} = $mirror;
325 $self->{email
} = $self->{orig_email
} = '';
329 # public constructor #1
332 my $name = shift || '';
334 open F
, '<', jailed_file
("/etc/group") or die "project load failed: $!";
338 next unless (shift eq $name);
340 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
341 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
344 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
346 $self->{users
} = [split /,/, $ulist];
347 $self->{HEAD
} = $self->get_HEAD;
348 $self->{orig_HEAD
} = $self->{HEAD
};
349 $self->{orig_users
} = [@
{$self->{users
}}];
350 $self->{mirror
} = ! -e
$self->_nofetch_path;
351 $self->{clone_in_progress
} = -e
$self->_clonep_path;
352 $self->{clone_logged
} = -e
$self->_clonelog_path;
353 $self->{clone_failed
} = -e
$self->_clonefail_path;
354 $self->{ccrypt
} = $self->{crypt};
356 $self->_properties_load;
357 $self->{orig_email
} = $self->{email
};
364 # $proj may not be in sane state if this returns false!
368 my $cgi = $gcgi->cgi;
370 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
371 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
372 if ($pwd or not $self->{crypt}) {
373 $self->{crypt} = scrypt_sha1
($pwd);
376 if ($pwd2 and $pwd ne $pwd2) {
377 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
380 $self->{cpwd
} = $cgi->param('cpwd');
382 if ($Girocco::Config
::project_owners
eq 'email') {
383 $self->{email
} = $gcgi->wparam('email');
384 valid_email
($self->{email
})
385 or $gcgi->err("Your email sure looks weird...?");
388 $self->{url
} = $gcgi->wparam('url');
390 valid_repo_url
($self->{url
})
391 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
394 $self->{desc
} = $gcgi->wparam('desc');
395 length($self->{desc
}) <= 1024
396 or $gcgi->err("<b>Short</b> description length > 1kb!");
398 $self->{README
} = $gcgi->wparam('README');
399 length($self->{README
}) <= 8192
400 or $gcgi->err("README length > 8kb!");
402 $self->{hp
} = $gcgi->wparam('hp');
404 valid_web_url
($self->{hp
})
405 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
408 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) && Girocco
::User
::does_exist
($_) } $cgi->param('user')];
410 $self->{HEAD
} = $cgi->param('HEAD') if $cgi->param('HEAD');
412 # schedule deletion of tags (will be committed by update() after auth)
413 $self->{tags_to_delete
} = [$cgi->param('tags')];
415 $self->{notifymail
} = $gcgi->wparam('notifymail');
416 if ($self->{notifymail
}) {
417 (valid_email_multi
($self->{notifymail
}) and length($self->{notifymail
}) <= 512)
418 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
421 $self->{notifyjson
} = $gcgi->wparam('notifyjson');
422 if ($self->{notifyjson
}) {
423 valid_web_url
($self->{notifyjson
})
424 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
427 $self->{notifycia
} = $gcgi->wparam('notifycia');
428 if ($self->{notifycia
}) {
429 $self->{notifycia
} =~ /^[a-zA-Z0-9._-]+$/
430 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
433 if ($cgi->param('setstatusupdates')) {
434 my $val = $gcgi->wparam('statusupdates') || '0';
435 $self->{statusupdates
} = $val ?
1 : 0;
438 not $gcgi->err_check;
444 name
=> $self->{name
},
445 email
=> $self->{email
},
447 desc
=> html_esc
($self->{desc
}),
448 README
=> html_esc
($self->{README
}),
450 users
=> $self->{users
},
451 notifymail
=> html_esc
($self->{notifymail
}),
452 notifyjson
=> html_esc
($self->{notifyjson
}),
453 notifycia
=> html_esc
($self->{notifycia
}),
457 # return true if $enc_passwd is a match for $plain_passwd
458 my $_check_passwd_match = sub {
459 my $enc_passwd = shift || '';
460 my $plain_passwd = shift || '';
461 # $enc_passwd may be crypt or crypt_sha1
462 if ($enc_passwd =~ m
(^\
$sha1\
$(\d
+)\
$([./0-9A-Za-z]{1,64})\$[./0-9A
-Za
-z
]{28}$)) {
463 # It's using sha1-crypt
464 return $enc_passwd eq crypt_sha1
($plain_passwd, $2, -(0+$1));
467 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
475 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
476 $self->{cpwd
} ||= '';
477 unless ($_check_passwd_match->($self->{ccrypt
}, $self->{cpwd
})) {
478 $gcgi->err("Your admin password does not match!");
488 $self->_mkdir_forkees;
490 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
491 if ($Girocco::Config
::owning_group
) {
492 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
493 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
494 chmod(02775, $self->{path
}) or die "chmod 02775 $self->{path} failed: $!";
496 chmod(02777, $self->{path
}) or die "chmod 02777 $self->{path} failed: $!";
498 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
499 or die "git init $self->{path} failed: $?";
500 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
501 or die "disabling receive.denyNonFastforwards failed: $?";
502 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
503 or die "disabling gc.auto failed: $?";
505 # /info must have right permissions,
506 # and git init didn't do it for some reason.
507 if ($Girocco::Config
::owning_group
) {
508 chmod(02775, $self->{path
}."/info") or die "chmod 02775 $self->{path}/info failed: $!";
510 chmod(02777, $self->{path
}."/info") or die "chmod 02777 $self->{path}/info failed: $!";
513 $self->_properties_save;
514 $self->_alternates_setup;
516 $self->_group_remove;
517 $self->_group_add($pushers);
518 $self->_hooks_install;
519 #$self->perm_initialize;
520 $self->_update_index;
528 $self->perm_initialize;
536 if ($Girocco::Config
::mob
&& $Girocco::Config
::mob
eq "mob") {
537 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name
}) == 0
538 or die "create-personal-mob-area $self->{name} failed";
540 $self->perm_initialize;
546 unlink ($self->_clonefail_path()); # Ignore EEXIST error
547 unlink ($self->_clonelog_path()); # Ignore EEXIST error
550 my $sock = IO
::Socket
::UNIX
->new($Girocco::Config
::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
551 $sock->print("clone ".$self->{name
}."\n");
552 # Just ignore reply, we are going to succeed anyway and the I/O
553 # would apparently get quite hairy.
562 $self->_properties_save;
563 $self->_group_update;
565 if (exists($self->{tags_to_delete
})) {
566 $self->delete_ctag($_) foreach(@
{$self->{tags_to_delete
}});
569 $self->set_HEAD($self->{HEAD
}) unless $self->{orig_HEAD
} eq $self->{HEAD
};
571 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
572 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
573 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
574 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
576 $self->_update_index if $self->{email
} ne $self->{orig_email
};
577 $self->{orig_email
} = $self->{email
};
582 sub update_password
{
586 $self->{crypt} = scrypt_sha1
($pwd);
587 $self->_group_update;
590 # You can explicitly do this just on a ghost() repository too.
594 if (-d
$self->{path
}) {
595 system('rm', '-rf', $self->{path
}) == 0
596 or die "rm -rf $self->{path} failed: $?";
598 # attempt to clean up any empty fork directories by removing them
599 my @pelems = split('/', $self->{name
});
600 while (@pelems > 1) {
603 rmdir join('/', $Girocco::Config
::reporoot
, @pelems) or last;
605 $self->_group_remove;
606 $self->_update_index;
609 sub _contains_files
{
611 (-d
$dir) or return 0;
612 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
613 while (my $entry = readdir($dh)) {
614 next if $entry eq '' || $entry eq '.' || $entry eq '..';
615 closedir($dh), return 1
616 if -f
"$dir/$entry" ||
617 -d
"$dir/$entry" && _contains_files
("$dir/$entry");
626 return _contains_files
($Girocco::Config
::reporoot
.'/'.$self->{name
});
630 # A project is considered empty if the git repository does not
631 # have any refs. This means packed-refs does not exist or is
632 # empty or only has lines starting with '#' AND there are no
633 # files in the refs subdirectory hierarchy (no matter how deep).
637 (-d
$self->{path
}) or return 0;
638 if (-e
$self->{path
}.'/packed-refs') {
639 open(my $pr, '<', $self->{path
}.'/packed-refs')
640 or die "open $self->{path}./packed-refs failed: $!";
642 while (my $ref = <$pr>) {
643 next if $ref =~ /^#/;
648 return 0 if $foundref;
650 (-d
$self->{path
}.'/refs') or return 1;
651 return !_contains_files
($self->{path
}.'/refs');
658 # sanity check, disallow filenames starting with . .. or /
659 unlink($self->{path
}.'/ctags/'.$ctag) if($ctag !~ m
|^(\
.\
.?
)/|);
665 opendir(my $dh, $self->{path
}.'/ctags')
667 @ctags = grep { -f
"$self->{path}/ctags/$_" } readdir($dh);
675 open($fh, '-|', "$Girocco::Config::git_bin --git-dir=$self->{path} show-ref --heads") or die "could not get list of heads";
679 next if !m
#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
688 my $HEAD = `$Girocco::Config::git_bin --git-dir=$self->{path} symbolic-ref HEAD`;
690 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
697 # Cursory checks only -- if you want to break your HEAD, be my guest
698 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
699 die "grossly invalid new HEAD: $newHEAD";
701 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic
-ref', 'HEAD
', "refs/heads/$newHEAD");
702 die "could not set HEAD" if ($? >> 8);
703 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob
'
704 or system('cp
', '-p
', '-f
', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
712 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
714 my $expire = time + 24 * 3600;
715 my $propval = "# DELAUTH $self->{auth} $expire";
716 system('git
', '--git
-dir
='.$self->{path}, 'config
', 'gitweb
.delauth
', $propval);
723 delete $self->{auth};
724 system('git
', '--git
-dir
='.$self->{path}, 'config
', '--unset
', 'gitweb
.delauth
');
731 my $before_count = @{$self->{users}};
732 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
733 return @{$self->{users}} != $before_count;
738 sub get_forkee_name {
740 (m#^(.*)/.*?$#)[0]; #
743 sub get_forkee_path {
744 no warnings; # avoid silly 'unsuccessful
stat on filename with
\n' warning
745 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git
';
746 -d $forkee ? $forkee : '';
749 sub _valid_name_characters {
755 and m#^[a-zA-Z0-9+./_-]+$#;
759 no warnings; # avoid silly 'unsuccessful
stat on filename with
\n' warning
761 _valid_name_characters($_)
762 and ((not m#/#) or -d get_forkee_path($_)); # will also catch ^/
765 # It's possible that some forks have been kept but the forkee is gone
.
766 # In this case the standard valid_name check is too strict.
768 no warnings
; # avoid silly 'unsuccessful stat on filename with \n' warning
769 my ($name, $nodie) = @_;
771 _valid_name_characters
($name)
772 and ((not $name =~ m
#/#)
773 or -d get_forkee_path
($name)
774 or -d
$Girocco::Config
::reporoot
.'/'.get_forkee_name
($name)));
775 (!$okay && $nodie) and return undef;
776 !$okay and die "tried to query for project with invalid name $name!";
777 (-d
$Girocco::Config
::reporoot
."/$name.git");
784 open F
, '<', jailed_file
("/etc/group") or die "getting project list failed: $!";
788 next if ($_[2] < 65536);
790 push @projects, $_[0];