fix bug that dispaly blog im...
|
1 |
#!/usr/bin/env perl |
2 | ||
3 |
use strict; |
|
4 |
use warnings; |
|
5 |
use utf8; |
|
6 |
use FindBin; |
|
7 |
use lib "$FindBin::Bin/../lib"; |
|
8 |
use lib "$FindBin::Bin/../extlib/lib/perl5"; |
|
9 |
use Gitprep; |
|
10 | ||
Complete gitprep-shell
|
11 |
my $debug = 0; |
12 | ||
write gitprep-shell
|
13 |
# Project name pattern |
14 |
my $project_re = qr/[a-zA-Z0-9_\-\.]+$/; |
|
fix bug that dispaly blog im...
|
15 | |
write gitprep-shell
|
16 |
# User |
add collaboration feature to...
|
17 |
my $session_user = shift; |
18 |
die "User not specifed" unless defined $session_user; |
|
fix bug that dispaly blog im...
|
19 | |
write gitprep-shell
|
20 |
# Application |
fix bug that dispaly blog im...
|
21 |
my $app = Gitprep->new; |
write gitprep-shell
|
22 | |
23 |
# Git |
|
24 |
my $git = $app->git; |
|
25 | ||
26 |
# DBI |
|
fix bug that dispaly blog im...
|
27 |
my $dbi = $app->dbi; |
28 | ||
write gitprep-shell
|
29 |
# SSH connection |
30 |
my $ssh_connection = $ENV{SSH_CONNECTION}; |
|
Complete gitprep-shell
|
31 |
warn "ssh_connection: $ssh_connection" if $debug; |
add collaboration feature to...
|
32 |
die "who the *heck* are you?\n" unless defined $ssh_connection; |
write gitprep-shell
|
33 | |
34 |
# SSH original command |
|
35 |
my $ssh_original_command = $ENV{SSH_ORIGINAL_COMMAND} || ''; |
|
fix ssh key authentication b...
|
36 |
warn "ssh_original_command: $ssh_original_command" if $debug; |
write gitprep-shell
|
37 | |
38 |
# IP address |
|
39 |
my $ip = $ssh_connection || '(no-IP)'; |
|
fix ssh key authentication b...
|
40 |
warn "ip: $ip" if $debug; |
write gitprep-shell
|
41 |
$ip =~ s/ .*//; |
42 | ||
43 |
# Check new line of SSH original command |
|
44 |
my $ssh_original_command_tmp = $ssh_original_command; |
|
45 |
$ssh_original_command_tmp =~ s/[\n\r]+/<<newline>>/g; |
|
46 |
die "I don't like newlines in the command: $ssh_original_command\n" |
|
47 |
if $ssh_original_command ne $ssh_original_command_tmp; |
|
48 | ||
add collaboration feature to...
|
49 |
# Project |
50 |
my ($verb, $user, $project) = parse_ssh_original_command($ssh_original_command); |
|
write gitprep-shell
|
51 |
sanity($project); |
52 | ||
add collaboration feature to...
|
53 |
# Can access |
54 |
my $can_access; |
|
55 |
if ($session_user eq $user) { |
|
56 |
$can_access = 1; |
|
57 |
} |
|
58 |
else { |
|
59 |
my $row = $app->dbi->model('collaboration')->select( |
|
60 |
id => [$user, $project, $session_user] |
|
61 |
)->one; |
|
62 |
|
|
63 |
$can_access = $row ? 1 : 0; |
|
64 |
} |
|
65 |
die qq|User "$session_user" can't access repository "$user/$project.git"\n| |
|
66 |
unless $can_access; |
|
67 | ||
68 |
# Command |
|
write gitprep-shell
|
69 |
my $rep_home = $git->rep_home; |
Complete gitprep-shell
|
70 |
my $repository = "'$rep_home/$user/$project.git'"; |
write gitprep-shell
|
71 |
my @git_shell_cmd = ("git", "shell", "-c", "$verb $repository"); |
Complete gitprep-shell
|
72 |
warn "@git_shell_cmd" if $debug; |
73 |
unless ($debug) { |
|
74 |
system(@git_shell_cmd) == 0 |
|
add collaboration feature to...
|
75 |
or die "Can't execute command: @git_shell_cmd\n" ; |
Complete gitprep-shell
|
76 |
} |
write gitprep-shell
|
77 | |
78 |
sub parse_ssh_original_command { |
|
79 |
my $ssh_original_command = shift; |
|
80 | ||
Complete gitprep-shell
|
81 |
$ssh_original_command ||= ''; |
write gitprep-shell
|
82 | |
83 |
my $git_commands = "git-upload-pack|git-receive-pack|git-upload-archive"; |
|
add collaboration feature to...
|
84 |
if ($ssh_original_command =~ m(^($git_commands) '.*/([a-zA-Z1-9_]+)/([^\/]+?)\.git'$)) { |
fix gitprep-shell
|
85 |
my ($verb, $user, $project) = ($1, $2, $3); |
fix ssh key authentication b...
|
86 |
warn "User:$user, Project:$project" if $debug; |
add collaboration feature to...
|
87 |
die "invalid repo name: '$project'\n" if $project !~ $project_re; |
fix gitprep-shell
|
88 |
return ($verb, $user, $project); |
write gitprep-shell
|
89 |
} |
90 |
else { |
|
add collaboration feature to...
|
91 |
die "Invalid command: $ssh_original_command\n"; |
write gitprep-shell
|
92 |
} |
93 |
} |
|
94 | ||
95 |
sub sanity { |
|
96 |
my $project = shift; |
|
add collaboration feature to...
|
97 |
die "'$project' contains bad characters\n" if $project !~ $project_re; |
98 |
die "'$project' ends with a '/'\n" if $project =~ m(/$); |
|
99 |
die "'$project' contains '..'\n" if $project =~ m(\.\.); |
|
write gitprep-shell
|
100 |
} |
fix bug that dispaly blog im...
|
101 | |
102 |
=head1 NAME |
|
103 | ||
write gitprep-shell
|
104 |
gitprep-shell - AuthorizedKeysCommand for sshd |
fix bug that dispaly blog im...
|
105 | |
106 |
=head1 USAGE |
|
107 | ||
write gitprep-shell
|
108 |
./gitprep-shell kimoto |
fix bug that dispaly blog im...
|
109 | |
110 |
This command return user public_key |
|
111 |