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 my $pname = $propmap{$name};
51 $pname or die "unknown property: $name";
52 if ($pname =~ s/^://) {
53 return `git --git-dir="$self->{path}" config "gitweb.$pname"`
56 open P
, $self->_property_path($pname) or return undef;
59 my $value = join('', @value); chomp $value;
65 my ($name, $value) = @_;
66 my $pname = $propmap{$name};
67 $pname or die "unknown property: $name";
69 if ($pname =~ s/^://) {
70 return `git --git-dir="$self->{path}" config "gitweb.$pname" "$value"`
73 my $P = lock_file
($self->_property_path($pname));
74 $value ne '' and print $P "$value\n";
76 unlock_file
($self->_property_path($pname));
79 sub _properties_load
{
81 foreach my $prop (keys %propmap) {
82 $self->{$prop} = $self->_property_fget($prop);
86 sub _properties_save
{
88 foreach my $prop (keys %propmap) {
89 $self->_property_fput($prop, $self->{$prop});
95 $self->_property_path('.nofetch');
101 my $np = $self->_nofetch_path;
103 open X
, '>'.$np or die "nofetch failed: $!";
106 unlink $np or die "yesfetch failed: $!";
112 $self->_property_path('.clonelog');
115 sub _clonefail_path
{
117 $self->_property_path('.clone_failed');
122 $self->_property_path('.clone_in_progress');
128 my $np = $self->_clonep_path;
130 open X
, '>'.$np or die "clonep failed: $!";
133 unlink $np or die "clonef failed: $!";
136 sub _alternates_setup
{
138 return unless $self->{name
} =~ m
#/#;
139 my $forkee_name = get_forkee_name
($self->{name
});
140 my $forkee_path = get_forkee_path
($self->{name
});
141 return unless -d
$forkee_path;
142 mkdir $self->{path
}.'/refs'; chmod 0775, $self->{path
}.'/refs';
143 mkdir $self->{path
}.'/objects'; chmod 0775, $self->{path
}.'/objects';
144 mkdir $self->{path
}.'/objects/info'; chmod 0775, $self->{path
}.'/objects/info';
146 # We set up both alternates and http_alternates since we cannot use
147 # relative path in alternates - that doesn't work recursively.
149 my $filename = $self->{path
}.'/objects/info/alternates';
150 open X
, '>'.$filename or die "alternates failed: $!";
151 print X
"$forkee_path/objects\n";
153 chmod 0664, $filename or warn "cannot chmod $filename: $!";
155 if ($Girocco::Config
::httppullurl
) {
156 $filename = $self->{path
}.'/objects/info/http-alternates';
157 open X
, '>'.$filename or die "http-alternates failed: $!";
158 my $upfork = $forkee_name;
159 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
161 chmod 0664, $filename or warn "cannot chmod $filename: $!";
164 # The symlink is problematic since git remote prune will traverse it.
165 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
170 mkdir $self->{path
}.'/ctags'; chmod 01777, $self->{path
}.'/ctags';
176 $xtra .= join(',', @
{$self->{users
}});
177 filedb_atomic_append
(jailed_file
('/etc/group'),
178 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
183 my $xtra = join(',', @
{$self->{users
}});
184 filedb_atomic_edit
(jailed_file
('/etc/group'),
188 if ($self->{name
} eq (split /:/)[0]) {
189 # preserve readonly flag
190 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
191 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
201 filedb_atomic_edit
(jailed_file
('/etc/group'),
203 $self->{name
} ne (split /:/)[0] and return $_;
211 $self->{path
}.'/hooks/'.$name;
217 open SRC
, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
218 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
219 while (<SRC
>) { print DST
$_; }
222 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
227 foreach my $hook ('update') {
228 $self->_hook_install($hook);
232 # private constructor, do not use
235 my ($name, $base_path, $path) = @_;
236 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
237 $path ||= "$base_path/$name.git";
238 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
243 # public constructor #0
244 # creates a virtual project not connected to disk image
245 # you can conjure() it later to disk
248 my ($name, $mirror) = @_;
249 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
251 $self->{mirror
} = $mirror;
255 # public constructor #1
260 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
264 next unless (shift eq $name);
266 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
267 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
270 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
272 $self->{users
} = [split /,/, $ulist];
273 $self->{orig_users
} = [@
{$self->{users
}}];
274 $self->{mirror
} = ! -e
$self->_nofetch_path;
275 $self->{clone_in_progress
} = -e
$self->_clonep_path;
276 $self->{clone_logged
} = -e
$self->_clonelog_path;
277 $self->{clone_failed
} = -e
$self->_clonefail_path;
278 $self->{ccrypt
} = $self->{crypt};
280 $self->_properties_load;
287 # $proj may not be in sane state if this returns false!
291 my $cgi = $gcgi->cgi;
293 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
294 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
295 if ($pwd or not $self->{crypt}) {
296 $self->{crypt} = scrypt
($pwd);
299 if ($pwd2 and $pwd ne $pwd2) {
300 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
303 $self->{cpwd
} = $cgi->param('cpwd');
305 if ($Girocco::Config
::project_owners
eq 'email') {
306 $self->{email
} = $gcgi->wparam('email');
307 valid_email
($self->{email
})
308 or $gcgi->err("Your email sure looks weird...?");
311 $self->{url
} = $gcgi->wparam('url');
313 valid_repo_url
($self->{url
})
314 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
317 $self->{desc
} = $gcgi->wparam('desc');
318 length($self->{desc
}) <= 1024
319 or $gcgi->err("<b>Short</b> description length > 1kb!");
321 $self->{README
} = $gcgi->wparam('README');
322 length($self->{README
}) <= 8192
323 or $gcgi->err("README length > 8kb!");
325 $self->{hp
} = $gcgi->wparam('hp');
327 valid_web_url
($self->{hp
})
328 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
331 # FIXME: Permit only existing users
332 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) } $cgi->param('user')];
334 not $gcgi->err_check;
340 name
=> $self->{name
},
341 email
=> $self->{email
},
343 desc
=> html_esc
($self->{desc
}),
344 README
=> html_esc
($self->{README
}),
346 users
=> $self->{users
},
354 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
355 $self->{cpwd
} ||= '';
356 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
357 $gcgi->err("Your admin password does not match!");
367 $self->_mkdir_forkees;
369 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
370 if ($Girocco::Config
::owning_group
) {
371 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
372 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
373 chmod(02775, $self->{path
}) or die "chmod 2775 $self->{path} failed: $!";
375 chmod(02777, $self->{path
}) or die "chmod 2777 $self->{path} failed: $!";
377 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
378 or die "git init $self->{path} failed: $?";
379 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
380 or die "disabling receive.denyNonFastforwards failed: $?";
381 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
382 or die "disabling gc.auto failed: $?";
384 # /info must have right permissions even before the fixup job,
385 # and git init didn't do it for some reason.
386 if ($Girocco::Config
::owning_group
) {
387 chmod(02775, $self->{path
}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
389 chmod(02777, $self->{path
}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
392 $self->_properties_save;
393 $self->_alternates_setup;
395 $self->_group_add($pushers);
396 $self->_hooks_install;
397 $self->perm_initialize;
398 system($Girocco::Config
::basedir
. '/gitweb/genindex.sh');
418 unlink ($self->_clonefail_path()); # Ignore EEXIST error
421 my $sock = IO
::Socket
::UNIX
->new('/tmp/girocco.cloned') or die "cannot connect to girocco.cloned: $!";
422 $sock->print($self->{name
}.".git\n");
423 # Just ignore reply, we are going to succeed anyway and the I/O
424 # would apparently get quite hairy.
433 $self->_properties_save;
434 $self->_group_update;
436 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
437 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
438 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
439 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
444 sub update_password
{
448 $self->{crypt} = scrypt
($pwd);
449 $self->_group_update;
452 # You can explicitly do this just on a ghost() repository too.
456 if (-d
$self->{path
}) {
457 system('rm', '-rf', $self->{path
}) == 0
458 or die "rm -rf $self->{path} failed: $?";
460 $self->_group_remove;
466 return glob($Girocco::Config
::reporoot
.'/'.$self->{name
}.'/*');
472 sub get_forkee_name
{
474 (m
#^(.*)/.*?$#)[0]; #
476 sub get_forkee_path
{
477 my $forkee = $Girocco::Config
::reporoot
.'/'.get_forkee_name
($_[0]).'.git';
478 -d
$forkee ?
$forkee : '';
483 (not m
#/# or -d get_forkee_path($_)) # will also catch ^/
486 and m
#^[a-zA-Z0-9+./_-]+$#;
491 valid_name
($name) or die "tried to query for project with invalid name $name!";
496 valid_name
($name) or die "tried to query for project with invalid name $name!";
497 (-d
$Girocco::Config
::reporoot
."/$name.git");