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 notifyjson
=> '%hooks.jsonurl',
64 notifycia
=> '%hooks.cianame',
68 system($Girocco::Config
::basedir
. '/gitweb/genindex.sh');
74 $self->{path
}.'/'.$name;
80 my $pname = $propmap{$name};
81 $pname or die "unknown property: $name";
82 if ($pname =~ s/^://) {
83 my $val = `git --git-dir="$self->{path}" config "gitweb.$pname"`;
86 } elsif ($pname =~ s/^%//) {
87 my $val = `git --git-dir="$self->{path}" config "$pname"`;
92 open P
, $self->_property_path($pname) or return undef;
95 my $value = join('', @value); chomp $value;
101 my ($name, $value) = @_;
102 my $pname = $propmap{$name};
103 $pname or die "unknown property: $name";
105 if ($pname =~ s/^://) {
106 system('git', '--git-dir='.$self->{path
}, 'config', "gitweb.$pname", $value);
108 } elsif ($pname =~ s/^%//) {
109 system('git', '--git-dir='.$self->{path
}, 'config', $pname, $value);
113 my $P = lock_file
($self->_property_path($pname));
114 $value ne '' and print $P "$value\n";
116 unlock_file
($self->_property_path($pname));
119 sub _properties_load
{
121 foreach my $prop (keys %propmap) {
122 $self->{$prop} = $self->_property_fget($prop);
124 my $val = `git --git-dir="$self->{path}" config --bool "gitweb.statusupdates" 2>/dev/null`;
126 $val = ($val eq 'false') ?
0 : 1;
127 $self->{statusupdates
} = $val;
128 delete $self->{auth
};
129 $val = `git --git-dir="$self->{path}" config "gitweb.delauth"`;
131 if ($val =~ /^# DELAUTH ([0-9a-f]+) (\d+)/) {
133 $self->{auth
} = $1 unless (time >= $expire);
137 sub _properties_save
{
139 foreach my $prop (keys %propmap) {
140 $self->_property_fput($prop, $self->{$prop});
142 $self->{statusupdates
} = 1
143 unless defined($self->{statusupdates
}) && $self->{statusupdates
} =~ /^\d+$/;
144 system('git', '--git-dir='.$self->{path
}, 'config', '--bool',
145 "gitweb.statusupdates", $self->{statusupdates
});
150 $self->_property_path('.nofetch');
156 my $np = $self->_nofetch_path;
158 open X
, '>'.$np or die "nofetch failed: $!";
161 unlink $np or die "yesfetch failed: $!";
167 $self->_property_path('.clonelog');
170 sub _clonefail_path
{
172 $self->_property_path('.clone_failed');
177 $self->_property_path('.clone_in_progress');
183 my $np = $self->_clonep_path;
185 open X
, '>'.$np or die "clonep failed: $!";
188 unlink $np or die "clonef failed: $!";
191 sub _alternates_setup
{
193 return unless $self->{name
} =~ m
#/#;
194 my $forkee_name = get_forkee_name
($self->{name
});
195 my $forkee_path = get_forkee_path
($self->{name
});
196 return unless -d
$forkee_path;
197 mkdir $self->{path
}.'/refs'; chmod 02775, $self->{path
}.'/refs';
198 mkdir $self->{path
}.'/objects'; chmod 02775, $self->{path
}.'/objects';
199 mkdir $self->{path
}.'/objects/info'; chmod 02775, $self->{path
}.'/objects/info';
201 # We set up both alternates and http_alternates since we cannot use
202 # relative path in alternates - that doesn't work recursively.
204 my $filename = $self->{path
}.'/objects/info/alternates';
205 open X
, '>'.$filename or die "alternates failed: $!";
206 print X
"$forkee_path/objects\n";
208 chmod 0664, $filename or warn "cannot chmod $filename: $!";
210 if ($Girocco::Config
::httppullurl
) {
211 $filename = $self->{path
}.'/objects/info/http-alternates';
212 open X
, '>'.$filename or die "http-alternates failed: $!";
213 my $upfork = $forkee_name;
214 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
216 chmod 0664, $filename or warn "cannot chmod $filename: $!";
219 # The symlink is problematic since git remote prune will traverse it.
220 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
222 # copy refs from parent project
223 # ownership information is changed by a root cronjob
224 system("cp -pR $forkee_path/refs $self->{path}/");
225 # initialize HEAD, hacky version
226 system("cp $forkee_path/HEAD $self->{path}/HEAD");
231 mkdir $self->{path
}.'/ctags'; chmod 01777, $self->{path
}.'/ctags';
237 $xtra .= join(',', @
{$self->{users
}});
238 filedb_atomic_append
(jailed_file
('/etc/group'),
239 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
244 my $xtra = join(',', @
{$self->{users
}});
245 filedb_atomic_edit
(jailed_file
('/etc/group'),
249 if ($self->{name
} eq (split /:/)[0]) {
250 # preserve readonly flag
251 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
252 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
262 filedb_atomic_edit
(jailed_file
('/etc/group'),
264 $self->{name
} ne (split /:/)[0] and return $_;
272 $self->{path
}.'/hooks/'.$name;
278 open SRC
, "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
279 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
280 while (<SRC
>) { print DST
$_; }
283 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
288 foreach my $hook ('post-receive', 'update', 'post-update') {
289 $self->_hook_install($hook);
293 # private constructor, do not use
296 my ($name, $base_path, $path) = @_;
297 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
298 $path ||= "$base_path/$name.git";
299 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
304 # public constructor #0
305 # creates a virtual project not connected to disk image
306 # you can conjure() it later to disk
309 my ($name, $mirror) = @_;
310 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
312 $self->{mirror
} = $mirror;
313 $self->{email
} = $self->{orig_email
} = '';
317 # public constructor #1
320 my $name = shift || '';
322 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
326 next unless (shift eq $name);
328 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
329 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
332 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
334 $self->{users
} = [split /,/, $ulist];
335 $self->{HEAD
} = $self->get_HEAD;
336 $self->{orig_HEAD
} = $self->{HEAD
};
337 $self->{orig_users
} = [@
{$self->{users
}}];
338 $self->{mirror
} = ! -e
$self->_nofetch_path;
339 $self->{clone_in_progress
} = -e
$self->_clonep_path;
340 $self->{clone_logged
} = -e
$self->_clonelog_path;
341 $self->{clone_failed
} = -e
$self->_clonefail_path;
342 $self->{ccrypt
} = $self->{crypt};
344 $self->_properties_load;
345 $self->{orig_email
} = $self->{email
};
352 # $proj may not be in sane state if this returns false!
356 my $cgi = $gcgi->cgi;
358 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
359 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
360 if ($pwd or not $self->{crypt}) {
361 $self->{crypt} = scrypt
($pwd);
364 if ($pwd2 and $pwd ne $pwd2) {
365 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
368 $self->{cpwd
} = $cgi->param('cpwd');
370 if ($Girocco::Config
::project_owners
eq 'email') {
371 $self->{email
} = $gcgi->wparam('email');
372 valid_email
($self->{email
})
373 or $gcgi->err("Your email sure looks weird...?");
376 $self->{url
} = $gcgi->wparam('url');
378 valid_repo_url
($self->{url
})
379 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
382 $self->{desc
} = $gcgi->wparam('desc');
383 length($self->{desc
}) <= 1024
384 or $gcgi->err("<b>Short</b> description length > 1kb!");
386 $self->{README
} = $gcgi->wparam('README');
387 length($self->{README
}) <= 8192
388 or $gcgi->err("README length > 8kb!");
390 $self->{hp
} = $gcgi->wparam('hp');
392 valid_web_url
($self->{hp
})
393 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
396 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) && Girocco
::User
::does_exist
($_) } $cgi->param('user')];
398 $self->{HEAD
} = $cgi->param('HEAD') if $cgi->param('HEAD');
400 # schedule deletion of tags (will be committed by update() after auth)
401 $self->{tags_to_delete
} = [$cgi->param('tags')];
403 $self->{notifymail
} = $gcgi->wparam('notifymail');
404 if ($self->{notifymail
}) {
405 (valid_email_multi
($self->{notifymail
}) and length($self->{notifymail
}) <= 512)
406 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
409 $self->{notifyjson
} = $gcgi->wparam('notifyjson');
410 if ($self->{notifyjson
}) {
411 valid_web_url
($self->{notifyjson
})
412 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
415 $self->{notifycia
} = $gcgi->wparam('notifycia');
416 if ($self->{notifycia
}) {
417 $self->{notifycia
} =~ /^[a-zA-Z0-9._-]+$/
418 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
421 if ($cgi->param('setstatusupdates')) {
422 my $val = $gcgi->wparam('statusupdates') || '0';
423 $self->{statusupdates
} = $val ?
1 : 0;
426 not $gcgi->err_check;
432 name
=> $self->{name
},
433 email
=> $self->{email
},
435 desc
=> html_esc
($self->{desc
}),
436 README
=> html_esc
($self->{README
}),
438 users
=> $self->{users
},
439 notifymail
=> html_esc
($self->{notifymail
}),
440 notifyjson
=> html_esc
($self->{notifyjson
}),
441 notifycia
=> html_esc
($self->{notifycia
}),
449 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
450 $self->{cpwd
} ||= '';
451 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
452 $gcgi->err("Your admin password does not match!");
462 $self->_mkdir_forkees;
464 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
465 if ($Girocco::Config
::owning_group
) {
466 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
467 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
468 chmod(02775, $self->{path
}) or die "chmod 2775 $self->{path} failed: $!";
470 chmod(02777, $self->{path
}) or die "chmod 2777 $self->{path} failed: $!";
472 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
473 or die "git init $self->{path} failed: $?";
474 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
475 or die "disabling receive.denyNonFastforwards failed: $?";
476 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
477 or die "disabling gc.auto failed: $?";
479 # /info must have right permissions,
480 # and git init didn't do it for some reason.
481 if ($Girocco::Config
::owning_group
) {
482 chmod(02775, $self->{path
}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
484 chmod(02777, $self->{path
}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
487 $self->_properties_save;
488 $self->_alternates_setup;
490 $self->_group_remove;
491 $self->_group_add($pushers);
492 $self->_hooks_install;
493 #$self->perm_initialize;
494 $self->_update_index;
502 $self->perm_initialize;
510 $self->perm_initialize;
516 unlink ($self->_clonefail_path()); # Ignore EEXIST error
517 unlink ($self->_clonelog_path()); # Ignore EEXIST error
520 my $sock = IO
::Socket
::UNIX
->new($Girocco::Config
::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
521 $sock->print("clone ".$self->{name
}."\n");
522 # Just ignore reply, we are going to succeed anyway and the I/O
523 # would apparently get quite hairy.
532 $self->_properties_save;
533 $self->_group_update;
535 if (exists($self->{tags_to_delete
})) {
536 $self->delete_ctag($_) foreach(@
{$self->{tags_to_delete
}});
539 $self->set_HEAD($self->{HEAD
}) unless $self->{orig_HEAD
} eq $self->{HEAD
};
541 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
542 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
543 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
544 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
546 $self->_update_index if $self->{email
} ne $self->{orig_email
};
547 $self->{orig_email
} = $self->{email
};
552 sub update_password
{
556 $self->{crypt} = scrypt
($pwd);
557 $self->_group_update;
560 # You can explicitly do this just on a ghost() repository too.
564 if (-d
$self->{path
}) {
565 system('rm', '-rf', $self->{path
}) == 0
566 or die "rm -rf $self->{path} failed: $?";
568 # attempt to clean up any empty fork directories by removing them
569 my @pelems = split('/', $self->{name
});
570 while (@pelems > 1) {
573 rmdir join('/', $Girocco::Config
::reporoot
, @pelems) or last;
575 $self->_group_remove;
576 $self->_update_index;
579 sub _contains_files
{
581 (-d
$dir) or return 0;
582 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
583 while (my $entry = readdir($dh)) {
584 next if $entry eq '' || $entry eq '.' || $entry eq '..';
585 closedir($dh), return 1
586 if -f
"$dir/$entry" ||
587 -d
"$dir/$entry" && _contains_files
("$dir/$entry");
596 return _contains_files
($Girocco::Config
::reporoot
.'/'.$self->{name
});
600 # A project is considered empty if the git repository does not
601 # have any refs. This means packed-refs does not exist or is
602 # empty or only has lines starting with '#' AND there are no
603 # files in the refs subdirectory hierarchy (no matter how deep).
607 (-d
$self->{path
}) or return 0;
608 if (-e
$self->{path
}.'/packed-refs') {
609 open(my $pr, '<', $self->{path
}.'/packed-refs')
610 or die "open $self->{path}./packed-refs failed: $!";
612 while (my $ref = <$pr>) {
613 next if $ref =~ /^#/;
618 return 0 if $foundref;
620 (-d
$self->{path
}.'/refs') or return 1;
621 return !_contains_files
($self->{path
}.'/refs');
628 # sanity check, disallow filenames starting with . .. or /
629 unlink($self->{path
}.'/ctags/'.$ctag) if($ctag !~ m
|^(\
.\
.?
)/|);
635 opendir(my $dh, $self->{path
}.'/ctags')
637 @ctags = grep { -f
"$self->{path}/ctags/$_" } readdir($dh);
645 open($fh, '-|', "$Girocco::Config::git_bin --git-dir=$self->{path} show-ref --heads") or die "could not get list of heads";
649 next if !m
#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
658 my $HEAD = `$Girocco::Config::git_bin --git-dir=$self->{path} symbolic-ref HEAD`;
660 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
667 # Cursory checks only -- if you want to break your HEAD, be my guest
668 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
669 die "grossly invalid new HEAD: $newHEAD";
671 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic
-ref', 'HEAD
', "refs/heads/$newHEAD");
672 die "could not set HEAD" if ($? >> 8);
678 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
679 my $expire = time + 24 * 3600;
680 my $propval = "# DELAUTH $self->{auth} $expire";
681 system('git
', '--git
-dir
='.$self->{path}, 'config
', 'gitweb
.delauth
', $propval);
688 delete $self->{auth};
689 system('git
', '--git
-dir
='.$self->{path}, 'config
', '--unset
', 'gitweb
.delauth
');
696 my $before_count = @{$self->{users}};
697 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
698 return @{$self->{users}} != $before_count;
703 sub get_forkee_name {
705 (m#^(.*)/.*?$#)[0]; #
707 sub get_forkee_path {
708 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git
';
709 -d $forkee ? $forkee : '';
714 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
717 and m#^[a-zA-Z0-9+./_-]+$#;
722 valid_name($name) or die "tried to query for project with invalid name $name!";
723 (-d $Girocco::Config::reporoot."/$name.git");
730 open F, jailed_file("/etc/group") or die "getting project list failed: $!";
734 next if ($_[2] < 65536);
736 push @projects, $_[0];