1 package Girocco
::Project
;
10 use Girocco
::ProjPerm
;
12 use base
('Girocco::ProjPerm::'.$Girocco::Config
::permission_control
); # mwahaha
15 our $metadata_fields = {
16 homepage
=> ['Homepage URL', 'hp', 'text'],
17 shortdesc
=> ['Short description', 'desc', 'text'],
18 README
=> ['README (HTML, lt 8kb)', 'README', 'textarea'],
23 my @pelems = split('/', $self->{name
});
24 pop @pelems; # do not create dir for the project itself
25 my $path = $self->{base_path
};
26 foreach my $pelem (@pelems) {
28 (-d
"$path") or mkdir $path or die "mkdir $path: $!";
29 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
36 desc
=> 'description',
37 README
=> 'README.html',
39 notifymail
=> '%hooks.mailinglist',
40 notifyjson
=> '%hooks.jsonurl',
46 $self->{path
}.'/'.$name;
52 my $pname = $propmap{$name};
53 $pname or die "unknown property: $name";
54 if ($pname =~ s/^://) {
55 return `git --git-dir="$self->{path}" config "gitweb.$pname"`
56 } elsif ($pname =~ s/^%//) {
57 return `git --git-dir="$self->{path}" config "$pname"`
60 open P
, $self->_property_path($pname) or return undef;
63 my $value = join('', @value); chomp $value;
69 my ($name, $value) = @_;
70 my $pname = $propmap{$name};
71 $pname or die "unknown property: $name";
73 if ($pname =~ s/^://) {
74 return `git --git-dir="$self->{path}" config "gitweb.$pname" "$value"`
75 } elsif ($pname =~ s/^%//) {
76 return `git --git-dir="$self->{path}" config "$pname" "$value"`
79 my $P = lock_file
($self->_property_path($pname));
80 $value ne '' and print $P "$value\n";
82 unlock_file
($self->_property_path($pname));
85 sub _properties_load
{
87 foreach my $prop (keys %propmap) {
88 $self->{$prop} = $self->_property_fget($prop);
92 sub _properties_save
{
94 foreach my $prop (keys %propmap) {
95 $self->_property_fput($prop, $self->{$prop});
101 $self->_property_path('.nofetch');
107 my $np = $self->_nofetch_path;
109 open X
, '>'.$np or die "nofetch failed: $!";
112 unlink $np or die "yesfetch failed: $!";
118 $self->_property_path('.clonelog');
121 sub _clonefail_path
{
123 $self->_property_path('.clone_failed');
128 $self->_property_path('.clone_in_progress');
134 my $np = $self->_clonep_path;
136 open X
, '>'.$np or die "clonep failed: $!";
139 unlink $np or die "clonef failed: $!";
142 sub _alternates_setup
{
144 return unless $self->{name
} =~ m
#/#;
145 my $forkee_name = get_forkee_name
($self->{name
});
146 my $forkee_path = get_forkee_path
($self->{name
});
147 return unless -d
$forkee_path;
148 mkdir $self->{path
}.'/refs'; chmod 0775, $self->{path
}.'/refs';
149 mkdir $self->{path
}.'/objects'; chmod 0775, $self->{path
}.'/objects';
150 mkdir $self->{path
}.'/objects/info'; chmod 0775, $self->{path
}.'/objects/info';
152 # We set up both alternates and http_alternates since we cannot use
153 # relative path in alternates - that doesn't work recursively.
155 my $filename = $self->{path
}.'/objects/info/alternates';
156 open X
, '>'.$filename or die "alternates failed: $!";
157 print X
"$forkee_path/objects\n";
159 chmod 0664, $filename or warn "cannot chmod $filename: $!";
161 if ($Girocco::Config
::httppullurl
) {
162 $filename = $self->{path
}.'/objects/info/http-alternates';
163 open X
, '>'.$filename or die "http-alternates failed: $!";
164 my $upfork = $forkee_name;
165 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
167 chmod 0664, $filename or warn "cannot chmod $filename: $!";
170 # The symlink is problematic since git remote prune will traverse it.
171 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
176 mkdir $self->{path
}.'/ctags'; chmod 01777, $self->{path
}.'/ctags';
182 $xtra .= join(',', @
{$self->{users
}});
183 filedb_atomic_append
(jailed_file
('/etc/group'),
184 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
189 my $xtra = join(',', @
{$self->{users
}});
190 filedb_atomic_edit
(jailed_file
('/etc/group'),
194 if ($self->{name
} eq (split /:/)[0]) {
195 # preserve readonly flag
196 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
197 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
207 filedb_atomic_edit
(jailed_file
('/etc/group'),
209 $self->{name
} ne (split /:/)[0] and return $_;
217 $self->{path
}.'/hooks/'.$name;
223 open SRC
, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
224 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
225 while (<SRC
>) { print DST
$_; }
228 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
233 foreach my $hook ('post-receive', 'update') {
234 $self->_hook_install($hook);
238 # private constructor, do not use
241 my ($name, $base_path, $path) = @_;
242 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
243 $path ||= "$base_path/$name.git";
244 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
249 # public constructor #0
250 # creates a virtual project not connected to disk image
251 # you can conjure() it later to disk
254 my ($name, $mirror) = @_;
255 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
257 $self->{mirror
} = $mirror;
261 # public constructor #1
266 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
270 next unless (shift eq $name);
272 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
273 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
276 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
278 $self->{users
} = [split /,/, $ulist];
279 $self->{orig_users
} = [@
{$self->{users
}}];
280 $self->{mirror
} = ! -e
$self->_nofetch_path;
281 $self->{clone_in_progress
} = -e
$self->_clonep_path;
282 $self->{clone_logged
} = -e
$self->_clonelog_path;
283 $self->{clone_failed
} = -e
$self->_clonefail_path;
284 $self->{ccrypt
} = $self->{crypt};
286 $self->_properties_load;
293 # $proj may not be in sane state if this returns false!
297 my $cgi = $gcgi->cgi;
299 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
300 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
301 if ($pwd or not $self->{crypt}) {
302 $self->{crypt} = scrypt
($pwd);
305 if ($pwd2 and $pwd ne $pwd2) {
306 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
309 $self->{cpwd
} = $cgi->param('cpwd');
311 if ($Girocco::Config
::project_owners
eq 'email') {
312 $self->{email
} = $gcgi->wparam('email');
313 valid_email
($self->{email
})
314 or $gcgi->err("Your email sure looks weird...?");
317 $self->{url
} = $gcgi->wparam('url');
319 valid_repo_url
($self->{url
})
320 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
323 $self->{desc
} = $gcgi->wparam('desc');
324 length($self->{desc
}) <= 1024
325 or $gcgi->err("<b>Short</b> description length > 1kb!");
327 $self->{README
} = $gcgi->wparam('README');
328 length($self->{README
}) <= 8192
329 or $gcgi->err("README length > 8kb!");
331 $self->{hp
} = $gcgi->wparam('hp');
333 valid_web_url
($self->{hp
})
334 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
337 # FIXME: Permit only existing users
338 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) } $cgi->param('user')];
340 not $gcgi->err_check;
346 name
=> $self->{name
},
347 email
=> $self->{email
},
349 desc
=> html_esc
($self->{desc
}),
350 README
=> html_esc
($self->{README
}),
352 users
=> $self->{users
},
360 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
361 $self->{cpwd
} ||= '';
362 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
363 $gcgi->err("Your admin password does not match!");
373 $self->_mkdir_forkees;
375 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
376 if ($Girocco::Config
::owning_group
) {
377 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
378 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
379 chmod(02775, $self->{path
}) or die "chmod 2775 $self->{path} failed: $!";
381 chmod(02777, $self->{path
}) or die "chmod 2777 $self->{path} failed: $!";
383 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
384 or die "git init $self->{path} failed: $?";
385 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
386 or die "disabling receive.denyNonFastforwards failed: $?";
387 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
388 or die "disabling gc.auto failed: $?";
390 # /info must have right permissions even before the fixup job,
391 # and git init didn't do it for some reason.
392 if ($Girocco::Config
::owning_group
) {
393 chmod(02775, $self->{path
}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
395 chmod(02777, $self->{path
}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
398 $self->_properties_save;
399 $self->_alternates_setup;
401 $self->_group_add($pushers);
402 $self->_hooks_install;
403 $self->perm_initialize;
404 system($Girocco::Config
::basedir
. '/gitweb/genindex.sh');
424 unlink ($self->_clonefail_path()); # Ignore EEXIST error
425 unlink ($self->_clonelog_path()); # Ignore EEXIST error
428 my $sock = IO
::Socket
::UNIX
->new($Girocco::Config
::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
429 $sock->print("clone ".$self->{name
}."\n");
430 # Just ignore reply, we are going to succeed anyway and the I/O
431 # would apparently get quite hairy.
440 $self->_properties_save;
441 $self->_group_update;
443 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
444 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
445 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
446 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
451 sub update_password
{
455 $self->{crypt} = scrypt
($pwd);
456 $self->_group_update;
459 # You can explicitly do this just on a ghost() repository too.
463 if (-d
$self->{path
}) {
464 system('rm', '-rf', $self->{path
}) == 0
465 or die "rm -rf $self->{path} failed: $?";
467 $self->_group_remove;
473 return glob($Girocco::Config
::reporoot
.'/'.$self->{name
}.'/*');
479 sub get_forkee_name
{
481 (m
#^(.*)/.*?$#)[0]; #
483 sub get_forkee_path
{
484 my $forkee = $Girocco::Config
::reporoot
.'/'.get_forkee_name
($_[0]).'.git';
485 -d
$forkee ?
$forkee : '';
490 (not m
#/# or -d get_forkee_path($_)) # will also catch ^/
493 and m
#^[a-zA-Z0-9+./_-]+$#;
498 valid_name
($name) or die "tried to query for project with invalid name $name!";
499 (-d
$Girocco::Config
::reporoot
."/$name.git");