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 ^/
207 and m
#^[a-zA-Z0-9+./_-]+$#;
209 sub valid_user_name
{
211 /^[a-zA-Z0-9+._-]+$/;
215 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/;
219 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?(#[a-zA-Z0-9._-]+)?$/;
223 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/ or
224 /^git:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/;
230 package Git
::RepoCGI
::Project
;
232 BEGIN { use Git
::RepoCGI
; }
236 my @pelems = split('/', $self->{name
});
237 pop @pelems; # do not create dir for the project itself
238 my $path = $self->{base_path
};
239 foreach my $pelem (@pelems) {
241 (-d
"$path") or mkdir $path or die "mkdir $path: $!";
242 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
249 desc
=> 'description',
250 README
=> 'README.html',
257 $self->{path
}.'/'.$name;
264 $propmap{$name} or die "unknown property: $name";
265 open P
, $self->_property_path($propmap{$name}) or return undef;
273 my ($name, $value) = @_;
274 $propmap{$name} or die "unknown property: $name";
276 my $P = lock_file
($self->_property_path($propmap{$name}));
277 $value ne '' and print $P "$value\n";
279 unlock_file
($self->_property_path($propmap{$name}));
282 sub _properties_load
{
284 foreach my $prop (keys %propmap) {
285 $self->{$prop} = $self->_property_fget($prop);
289 sub _properties_save
{
291 foreach my $prop (keys %propmap) {
292 $self->_property_fput($prop, $self->{$prop});
298 $self->_property_path('.nofetch');
304 my $np = $self->_nofetch_path;
306 open X
, '>'.$np or die "nofetch failed: $!";
309 unlink $np or die "yesfetch failed: $!";
313 sub _alternates_setup
{
315 return unless $self->{name
} =~ m
#/#;
316 my $forkee_name = proj_get_forkee_name
($self->{name
});
317 my $forkee_path = proj_get_forkee_path
($self->{name
});
318 return unless -d
$forkee_path;
319 mkdir $self->{path
}.'/refs'; chmod 0775, $self->{path
}.'/refs';
320 mkdir $self->{path
}.'/objects'; chmod 0775, $self->{path
}.'/objects';
321 mkdir $self->{path
}.'/objects/info'; chmod 0775, $self->{path
}.'/objects/info';
322 my $filename = $self->{path
}.'/objects/info/alternates';
323 open X
, '>'.$filename or die "alternates failed: $!";
324 # We use relative URL here so that http-fetch groks it automagically.
325 $forkee_name =~ s
#^.*/##; # Only the last element of the path matters, again.
326 print X
"../../../$forkee_name.git/objects\n";
328 chmod 0664, $filename or warn "cannot chmod $filename: $!";
329 symlink "../../../$forkee_name.git/refs", $self->{path
}.'/refs/forkee';
335 $xtra .= join(',', @
{$self->{users
}});
336 filedb_atomic_append
(jailed_file
('/etc/group'),
337 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
342 my $xtra = join(',', @
{$self->{users
}});
343 filedb_atomic_edit
(jailed_file
('/etc/group'),
347 if ($self->{name
} eq (split /:/)[0]) {
348 # preserve readonly flag
349 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
350 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
360 filedb_atomic_edit
(jailed_file
('/etc/group'),
362 $self->{name
} ne (split /:/)[0] and return $_;
370 $self->{path
}.'/hooks/'.$name;
376 open SRC
, "/home/repo/repomgr/$name-hook" or die "cannot open hook $name: $!";
377 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
378 while (<SRC
>) { print DST
$_; }
381 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
386 foreach my $hook ('update') {
387 $self->_hook_install($hook);
391 # private constructor, do not use
394 my ($name, $base_path, $path) = @_;
395 valid_proj_name
($name) or die "refusing to create project with invalid name ($name)!";
396 $path ||= "$base_path/$name.git";
397 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
402 # public constructor #0
403 # creates a virtual project not connected to disk image
404 # you can conjure() it later to disk
407 my ($name, $mirror) = @_;
408 my $self = $class->_new($name, $mirror ?
"/home/repo/repodata/to-clone" : "/srv/git",
409 $mirror ?
"/home/repo/repodata/to-clone/$name" : "/srv/git/$name.git");
411 $self->{mirror
} = $mirror;
415 # public constructor #1
420 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
424 next unless (shift eq $name);
426 my $self = $class->_new($name, "/srv/git");
427 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
430 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
432 $self->{users
} = [split /,/, $ulist];
433 $self->{mirror
} = ! -e
$self->_nofetch_path;
435 $self->_properties_load;
442 # $proj may not be in sane state if this returns false!
446 my $cgi = $repo->cgi;
448 my $pwd = $cgi->param('pwd');
449 if ($pwd ne '' or not $self->{crypt}) {
450 $self->{crypt} = scrypt
($pwd);
453 $self->{email
} = $repo->wparam('email');
454 valid_email
($self->{email
})
455 or $repo->err("Your email sure looks weird...?");
457 $self->{url
} = $repo->wparam('url');
459 valid_repo_url
($self->{url
})
460 or $repo->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
463 $self->{desc
} = $repo->wparam('desc');
464 length($self->{desc
}) <= 1024
465 or $repo->err("<b>Short</b> description length > 1kb!");
467 $self->{README
} = $repo->wparam('README');
468 length($self->{README
}) <= 8192
469 or $repo->err("README length > 8kb!");
471 $self->{hp
} = $repo->wparam('hp');
473 valid_web_url
($self->{hp
})
474 or $repo->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
477 # FIXME: Permit only existing users
478 $self->{users
} = [grep { valid_user_name
($_) } $cgi->param('user')];
480 not $repo->err_check;
486 name
=> $self->{name
},
487 email
=> $self->{email
},
489 desc
=> html_esc
($self->{desc
}),
490 README
=> html_esc
($self->{README
}),
492 users
=> $self->{users
},
499 $self->_mkdir_forkees;
500 mkdir $self->{path
} or die "mkdir failed: $!";
501 chmod 0775, $self->{path
} or die "chmod failed: $!";
502 $self->_properties_save;
503 $self->_alternates_setup;
504 $self->_group_add(':');
510 $self->_mkdir_forkees;
511 system('cg-admin-setuprepo', '-g', 'repo', $self->{path
}) == 0
512 or die "cg-admin-setuprepo failed: $?";
514 $self->_properties_save;
515 $self->_alternates_setup;
517 $self->_hooks_install;
523 $self->_properties_save;
524 $self->_group_update;
527 # You can explicitly do this just on a ghost() repository too.
531 if (-d
$self->{path
}) {
532 system('rm', '-r', $self->{path
}) == 0
533 or die "rm -r failed: $?";
535 $self->_group_remove;
541 valid_proj_name
($name) or die "tried to query for project with invalid name $name!";
543 or -d
"/home/repo/repodata/cloning/$name"
544 or -d
"/home/repo/repodata/to-clone/$name");
548 valid_proj_name
($name) or die "tried to query for project with invalid name $name!";
549 (-d
"/srv/git/$name.git");
555 package Git
::RepoCGI
::User
;
557 BEGIN { use Git
::RepoCGI
; }
561 filedb_atomic_append
(jailed_file
('/etc/passwd'),
562 join(':', $self->{name
}, 'x', '\i', 65534, $self->{email
}, '/', '/bin/git-shell'));
567 '/etc/sshkeys/'.$self->{name
};
572 open F
, ">".jailed_file
($self->_sshkey_path) or die "sshkey failed: $!";
573 print F
$self->{keys}."\n";
575 chmod 0664, jailed_file
($self->_sshkey_path);
578 # private constructor, do not use
582 valid_user_name
($name) or die "refusing to create user with invalid name ($name)!";
583 my $proj = { name
=> $name };
588 # public constructor #0
589 # creates a virtual user not connected to disk record
590 # you can conjure() it later to disk
594 my $self = $class->_new($name);
598 # $user may not be in sane state if this returns false!
602 my $cgi = $repo->cgi;
604 $self->{name
} = $repo->wparam('name');
605 valid_user_name
($self->{name
})
606 or $repo->err("Name contains invalid characters.");
608 $self->{email
} = $repo->wparam('email');
609 valid_email
($self->{email
})
610 or $repo->err("Your email sure looks weird...?");
612 $self->{keys} = $cgi->param('keys');
613 length($self->{keys}) <= 4096
614 or $repo->err("The list of keys is more than 4kb. Do you really need that much?");
616 not $repo->err_check;
629 valid_user_name
($name) or die "tried to query for user with invalid name $name!";
630 (-e jailed_file
("/etc/sshkeys/$name"));