11 our @ISA = qw(Exporter);
12 our @EXPORT = qw(scrypt html_esc jailed_file
14 filedb_atomic_append filedb_atomic_edit
15 valid_name valid_email valid_repo_url valid_web_url);
17 use CGI
qw(:standard :escapeHTML -nosticky);
18 use CGI
::Util
qw(unescape);
19 use CGI
::Carp
qw(fatalsToBrowser);
30 $repo->{cgi
} = CGI
->new;
32 print $repo->{cgi
}->header(-type
=>'text/html', -charset
=> 'utf-8');
35 <?xml version="1.0" encoding="utf-8"?>
36 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
37 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
40 <title>repo.or.cz :: $heading</title>
41 <link rel="stylesheet" type="text/css" href="/gitweb.css"/>
42 <link rel="shortcut icon" href="/git-favicon.png" type="image/png"/>
47 <div class="page_header">
48 <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>
49 <a href="/">repo.or.cz</a> / administration
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 # BOTH user AND project name!
201 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/;
205 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?(#[a-zA-Z0-9._-]+)?$/;
209 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/ or
210 /^git:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/;
216 package Git
::RepoCGI
::Project
;
218 BEGIN { use Git
::RepoCGI
; }
223 desc
=> 'description',
231 $self->{path
}.'/'.$name;
238 $propmap{$name} or die "unknown property: $name";
239 open P
, $self->_property_path($propmap{$name}) or return undef;
247 my ($name, $value) = @_;
248 $propmap{$name} or die "unknown property: $name";
250 my $P = lock_file
($propmap{$name});
251 $value ne '' and print $P "$value\n";
253 unlock_file
($propmap{$name});
256 sub _properties_load
{
258 foreach my $prop (keys %propmap) {
259 $self->{$prop} = $self->_property_fget($prop);
263 sub _properties_save
{
265 foreach my $prop (keys %propmap) {
266 $self->_property_fput($prop, $self->{$prop});
272 $self->_property_path('.nofetch');
278 my $np = $self->_nofetch_path;
280 open X
, '>'.$np or die "nofetch failed: $!";
283 unlink $np or die "yesfetch failed: $!";
290 $xtra .= join(',', @
{$self->{users
}});
291 filedb_atomic_append
(jailed_file
('/etc/group'),
292 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
297 my $xtra = join(',', @
{$self->{users
}});
298 filedb_atomic_edit
(jailed_file
('/etc/group'),
302 if ($self->{name
} eq (split /:/)[0]) {
303 # preserve readonly flag
304 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
305 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
315 filedb_atomic_edit
(jailed_file
('/etc/group'),
317 $self->{name
} ne (split /:/)[0] and return $_;
325 $self->{path
}.'/hooks/'.$name;
331 open SRC
, "/home/repo/repomgr/$name-hook" or die "cannot open hook $name: $!";
332 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
333 while (<SRC
>) { print DST
$_; }
336 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
341 foreach my $hook ('update') {
342 $self->_hook_install($hook);
346 # private constructor, do not use
349 my ($name, $path) = @_;
350 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
351 my $proj = { name
=> $name, path
=> $path };
356 # public constructor #0
357 # creates a virtual project not connected to disk image
358 # you can conjure() it later to disk
361 my ($name, $mirror) = @_;
362 my $self = $class->_new($name, $mirror ?
"/home/repo/repodata/to-clone/$name" : "/srv/git/$name.git");
364 $self->{mirror
} = $mirror;
368 # public constructor #1
373 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
377 next unless (shift eq $name);
379 my $self = $class->_new($name, "/srv/git/$name.git");
380 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
383 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
385 $self->{users
} = [split /,/, $ulist];
386 $self->{mirror
} = ! -e
$self->_nofetch_path;
388 $self->_properties_load;
395 # $proj may not be in sane state if this returns false!
399 my $cgi = $repo->cgi;
401 my $pwd = $cgi->param('pwd');
402 if ($pwd ne '' or not $self->{crypt}) {
403 $self->{crypt} = scrypt
($pwd);
406 $self->{email
} = $repo->wparam('email');
407 valid_email
($self->{email
})
408 or $repo->err("Your email sure looks weird...?");
410 $self->{url
} = $repo->wparam('url');
412 valid_repo_url
($self->{url
})
413 or $repo->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
416 $self->{desc
} = $repo->wparam('desc');
417 length($self->{desc
}) <= 1024
418 or $repo->err("<b>Short</b> description length > 1kb!");
420 $self->{README
} = $repo->wparam('README');
421 length($self->{README
}) <= 8192
422 or $repo->err("README length > 8kb!");
424 $self->{hp
} = $repo->wparam('hp');
426 valid_web_url
($self->{hp
})
427 or $repo->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
430 # FIXME: Permit only existing users
431 $self->{users
} = [grep { valid_name
($_) } $cgi->param('user')];
433 not $repo->err_check;
439 name
=> $self->{name
},
440 email
=> $self->{email
},
442 desc
=> html_esc
($self->{desc
}),
443 README
=> html_esc
($self->{README
}),
445 users
=> $self->{users
},
452 mkdir $self->{path
} or die "mkdir failed: $!";
453 chmod 0775, $self->{path
} or die "chmod failed: $!";
454 $self->_properties_save;
455 $self->_group_add(':');
461 system('cg-admin-setuprepo', '-g', 'repo', $self->{path
}) == 0
462 or die "cg-admin-setuprepo failed: $?";
464 $self->_properties_save;
466 $self->_hooks_install;
472 $self->_properties_save;
473 $self->_group_update;
476 # You can explicitly do this just on a ghost() repository too.
480 if (-d
$self->{path
}) {
481 system('rm', '-r', $self->{path
}) == 0
482 or die "rm -r failed: $?";
484 $self->_group_remove;
490 valid_name
($name) or die "tried to query for project with invalid name $name!";
492 or -d
"/home/repo/repodata/cloning/$name"
493 or -d
"/home/repo/repodata/to-clone/$name");
497 valid_name
($name) or die "tried to query for project with invalid name $name!";
498 (-d
"/srv/git/$name.git");
504 package Git
::RepoCGI
::User
;
506 BEGIN { use Git
::RepoCGI
; }
510 filedb_atomic_append
(jailed_file
('/etc/passwd'),
511 join(':', $self->{name
}, 'x', '\i', 65534, $self->{email
}, '/', '/bin/git-shell'));
516 '/etc/sshkeys/'.$self->{name
};
521 open F
, ">".jailed_file
($self->_sshkey_path) or die "sshkey failed: $!";
522 print F
$self->{keys}."\n";
524 chmod 0664, jailed_file
($self->_sshkey_path);
527 # private constructor, do not use
531 valid_name
($name) or die "refusing to create user with invalid name ($name)!";
532 my $proj = { name
=> $name };
537 # public constructor #0
538 # creates a virtual user not connected to disk record
539 # you can conjure() it later to disk
543 my $self = $class->_new($name);
547 # $user may not be in sane state if this returns false!
551 my $cgi = $repo->cgi;
553 $self->{name
} = $repo->wparam('name');
554 valid_name
($self->{name
})
555 or $repo->err("Name contains invalid characters.");
557 $self->{email
} = $repo->wparam('email');
558 valid_email
($self->{email
})
559 or $repo->err("Your email sure looks weird...?");
561 $self->{keys} = $cgi->param('keys');
562 length($self->{keys}) <= 4096
563 or $repo->err("The list of keys is more than 4kb. Do you really need that much?");
565 not $repo->err_check;
578 valid_name
($name) or die "tried to query for user with invalid name $name!";
579 (-e jailed_file
("/etc/sshkeys/$name"));