Guide the user to fix settings when clone failed
[girocco.git] / Girocco / Project.pm
blob6edfc2ab9950810d1e76bb6b3601ad28ecb3800a
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 = $cgi->param('pwd');
278 if ($pwd ne '' or not $self->{crypt}) {
279 $self->{crypt} = scrypt($pwd);
282 if ($cgi->param('pwd2') and $pwd ne $cgi->param('pwd2')) {
283 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
286 $self->{cpwd} = $cgi->param('cpwd');
288 $self->{email} = $gcgi->wparam('email');
289 valid_email($self->{email})
290 or $gcgi->err("Your email sure looks weird...?");
292 $self->{url} = $gcgi->wparam('url');
293 if ($self->{url}) {
294 valid_repo_url($self->{url})
295 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
298 $self->{desc} = $gcgi->wparam('desc');
299 length($self->{desc}) <= 1024
300 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
302 $self->{README} = $gcgi->wparam('README');
303 length($self->{README}) <= 8192
304 or $gcgi->err("README length &gt; 8kb!");
306 $self->{hp} = $gcgi->wparam('hp');
307 if ($self->{hp}) {
308 valid_web_url($self->{hp})
309 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
312 # FIXME: Permit only existing users
313 $self->{users} = [grep { Girocco::User::valid_name($_) } $cgi->param('user')];
315 not $gcgi->err_check;
318 sub form_defaults {
319 my $self = shift;
321 name => $self->{name},
322 email => $self->{email},
323 url => $self->{url},
324 desc => html_esc($self->{desc}),
325 README => html_esc($self->{README}),
326 hp => $self->{hp},
327 users => $self->{users},
331 sub authenticate {
332 my $self = shift;
333 my ($gcgi) = @_;
335 $self->{ccrypt} or die "Can't authenticate against a project with no password";
336 $self->{cpwd} or $gcgi->err("No password entered.");
337 unless ($self->{ccrypt} eq crypt($self->{cpwd}, $self->{ccrypt})) {
338 $gcgi->err("Your admin password does not match!");
339 return 0;
341 return 1;
344 sub _setup {
345 my $self = shift;
346 my ($pushers) = @_;
348 $self->_mkdir_forkees;
350 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
351 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
352 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
353 chmod(02775, $self->{path}) or die "chmod 2775 $self->{path} failed: $!";
354 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
355 or die "git init $self->{path} failed: $?";
356 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
357 or die "disabling receive.denyNonFastforwards failed: $?";
359 $self->_properties_save;
360 $self->_alternates_setup;
361 $self->_ctags_setup;
362 $self->_group_add($pushers);
363 $self->_hooks_install;
364 $self->perm_initialize;
367 sub premirror {
368 my $self = shift;
370 $self->_setup(':');
371 $self->_clonep(1);
374 sub conjure {
375 my $self = shift;
377 $self->_setup;
378 $self->_nofetch(1);
381 sub clone {
382 my $self = shift;
384 unlink ($self->_clonefail_path()); # Ignore EEXIST error
386 use IO::Socket;
387 my $sock = IO::Socket::UNIX->new('/tmp/girocco.cloned') or die "cannot connect to girocco.cloned: $!";
388 $sock->print($self->{name}.".git\n");
389 # Just ignore reply, we are going to succeed anyway and the I/O
390 # would apparently get quite hairy.
391 $sock->flush();
392 sleep 2; # *cough*
393 $sock->close();
396 sub update {
397 my $self = shift;
399 $self->_properties_save;
400 $self->_group_update;
402 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
403 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
404 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
405 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
408 sub update_password {
409 my $self = shift;
410 my ($pwd) = @_;
412 $self->{crypt} = scrypt($pwd);
413 $self->_group_update;
416 # You can explicitly do this just on a ghost() repository too.
417 sub delete {
418 my $self = shift;
420 if (-d $self->{path}) {
421 system('rm', '-rf', $self->{path}) == 0
422 or die "rm -rf $self->{path} failed: $?";
424 $self->_group_remove;
427 sub has_forks {
428 my $self = shift;
430 return glob($Girocco::Config::reporoot.'/'.$self->{name}.'/*');
434 ### static methods
436 sub get_forkee_name {
437 $_ = $_[0];
438 (m#^(.*)/.*?$#)[0]; #
440 sub get_forkee_path {
441 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
442 -d $forkee ? $forkee : '';
445 sub valid_name {
446 $_ = $_[0];
447 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
448 and (not m#\./#)
449 and (not m#/$#)
450 and m#^[a-zA-Z0-9+./_-]+$#;
453 sub does_exist {
454 my ($name) = @_;
455 valid_name($name) or die "tried to query for project with invalid name $name!";
456 available($name);
458 sub available {
459 my ($name) = @_;
460 valid_name($name) or die "tried to query for project with invalid name $name!";
461 (-d $Girocco::Config::reporoot."/$name.git");