Girocco::Config::project_owners: Introduce configuration variable
[girocco/testingthisout.git] / Girocco / Project.pm
blobb1f1e84e91b6e484316cc3988790fc4762a61889
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 sub _mkdir_forkees {
16 my $self = shift;
17 my @pelems = split('/', $self->{name});
18 pop @pelems; # do not create dir for the project itself
19 my $path = $self->{base_path};
20 foreach my $pelem (@pelems) {
21 $path .= "/$pelem";
22 (-d "$path") or mkdir $path or die "mkdir $path: $!";
23 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
27 our %propmap = (
28 url => 'base_url',
29 email => 'owner',
30 desc => 'description',
31 README => 'README.html',
32 hp => 'homepage',
35 sub _property_path {
36 my $self = shift;
37 my ($name) = @_;
38 $self->{path}.'/'.$name;
41 sub _property_fget {
42 my $self = shift;
43 my ($name) = @_;
44 $propmap{$name} or die "unknown property: $name";
45 open P, $self->_property_path($propmap{$name}) or return undef;
46 my @value = <P>;
47 close P;
48 my $value = join('', @value); chomp $value;
49 $value;
52 sub _property_fput {
53 my $self = shift;
54 my ($name, $value) = @_;
55 $propmap{$name} or die "unknown property: $name";
56 $value ||= '';
58 my $P = lock_file($self->_property_path($propmap{$name}));
59 $value ne '' and print $P "$value\n";
60 close $P;
61 unlock_file($self->_property_path($propmap{$name}));
64 sub _properties_load {
65 my $self = shift;
66 foreach my $prop (keys %propmap) {
67 $self->{$prop} = $self->_property_fget($prop);
71 sub _properties_save {
72 my $self = shift;
73 foreach my $prop (keys %propmap) {
74 $self->_property_fput($prop, $self->{$prop});
78 sub _nofetch_path {
79 my $self = shift;
80 $self->_property_path('.nofetch');
83 sub _nofetch {
84 my $self = shift;
85 my ($nofetch) = @_;
86 my $np = $self->_nofetch_path;
87 if ($nofetch) {
88 open X, '>'.$np or die "nofetch failed: $!";
89 close X;
90 } else {
91 unlink $np or die "yesfetch failed: $!";
95 sub _clonelog_path {
96 my $self = shift;
97 $self->_property_path('.clonelog');
100 sub _clonefail_path {
101 my $self = shift;
102 $self->_property_path('.clone_failed');
105 sub _clonep_path {
106 my $self = shift;
107 $self->_property_path('.clone_in_progress');
110 sub _clonep {
111 my $self = shift;
112 my ($nofetch) = @_;
113 my $np = $self->_clonep_path;
114 if ($nofetch) {
115 open X, '>'.$np or die "clonep failed: $!";
116 close X;
117 } else {
118 unlink $np or die "clonef failed: $!";
121 sub _alternates_setup {
122 my $self = shift;
123 return unless $self->{name} =~ m#/#;
124 my $forkee_name = get_forkee_name($self->{name});
125 my $forkee_path = get_forkee_path($self->{name});
126 return unless -d $forkee_path;
127 mkdir $self->{path}.'/refs'; chmod 0775, $self->{path}.'/refs';
128 mkdir $self->{path}.'/objects'; chmod 0775, $self->{path}.'/objects';
129 mkdir $self->{path}.'/objects/info'; chmod 0775, $self->{path}.'/objects/info';
131 # We set up both alternates and http_alternates since we cannot use
132 # relative path in alternates - that doesn't work recursively.
134 my $filename = $self->{path}.'/objects/info/alternates';
135 open X, '>'.$filename or die "alternates failed: $!";
136 print X "$forkee_path/objects\n";
137 close X;
138 chmod 0664, $filename or warn "cannot chmod $filename: $!";
140 if ($Girocco::Config::httppullurl) {
141 $filename = $self->{path}.'/objects/info/http-alternates';
142 open X, '>'.$filename or die "http-alternates failed: $!";
143 my $upfork = $forkee_name;
144 do { print X "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
145 close X;
146 chmod 0664, $filename or warn "cannot chmod $filename: $!";
149 symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
152 sub _ctags_setup {
153 my $self = shift;
154 mkdir $self->{path}.'/ctags'; chmod 01777, $self->{path}.'/ctags';
157 sub _group_add {
158 my $self = shift;
159 my ($xtra) = @_;
160 $xtra .= join(',', @{$self->{users}});
161 filedb_atomic_append(jailed_file('/etc/group'),
162 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
165 sub _group_update {
166 my $self = shift;
167 my $xtra = join(',', @{$self->{users}});
168 filedb_atomic_edit(jailed_file('/etc/group'),
169 sub {
170 $_ = $_[0];
171 chomp;
172 if ($self->{name} eq (split /:/)[0]) {
173 # preserve readonly flag
174 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
175 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
176 } else {
177 return "$_\n";
183 sub _group_remove {
184 my $self = shift;
185 filedb_atomic_edit(jailed_file('/etc/group'),
186 sub {
187 $self->{name} ne (split /:/)[0] and return $_;
192 sub _hook_path {
193 my $self = shift;
194 my ($name) = @_;
195 $self->{path}.'/hooks/'.$name;
198 sub _hook_install {
199 my $self = shift;
200 my ($name) = @_;
201 open SRC, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
202 open DST, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
203 while (<SRC>) { print DST $_; }
204 close DST;
205 close SRC;
206 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
209 sub _hooks_install {
210 my $self = shift;
211 foreach my $hook ('update') {
212 $self->_hook_install($hook);
216 # private constructor, do not use
217 sub _new {
218 my $class = shift;
219 my ($name, $base_path, $path) = @_;
220 valid_name($name) or die "refusing to create project with invalid name ($name)!";
221 $path ||= "$base_path/$name.git";
222 my $proj = { name => $name, base_path => $base_path, path => $path };
224 bless $proj, $class;
227 # public constructor #0
228 # creates a virtual project not connected to disk image
229 # you can conjure() it later to disk
230 sub ghost {
231 my $class = shift;
232 my ($name, $mirror) = @_;
233 my $self = $class->_new($name, $Girocco::Config::reporoot);
234 $self->{users} = [];
235 $self->{mirror} = $mirror;
236 $self;
239 # public constructor #1
240 sub load {
241 my $class = shift;
242 my ($name) = @_;
244 open F, jailed_file("/etc/group") or die "project load failed: $!";
245 while (<F>) {
246 chomp;
247 @_ = split /:+/;
248 next unless (shift eq $name);
250 my $self = $class->_new($name, $Girocco::Config::reporoot);
251 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
253 my $ulist;
254 ($self->{crypt}, $self->{gid}, $ulist) = @_;
255 $ulist ||= '';
256 $self->{users} = [split /,/, $ulist];
257 $self->{orig_users} = [@{$self->{users}}];
258 $self->{mirror} = ! -e $self->_nofetch_path;
259 $self->{clone_in_progress} = -e $self->_clonep_path;
260 $self->{clone_logged} = -e $self->_clonelog_path;
261 $self->{clone_failed} = -e $self->_clonefail_path;
262 $self->{ccrypt} = $self->{crypt};
264 $self->_properties_load;
265 return $self;
267 close F;
268 undef;
271 # $proj may not be in sane state if this returns false!
272 sub cgi_fill {
273 my $self = shift;
274 my ($gcgi) = @_;
275 my $cgi = $gcgi->cgi;
277 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
278 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
279 if ($pwd or not $self->{crypt}) {
280 $self->{crypt} = scrypt($pwd);
283 if ($pwd2 and $pwd ne $pwd2) {
284 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
287 $self->{cpwd} = $cgi->param('cpwd');
289 if ($Girocco::Config::project_owners eq 'email') {
290 $self->{email} = $gcgi->wparam('email');
291 valid_email($self->{email})
292 or $gcgi->err("Your email sure looks weird...?");
295 $self->{url} = $gcgi->wparam('url');
296 if ($self->{url}) {
297 valid_repo_url($self->{url})
298 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
301 $self->{desc} = $gcgi->wparam('desc');
302 length($self->{desc}) <= 1024
303 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
305 $self->{README} = $gcgi->wparam('README');
306 length($self->{README}) <= 8192
307 or $gcgi->err("README length &gt; 8kb!");
309 $self->{hp} = $gcgi->wparam('hp');
310 if ($self->{hp}) {
311 valid_web_url($self->{hp})
312 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
315 # FIXME: Permit only existing users
316 $self->{users} = [grep { Girocco::User::valid_name($_) } $cgi->param('user')];
318 not $gcgi->err_check;
321 sub form_defaults {
322 my $self = shift;
324 name => $self->{name},
325 email => $self->{email},
326 url => $self->{url},
327 desc => html_esc($self->{desc}),
328 README => html_esc($self->{README}),
329 hp => $self->{hp},
330 users => $self->{users},
334 sub authenticate {
335 my $self = shift;
336 my ($gcgi) = @_;
338 $self->{ccrypt} or die "Can't authenticate against a project with no password";
339 $self->{cpwd} ||= '';
340 unless ($self->{ccrypt} eq crypt($self->{cpwd}, $self->{ccrypt})) {
341 $gcgi->err("Your admin password does not match!");
342 return 0;
344 return 1;
347 sub _setup {
348 my $self = shift;
349 my ($pushers) = @_;
351 $self->_mkdir_forkees;
353 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
354 if ($Girocco::Config::owning_group) {
355 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
356 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
357 chmod(02775, $self->{path}) or die "chmod 2775 $self->{path} failed: $!";
358 } else {
359 chmod(02777, $self->{path}) or die "chmod 2777 $self->{path} failed: $!";
361 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
362 or die "git init $self->{path} failed: $?";
363 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
364 or die "disabling receive.denyNonFastforwards failed: $?";
366 $self->_properties_save;
367 $self->_alternates_setup;
368 $self->_ctags_setup;
369 $self->_group_add($pushers);
370 $self->_hooks_install;
371 $self->perm_initialize;
374 sub premirror {
375 my $self = shift;
377 $self->_setup(':');
378 $self->_clonep(1);
381 sub conjure {
382 my $self = shift;
384 $self->_setup;
385 $self->_nofetch(1);
388 sub clone {
389 my $self = shift;
391 unlink ($self->_clonefail_path()); # Ignore EEXIST error
393 use IO::Socket;
394 my $sock = IO::Socket::UNIX->new('/tmp/girocco.cloned') or die "cannot connect to girocco.cloned: $!";
395 $sock->print($self->{name}.".git\n");
396 # Just ignore reply, we are going to succeed anyway and the I/O
397 # would apparently get quite hairy.
398 $sock->flush();
399 sleep 2; # *cough*
400 $sock->close();
403 sub update {
404 my $self = shift;
406 $self->_properties_save;
407 $self->_group_update;
409 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
410 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
411 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
412 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
417 sub update_password {
418 my $self = shift;
419 my ($pwd) = @_;
421 $self->{crypt} = scrypt($pwd);
422 $self->_group_update;
425 # You can explicitly do this just on a ghost() repository too.
426 sub delete {
427 my $self = shift;
429 if (-d $self->{path}) {
430 system('rm', '-rf', $self->{path}) == 0
431 or die "rm -rf $self->{path} failed: $?";
433 $self->_group_remove;
436 sub has_forks {
437 my $self = shift;
439 return glob($Girocco::Config::reporoot.'/'.$self->{name}.'/*');
443 ### static methods
445 sub get_forkee_name {
446 $_ = $_[0];
447 (m#^(.*)/.*?$#)[0]; #
449 sub get_forkee_path {
450 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
451 -d $forkee ? $forkee : '';
454 sub valid_name {
455 $_ = $_[0];
456 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
457 and (not m#\./#)
458 and (not m#/$#)
459 and m#^[a-zA-Z0-9+./_-]+$#;
462 sub does_exist {
463 my ($name) = @_;
464 valid_name($name) or die "tried to query for project with invalid name $name!";
465 available($name);
467 sub available {
468 my ($name) = @_;
469 valid_name($name) or die "tried to query for project with invalid name $name!";
470 (-d $Girocco::Config::reporoot."/$name.git");