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',
73 system($Girocco::Config
::basedir
. '/gitweb/genindex.sh');
79 $self->{path
}.'/'.$name;
85 my $pname = $propmap{$name};
86 $pname = $propmapro{$name} unless $pname;
87 $pname or die "unknown property: $name";
88 if ($pname =~ s/^://) {
89 my $val = `git --git-dir="$self->{path}" config "gitweb.$pname"`;
92 } elsif ($pname =~ s/^%//) {
93 my $val = `git --git-dir="$self->{path}" config "$pname"`;
98 open P
, '<', $self->_property_path($pname) or return undef;
101 my $value = join('', @value); chomp $value;
107 my ($name, $value) = @_;
108 my $pname = $propmap{$name};
109 $pname or die "unknown property: $name";
111 if ($pname =~ s/^://) {
112 system('git', '--git-dir='.$self->{path
}, 'config', "gitweb.$pname", $value);
114 } elsif ($pname =~ s/^%//) {
115 system('git', '--git-dir='.$self->{path
}, 'config', $pname, $value);
119 my $P = lock_file
($self->_property_path($pname));
120 $value ne '' and print $P "$value\n";
122 unlock_file
($self->_property_path($pname));
125 sub _properties_load
{
127 foreach my $prop (keys %propmap) {
128 $self->{$prop} = $self->_property_fget($prop);
130 foreach my $prop (keys %propmapro) {
131 $self->{$prop} = $self->_property_fget($prop);
133 my $val = `git --git-dir="$self->{path}" config --bool "gitweb.statusupdates" 2>/dev/null`;
135 $val = ($val eq 'false') ?
0 : 1;
136 $self->{statusupdates
} = $val;
137 delete $self->{auth
};
138 $val = `git --git-dir="$self->{path}" config "gitweb.delauth"`;
140 if ($val =~ /^# DELAUTH ([0-9a-f]+) (\d+)/) {
142 $self->{auth
} = $1 unless (time >= $expire);
146 sub _properties_save
{
148 foreach my $prop (keys %propmap) {
149 $self->_property_fput($prop, $self->{$prop});
151 $self->{statusupdates
} = 1
152 unless defined($self->{statusupdates
}) && $self->{statusupdates
} =~ /^\d+$/;
153 system('git', '--git-dir='.$self->{path
}, 'config', '--bool',
154 "gitweb.statusupdates", $self->{statusupdates
});
159 $self->_property_path('.nofetch');
165 my $nf = $self->_nofetch_path;
167 open X
, '>', $nf or die "nofetch failed: $!";
170 unlink $nf or die "yesfetch failed: $!";
176 $self->_property_path('.clonelog');
179 sub _clonefail_path
{
181 $self->_property_path('.clone_failed');
186 $self->_property_path('.clone_in_progress');
192 my $np = $self->_clonep_path;
194 open X
, '>', $np or die "clonep failed: $!";
197 unlink $np or die "clonef failed: $!";
200 sub _alternates_setup
{
202 return unless $self->{name
} =~ m
#/#;
203 my $forkee_name = get_forkee_name
($self->{name
});
204 my $forkee_path = get_forkee_path
($self->{name
});
205 return unless -d
$forkee_path;
206 mkdir $self->{path
}.'/refs'; chmod 02775, $self->{path
}.'/refs';
207 mkdir $self->{path
}.'/objects'; chmod 02775, $self->{path
}.'/objects';
208 mkdir $self->{path
}.'/objects/info'; chmod 02775, $self->{path
}.'/objects/info';
210 # We set up both alternates and http_alternates since we cannot use
211 # relative path in alternates - that doesn't work recursively.
213 my $filename = $self->{path
}.'/objects/info/alternates';
214 open X
, '>', $filename or die "alternates failed: $!";
215 print X
"$forkee_path/objects\n";
217 chmod 0664, $filename or warn "cannot chmod $filename: $!";
219 if ($Girocco::Config
::httppullurl
) {
220 $filename = $self->{path
}.'/objects/info/http-alternates';
221 open X
, '>', $filename or die "http-alternates failed: $!";
222 my $upfork = $forkee_name;
223 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
225 chmod 0664, $filename or warn "cannot chmod $filename: $!";
228 # The symlink is problematic since git remote prune will traverse it.
229 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
231 # copy refs from parent project
232 # ownership information is changed by a root cronjob
233 system("cp -pR $forkee_path/refs $self->{path}/");
234 # initialize HEAD, hacky version
235 system("cp $forkee_path/HEAD $self->{path}/HEAD");
240 mkdir $self->{path
}.'/ctags'; chmod 01777, $self->{path
}.'/ctags';
246 $xtra .= join(',', @
{$self->{users
}});
247 filedb_atomic_append
(jailed_file
('/etc/group'),
248 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
253 my $xtra = join(',', @
{$self->{users
}});
254 filedb_atomic_edit
(jailed_file
('/etc/group'),
258 if ($self->{name
} eq (split /:/)[0]) {
259 # preserve readonly flag
260 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
261 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
271 filedb_atomic_edit
(jailed_file
('/etc/group'),
273 $self->{name
} ne (split /:/)[0] and return $_;
281 $self->{path
}.'/hooks/'.$name;
287 open SRC
, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
288 open DST
, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
289 while (<SRC
>) { print DST
$_; }
292 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
297 foreach my $hook ('post-receive', 'update', 'post-update') {
298 $self->_hook_install($hook);
302 # private constructor, do not use
305 my ($name, $base_path, $path) = @_;
306 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
307 $path ||= "$base_path/$name.git";
308 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
313 # public constructor #0
314 # creates a virtual project not connected to disk image
315 # you can conjure() it later to disk
318 my ($name, $mirror) = @_;
319 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
321 $self->{mirror
} = $mirror;
322 $self->{email
} = $self->{orig_email
} = '';
326 # public constructor #1
329 my $name = shift || '';
331 open F
, '<', jailed_file
("/etc/group") or die "project load failed: $!";
335 next unless (shift eq $name);
337 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
338 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
341 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
343 $self->{users
} = [split /,/, $ulist];
344 $self->{HEAD
} = $self->get_HEAD;
345 $self->{orig_HEAD
} = $self->{HEAD
};
346 $self->{orig_users
} = [@
{$self->{users
}}];
347 $self->{mirror
} = ! -e
$self->_nofetch_path;
348 $self->{clone_in_progress
} = -e
$self->_clonep_path;
349 $self->{clone_logged
} = -e
$self->_clonelog_path;
350 $self->{clone_failed
} = -e
$self->_clonefail_path;
351 $self->{ccrypt
} = $self->{crypt};
353 $self->_properties_load;
354 $self->{orig_email
} = $self->{email
};
361 # $proj may not be in sane state if this returns false!
365 my $cgi = $gcgi->cgi;
367 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
368 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
369 if ($pwd or not $self->{crypt}) {
370 $self->{crypt} = scrypt
($pwd);
373 if ($pwd2 and $pwd ne $pwd2) {
374 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
377 $self->{cpwd
} = $cgi->param('cpwd');
379 if ($Girocco::Config
::project_owners
eq 'email') {
380 $self->{email
} = $gcgi->wparam('email');
381 valid_email
($self->{email
})
382 or $gcgi->err("Your email sure looks weird...?");
385 $self->{url
} = $gcgi->wparam('url');
387 valid_repo_url
($self->{url
})
388 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
391 $self->{desc
} = $gcgi->wparam('desc');
392 length($self->{desc
}) <= 1024
393 or $gcgi->err("<b>Short</b> description length > 1kb!");
395 $self->{README
} = $gcgi->wparam('README');
396 length($self->{README
}) <= 8192
397 or $gcgi->err("README length > 8kb!");
399 $self->{hp
} = $gcgi->wparam('hp');
401 valid_web_url
($self->{hp
})
402 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
405 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) && Girocco
::User
::does_exist
($_) } $cgi->param('user')];
407 $self->{HEAD
} = $cgi->param('HEAD') if $cgi->param('HEAD');
409 # schedule deletion of tags (will be committed by update() after auth)
410 $self->{tags_to_delete
} = [$cgi->param('tags')];
412 $self->{notifymail
} = $gcgi->wparam('notifymail');
413 if ($self->{notifymail
}) {
414 (valid_email_multi
($self->{notifymail
}) and length($self->{notifymail
}) <= 512)
415 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
418 $self->{notifyjson
} = $gcgi->wparam('notifyjson');
419 if ($self->{notifyjson
}) {
420 valid_web_url
($self->{notifyjson
})
421 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
424 $self->{notifycia
} = $gcgi->wparam('notifycia');
425 if ($self->{notifycia
}) {
426 $self->{notifycia
} =~ /^[a-zA-Z0-9._-]+$/
427 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
430 if ($cgi->param('setstatusupdates')) {
431 my $val = $gcgi->wparam('statusupdates') || '0';
432 $self->{statusupdates
} = $val ?
1 : 0;
435 not $gcgi->err_check;
441 name
=> $self->{name
},
442 email
=> $self->{email
},
444 desc
=> html_esc
($self->{desc
}),
445 README
=> html_esc
($self->{README
}),
447 users
=> $self->{users
},
448 notifymail
=> html_esc
($self->{notifymail
}),
449 notifyjson
=> html_esc
($self->{notifyjson
}),
450 notifycia
=> html_esc
($self->{notifycia
}),
458 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
459 $self->{cpwd
} ||= '';
460 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
461 $gcgi->err("Your admin password does not match!");
471 $self->_mkdir_forkees;
473 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
474 if ($Girocco::Config
::owning_group
) {
475 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
476 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
477 chmod(02775, $self->{path
}) or die "chmod 2775 $self->{path} failed: $!";
479 chmod(02777, $self->{path
}) or die "chmod 2777 $self->{path} failed: $!";
481 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
482 or die "git init $self->{path} failed: $?";
483 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
484 or die "disabling receive.denyNonFastforwards failed: $?";
485 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
486 or die "disabling gc.auto failed: $?";
488 # /info must have right permissions,
489 # and git init didn't do it for some reason.
490 if ($Girocco::Config
::owning_group
) {
491 chmod(02775, $self->{path
}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
493 chmod(02777, $self->{path
}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
496 $self->_properties_save;
497 $self->_alternates_setup;
499 $self->_group_remove;
500 $self->_group_add($pushers);
501 $self->_hooks_install;
502 #$self->perm_initialize;
503 $self->_update_index;
511 $self->perm_initialize;
519 $self->perm_initialize;
525 unlink ($self->_clonefail_path()); # Ignore EEXIST error
526 unlink ($self->_clonelog_path()); # Ignore EEXIST error
529 my $sock = IO
::Socket
::UNIX
->new($Girocco::Config
::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
530 $sock->print("clone ".$self->{name
}."\n");
531 # Just ignore reply, we are going to succeed anyway and the I/O
532 # would apparently get quite hairy.
541 $self->_properties_save;
542 $self->_group_update;
544 if (exists($self->{tags_to_delete
})) {
545 $self->delete_ctag($_) foreach(@
{$self->{tags_to_delete
}});
548 $self->set_HEAD($self->{HEAD
}) unless $self->{orig_HEAD
} eq $self->{HEAD
};
550 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
551 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
552 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
553 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
555 $self->_update_index if $self->{email
} ne $self->{orig_email
};
556 $self->{orig_email
} = $self->{email
};
561 sub update_password
{
565 $self->{crypt} = scrypt
($pwd);
566 $self->_group_update;
569 # You can explicitly do this just on a ghost() repository too.
573 if (-d
$self->{path
}) {
574 system('rm', '-rf', $self->{path
}) == 0
575 or die "rm -rf $self->{path} failed: $?";
577 # attempt to clean up any empty fork directories by removing them
578 my @pelems = split('/', $self->{name
});
579 while (@pelems > 1) {
582 rmdir join('/', $Girocco::Config
::reporoot
, @pelems) or last;
584 $self->_group_remove;
585 $self->_update_index;
588 sub _contains_files
{
590 (-d
$dir) or return 0;
591 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
592 while (my $entry = readdir($dh)) {
593 next if $entry eq '' || $entry eq '.' || $entry eq '..';
594 closedir($dh), return 1
595 if -f
"$dir/$entry" ||
596 -d
"$dir/$entry" && _contains_files
("$dir/$entry");
605 return _contains_files
($Girocco::Config
::reporoot
.'/'.$self->{name
});
609 # A project is considered empty if the git repository does not
610 # have any refs. This means packed-refs does not exist or is
611 # empty or only has lines starting with '#' AND there are no
612 # files in the refs subdirectory hierarchy (no matter how deep).
616 (-d
$self->{path
}) or return 0;
617 if (-e
$self->{path
}.'/packed-refs') {
618 open(my $pr, '<', $self->{path
}.'/packed-refs')
619 or die "open $self->{path}./packed-refs failed: $!";
621 while (my $ref = <$pr>) {
622 next if $ref =~ /^#/;
627 return 0 if $foundref;
629 (-d
$self->{path
}.'/refs') or return 1;
630 return !_contains_files
($self->{path
}.'/refs');
637 # sanity check, disallow filenames starting with . .. or /
638 unlink($self->{path
}.'/ctags/'.$ctag) if($ctag !~ m
|^(\
.\
.?
)/|);
644 opendir(my $dh, $self->{path
}.'/ctags')
646 @ctags = grep { -f
"$self->{path}/ctags/$_" } readdir($dh);
654 open($fh, '-|', "$Girocco::Config::git_bin --git-dir=$self->{path} show-ref --heads") or die "could not get list of heads";
658 next if !m
#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
667 my $HEAD = `$Girocco::Config::git_bin --git-dir=$self->{path} symbolic-ref HEAD`;
669 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
676 # Cursory checks only -- if you want to break your HEAD, be my guest
677 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
678 die "grossly invalid new HEAD: $newHEAD";
680 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic
-ref', 'HEAD
', "refs/heads/$newHEAD");
681 die "could not set HEAD" if ($? >> 8);
687 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
688 my $expire = time + 24 * 3600;
689 my $propval = "# DELAUTH $self->{auth} $expire";
690 system('git
', '--git
-dir
='.$self->{path}, 'config
', 'gitweb
.delauth
', $propval);
697 delete $self->{auth};
698 system('git
', '--git
-dir
='.$self->{path}, 'config
', '--unset
', 'gitweb
.delauth
');
705 my $before_count = @{$self->{users}};
706 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
707 return @{$self->{users}} != $before_count;
712 sub get_forkee_name {
714 (m#^(.*)/.*?$#)[0]; #
716 sub get_forkee_path {
717 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git
';
718 -d $forkee ? $forkee : '';
723 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
726 and m#^[a-zA-Z0-9+./_-]+$#;
731 valid_name($name) or die "tried to query for project with invalid name $name!";
732 (-d $Girocco::Config::reporoot."/$name.git");
739 open F, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
743 next if ($_[2] < 65536);
745 push @projects, $_[0];