1 package Girocco
::Project
;
10 use Girocco
::ProjPerm
;
12 use base
('Girocco::ProjPerm::'.$Girocco::Config
::permission_control
); # mwahaha
27 require Digest
::SHA
::PurePerl
;
28 Digest
::SHA
::PurePerl
->import(
31 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
32 . "must be available\n";
35 our $metadata_fields = {
36 homepage
=> ['Homepage URL', 'hp', 'text'],
37 shortdesc
=> ['Short description', 'desc', 'text'],
38 README
=> ['README (HTML, lt 8kb)', 'README', 'textarea'],
39 notifymail
=> ['Commit notify - mail to', 'notifymail', 'text'],
40 notifyjson
=> ['Commit notify - <a href="http://help.github.com/post-receive-hooks/">POST JSON</a> at', 'notifyjson', 'text'],
41 notifycia
=> ['Commit notify - <a href="http://cia.vc/doc/">CIA project</a> name', 'notifycia', 'text'],
46 my @pelems = split('/', $self->{name
});
47 pop @pelems; # do not create dir for the project itself
48 my $path = $self->{base_path
};
49 foreach my $pelem (@pelems) {
51 (-d
"$path") or mkdir $path or die "mkdir $path: $!";
52 chmod 02775, $path; # ok if fails (dir may already exist and be owned by someone else)
59 desc
=> 'description',
60 README
=> 'README.html',
62 notifymail
=> '%hooks.mailinglist',
63 notifytag
=> '%hooks.announcelist',
64 notifyjson
=> '%hooks.jsonurl',
65 notifycia
=> '%hooks.cianame',
69 lastchange
=> ':lastchange',
70 lastactivity
=> 'info/lastactivity',
74 system($Girocco::Config
::basedir
. '/gitweb/genindex.sh');
80 $self->{path
}.'/'.$name;
86 my $pname = $propmap{$name};
87 $pname = $propmapro{$name} unless $pname;
88 $pname or die "unknown property: $name";
89 if ($pname =~ s/^://) {
90 my $val = `git --git-dir="$self->{path}" config "gitweb.$pname"`;
93 } elsif ($pname =~ s/^%//) {
94 my $val = `git --git-dir="$self->{path}" config "$pname"`;
99 open P
, '<', $self->_property_path($pname) or return undef;
102 my $value = join('', @value); chomp $value;
108 my ($name, $value) = @_;
109 my $pname = $propmap{$name};
110 $pname or die "unknown property: $name";
112 if ($pname =~ s/^://) {
113 system('git', '--git-dir='.$self->{path
}, 'config', "gitweb.$pname", $value);
115 } elsif ($pname =~ s/^%//) {
116 system('git', '--git-dir='.$self->{path
}, 'config', $pname, $value);
120 my $P = lock_file
($self->_property_path($pname));
121 $value ne '' and print $P "$value\n";
123 unlock_file
($self->_property_path($pname));
126 sub _properties_load
{
128 foreach my $prop (keys %propmap) {
129 $self->{$prop} = $self->_property_fget($prop);
131 foreach my $prop (keys %propmapro) {
132 $self->{$prop} = $self->_property_fget($prop);
134 my $val = `git --git-dir="$self->{path}" config --bool "gitweb.statusupdates" 2>/dev/null`;
136 $val = ($val eq 'false') ?
0 : 1;
137 $self->{statusupdates
} = $val;
138 delete $self->{auth
};
139 $val = `git --git-dir="$self->{path}" config "gitweb.delauth"`;
141 if ($val =~ /^# DELAUTH ([0-9a-f]+) (\d+)/) {
143 $self->{auth
} = $1 unless (time >= $expire);
147 sub _properties_save
{
149 foreach my $prop (keys %propmap) {
150 $self->_property_fput($prop, $self->{$prop});
152 $self->{statusupdates
} = 1
153 unless defined($self->{statusupdates
}) && $self->{statusupdates
} =~ /^\d+$/;
154 system('git', '--git-dir='.$self->{path
}, 'config', '--bool',
155 "gitweb.statusupdates", $self->{statusupdates
});
160 $self->_property_path('.nofetch');
166 my $nf = $self->_nofetch_path;
168 open X
, '>', $nf or die "nofetch failed: $!";
171 unlink $nf or die "yesfetch failed: $!";
177 $self->_property_path('.clonelog');
180 sub _clonefail_path
{
182 $self->_property_path('.clone_failed');
187 $self->_property_path('.clone_in_progress');
193 my $np = $self->_clonep_path;
195 open X
, '>', $np or die "clonep failed: $!";
198 unlink $np or die "clonef failed: $!";
201 sub _alternates_setup
{
203 return unless $self->{name
} =~ m
#/#;
204 my $forkee_name = get_forkee_name
($self->{name
});
205 my $forkee_path = get_forkee_path
($self->{name
});
206 return unless -d
$forkee_path;
207 mkdir $self->{path
}.'/refs'; chmod 02775, $self->{path
}.'/refs';
208 mkdir $self->{path
}.'/objects'; chmod 02775, $self->{path
}.'/objects';
209 mkdir $self->{path
}.'/objects/info'; chmod 02775, $self->{path
}.'/objects/info';
211 # We set up both alternates and http_alternates since we cannot use
212 # relative path in alternates - that doesn't work recursively.
214 my $filename = $self->{path
}.'/objects/info/alternates';
215 open X
, '>', $filename or die "alternates failed: $!";
216 print X
"$forkee_path/objects\n";
218 chmod 0664, $filename or warn "cannot chmod $filename: $!";
220 if ($Girocco::Config
::httppullurl
) {
221 $filename = $self->{path
}.'/objects/info/http-alternates';
222 open X
, '>', $filename or die "http-alternates failed: $!";
223 my $upfork = $forkee_name;
224 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
226 chmod 0664, $filename or warn "cannot chmod $filename: $!";
229 # The symlink is problematic since git remote prune will traverse it.
230 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
232 # copy refs from parent project
233 # ownership information is changed by a root cronjob
234 system("cp -pR $forkee_path/refs $self->{path}/");
235 # initialize HEAD, hacky version
236 system("cp $forkee_path/HEAD $self->{path}/HEAD");
241 my $perms = $Girocco::Config
::permission_control
eq 'Hooks' ?
02777 : 02775;
242 mkdir $self->{path
}.'/ctags'; chmod $perms, $self->{path
}.'/ctags';
248 $xtra .= join(',', @
{$self->{users
}});
249 filedb_atomic_append
(jailed_file
('/etc/group'),
250 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
255 my $xtra = join(',', @
{$self->{users
}});
256 filedb_atomic_edit
(jailed_file
('/etc/group'),
260 if ($self->{name
} eq (split /:/)[0]) {
261 # preserve readonly flag
262 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
263 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
273 filedb_atomic_edit
(jailed_file
('/etc/group'),
275 $self->{name
} ne (split /:/)[0] and return $_;
283 $self->{path
}.'/hooks/'.$name;
289 open SRC
, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
290 open DST
, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
291 while (<SRC
>) { print DST
$_; }
294 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
299 foreach my $hook ('post-receive', 'update', 'post-update') {
300 $self->_hook_install($hook);
304 # private constructor, do not use
307 my ($name, $base_path, $path) = @_;
308 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
309 $path ||= "$base_path/$name.git";
310 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
315 # public constructor #0
316 # creates a virtual project not connected to disk image
317 # you can conjure() it later to disk
320 my ($name, $mirror) = @_;
321 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
323 $self->{mirror
} = $mirror;
324 $self->{email
} = $self->{orig_email
} = '';
328 # public constructor #1
331 my $name = shift || '';
333 open F
, '<', jailed_file
("/etc/group") or die "project load failed: $!";
337 next unless (shift eq $name);
339 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
340 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
343 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
345 $self->{users
} = [split /,/, $ulist];
346 $self->{HEAD
} = $self->get_HEAD;
347 $self->{orig_HEAD
} = $self->{HEAD
};
348 $self->{orig_users
} = [@
{$self->{users
}}];
349 $self->{mirror
} = ! -e
$self->_nofetch_path;
350 $self->{clone_in_progress
} = -e
$self->_clonep_path;
351 $self->{clone_logged
} = -e
$self->_clonelog_path;
352 $self->{clone_failed
} = -e
$self->_clonefail_path;
353 $self->{ccrypt
} = $self->{crypt};
355 $self->_properties_load;
356 $self->{orig_email
} = $self->{email
};
363 # $proj may not be in sane state if this returns false!
367 my $cgi = $gcgi->cgi;
369 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
370 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
371 if ($pwd or not $self->{crypt}) {
372 $self->{crypt} = scrypt
($pwd);
375 if ($pwd2 and $pwd ne $pwd2) {
376 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
379 $self->{cpwd
} = $cgi->param('cpwd');
381 if ($Girocco::Config
::project_owners
eq 'email') {
382 $self->{email
} = $gcgi->wparam('email');
383 valid_email
($self->{email
})
384 or $gcgi->err("Your email sure looks weird...?");
387 $self->{url
} = $gcgi->wparam('url');
389 valid_repo_url
($self->{url
})
390 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
393 $self->{desc
} = $gcgi->wparam('desc');
394 length($self->{desc
}) <= 1024
395 or $gcgi->err("<b>Short</b> description length > 1kb!");
397 $self->{README
} = $gcgi->wparam('README');
398 length($self->{README
}) <= 8192
399 or $gcgi->err("README length > 8kb!");
401 $self->{hp
} = $gcgi->wparam('hp');
403 valid_web_url
($self->{hp
})
404 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
407 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) && Girocco
::User
::does_exist
($_) } $cgi->param('user')];
409 $self->{HEAD
} = $cgi->param('HEAD') if $cgi->param('HEAD');
411 # schedule deletion of tags (will be committed by update() after auth)
412 $self->{tags_to_delete
} = [$cgi->param('tags')];
414 $self->{notifymail
} = $gcgi->wparam('notifymail');
415 if ($self->{notifymail
}) {
416 (valid_email_multi
($self->{notifymail
}) and length($self->{notifymail
}) <= 512)
417 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
420 $self->{notifyjson
} = $gcgi->wparam('notifyjson');
421 if ($self->{notifyjson
}) {
422 valid_web_url
($self->{notifyjson
})
423 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
426 $self->{notifycia
} = $gcgi->wparam('notifycia');
427 if ($self->{notifycia
}) {
428 $self->{notifycia
} =~ /^[a-zA-Z0-9._-]+$/
429 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
432 if ($cgi->param('setstatusupdates')) {
433 my $val = $gcgi->wparam('statusupdates') || '0';
434 $self->{statusupdates
} = $val ?
1 : 0;
437 not $gcgi->err_check;
443 name
=> $self->{name
},
444 email
=> $self->{email
},
446 desc
=> html_esc
($self->{desc
}),
447 README
=> html_esc
($self->{README
}),
449 users
=> $self->{users
},
450 notifymail
=> html_esc
($self->{notifymail
}),
451 notifyjson
=> html_esc
($self->{notifyjson
}),
452 notifycia
=> html_esc
($self->{notifycia
}),
460 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
461 $self->{cpwd
} ||= '';
462 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
463 $gcgi->err("Your admin password does not match!");
473 $self->_mkdir_forkees;
475 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
476 if ($Girocco::Config
::owning_group
) {
477 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
478 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
479 chmod(02775, $self->{path
}) or die "chmod 02775 $self->{path} failed: $!";
481 chmod(02777, $self->{path
}) or die "chmod 02777 $self->{path} failed: $!";
483 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
484 or die "git init $self->{path} failed: $?";
485 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
486 or die "disabling receive.denyNonFastforwards failed: $?";
487 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
488 or die "disabling gc.auto failed: $?";
490 # /info must have right permissions,
491 # and git init didn't do it for some reason.
492 if ($Girocco::Config
::owning_group
) {
493 chmod(02775, $self->{path
}."/info") or die "chmod 02775 $self->{path}/info failed: $!";
495 chmod(02777, $self->{path
}."/info") or die "chmod 02777 $self->{path}/info failed: $!";
498 $self->_properties_save;
499 $self->_alternates_setup;
501 $self->_group_remove;
502 $self->_group_add($pushers);
503 $self->_hooks_install;
504 #$self->perm_initialize;
505 $self->_update_index;
513 $self->perm_initialize;
521 if ($Girocco::Config
::mob
&& $Girocco::Config
::mob
eq "mob") {
522 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name
}) == 0
523 or die "create-personal-mob-area $self->{name} failed";
525 $self->perm_initialize;
531 unlink ($self->_clonefail_path()); # Ignore EEXIST error
532 unlink ($self->_clonelog_path()); # Ignore EEXIST error
535 my $sock = IO
::Socket
::UNIX
->new($Girocco::Config
::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
536 $sock->print("clone ".$self->{name
}."\n");
537 # Just ignore reply, we are going to succeed anyway and the I/O
538 # would apparently get quite hairy.
547 $self->_properties_save;
548 $self->_group_update;
550 if (exists($self->{tags_to_delete
})) {
551 $self->delete_ctag($_) foreach(@
{$self->{tags_to_delete
}});
554 $self->set_HEAD($self->{HEAD
}) unless $self->{orig_HEAD
} eq $self->{HEAD
};
556 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
557 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
558 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
559 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
561 $self->_update_index if $self->{email
} ne $self->{orig_email
};
562 $self->{orig_email
} = $self->{email
};
567 sub update_password
{
571 $self->{crypt} = scrypt
($pwd);
572 $self->_group_update;
575 # You can explicitly do this just on a ghost() repository too.
579 if (-d
$self->{path
}) {
580 system('rm', '-rf', $self->{path
}) == 0
581 or die "rm -rf $self->{path} failed: $?";
583 # attempt to clean up any empty fork directories by removing them
584 my @pelems = split('/', $self->{name
});
585 while (@pelems > 1) {
588 rmdir join('/', $Girocco::Config
::reporoot
, @pelems) or last;
590 $self->_group_remove;
591 $self->_update_index;
594 sub _contains_files
{
596 (-d
$dir) or return 0;
597 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
598 while (my $entry = readdir($dh)) {
599 next if $entry eq '' || $entry eq '.' || $entry eq '..';
600 closedir($dh), return 1
601 if -f
"$dir/$entry" ||
602 -d
"$dir/$entry" && _contains_files
("$dir/$entry");
611 return _contains_files
($Girocco::Config
::reporoot
.'/'.$self->{name
});
615 # A project is considered empty if the git repository does not
616 # have any refs. This means packed-refs does not exist or is
617 # empty or only has lines starting with '#' AND there are no
618 # files in the refs subdirectory hierarchy (no matter how deep).
622 (-d
$self->{path
}) or return 0;
623 if (-e
$self->{path
}.'/packed-refs') {
624 open(my $pr, '<', $self->{path
}.'/packed-refs')
625 or die "open $self->{path}./packed-refs failed: $!";
627 while (my $ref = <$pr>) {
628 next if $ref =~ /^#/;
633 return 0 if $foundref;
635 (-d
$self->{path
}.'/refs') or return 1;
636 return !_contains_files
($self->{path
}.'/refs');
643 # sanity check, disallow filenames starting with . .. or /
644 unlink($self->{path
}.'/ctags/'.$ctag) if($ctag !~ m
|^(\
.\
.?
)/|);
650 opendir(my $dh, $self->{path
}.'/ctags')
652 @ctags = grep { -f
"$self->{path}/ctags/$_" } readdir($dh);
660 open($fh, '-|', "$Girocco::Config::git_bin --git-dir=$self->{path} show-ref --heads") or die "could not get list of heads";
664 next if !m
#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
673 my $HEAD = `$Girocco::Config::git_bin --git-dir=$self->{path} symbolic-ref HEAD`;
675 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
682 # Cursory checks only -- if you want to break your HEAD, be my guest
683 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
684 die "grossly invalid new HEAD: $newHEAD";
686 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic
-ref', 'HEAD
', "refs/heads/$newHEAD");
687 die "could not set HEAD" if ($? >> 8);
688 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob
'
689 or system('cp
', '-p
', '-f
', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
697 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
699 my $expire = time + 24 * 3600;
700 my $propval = "# DELAUTH $self->{auth} $expire";
701 system('git
', '--git
-dir
='.$self->{path}, 'config
', 'gitweb
.delauth
', $propval);
708 delete $self->{auth};
709 system('git
', '--git
-dir
='.$self->{path}, 'config
', '--unset
', 'gitweb
.delauth
');
716 my $before_count = @{$self->{users}};
717 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
718 return @{$self->{users}} != $before_count;
723 sub get_forkee_name {
725 (m#^(.*)/.*?$#)[0]; #
728 sub get_forkee_path {
729 no warnings; # avoid silly 'unsuccessful
stat on filename with
\n' warning
730 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git
';
731 -d $forkee ? $forkee : '';
735 no warnings; # avoid silly 'unsuccessful
stat on filename with
\n' warning
737 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
740 and m#^[a-zA-Z0-9+./_-]+$#;
744 no warnings; # avoid silly 'unsuccessful
stat on filename with
\n' warning
746 valid_name($name) or die "tried to query for project with invalid name $name!";
747 (-d $Girocco::Config::reporoot."/$name.git");
754 open F, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
758 next if ($_[2] < 65536);
760 push @projects, $_[0];