11 our @ISA = qw(Exporter);
12 our @EXPORT = qw(scrypt html_esc jailed_file
14 filedb_atomic_append filedb_atomic_edit
15 proj_get_forkee_name proj_get_forkee_path
16 valid_proj_name valid_user_name valid_email valid_repo_url valid_web_url);
18 use CGI
qw(:standard :escapeHTML -nosticky);
19 use CGI
::Util
qw(unescape);
20 use CGI
::Carp
qw(fatalsToBrowser);
31 $repo->{cgi
} = CGI
->new;
33 print $repo->{cgi
}->header(-type
=>'text/html', -charset
=> 'utf-8');
36 <?xml version="1.0" encoding="utf-8"?>
37 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
38 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
41 <title>repo.or.cz :: $heading</title>
42 <link rel="stylesheet" type="text/css" href="/gitweb.css"/>
43 <link rel="shortcut icon" href="/git-favicon.png" type="image/png"/>
48 <div class="page_header">
49 <a href="http://git.or.cz/" title="Git homepage"><img src="/git-logo.png" width="72" height="27" alt="git" style="float:right; border-width:0px;"/></a>
50 <a href="/">repo.or.cz</a> / administration / $heading
61 my $cginame = $cgi->url(-absolute
=> 1);
63 if ($cginame =~ /^[a-zA-Z0-9_.\/-]+\
.cgi
$/) {
66 <a href="http://repo.or.cz/w/repo.git?a=blob;f=cgi/$cginame">(view source)</a>
83 print "<p style=\"text-color: red\">@_</p>\n";
89 my $err = $self->{err
};
90 $err and print "<p>Operation aborted due to $err errors.</p>\n";
97 my $val = $self->{cgi
}->param($param);
98 $val =~ s/^\s*(.*?)\s*$/$1/;
103 ### Random utility functions
107 crypt($pwd, join ('', ('.', '/', 2..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
113 $str =~ s/</</g; $str =~ s/>/>/g;
114 $str =~ s/"/"/g;
120 "/home/repo/j/$filename";
128 use Errno
qw(EEXIST);
129 use Fcntl
qw(O_WRONLY O_CREAT O_EXCL);
131 my $handle = new IO
::Handle
;
133 unless (sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
135 while (not sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
136 ($! == EEXIST
) or die "$path open failed: $!";
137 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
141 # XXX: filedb-specific
142 chmod 0664, $path or die "$path g+w failed: $!";
150 rename "$path.lock", $path or die "$path unlock failed: $!";
153 sub filedb_atomic_append
{
154 my ($file, $line) = @_;
157 open my $src, $file or die "$file open for reading failed: $!";
158 my $dst = lock_file
($file);
161 my $aid = (split /:/)[2];
162 $id = $aid + 1 if ($aid >= $id);
164 print $dst $_ or die "$file(l) write failed: $!";
167 $line =~ s/\\i/$id/g;
168 print $dst "$line\n" or die "$file(l) write failed: $!";
170 close $dst or die "$file(l) close failed: $!";
178 sub filedb_atomic_edit
{
179 my ($file, $fn) = @_;
181 open my $src, $file or die "$file open for reading failed: $!";
182 my $dst = lock_file
($file);
185 print $dst $fn->($_) or die "$file(l) write failed: $!";
188 close $dst or die "$file(l) close failed: $!";
194 sub proj_get_forkee_name
{
198 sub proj_get_forkee_path
{
199 my $forkee = '/srv/git/'.proj_get_forkee_name
($_[0]).'.git';
200 -d
$forkee ?
$forkee : '';
202 sub valid_proj_name
{
204 (not m
#/# or -d proj_get_forkee_path($_)) # will also catch ^/
206 and m
#^[a-zA-Z0-9+./_-]+$#;
208 sub valid_user_name
{
210 /^[a-zA-Z0-9+._-]+$/;
214 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/;
218 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?(#[a-zA-Z0-9._-]+)?$/;
222 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/ or
223 /^git:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/;
229 package Git
::RepoCGI
::Project
;
231 BEGIN { use Git
::RepoCGI
; }
235 my @pelems = split('/', $self->{name
});
236 pop @pelems; # do not create dir for the project itself
237 my $path = "/srv/git";
238 foreach my $pelem (@pelems) {
239 (-d
"$path/$pelem" ) or mkdir "$path/$pelem" or die "mkdir $path/$pelem: $!";
247 desc
=> 'description',
248 README
=> 'README.html',
255 $self->{path
}.'/'.$name;
262 $propmap{$name} or die "unknown property: $name";
263 open P
, $self->_property_path($propmap{$name}) or return undef;
271 my ($name, $value) = @_;
272 $propmap{$name} or die "unknown property: $name";
274 my $P = lock_file
($self->_property_path($propmap{$name}));
275 $value ne '' and print $P "$value\n";
277 unlock_file
($self->_property_path($propmap{$name}));
280 sub _properties_load
{
282 foreach my $prop (keys %propmap) {
283 $self->{$prop} = $self->_property_fget($prop);
287 sub _properties_save
{
289 foreach my $prop (keys %propmap) {
290 $self->_property_fput($prop, $self->{$prop});
296 $self->_property_path('.nofetch');
302 my $np = $self->_nofetch_path;
304 open X
, '>'.$np or die "nofetch failed: $!";
307 unlink $np or die "yesfetch failed: $!";
311 sub _alternates_setup
{
313 return unless $self->{name
} =~ m
#/#;
314 my $forkee_name = proj_get_forkee_name
($self->{name
});
315 my $forkee_path = proj_get_forkee_path
($self->{name
});
316 return unless -d
$forkee_path;
317 open X
, '>'.$self->{path
}.'/objects/info/alternates' or die "alternates failed: $!";
318 # We use relative URL here so that http-fetch groks it automagically.
319 $forkee_name =~ s
#^.*/##; # Only the last element of the path matters, again.
320 print X
"../../$forkee_name.git/objects\n";
327 $xtra .= join(',', @
{$self->{users
}});
328 filedb_atomic_append
(jailed_file
('/etc/group'),
329 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
334 my $xtra = join(',', @
{$self->{users
}});
335 filedb_atomic_edit
(jailed_file
('/etc/group'),
339 if ($self->{name
} eq (split /:/)[0]) {
340 # preserve readonly flag
341 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
342 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
352 filedb_atomic_edit
(jailed_file
('/etc/group'),
354 $self->{name
} ne (split /:/)[0] and return $_;
362 $self->{path
}.'/hooks/'.$name;
368 open SRC
, "/home/repo/repomgr/$name-hook" or die "cannot open hook $name: $!";
369 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
370 while (<SRC
>) { print DST
$_; }
373 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
378 foreach my $hook ('update') {
379 $self->_hook_install($hook);
383 # private constructor, do not use
386 my ($name, $path) = @_;
387 valid_proj_name
($name) or die "refusing to create project with invalid name ($name)!";
388 my $proj = { name
=> $name, path
=> $path };
393 # public constructor #0
394 # creates a virtual project not connected to disk image
395 # you can conjure() it later to disk
398 my ($name, $mirror) = @_;
399 my $self = $class->_new($name, $mirror ?
"/home/repo/repodata/to-clone/$name" : "/srv/git/$name.git");
401 $self->{mirror
} = $mirror;
405 # public constructor #1
410 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
414 next unless (shift eq $name);
416 my $self = $class->_new($name, "/srv/git/$name.git");
417 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
420 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
422 $self->{users
} = [split /,/, $ulist];
423 $self->{mirror
} = ! -e
$self->_nofetch_path;
425 $self->_properties_load;
432 # $proj may not be in sane state if this returns false!
436 my $cgi = $repo->cgi;
438 my $pwd = $cgi->param('pwd');
439 if ($pwd ne '' or not $self->{crypt}) {
440 $self->{crypt} = scrypt
($pwd);
443 $self->{email
} = $repo->wparam('email');
444 valid_email
($self->{email
})
445 or $repo->err("Your email sure looks weird...?");
447 $self->{url
} = $repo->wparam('url');
449 valid_repo_url
($self->{url
})
450 or $repo->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
453 $self->{desc
} = $repo->wparam('desc');
454 length($self->{desc
}) <= 1024
455 or $repo->err("<b>Short</b> description length > 1kb!");
457 $self->{README
} = $repo->wparam('README');
458 length($self->{README
}) <= 8192
459 or $repo->err("README length > 8kb!");
461 $self->{hp
} = $repo->wparam('hp');
463 valid_web_url
($self->{hp
})
464 or $repo->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
467 # FIXME: Permit only existing users
468 $self->{users
} = [grep { valid_user_name
($_) } $cgi->param('user')];
470 not $repo->err_check;
476 name
=> $self->{name
},
477 email
=> $self->{email
},
479 desc
=> html_esc
($self->{desc
}),
480 README
=> html_esc
($self->{README
}),
482 users
=> $self->{users
},
489 $self->_mkdir_forkees;
490 mkdir $self->{path
} or die "mkdir failed: $!";
491 chmod 0775, $self->{path
} or die "chmod failed: $!";
492 $self->_properties_save;
493 $self->_alternates_setup;
494 $self->_group_add(':');
500 $self->_mkdir_forkees;
501 system('cg-admin-setuprepo', '-g', 'repo', $self->{path
}) == 0
502 or die "cg-admin-setuprepo failed: $?";
504 $self->_properties_save;
505 $self->_alternates_setup;
507 $self->_hooks_install;
513 $self->_properties_save;
514 $self->_group_update;
517 # You can explicitly do this just on a ghost() repository too.
521 if (-d
$self->{path
}) {
522 system('rm', '-r', $self->{path
}) == 0
523 or die "rm -r failed: $?";
525 $self->_group_remove;
531 valid_proj_name
($name) or die "tried to query for project with invalid name $name!";
533 or -d
"/home/repo/repodata/cloning/$name"
534 or -d
"/home/repo/repodata/to-clone/$name");
538 valid_proj_name
($name) or die "tried to query for project with invalid name $name!";
539 (-d
"/srv/git/$name.git");
545 package Git
::RepoCGI
::User
;
547 BEGIN { use Git
::RepoCGI
; }
551 filedb_atomic_append
(jailed_file
('/etc/passwd'),
552 join(':', $self->{name
}, 'x', '\i', 65534, $self->{email
}, '/', '/bin/git-shell'));
557 '/etc/sshkeys/'.$self->{name
};
562 open F
, ">".jailed_file
($self->_sshkey_path) or die "sshkey failed: $!";
563 print F
$self->{keys}."\n";
565 chmod 0664, jailed_file
($self->_sshkey_path);
568 # private constructor, do not use
572 valid_user_name
($name) or die "refusing to create user with invalid name ($name)!";
573 my $proj = { name
=> $name };
578 # public constructor #0
579 # creates a virtual user not connected to disk record
580 # you can conjure() it later to disk
584 my $self = $class->_new($name);
588 # $user may not be in sane state if this returns false!
592 my $cgi = $repo->cgi;
594 $self->{name
} = $repo->wparam('name');
595 valid_user_name
($self->{name
})
596 or $repo->err("Name contains invalid characters.");
598 $self->{email
} = $repo->wparam('email');
599 valid_email
($self->{email
})
600 or $repo->err("Your email sure looks weird...?");
602 $self->{keys} = $cgi->param('keys');
603 length($self->{keys}) <= 4096
604 or $repo->err("The list of keys is more than 4kb. Do you really need that much?");
606 not $repo->err_check;
619 valid_user_name
($name) or die "tried to query for user with invalid name $name!";
620 (-e jailed_file
("/etc/sshkeys/$name"));