1 package Girocco
::Project
;
10 use Girocco
::ProjPerm
;
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'],
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) {
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)
36 desc
=> 'description',
37 README
=> 'README.html',
44 $self->{path
}.'/'.$name;
50 $propmap{$name} or die "unknown property: $name";
51 open P
, $self->_property_path($propmap{$name}) or return undef;
54 my $value = join('', @value); chomp $value;
60 my ($name, $value) = @_;
61 $propmap{$name} or die "unknown property: $name";
64 my $P = lock_file
($self->_property_path($propmap{$name}));
65 $value ne '' and print $P "$value\n";
67 unlock_file
($self->_property_path($propmap{$name}));
70 sub _properties_load
{
72 foreach my $prop (keys %propmap) {
73 $self->{$prop} = $self->_property_fget($prop);
77 sub _properties_save
{
79 foreach my $prop (keys %propmap) {
80 $self->_property_fput($prop, $self->{$prop});
86 $self->_property_path('.nofetch');
92 my $np = $self->_nofetch_path;
94 open X
, '>'.$np or die "nofetch failed: $!";
97 unlink $np or die "yesfetch failed: $!";
103 $self->_property_path('.clonelog');
106 sub _clonefail_path
{
108 $self->_property_path('.clone_failed');
113 $self->_property_path('.clone_in_progress');
119 my $np = $self->_clonep_path;
121 open X
, '>'.$np or die "clonep failed: $!";
124 unlink $np or die "clonef failed: $!";
127 sub _alternates_setup
{
129 return unless $self->{name
} =~ m
#/#;
130 my $forkee_name = get_forkee_name
($self->{name
});
131 my $forkee_path = get_forkee_path
($self->{name
});
132 return unless -d
$forkee_path;
133 mkdir $self->{path
}.'/refs'; chmod 0775, $self->{path
}.'/refs';
134 mkdir $self->{path
}.'/objects'; chmod 0775, $self->{path
}.'/objects';
135 mkdir $self->{path
}.'/objects/info'; chmod 0775, $self->{path
}.'/objects/info';
137 # We set up both alternates and http_alternates since we cannot use
138 # relative path in alternates - that doesn't work recursively.
140 my $filename = $self->{path
}.'/objects/info/alternates';
141 open X
, '>'.$filename or die "alternates failed: $!";
142 print X
"$forkee_path/objects\n";
144 chmod 0664, $filename or warn "cannot chmod $filename: $!";
146 if ($Girocco::Config
::httppullurl
) {
147 $filename = $self->{path
}.'/objects/info/http-alternates';
148 open X
, '>'.$filename or die "http-alternates failed: $!";
149 my $upfork = $forkee_name;
150 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
152 chmod 0664, $filename or warn "cannot chmod $filename: $!";
155 symlink "$forkee_path/refs", $self->{path
}.'/refs/forkee';
160 mkdir $self->{path
}.'/ctags'; chmod 01777, $self->{path
}.'/ctags';
166 $xtra .= join(',', @
{$self->{users
}});
167 filedb_atomic_append
(jailed_file
('/etc/group'),
168 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
173 my $xtra = join(',', @
{$self->{users
}});
174 filedb_atomic_edit
(jailed_file
('/etc/group'),
178 if ($self->{name
} eq (split /:/)[0]) {
179 # preserve readonly flag
180 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
181 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
191 filedb_atomic_edit
(jailed_file
('/etc/group'),
193 $self->{name
} ne (split /:/)[0] and return $_;
201 $self->{path
}.'/hooks/'.$name;
207 open SRC
, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
208 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
209 while (<SRC
>) { print DST
$_; }
212 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
217 foreach my $hook ('update') {
218 $self->_hook_install($hook);
222 # private constructor, do not use
225 my ($name, $base_path, $path) = @_;
226 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
227 $path ||= "$base_path/$name.git";
228 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
233 # public constructor #0
234 # creates a virtual project not connected to disk image
235 # you can conjure() it later to disk
238 my ($name, $mirror) = @_;
239 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
241 $self->{mirror
} = $mirror;
245 # public constructor #1
250 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
254 next unless (shift eq $name);
256 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
257 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
260 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
262 $self->{users
} = [split /,/, $ulist];
263 $self->{orig_users
} = [@
{$self->{users
}}];
264 $self->{mirror
} = ! -e
$self->_nofetch_path;
265 $self->{clone_in_progress
} = -e
$self->_clonep_path;
266 $self->{clone_logged
} = -e
$self->_clonelog_path;
267 $self->{clone_failed
} = -e
$self->_clonefail_path;
268 $self->{ccrypt
} = $self->{crypt};
270 $self->_properties_load;
277 # $proj may not be in sane state if this returns false!
281 my $cgi = $gcgi->cgi;
283 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
284 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
285 if ($pwd or not $self->{crypt}) {
286 $self->{crypt} = scrypt
($pwd);
289 if ($pwd2 and $pwd ne $pwd2) {
290 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
293 $self->{cpwd
} = $cgi->param('cpwd');
295 if ($Girocco::Config
::project_owners
eq 'email') {
296 $self->{email
} = $gcgi->wparam('email');
297 valid_email
($self->{email
})
298 or $gcgi->err("Your email sure looks weird...?");
301 $self->{url
} = $gcgi->wparam('url');
303 valid_repo_url
($self->{url
})
304 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
307 $self->{desc
} = $gcgi->wparam('desc');
308 length($self->{desc
}) <= 1024
309 or $gcgi->err("<b>Short</b> description length > 1kb!");
311 $self->{README
} = $gcgi->wparam('README');
312 length($self->{README
}) <= 8192
313 or $gcgi->err("README length > 8kb!");
315 $self->{hp
} = $gcgi->wparam('hp');
317 valid_web_url
($self->{hp
})
318 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
321 # FIXME: Permit only existing users
322 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) } $cgi->param('user')];
324 not $gcgi->err_check;
330 name
=> $self->{name
},
331 email
=> $self->{email
},
333 desc
=> html_esc
($self->{desc
}),
334 README
=> html_esc
($self->{README
}),
336 users
=> $self->{users
},
344 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
345 $self->{cpwd
} ||= '';
346 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
347 $gcgi->err("Your admin password does not match!");
357 $self->_mkdir_forkees;
359 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
360 if ($Girocco::Config
::owning_group
) {
361 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
362 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
363 chmod(02775, $self->{path
}) or die "chmod 2775 $self->{path} failed: $!";
365 chmod(02777, $self->{path
}) or die "chmod 2777 $self->{path} failed: $!";
367 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
368 or die "git init $self->{path} failed: $?";
369 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
370 or die "disabling receive.denyNonFastforwards failed: $?";
371 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
372 or die "disabling gc.auto failed: $?";
374 $self->_properties_save;
375 $self->_alternates_setup;
377 $self->_group_add($pushers);
378 $self->_hooks_install;
379 $self->perm_initialize;
399 unlink ($self->_clonefail_path()); # Ignore EEXIST error
402 my $sock = IO
::Socket
::UNIX
->new('/tmp/girocco.cloned') or die "cannot connect to girocco.cloned: $!";
403 $sock->print($self->{name
}.".git\n");
404 # Just ignore reply, we are going to succeed anyway and the I/O
405 # would apparently get quite hairy.
414 $self->_properties_save;
415 $self->_group_update;
417 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
418 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
419 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
420 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
425 sub update_password
{
429 $self->{crypt} = scrypt
($pwd);
430 $self->_group_update;
433 # You can explicitly do this just on a ghost() repository too.
437 if (-d
$self->{path
}) {
438 system('rm', '-rf', $self->{path
}) == 0
439 or die "rm -rf $self->{path} failed: $?";
441 $self->_group_remove;
447 return glob($Girocco::Config
::reporoot
.'/'.$self->{name
}.'/*');
453 sub get_forkee_name
{
455 (m
#^(.*)/.*?$#)[0]; #
457 sub get_forkee_path
{
458 my $forkee = $Girocco::Config
::reporoot
.'/'.get_forkee_name
($_[0]).'.git';
459 -d
$forkee ?
$forkee : '';
464 (not m
#/# or -d get_forkee_path($_)) # will also catch ^/
467 and m
#^[a-zA-Z0-9+./_-]+$#;
472 valid_name
($name) or die "tried to query for project with invalid name $name!";
477 valid_name
($name) or die "tried to query for project with invalid name $name!";
478 (-d
$Girocco::Config
::reporoot
."/$name.git");