11 our @ISA = qw(Exporter);
12 our @EXPORT = qw(scrypt html_esc jailed_file
13 filedb_atomic_append filedb_atomic_edit
14 valid_name valid_email valid_repo_url valid_web_url);
16 use CGI
qw(:standard :escapeHTML -nosticky);
17 use CGI
::Util
qw(unescape);
18 use CGI
::Carp
qw(fatalsToBrowser);
29 $repo->{cgi
} = CGI
->new;
31 print $repo->{cgi
}->header(-type
=>'text/html', -charset
=> 'utf-8');
34 <?xml version="1.0" encoding="utf-8"?>
35 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
36 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
39 <title>repo.or.cz :: $heading</title>
40 <link rel="stylesheet" type="text/css" href="/gitweb.css"/>
41 <link rel="shortcut icon" href="/git-favicon.png" type="image/png"/>
46 <div class="page_header">
47 <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>
48 <a href="/">repo.or.cz</a>
50 Administration Interface
63 my $cginame = $cgi->url(-absolute
=> 1);
65 if ($cginame =~ /^[a-zA-Z0-9_.\/-]+\
.cgi
$/) {
68 <a href="http://repo.or.cz/gitweb.cgi/repo.git?a=blob;f=cgi/$cginame">(view source)</a>
85 print "<p style=\"text-color: red\">@_</p>\n";
91 my $err = $self->{err
};
92 $err and print "<p>Operation aborted due to $err errors.</p>\n";
99 my $val = $self->{cgi
}->param($param);
100 $val =~ s/^\s*(.*?)\s*$/$1/;
105 ### Random utility functions
109 crypt($pwd, join ('', ('.', '/', 2..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
115 $str =~ s/</</g; $str =~ s/>/>/g;
116 $str =~ s/"/"/g;
122 "/home/repo/j/$filename";
130 use Errno
qw(EEXIST);
131 use Fcntl
qw(O_WRONLY O_CREAT O_EXCL);
133 my $handle = new IO
::Handle
;
135 unless (sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
137 while (not sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
138 ($! == EEXIST
) or die "$path open failed: $!";
139 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
143 # XXX: filedb-specific
144 chmod 0664, $path or die "$path g+w failed: $!";
152 rename "$path.lock", $path or die "$path unlock failed: $!";
155 sub filedb_atomic_append
{
156 my ($file, $line) = @_;
159 open my $src, $file or die "$file open for reading failed: $!";
160 my $dst = lock_file
($file);
163 my $aid = (split /:/)[2];
164 $id = $aid + 1 if ($aid >= $id);
166 print $dst $_ or die "$file(l) write failed: $!";
169 $line =~ s/\\i/$id/g;
170 print $dst "$line\n" or die "$file(l) write failed: $!";
172 close $dst or die "$file(l) close failed: $!";
180 sub filedb_atomic_edit
{
181 my ($file, $fn) = @_;
183 open my $src, $file or die "$file open for reading failed: $!";
184 my $dst = lock_file
($file);
187 print $dst $fn->($_) or die "$file(l) write failed: $!";
190 close $dst or die "$file(l) close failed: $!";
196 # BOTH user AND project name!
203 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/;
207 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?(#[a-zA-Z0-9._-]+)?$/;
211 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/ or
212 /^git:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/;
218 package Git
::RepoCGI
::Project
;
220 BEGIN { use Git
::RepoCGI
; }
225 desc
=> 'description',
233 $self->{path
}.'/'.$name;
240 $propmap{$name} or die "unknown property: $name";
241 open P
, $self->_property_path($propmap{$name}) or return undef;
249 my ($name, $value) = @_;
250 $propmap{$name} or die "unknown property: $name";
251 open P
, '>'.$self->_property_path($propmap{$name}) or die "$name put failed: $!";
252 $value ne '' and print P
"$value\n";
254 chmod 0664, $self->_property_path($propmap{$name}) or die "$name chmod failed: $!";
257 sub _properties_load
{
259 foreach my $prop (keys %propmap) {
260 $self->{$prop} = $self->_property_fget($prop);
264 sub _properties_save
{
266 foreach my $prop (keys %propmap) {
267 $self->_property_fput($prop, $self->{$prop});
273 $self->_property_path('.nofetch');
279 my $np = $self->_nofetch_path;
281 open X
, '>'.$np or die "nofetch failed: $!";
284 unlink $np or die "yesfetch failed: $!";
291 $xtra .= join(',', @
{$self->{users
}});
292 filedb_atomic_append
(jailed_file
('/etc/group'),
293 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
298 my $xtra = join(',', @
{$self->{users
}});
299 filedb_atomic_edit
(jailed_file
('/etc/group'),
303 if ($self->{name
} eq (split /:/)[0]) {
304 # preserve readonly flag
305 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
306 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
316 filedb_atomic_edit
(jailed_file
('/etc/group'),
318 $self->{name
} ne (split /:/)[0]) and return $_;
326 $self->{path
}.'/hooks/'.$name;
332 open SRC
, "/home/repo/repomgr/$name-hook" or die "cannot open hook $name: $!";
333 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
334 while (<SRC
>) { print DST
$_; }
337 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
342 foreach my $hook ('update') {
343 $self->_hook_install($hook);
347 # private constructor, do not use
350 my ($name, $path) = @_;
351 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
352 my $proj = { name
=> $name, path
=> $path };
357 # public constructor #0
358 # creates a virtual project not connected to disk image
359 # you can conjure() it later to disk
362 my ($name, $mirror) = @_;
363 my $self = $class->_new($name, $mirror ?
"/home/repo/repodata/to-clone/$name" : "/srv/git/$name.git");
365 $self->{mirror
} = $mirror;
369 # public constructor #1
374 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
378 next unless (shift eq $name);
380 my $self = $class->_new($name, "/srv/git/$name.git");
381 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
384 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
386 $self->{users
} = [split /,/, $ulist];
387 $self->{mirror
} = ! -e
$self->_nofetch_path;
389 $self->_properties_load;
396 # $proj may not be in sane state if this returns false!
400 my $cgi = $repo->cgi;
402 my $pwd = $cgi->param('pwd');
403 if ($pwd ne '' or not $self->{crypt}) {
404 $self->{crypt} = scrypt
($pwd);
407 $self->{email
} = $repo->wparam('email');
408 valid_email
($self->{email
})
409 or $repo->err("Your email sure looks weird...?");
411 $self->{url
} = $repo->wparam('url');
413 valid_repo_url
($self->{url
})
414 or $repo->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
417 $self->{desc
} = $repo->wparam('desc');
418 length($self->{desc
}) <= 1024
419 or $repo->err("<b>Short</b> description length > 1kb!");
421 $self->{README
} = $repo->wparam('README');
422 length($self->{README
}) <= 8192
423 or $repo->err("README length > 8kb!");
425 $self->{hp
} = $repo->wparam('hp');
427 valid_web_url
($self->{hp
})
428 or $repo->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
431 # FIXME: Permit only existing users
432 $self->{users
} = [grep { valid_name
($_) } $cgi->param('user')];
434 not $repo->err_check;
440 name
=> $self->{name
},
441 email
=> $self->{email
},
443 desc
=> html_esc
($self->{desc
}),
444 README
=> html_esc
($self->{README
}),
446 users
=> $self->{users
},
453 mkdir $self->{path
} or die "mkdir failed: $!";
454 chmod 0775, $self->{path
} or die "chmod failed: $!";
455 $self->_properties_save;
456 $self->_group_add(':');
462 system('cg-admin-setuprepo', '-g', 'repo', $self->{path
}) == 0
463 or die "cg-admin-setuprepo failed: $?";
465 $self->_properties_save;
467 $self->_hooks_install;
473 $self->_properties_save;
474 $self->_group_update;
477 # You can explicitly do this just on a ghost() repository too.
481 if (-d
$self->{path
}) {
482 system('rm', '-r', $self->{path
}) == 0
483 or die "rm -r failed: $?";
485 $self->_group_remove;
491 valid_name
($name) or die "tried to query for project with invalid name $name!";
493 or -d
"/home/repo/repodata/cloning/$name"
494 or -d
"/home/repo/repodata/to-clone/$name");
498 valid_name
($name) or die "tried to query for project with invalid name $name!";
499 (-d
"/srv/git/$name.git");
505 package Git
::RepoCGI
::User
;
507 BEGIN { use Git
::RepoCGI
; }
511 filedb_atomic_append
(jailed_file
('/etc/passwd'),
512 join(':', $self->{name
}, 'x', '\i', 65536, $self->{email
}, '/', '/bin/git-shell'));
517 '/etc/sshkeys/'.$self->{name
};
522 open F
, ">".jailed_file
($self->_sshkey_path) or die "sshkey failed: $!";
523 print F
$self->{keys}."\n";
525 chmod 0664, jailed_file
($self->_sshkey_path);
528 # private constructor, do not use
532 valid_name
($name) or die "refusing to create user with invalid name ($name)!";
533 my $proj = { name
=> $name };
538 # public constructor #0
539 # creates a virtual user not connected to disk record
540 # you can conjure() it later to disk
544 my $self = $class->_new($name);
548 # $user may not be in sane state if this returns false!
552 my $cgi = $repo->cgi;
554 $self->{name
} = $repo->wparam('name');
555 valid_name
($self->{name
})
556 or $repo->err("Name contains invalid characters.");
558 $self->{email
} = $repo->wparam('email');
559 valid_email
($self->{email
})
560 or $repo->err("Your email sure looks weird...?");
562 $self->{keys} = $cgi->param('keys');
563 length($self->{keys}) <= 4096
564 or $repo->err("The list of keys is more than 4kb. Do you really need that much?");
566 not $repo->err_check;
579 valid_name
($name) or die "tried to query for user with invalid name $name!";
580 (-e jailed_file
("/etc/sshkeys/$name"));