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'],
19 notifymail
=> ['Commit notify - mail to', 'notifymail', 'text'],
20 notifyjson
=> ['Commit notify - <a href="http://help.github.com/post-receive-hooks/">POST JSON</a> at', 'notifyjson', 'text'],
21 notifycia
=> ['Commit notify - <a href="http://cia.vc/doc/">CIA project</a> name', 'notifycia', 'text'],
26 my @pelems = split('/', $self->{name
});
27 pop @pelems; # do not create dir for the project itself
28 my $path = $self->{base_path
};
29 foreach my $pelem (@pelems) {
31 (-d
"$path") or mkdir $path or die "mkdir $path: $!";
32 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
39 desc
=> 'description',
40 README
=> 'README.html',
42 notifymail
=> '%hooks.mailinglist',
43 notifyjson
=> '%hooks.jsonurl',
44 notifycia
=> '%hooks.cianame',
50 $self->{path
}.'/'.$name;
56 my $pname = $propmap{$name};
57 $pname or die "unknown property: $name";
58 if ($pname =~ s/^://) {
59 my $val = `git --git-dir="$self->{path}" config "gitweb.$pname"`;
62 } elsif ($pname =~ s/^%//) {
63 my $val = `git --git-dir="$self->{path}" config "$pname"`;
68 open P
, $self->_property_path($pname) or return undef;
71 my $value = join('', @value); chomp $value;
77 my ($name, $value) = @_;
78 my $pname = $propmap{$name};
79 $pname or die "unknown property: $name";
81 if ($pname =~ s/^://) {
82 system('git', '--git-dir='.$self->{path
}, 'config', "gitweb.$pname", $value);
84 } elsif ($pname =~ s/^%//) {
85 system('git', '--git-dir='.$self->{path
}, 'config', $pname, $value);
89 my $P = lock_file
($self->_property_path($pname));
90 $value ne '' and print $P "$value\n";
92 unlock_file
($self->_property_path($pname));
95 sub _properties_load
{
97 foreach my $prop (keys %propmap) {
98 $self->{$prop} = $self->_property_fget($prop);
102 sub _properties_save
{
104 foreach my $prop (keys %propmap) {
105 $self->_property_fput($prop, $self->{$prop});
111 $self->_property_path('.nofetch');
117 my $np = $self->_nofetch_path;
119 open X
, '>'.$np or die "nofetch failed: $!";
122 unlink $np or die "yesfetch failed: $!";
128 $self->_property_path('.clonelog');
131 sub _clonefail_path
{
133 $self->_property_path('.clone_failed');
138 $self->_property_path('.clone_in_progress');
144 my $np = $self->_clonep_path;
146 open X
, '>'.$np or die "clonep failed: $!";
149 unlink $np or die "clonef failed: $!";
152 sub _alternates_setup
{
154 return unless $self->{name
} =~ m
#/#;
155 my $forkee_name = get_forkee_name
($self->{name
});
156 my $forkee_path = get_forkee_path
($self->{name
});
157 return unless -d
$forkee_path;
158 mkdir $self->{path
}.'/refs'; chmod 0775, $self->{path
}.'/refs';
159 mkdir $self->{path
}.'/objects'; chmod 0775, $self->{path
}.'/objects';
160 mkdir $self->{path
}.'/objects/info'; chmod 0775, $self->{path
}.'/objects/info';
162 # We set up both alternates and http_alternates since we cannot use
163 # relative path in alternates - that doesn't work recursively.
165 my $filename = $self->{path
}.'/objects/info/alternates';
166 open X
, '>'.$filename or die "alternates failed: $!";
167 print X
"$forkee_path/objects\n";
169 chmod 0664, $filename or warn "cannot chmod $filename: $!";
171 if ($Girocco::Config
::httppullurl
) {
172 $filename = $self->{path
}.'/objects/info/http-alternates';
173 open X
, '>'.$filename or die "http-alternates failed: $!";
174 my $upfork = $forkee_name;
175 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
177 chmod 0664, $filename or warn "cannot chmod $filename: $!";
180 # The symlink is problematic since git remote prune will traverse it.
181 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
186 mkdir $self->{path
}.'/ctags'; chmod 01777, $self->{path
}.'/ctags';
192 $xtra .= join(',', @
{$self->{users
}});
193 filedb_atomic_append
(jailed_file
('/etc/group'),
194 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
199 my $xtra = join(',', @
{$self->{users
}});
200 filedb_atomic_edit
(jailed_file
('/etc/group'),
204 if ($self->{name
} eq (split /:/)[0]) {
205 # preserve readonly flag
206 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
207 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
217 filedb_atomic_edit
(jailed_file
('/etc/group'),
219 $self->{name
} ne (split /:/)[0] and return $_;
227 $self->{path
}.'/hooks/'.$name;
233 open SRC
, "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
234 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
235 while (<SRC
>) { print DST
$_; }
238 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
243 foreach my $hook ('post-receive', 'update', 'post-update') {
244 $self->_hook_install($hook);
248 # private constructor, do not use
251 my ($name, $base_path, $path) = @_;
252 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
253 $path ||= "$base_path/$name.git";
254 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
259 # public constructor #0
260 # creates a virtual project not connected to disk image
261 # you can conjure() it later to disk
264 my ($name, $mirror) = @_;
265 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
267 $self->{mirror
} = $mirror;
271 # public constructor #1
276 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
280 next unless (shift eq $name);
282 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
283 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
286 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
288 $self->{users
} = [split /,/, $ulist];
289 $self->{orig_users
} = [@
{$self->{users
}}];
290 $self->{mirror
} = ! -e
$self->_nofetch_path;
291 $self->{clone_in_progress
} = -e
$self->_clonep_path;
292 $self->{clone_logged
} = -e
$self->_clonelog_path;
293 $self->{clone_failed
} = -e
$self->_clonefail_path;
294 $self->{ccrypt
} = $self->{crypt};
296 $self->_properties_load;
303 # $proj may not be in sane state if this returns false!
307 my $cgi = $gcgi->cgi;
309 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
310 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
311 if ($pwd or not $self->{crypt}) {
312 $self->{crypt} = scrypt
($pwd);
315 if ($pwd2 and $pwd ne $pwd2) {
316 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
319 $self->{cpwd
} = $cgi->param('cpwd');
321 if ($Girocco::Config
::project_owners
eq 'email') {
322 $self->{email
} = $gcgi->wparam('email');
323 valid_email
($self->{email
})
324 or $gcgi->err("Your email sure looks weird...?");
327 $self->{url
} = $gcgi->wparam('url');
329 valid_repo_url
($self->{url
})
330 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
333 $self->{desc
} = $gcgi->wparam('desc');
334 length($self->{desc
}) <= 1024
335 or $gcgi->err("<b>Short</b> description length > 1kb!");
337 $self->{README
} = $gcgi->wparam('README');
338 length($self->{README
}) <= 8192
339 or $gcgi->err("README length > 8kb!");
341 $self->{hp
} = $gcgi->wparam('hp');
343 valid_web_url
($self->{hp
})
344 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
347 # FIXME: Permit only existing users
348 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) } $cgi->param('user')];
350 $self->{notifymail
} = $gcgi->wparam('notifymail');
351 if ($self->{notifymail
}) {
352 (valid_email_multi
($self->{notifymail
}) and length($self->{notifymail
}) <= 512)
353 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
356 $self->{notifyjson
} = $gcgi->wparam('notifyjson');
357 if ($self->{notifyjson
}) {
358 valid_web_url
($self->{notifyjson
})
359 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
362 $self->{notifycia
} = $gcgi->wparam('notifycia');
363 if ($self->{notifycia
}) {
364 $self->{notifycia
} =~ /^[a-zA-Z0-9._-]+$/
365 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
368 not $gcgi->err_check;
374 name
=> $self->{name
},
375 email
=> $self->{email
},
377 desc
=> html_esc
($self->{desc
}),
378 README
=> html_esc
($self->{README
}),
380 users
=> $self->{users
},
381 notifymail
=> html_esc
($self->{notifymail
}),
382 notifyjson
=> html_esc
($self->{notifyjson
}),
383 notifycia
=> html_esc
($self->{notifycia
}),
391 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
392 $self->{cpwd
} ||= '';
393 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
394 $gcgi->err("Your admin password does not match!");
404 $self->_mkdir_forkees;
406 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
407 if ($Girocco::Config
::owning_group
) {
408 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
409 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
410 chmod(02775, $self->{path
}) or die "chmod 2775 $self->{path} failed: $!";
412 chmod(02777, $self->{path
}) or die "chmod 2777 $self->{path} failed: $!";
414 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
415 or die "git init $self->{path} failed: $?";
416 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
417 or die "disabling receive.denyNonFastforwards failed: $?";
418 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
419 or die "disabling gc.auto failed: $?";
421 # /info must have right permissions even before the fixup job,
422 # and git init didn't do it for some reason.
423 if ($Girocco::Config
::owning_group
) {
424 chmod(02775, $self->{path
}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
426 chmod(02777, $self->{path
}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
429 $self->_properties_save;
430 $self->_alternates_setup;
432 $self->_group_add($pushers);
433 $self->_hooks_install;
434 $self->perm_initialize;
435 system($Girocco::Config
::basedir
. '/gitweb/genindex.sh');
455 unlink ($self->_clonefail_path()); # Ignore EEXIST error
456 unlink ($self->_clonelog_path()); # Ignore EEXIST error
459 my $sock = IO
::Socket
::UNIX
->new($Girocco::Config
::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
460 $sock->print("clone ".$self->{name
}."\n");
461 # Just ignore reply, we are going to succeed anyway and the I/O
462 # would apparently get quite hairy.
471 $self->_properties_save;
472 $self->_group_update;
474 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
475 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
476 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
477 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
482 sub update_password
{
486 $self->{crypt} = scrypt
($pwd);
487 $self->_group_update;
490 # You can explicitly do this just on a ghost() repository too.
494 if (-d
$self->{path
}) {
495 system('rm', '-rf', $self->{path
}) == 0
496 or die "rm -rf $self->{path} failed: $?";
498 $self->_group_remove;
504 return glob($Girocco::Config
::reporoot
.'/'.$self->{name
}.'/*');
510 sub get_forkee_name
{
512 (m
#^(.*)/.*?$#)[0]; #
514 sub get_forkee_path
{
515 my $forkee = $Girocco::Config
::reporoot
.'/'.get_forkee_name
($_[0]).'.git';
516 -d
$forkee ?
$forkee : '';
521 (not m
#/# or -d get_forkee_path($_)) # will also catch ^/
524 and m
#^[a-zA-Z0-9+./_-]+$#;
529 valid_name
($name) or die "tried to query for project with invalid name $name!";
530 (-d
$Girocco::Config
::reporoot
."/$name.git");