Girocco::Project: Fix config lookup
[girocco.git] / Girocco / Project.pm
blobb3c8fe9fa086c03573318dbf1327a861273340de
1 package Girocco::Project;
3 use strict;
4 use warnings;
6 BEGIN {
7 use Girocco::CGI;
8 use Girocco::User;
9 use Girocco::Util;
10 use Girocco::ProjPerm;
11 use Girocco::Config;
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'],
21 sub _mkdir_forkees {
22 my $self = shift;
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) {
27 $path .= "/$pelem";
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)
33 our %propmap = (
34 url => ':baseurl',
35 email => ':owner',
36 desc => 'description',
37 README => 'README.html',
38 hp => ':homepage',
41 sub _property_path {
42 my $self = shift;
43 my ($name) = @_;
44 $self->{path}.'/'.$name;
47 sub _property_fget {
48 my $self = shift;
49 my ($name) = @_;
50 my $pname = $propmap{$name};
51 $pname or die "unknown property: $name";
52 if ($pname =~ s/^://) {
53 return `git --git-dir="$self->{path}" config "gitweb.$pname"`
56 open P, $self->_property_path($pname) or return undef;
57 my @value = <P>;
58 close P;
59 my $value = join('', @value); chomp $value;
60 $value;
63 sub _property_fput {
64 my $self = shift;
65 my ($name, $value) = @_;
66 my $pname = $propmap{$name};
67 $pname or die "unknown property: $name";
68 $value ||= '';
69 if ($pname =~ s/^://) {
70 return `git --git-dir="$self->{path}" config "gitweb.$pname" "$value"`
73 my $P = lock_file($self->_property_path($pname));
74 $value ne '' and print $P "$value\n";
75 close $P;
76 unlock_file($self->_property_path($pname));
79 sub _properties_load {
80 my $self = shift;
81 foreach my $prop (keys %propmap) {
82 $self->{$prop} = $self->_property_fget($prop);
86 sub _properties_save {
87 my $self = shift;
88 foreach my $prop (keys %propmap) {
89 $self->_property_fput($prop, $self->{$prop});
93 sub _nofetch_path {
94 my $self = shift;
95 $self->_property_path('.nofetch');
98 sub _nofetch {
99 my $self = shift;
100 my ($nofetch) = @_;
101 my $np = $self->_nofetch_path;
102 if ($nofetch) {
103 open X, '>'.$np or die "nofetch failed: $!";
104 close X;
105 } else {
106 unlink $np or die "yesfetch failed: $!";
110 sub _clonelog_path {
111 my $self = shift;
112 $self->_property_path('.clonelog');
115 sub _clonefail_path {
116 my $self = shift;
117 $self->_property_path('.clone_failed');
120 sub _clonep_path {
121 my $self = shift;
122 $self->_property_path('.clone_in_progress');
125 sub _clonep {
126 my $self = shift;
127 my ($nofetch) = @_;
128 my $np = $self->_clonep_path;
129 if ($nofetch) {
130 open X, '>'.$np or die "clonep failed: $!";
131 close X;
132 } else {
133 unlink $np or die "clonef failed: $!";
136 sub _alternates_setup {
137 my $self = shift;
138 return unless $self->{name} =~ m#/#;
139 my $forkee_name = get_forkee_name($self->{name});
140 my $forkee_path = get_forkee_path($self->{name});
141 return unless -d $forkee_path;
142 mkdir $self->{path}.'/refs'; chmod 0775, $self->{path}.'/refs';
143 mkdir $self->{path}.'/objects'; chmod 0775, $self->{path}.'/objects';
144 mkdir $self->{path}.'/objects/info'; chmod 0775, $self->{path}.'/objects/info';
146 # We set up both alternates and http_alternates since we cannot use
147 # relative path in alternates - that doesn't work recursively.
149 my $filename = $self->{path}.'/objects/info/alternates';
150 open X, '>'.$filename or die "alternates failed: $!";
151 print X "$forkee_path/objects\n";
152 close X;
153 chmod 0664, $filename or warn "cannot chmod $filename: $!";
155 if ($Girocco::Config::httppullurl) {
156 $filename = $self->{path}.'/objects/info/http-alternates';
157 open X, '>'.$filename or die "http-alternates failed: $!";
158 my $upfork = $forkee_name;
159 do { print X "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
160 close X;
161 chmod 0664, $filename or warn "cannot chmod $filename: $!";
164 symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
167 sub _ctags_setup {
168 my $self = shift;
169 mkdir $self->{path}.'/ctags'; chmod 01777, $self->{path}.'/ctags';
172 sub _group_add {
173 my $self = shift;
174 my ($xtra) = @_;
175 $xtra .= join(',', @{$self->{users}});
176 filedb_atomic_append(jailed_file('/etc/group'),
177 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
180 sub _group_update {
181 my $self = shift;
182 my $xtra = join(',', @{$self->{users}});
183 filedb_atomic_edit(jailed_file('/etc/group'),
184 sub {
185 $_ = $_[0];
186 chomp;
187 if ($self->{name} eq (split /:/)[0]) {
188 # preserve readonly flag
189 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
190 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
191 } else {
192 return "$_\n";
198 sub _group_remove {
199 my $self = shift;
200 filedb_atomic_edit(jailed_file('/etc/group'),
201 sub {
202 $self->{name} ne (split /:/)[0] and return $_;
207 sub _hook_path {
208 my $self = shift;
209 my ($name) = @_;
210 $self->{path}.'/hooks/'.$name;
213 sub _hook_install {
214 my $self = shift;
215 my ($name) = @_;
216 open SRC, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
217 open DST, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
218 while (<SRC>) { print DST $_; }
219 close DST;
220 close SRC;
221 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
224 sub _hooks_install {
225 my $self = shift;
226 foreach my $hook ('update') {
227 $self->_hook_install($hook);
231 # private constructor, do not use
232 sub _new {
233 my $class = shift;
234 my ($name, $base_path, $path) = @_;
235 valid_name($name) or die "refusing to create project with invalid name ($name)!";
236 $path ||= "$base_path/$name.git";
237 my $proj = { name => $name, base_path => $base_path, path => $path };
239 bless $proj, $class;
242 # public constructor #0
243 # creates a virtual project not connected to disk image
244 # you can conjure() it later to disk
245 sub ghost {
246 my $class = shift;
247 my ($name, $mirror) = @_;
248 my $self = $class->_new($name, $Girocco::Config::reporoot);
249 $self->{users} = [];
250 $self->{mirror} = $mirror;
251 $self;
254 # public constructor #1
255 sub load {
256 my $class = shift;
257 my ($name) = @_;
259 open F, jailed_file("/etc/group") or die "project load failed: $!";
260 while (<F>) {
261 chomp;
262 @_ = split /:+/;
263 next unless (shift eq $name);
265 my $self = $class->_new($name, $Girocco::Config::reporoot);
266 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
268 my $ulist;
269 ($self->{crypt}, $self->{gid}, $ulist) = @_;
270 $ulist ||= '';
271 $self->{users} = [split /,/, $ulist];
272 $self->{orig_users} = [@{$self->{users}}];
273 $self->{mirror} = ! -e $self->_nofetch_path;
274 $self->{clone_in_progress} = -e $self->_clonep_path;
275 $self->{clone_logged} = -e $self->_clonelog_path;
276 $self->{clone_failed} = -e $self->_clonefail_path;
277 $self->{ccrypt} = $self->{crypt};
279 $self->_properties_load;
280 return $self;
282 close F;
283 undef;
286 # $proj may not be in sane state if this returns false!
287 sub cgi_fill {
288 my $self = shift;
289 my ($gcgi) = @_;
290 my $cgi = $gcgi->cgi;
292 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
293 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
294 if ($pwd or not $self->{crypt}) {
295 $self->{crypt} = scrypt($pwd);
298 if ($pwd2 and $pwd ne $pwd2) {
299 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
302 $self->{cpwd} = $cgi->param('cpwd');
304 if ($Girocco::Config::project_owners eq 'email') {
305 $self->{email} = $gcgi->wparam('email');
306 valid_email($self->{email})
307 or $gcgi->err("Your email sure looks weird...?");
310 $self->{url} = $gcgi->wparam('url');
311 if ($self->{url}) {
312 valid_repo_url($self->{url})
313 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
316 $self->{desc} = $gcgi->wparam('desc');
317 length($self->{desc}) <= 1024
318 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
320 $self->{README} = $gcgi->wparam('README');
321 length($self->{README}) <= 8192
322 or $gcgi->err("README length &gt; 8kb!");
324 $self->{hp} = $gcgi->wparam('hp');
325 if ($self->{hp}) {
326 valid_web_url($self->{hp})
327 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
330 # FIXME: Permit only existing users
331 $self->{users} = [grep { Girocco::User::valid_name($_) } $cgi->param('user')];
333 not $gcgi->err_check;
336 sub form_defaults {
337 my $self = shift;
339 name => $self->{name},
340 email => $self->{email},
341 url => $self->{url},
342 desc => html_esc($self->{desc}),
343 README => html_esc($self->{README}),
344 hp => $self->{hp},
345 users => $self->{users},
349 sub authenticate {
350 my $self = shift;
351 my ($gcgi) = @_;
353 $self->{ccrypt} or die "Can't authenticate against a project with no password";
354 $self->{cpwd} ||= '';
355 unless ($self->{ccrypt} eq crypt($self->{cpwd}, $self->{ccrypt})) {
356 $gcgi->err("Your admin password does not match!");
357 return 0;
359 return 1;
362 sub _setup {
363 my $self = shift;
364 my ($pushers) = @_;
366 $self->_mkdir_forkees;
368 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
369 if ($Girocco::Config::owning_group) {
370 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
371 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
372 chmod(02775, $self->{path}) or die "chmod 2775 $self->{path} failed: $!";
373 } else {
374 chmod(02777, $self->{path}) or die "chmod 2777 $self->{path} failed: $!";
376 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
377 or die "git init $self->{path} failed: $?";
378 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
379 or die "disabling receive.denyNonFastforwards failed: $?";
380 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
381 or die "disabling gc.auto failed: $?";
383 $self->_properties_save;
384 $self->_alternates_setup;
385 $self->_ctags_setup;
386 $self->_group_add($pushers);
387 $self->_hooks_install;
388 $self->perm_initialize;
391 sub premirror {
392 my $self = shift;
394 $self->_setup(':');
395 $self->_clonep(1);
398 sub conjure {
399 my $self = shift;
401 $self->_setup;
402 $self->_nofetch(1);
405 sub clone {
406 my $self = shift;
408 unlink ($self->_clonefail_path()); # Ignore EEXIST error
410 use IO::Socket;
411 my $sock = IO::Socket::UNIX->new('/tmp/girocco.cloned') or die "cannot connect to girocco.cloned: $!";
412 $sock->print($self->{name}.".git\n");
413 # Just ignore reply, we are going to succeed anyway and the I/O
414 # would apparently get quite hairy.
415 $sock->flush();
416 sleep 2; # *cough*
417 $sock->close();
420 sub update {
421 my $self = shift;
423 $self->_properties_save;
424 $self->_group_update;
426 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
427 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
428 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
429 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
434 sub update_password {
435 my $self = shift;
436 my ($pwd) = @_;
438 $self->{crypt} = scrypt($pwd);
439 $self->_group_update;
442 # You can explicitly do this just on a ghost() repository too.
443 sub delete {
444 my $self = shift;
446 if (-d $self->{path}) {
447 system('rm', '-rf', $self->{path}) == 0
448 or die "rm -rf $self->{path} failed: $?";
450 $self->_group_remove;
453 sub has_forks {
454 my $self = shift;
456 return glob($Girocco::Config::reporoot.'/'.$self->{name}.'/*');
460 ### static methods
462 sub get_forkee_name {
463 $_ = $_[0];
464 (m#^(.*)/.*?$#)[0]; #
466 sub get_forkee_path {
467 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
468 -d $forkee ? $forkee : '';
471 sub valid_name {
472 $_ = $_[0];
473 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
474 and (not m#\./#)
475 and (not m#/$#)
476 and m#^[a-zA-Z0-9+./_-]+$#;
479 sub does_exist {
480 my ($name) = @_;
481 valid_name($name) or die "tried to query for project with invalid name $name!";
482 available($name);
484 sub available {
485 my ($name) = @_;
486 valid_name($name) or die "tried to query for project with invalid name $name!";
487 (-d $Girocco::Config::reporoot."/$name.git");