jobs/clonecheck.sh: Fix permission setup
[girocco.git] / Girocco / Project.pm
bloba44d583fa119560ec784e8501511aec2d2d7f082
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 _alternates_setup {
96 my $self = shift;
97 return unless $self->{name} =~ m#/#;
98 my $forkee_name = get_forkee_name($self->{name});
99 my $forkee_path = get_forkee_path($self->{name});
100 return unless -d $forkee_path;
101 mkdir $self->{path}.'/refs'; chmod 0775, $self->{path}.'/refs';
102 mkdir $self->{path}.'/objects'; chmod 0775, $self->{path}.'/objects';
103 mkdir $self->{path}.'/objects/info'; chmod 0775, $self->{path}.'/objects/info';
105 # We set up both alternates and http_alternates since we cannot use
106 # relative path in alternates - that doesn't work recursively.
108 my $filename = $self->{path}.'/objects/info/alternates';
109 open X, '>'.$filename or die "alternates failed: $!";
110 print X "$forkee_path/objects\n";
111 close X;
112 chmod 0664, $filename or warn "cannot chmod $filename: $!";
114 if ($Girocco::Config::httppullurl) {
115 $filename = $self->{path}.'/objects/info/http-alternates';
116 open X, '>'.$filename or die "http-alternates failed: $!";
117 my $upfork = $forkee_name;
118 do { print X "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
119 close X;
120 chmod 0664, $filename or warn "cannot chmod $filename: $!";
123 symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
126 sub _ctags_setup {
127 my $self = shift;
128 mkdir $self->{path}.'/ctags'; chmod 01777, $self->{path}.'/ctags';
131 sub _group_add {
132 my $self = shift;
133 my ($xtra) = @_;
134 $xtra .= join(',', @{$self->{users}});
135 filedb_atomic_append(jailed_file('/etc/group'),
136 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
139 sub _group_update {
140 my $self = shift;
141 my $xtra = join(',', @{$self->{users}});
142 filedb_atomic_edit(jailed_file('/etc/group'),
143 sub {
144 $_ = $_[0];
145 chomp;
146 if ($self->{name} eq (split /:/)[0]) {
147 # preserve readonly flag
148 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
149 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
150 } else {
151 return "$_\n";
157 sub _group_remove {
158 my $self = shift;
159 filedb_atomic_edit(jailed_file('/etc/group'),
160 sub {
161 $self->{name} ne (split /:/)[0] and return $_;
166 sub _hook_path {
167 my $self = shift;
168 my ($name) = @_;
169 $self->{path}.'/hooks/'.$name;
172 sub _hook_install {
173 my $self = shift;
174 my ($name) = @_;
175 open SRC, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
176 open DST, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
177 while (<SRC>) { print DST $_; }
178 close DST;
179 close SRC;
180 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
183 sub _hooks_install {
184 my $self = shift;
185 foreach my $hook ('update') {
186 $self->_hook_install($hook);
190 # private constructor, do not use
191 sub _new {
192 my $class = shift;
193 my ($name, $base_path, $path) = @_;
194 valid_name($name) or die "refusing to create project with invalid name ($name)!";
195 $path ||= "$base_path/$name.git";
196 my $proj = { name => $name, base_path => $base_path, path => $path };
198 bless $proj, $class;
201 # public constructor #0
202 # creates a virtual project not connected to disk image
203 # you can conjure() it later to disk
204 sub ghost {
205 my $class = shift;
206 my ($name, $mirror) = @_;
207 my $self = $class->_new($name,
208 $mirror ? "$Girocco::Config::mqueuedir/to-clone" : $Girocco::Config::reporoot);
209 $self->{users} = [];
210 $self->{mirror} = $mirror;
211 $self;
214 # public constructor #1
215 sub load {
216 my $class = shift;
217 my ($name) = @_;
219 open F, jailed_file("/etc/group") or die "project load failed: $!";
220 while (<F>) {
221 chomp;
222 @_ = split /:+/;
223 next unless (shift eq $name);
225 my $self = $class->_new($name, $Girocco::Config::reporoot);
226 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
228 my $ulist;
229 ($self->{crypt}, $self->{gid}, $ulist) = @_;
230 $ulist ||= '';
231 $self->{users} = [split /,/, $ulist];
232 $self->{orig_users} = [@{$self->{users}}];
233 $self->{mirror} = ! -e $self->_nofetch_path;
234 $self->{ccrypt} = $self->{crypt};
236 $self->_properties_load;
237 return $self;
239 close F;
240 undef;
243 # $proj may not be in sane state if this returns false!
244 sub cgi_fill {
245 my $self = shift;
246 my ($gcgi) = @_;
247 my $cgi = $gcgi->cgi;
249 my $pwd = $cgi->param('pwd');
250 if ($pwd ne '' or not $self->{crypt}) {
251 $self->{crypt} = scrypt($pwd);
254 if ($cgi->param('pwd2') and $pwd ne $cgi->param('pwd2')) {
255 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
258 $self->{cpwd} = $cgi->param('cpwd');
260 $self->{email} = $gcgi->wparam('email');
261 valid_email($self->{email})
262 or $gcgi->err("Your email sure looks weird...?");
264 $self->{url} = $gcgi->wparam('url');
265 if ($self->{url}) {
266 valid_repo_url($self->{url})
267 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
270 $self->{desc} = $gcgi->wparam('desc');
271 length($self->{desc}) <= 1024
272 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
274 $self->{README} = $gcgi->wparam('README');
275 length($self->{README}) <= 8192
276 or $gcgi->err("README length &gt; 8kb!");
278 $self->{hp} = $gcgi->wparam('hp');
279 if ($self->{hp}) {
280 valid_web_url($self->{hp})
281 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
284 # FIXME: Permit only existing users
285 $self->{users} = [grep { Girocco::User::valid_name($_) } $cgi->param('user')];
287 not $gcgi->err_check;
290 sub form_defaults {
291 my $self = shift;
293 name => $self->{name},
294 email => $self->{email},
295 url => $self->{url},
296 desc => html_esc($self->{desc}),
297 README => html_esc($self->{README}),
298 hp => $self->{hp},
299 users => $self->{users},
303 sub authenticate {
304 my $self = shift;
305 my ($gcgi) = @_;
307 $self->{ccrypt} or die "Can't authenticate against a project with no password";
308 $self->{cpwd} or $gcgi->err("No password entered.");
309 unless ($self->{ccrypt} eq crypt($self->{cpwd}, $self->{ccrypt})) {
310 $gcgi->err("Your admin password does not match!");
311 return 0;
313 return 1;
316 sub premirror {
317 my $self = shift;
319 $self->_mkdir_forkees;
320 mkdir $self->{path} or die "mkdir $self->{path} failed: $!";
321 chmod 0775, $self->{path} or die "chmod $self->{path} failed: $!";
322 $self->_properties_save;
323 $self->_alternates_setup;
324 $self->_ctags_setup;
325 $self->_group_add(':');
328 sub conjure {
329 my $self = shift;
331 $self->_mkdir_forkees;
333 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
334 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
335 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
336 chmod(2775, $self->{path}) or die "chmod 2775 $self->{path} failed: $!";
337 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
338 or die "git init $self->{path} failed: $?";
339 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
340 or die "disabling receive.denyNonFastforwards failed: $?";
342 $self->_nofetch(1);
343 $self->_properties_save;
344 $self->_alternates_setup;
345 $self->_ctags_setup;
346 $self->_group_add;
347 $self->_hooks_install;
348 $self->perm_initialize;
351 sub update {
352 my $self = shift;
354 $self->_properties_save;
355 $self->_group_update;
357 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
358 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
359 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
360 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
363 sub update_password {
364 my $self = shift;
365 my ($pwd) = @_;
367 $self->{crypt} = scrypt($pwd);
368 $self->_group_update;
371 # You can explicitly do this just on a ghost() repository too.
372 sub delete {
373 my $self = shift;
375 if (-d $self->{path}) {
376 system('rm', '-rf', $self->{path}) == 0
377 or die "rm -rf $self->{path} failed: $?";
379 $self->_group_remove;
382 sub has_forks {
383 my $self = shift;
385 return glob($Girocco::Config::reporoot.'/'.$self->{name}.'/*');
389 ### static methods
391 sub get_forkee_name {
392 $_ = $_[0];
393 (m#^(.*)/.*?$#)[0]; #
395 sub get_forkee_path {
396 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
397 -d $forkee ? $forkee : '';
400 sub valid_name {
401 $_ = $_[0];
402 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
403 and (not m#\./#)
404 and (not m#/$#)
405 and m#^[a-zA-Z0-9+./_-]+$#;
408 sub does_exist {
409 my ($name) = @_;
410 valid_name($name) or die "tried to query for project with invalid name $name!";
411 (available($name)
412 or ($Girocco::Config::mqueuedir
413 and (-d $Girocco::Config::mqueuedir."/cloning/$name"
414 or -d $Girocco::Config::mqueuedir."/to-clone/$name")));
416 sub available {
417 my ($name) = @_;
418 valid_name($name) or die "tried to query for project with invalid name $name!";
419 (-d $Girocco::Config::reporoot."/$name.git");