1 package Girocco
::Project
;
10 use Girocco
::ProjPerm
;
12 use base
('Girocco::ProjPerm::'.$Girocco::Config
::permission_control
); # mwahaha
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) {
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)
30 desc
=> 'description',
31 README
=> 'README.html',
38 $self->{path
}.'/'.$name;
44 $propmap{$name} or die "unknown property: $name";
45 open P
, $self->_property_path($propmap{$name}) or return undef;
48 my $value = join('', @value); chomp $value;
54 my ($name, $value) = @_;
55 $propmap{$name} or die "unknown property: $name";
58 my $P = lock_file
($self->_property_path($propmap{$name}));
59 $value ne '' and print $P "$value\n";
61 unlock_file
($self->_property_path($propmap{$name}));
64 sub _properties_load
{
66 foreach my $prop (keys %propmap) {
67 $self->{$prop} = $self->_property_fget($prop);
71 sub _properties_save
{
73 foreach my $prop (keys %propmap) {
74 $self->_property_fput($prop, $self->{$prop});
80 $self->_property_path('.nofetch');
86 my $np = $self->_nofetch_path;
88 open X
, '>'.$np or die "nofetch failed: $!";
91 unlink $np or die "yesfetch failed: $!";
95 sub _alternates_setup
{
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";
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); #
120 chmod 0664, $filename or warn "cannot chmod $filename: $!";
123 symlink "$forkee_path/refs", $self->{path
}.'/refs/forkee';
128 mkdir $self->{path
}.'/ctags'; chmod 0775, $self->{path
}.'/ctags';
134 $xtra .= join(',', @
{$self->{users
}});
135 filedb_atomic_append
(jailed_file
('/etc/group'),
136 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
141 my $xtra = join(',', @
{$self->{users
}});
142 filedb_atomic_edit
(jailed_file
('/etc/group'),
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";
159 filedb_atomic_edit
(jailed_file
('/etc/group'),
161 $self->{name
} ne (split /:/)[0] and return $_;
169 $self->{path
}.'/hooks/'.$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
$_; }
180 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
185 foreach my $hook ('update') {
186 $self->_hook_install($hook);
190 # private constructor, do not use
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 };
201 # public constructor #0
202 # creates a virtual project not connected to disk image
203 # you can conjure() it later to disk
206 my ($name, $mirror) = @_;
207 my $self = $class->_new($name, $mirror ?
"$Girocco::Config::mqueuedir/to-clone" : $Girocco::Config
::reporoot
,
208 $mirror ?
"$Girocco::Config::mqueuedir/to-clone/$name" : $Girocco::Config
::reporoot
."/$name.git");
210 $self->{mirror
} = $mirror;
214 # public constructor #1
219 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
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
};
229 ($self->{crypt}, $self->{gid
}, $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;
243 # $proj may not be in sane state if this returns false!
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');
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 > 1kb!");
274 $self->{README
} = $gcgi->wparam('README');
275 length($self->{README
}) <= 8192
276 or $gcgi->err("README length > 8kb!");
278 $self->{hp
} = $gcgi->wparam('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;
293 name
=> $self->{name
},
294 email
=> $self->{email
},
296 desc
=> html_esc
($self->{desc
}),
297 README
=> html_esc
($self->{README
}),
299 users
=> $self->{users
},
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!");
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;
325 $self->_group_add(':');
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: $?";
343 $self->_properties_save;
344 $self->_alternates_setup;
347 $self->_hooks_install;
348 $self->perm_initialize;
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
{
367 $self->{crypt} = scrypt
($pwd);
368 $self->_group_update;
371 # You can explicitly do this just on a ghost() repository too.
375 if (-d
$self->{path
}) {
376 system('rm', '-r', $self->{path
}) == 0
377 or die "rm -r $self->{path} failed: $?";
379 $self->_group_remove;
385 return glob($Girocco::Config
::reporoot
.'/'.$self->{name
}.'/*');
391 sub get_forkee_name
{
393 (m
#^(.*)/.*?$#)[0]; #
395 sub get_forkee_path
{
396 my $forkee = $Girocco::Config
::reporoot
.'/'.get_forkee_name
($_[0]).'.git';
397 -d
$forkee ?
$forkee : '';
402 (not m
#/# or -d get_forkee_path($_)) # will also catch ^/
405 and m
#^[a-zA-Z0-9+./_-]+$#;
410 valid_name
($name) or die "tried to query for project with invalid name $name!";
412 or ($Girocco::Config
::mqueuedir
413 and (-d
$Girocco::Config
::mqueuedir
."/cloning/$name"
414 or -d
$Girocco::Config
::mqueuedir
."/to-clone/$name")));
418 valid_name
($name) or die "tried to query for project with invalid name $name!";
419 (-d
$Girocco::Config
::reporoot
."/$name.git");