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 - POST JSON at', 'notifyjson', 'text'],
21 notifycia
=> ['Commit notify - CIA project 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);
83 } elsif ($pname =~ s/^%//) {
84 system('git', '--git-dir='.$self->{path
}, 'config', $pname, $value);
87 my $P = lock_file
($self->_property_path($pname));
88 $value ne '' and print $P "$value\n";
90 unlock_file
($self->_property_path($pname));
93 sub _properties_load
{
95 foreach my $prop (keys %propmap) {
96 $self->{$prop} = $self->_property_fget($prop);
100 sub _properties_save
{
102 foreach my $prop (keys %propmap) {
103 $self->_property_fput($prop, $self->{$prop});
109 $self->_property_path('.nofetch');
115 my $np = $self->_nofetch_path;
117 open X
, '>'.$np or die "nofetch failed: $!";
120 unlink $np or die "yesfetch failed: $!";
126 $self->_property_path('.clonelog');
129 sub _clonefail_path
{
131 $self->_property_path('.clone_failed');
136 $self->_property_path('.clone_in_progress');
142 my $np = $self->_clonep_path;
144 open X
, '>'.$np or die "clonep failed: $!";
147 unlink $np or die "clonef failed: $!";
150 sub _alternates_setup
{
152 return unless $self->{name
} =~ m
#/#;
153 my $forkee_name = get_forkee_name
($self->{name
});
154 my $forkee_path = get_forkee_path
($self->{name
});
155 return unless -d
$forkee_path;
156 mkdir $self->{path
}.'/refs'; chmod 0775, $self->{path
}.'/refs';
157 mkdir $self->{path
}.'/objects'; chmod 0775, $self->{path
}.'/objects';
158 mkdir $self->{path
}.'/objects/info'; chmod 0775, $self->{path
}.'/objects/info';
160 # We set up both alternates and http_alternates since we cannot use
161 # relative path in alternates - that doesn't work recursively.
163 my $filename = $self->{path
}.'/objects/info/alternates';
164 open X
, '>'.$filename or die "alternates failed: $!";
165 print X
"$forkee_path/objects\n";
167 chmod 0664, $filename or warn "cannot chmod $filename: $!";
169 if ($Girocco::Config
::httppullurl
) {
170 $filename = $self->{path
}.'/objects/info/http-alternates';
171 open X
, '>'.$filename or die "http-alternates failed: $!";
172 my $upfork = $forkee_name;
173 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
175 chmod 0664, $filename or warn "cannot chmod $filename: $!";
178 # The symlink is problematic since git remote prune will traverse it.
179 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
184 mkdir $self->{path
}.'/ctags'; chmod 01777, $self->{path
}.'/ctags';
190 $xtra .= join(',', @
{$self->{users
}});
191 filedb_atomic_append
(jailed_file
('/etc/group'),
192 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
197 my $xtra = join(',', @
{$self->{users
}});
198 filedb_atomic_edit
(jailed_file
('/etc/group'),
202 if ($self->{name
} eq (split /:/)[0]) {
203 # preserve readonly flag
204 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
205 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
215 filedb_atomic_edit
(jailed_file
('/etc/group'),
217 $self->{name
} ne (split /:/)[0] and return $_;
225 $self->{path
}.'/hooks/'.$name;
231 open SRC
, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
232 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
233 while (<SRC
>) { print DST
$_; }
236 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
241 foreach my $hook ('post-receive', 'update') {
242 $self->_hook_install($hook);
246 # private constructor, do not use
249 my ($name, $base_path, $path) = @_;
250 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
251 $path ||= "$base_path/$name.git";
252 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
257 # public constructor #0
258 # creates a virtual project not connected to disk image
259 # you can conjure() it later to disk
262 my ($name, $mirror) = @_;
263 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
265 $self->{mirror
} = $mirror;
269 # public constructor #1
274 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
278 next unless (shift eq $name);
280 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
281 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
284 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
286 $self->{users
} = [split /,/, $ulist];
287 $self->{orig_users
} = [@
{$self->{users
}}];
288 $self->{mirror
} = ! -e
$self->_nofetch_path;
289 $self->{clone_in_progress
} = -e
$self->_clonep_path;
290 $self->{clone_logged
} = -e
$self->_clonelog_path;
291 $self->{clone_failed
} = -e
$self->_clonefail_path;
292 $self->{ccrypt
} = $self->{crypt};
294 $self->_properties_load;
301 # $proj may not be in sane state if this returns false!
305 my $cgi = $gcgi->cgi;
307 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
308 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
309 if ($pwd or not $self->{crypt}) {
310 $self->{crypt} = scrypt
($pwd);
313 if ($pwd2 and $pwd ne $pwd2) {
314 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
317 $self->{cpwd
} = $cgi->param('cpwd');
319 if ($Girocco::Config
::project_owners
eq 'email') {
320 $self->{email
} = $gcgi->wparam('email');
321 valid_email
($self->{email
})
322 or $gcgi->err("Your email sure looks weird...?");
325 $self->{url
} = $gcgi->wparam('url');
327 valid_repo_url
($self->{url
})
328 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
331 $self->{desc
} = $gcgi->wparam('desc');
332 length($self->{desc
}) <= 1024
333 or $gcgi->err("<b>Short</b> description length > 1kb!");
335 $self->{README
} = $gcgi->wparam('README');
336 length($self->{README
}) <= 8192
337 or $gcgi->err("README length > 8kb!");
339 $self->{hp
} = $gcgi->wparam('hp');
341 valid_web_url
($self->{hp
})
342 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
345 # FIXME: Permit only existing users
346 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) } $cgi->param('user')];
348 not $gcgi->err_check;
354 name
=> $self->{name
},
355 email
=> $self->{email
},
357 desc
=> html_esc
($self->{desc
}),
358 README
=> html_esc
($self->{README
}),
360 users
=> $self->{users
},
361 notifymail
=> html_esc
($self->{notifymail
}),
362 notifyjson
=> html_esc
($self->{notifyjson
}),
363 notifycia
=> html_esc
($self->{notifycia
}),
371 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
372 $self->{cpwd
} ||= '';
373 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
374 $gcgi->err("Your admin password does not match!");
384 $self->_mkdir_forkees;
386 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
387 if ($Girocco::Config
::owning_group
) {
388 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
389 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
390 chmod(02775, $self->{path
}) or die "chmod 2775 $self->{path} failed: $!";
392 chmod(02777, $self->{path
}) or die "chmod 2777 $self->{path} failed: $!";
394 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
395 or die "git init $self->{path} failed: $?";
396 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
397 or die "disabling receive.denyNonFastforwards failed: $?";
398 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
399 or die "disabling gc.auto failed: $?";
401 # /info must have right permissions even before the fixup job,
402 # and git init didn't do it for some reason.
403 if ($Girocco::Config
::owning_group
) {
404 chmod(02775, $self->{path
}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
406 chmod(02777, $self->{path
}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
409 $self->_properties_save;
410 $self->_alternates_setup;
412 $self->_group_add($pushers);
413 $self->_hooks_install;
414 $self->perm_initialize;
415 system($Girocco::Config
::basedir
. '/gitweb/genindex.sh');
435 unlink ($self->_clonefail_path()); # Ignore EEXIST error
436 unlink ($self->_clonelog_path()); # Ignore EEXIST error
439 my $sock = IO
::Socket
::UNIX
->new($Girocco::Config
::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
440 $sock->print("clone ".$self->{name
}."\n");
441 # Just ignore reply, we are going to succeed anyway and the I/O
442 # would apparently get quite hairy.
451 $self->_properties_save;
452 $self->_group_update;
454 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
455 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
456 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
457 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
462 sub update_password
{
466 $self->{crypt} = scrypt
($pwd);
467 $self->_group_update;
470 # You can explicitly do this just on a ghost() repository too.
474 if (-d
$self->{path
}) {
475 system('rm', '-rf', $self->{path
}) == 0
476 or die "rm -rf $self->{path} failed: $?";
478 $self->_group_remove;
484 return glob($Girocco::Config
::reporoot
.'/'.$self->{name
}.'/*');
490 sub get_forkee_name
{
492 (m
#^(.*)/.*?$#)[0]; #
494 sub get_forkee_path
{
495 my $forkee = $Girocco::Config
::reporoot
.'/'.get_forkee_name
($_[0]).'.git';
496 -d
$forkee ?
$forkee : '';
501 (not m
#/# or -d get_forkee_path($_)) # will also catch ^/
504 and m
#^[a-zA-Z0-9+./_-]+$#;
509 valid_name
($name) or die "tried to query for project with invalid name $name!";
510 (-d
$Girocco::Config
::reporoot
."/$name.git");