1 package Girocco
::Project
;
10 use Girocco
::ProjPerm
;
12 use base
('Girocco::ProjPerm::'.$Girocco::Config
::permission_control
); # mwahaha
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) {
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)
30 desc
=> 'description',
31 README
=> 'README.html',
38 $self->{path
}.'/'.$name;
44 $propmap{$name} or die "unknown property: $name";
45 open P
, $self->_property_path($propmap{$name}) or return undef;
48 my $value = join('', @value); chomp $value;
54 my ($name, $value) = @_;
55 $propmap{$name} or die "unknown property: $name";
58 my $P = lock_file
($self->_property_path($propmap{$name}));
59 $value ne '' and print $P "$value\n";
61 unlock_file
($self->_property_path($propmap{$name}));
64 sub _properties_load
{
66 foreach my $prop (keys %propmap) {
67 $self->{$prop} = $self->_property_fget($prop);
71 sub _properties_save
{
73 foreach my $prop (keys %propmap) {
74 $self->_property_fput($prop, $self->{$prop});
80 $self->_property_path('.nofetch');
86 my $np = $self->_nofetch_path;
88 open X
, '>'.$np or die "nofetch failed: $!";
91 unlink $np or die "yesfetch failed: $!";
97 $self->_property_path('.clonelog');
100 sub _clonefail_path
{
102 $self->_property_path('.clone_failed');
107 $self->_property_path('.clone_in_progress');
113 my $np = $self->_clonep_path;
115 open X
, '>'.$np or die "clonep failed: $!";
118 unlink $np or die "clonef failed: $!";
121 sub _alternates_setup
{
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";
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); #
146 chmod 0664, $filename or warn "cannot chmod $filename: $!";
149 symlink "$forkee_path/refs", $self->{path
}.'/refs/forkee';
154 mkdir $self->{path
}.'/ctags'; chmod 01777, $self->{path
}.'/ctags';
160 $xtra .= join(',', @
{$self->{users
}});
161 filedb_atomic_append
(jailed_file
('/etc/group'),
162 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
167 my $xtra = join(',', @
{$self->{users
}});
168 filedb_atomic_edit
(jailed_file
('/etc/group'),
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";
185 filedb_atomic_edit
(jailed_file
('/etc/group'),
187 $self->{name
} ne (split /:/)[0] and return $_;
195 $self->{path
}.'/hooks/'.$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
$_; }
206 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
211 foreach my $hook ('update') {
212 $self->_hook_install($hook);
216 # private constructor, do not use
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 };
227 # public constructor #0
228 # creates a virtual project not connected to disk image
229 # you can conjure() it later to disk
232 my ($name, $mirror) = @_;
233 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
235 $self->{mirror
} = $mirror;
239 # public constructor #1
244 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
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
};
254 ($self->{crypt}, $self->{gid
}, $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;
271 # $proj may not be in sane state if this returns false!
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');
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 > 1kb!");
305 $self->{README
} = $gcgi->wparam('README');
306 length($self->{README
}) <= 8192
307 or $gcgi->err("README length > 8kb!");
309 $self->{hp
} = $gcgi->wparam('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;
324 name
=> $self->{name
},
325 email
=> $self->{email
},
327 desc
=> html_esc
($self->{desc
}),
328 README
=> html_esc
($self->{README
}),
330 users
=> $self->{users
},
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!");
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: $!";
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;
369 $self->_group_add($pushers);
370 $self->_hooks_install;
371 $self->perm_initialize;
391 unlink ($self->_clonefail_path()); # Ignore EEXIST error
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.
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
{
421 $self->{crypt} = scrypt
($pwd);
422 $self->_group_update;
425 # You can explicitly do this just on a ghost() repository too.
429 if (-d
$self->{path
}) {
430 system('rm', '-rf', $self->{path
}) == 0
431 or die "rm -rf $self->{path} failed: $?";
433 $self->_group_remove;
439 return glob($Girocco::Config
::reporoot
.'/'.$self->{name
}.'/*');
445 sub get_forkee_name
{
447 (m
#^(.*)/.*?$#)[0]; #
449 sub get_forkee_path
{
450 my $forkee = $Girocco::Config
::reporoot
.'/'.get_forkee_name
($_[0]).'.git';
451 -d
$forkee ?
$forkee : '';
456 (not m
#/# or -d get_forkee_path($_)) # will also catch ^/
459 and m
#^[a-zA-Z0-9+./_-]+$#;
464 valid_name
($name) or die "tried to query for project with invalid name $name!";
469 valid_name
($name) or die "tried to query for project with invalid name $name!";
470 (-d
$Girocco::Config
::reporoot
."/$name.git");