gitprep / script / gitprep-shell /
Newer Older
111 lines | 2.787kb
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
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
gitprep authored on 2014-05-16
11
my $debug = 0;
12

            
write gitprep-shell
gitprep authored on 2014-05-16
13
# Project name pattern
14
my $project_re = qr/[a-zA-Z0-9_\-\.]+$/;
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
15

            
write gitprep-shell
gitprep authored on 2014-05-16
16
# User
add collaboration feature to...
gitprep authored on 2014-05-20
17
my $session_user = shift;
18
die "User not specifed" unless defined $session_user;
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
19

            
write gitprep-shell
gitprep authored on 2014-05-16
20
# Application
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
21
my $app = Gitprep->new;
write gitprep-shell
gitprep authored on 2014-05-16
22

            
23
# Git
24
my $git = $app->git;
25

            
26
# DBI
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
27
my $dbi = $app->dbi;
28

            
write gitprep-shell
gitprep authored on 2014-05-16
29
# SSH connection
30
my $ssh_connection = $ENV{SSH_CONNECTION};
Complete gitprep-shell
gitprep authored on 2014-05-16
31
warn "ssh_connection: $ssh_connection" if $debug;
add collaboration feature to...
gitprep authored on 2014-05-20
32
die "who the *heck* are you?\n" unless defined $ssh_connection;
write gitprep-shell
gitprep authored on 2014-05-16
33

            
34
# SSH original command
35
my $ssh_original_command = $ENV{SSH_ORIGINAL_COMMAND} || '';
fix ssh key authentication b...
gitprep authored on 2014-05-20
36
warn "ssh_original_command: $ssh_original_command" if $debug;
write gitprep-shell
gitprep authored on 2014-05-16
37

            
38
# IP address
39
my $ip = $ssh_connection || '(no-IP)';
fix ssh key authentication b...
gitprep authored on 2014-05-20
40
warn "ip: $ip" if $debug;
write gitprep-shell
gitprep authored on 2014-05-16
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...
gitprep authored on 2014-05-20
49
# Project
50
my ($verb, $user, $project) = parse_ssh_original_command($ssh_original_command);
write gitprep-shell
gitprep authored on 2014-05-16
51
sanity($project);
52

            
add collaboration feature to...
gitprep authored on 2014-05-20
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
gitprep authored on 2014-05-16
69
my $rep_home = $git->rep_home;
Complete gitprep-shell
gitprep authored on 2014-05-16
70
my $repository = "'$rep_home/$user/$project.git'";
write gitprep-shell
gitprep authored on 2014-05-16
71
my @git_shell_cmd = ("git", "shell", "-c", "$verb $repository");
Complete gitprep-shell
gitprep authored on 2014-05-16
72
warn "@git_shell_cmd" if $debug;
73
unless ($debug) {
74
  system(@git_shell_cmd) == 0
add collaboration feature to...
gitprep authored on 2014-05-20
75
    or die "Can't execute command: @git_shell_cmd\n" ;
Complete gitprep-shell
gitprep authored on 2014-05-16
76
}
write gitprep-shell
gitprep authored on 2014-05-16
77

            
78
sub parse_ssh_original_command {
79
  my $ssh_original_command = shift;
80

            
Complete gitprep-shell
gitprep authored on 2014-05-16
81
  $ssh_original_command ||= '';
write gitprep-shell
gitprep authored on 2014-05-16
82

            
83
  my $git_commands = "git-upload-pack|git-receive-pack|git-upload-archive";
add collaboration feature to...
gitprep authored on 2014-05-20
84
  if ($ssh_original_command =~ m(^($git_commands) '.*/([a-zA-Z1-9_]+)/([^\/]+?)\.git'$)) {
fix gitprep-shell
gitprep authored on 2014-05-19
85
    my ($verb, $user, $project) = ($1, $2, $3);
fix ssh key authentication b...
gitprep authored on 2014-05-20
86
    warn "User:$user, Project:$project" if $debug;
add collaboration feature to...
gitprep authored on 2014-05-20
87
    die "invalid repo name: '$project'\n" if $project !~ $project_re;
fix gitprep-shell
gitprep authored on 2014-05-19
88
    return ($verb, $user, $project);
write gitprep-shell
gitprep authored on 2014-05-16
89
  }
90
  else {
add collaboration feature to...
gitprep authored on 2014-05-20
91
    die "Invalid command: $ssh_original_command\n";
write gitprep-shell
gitprep authored on 2014-05-16
92
  }
93
}
94

            
95
sub sanity {
96
  my $project = shift;
add collaboration feature to...
gitprep authored on 2014-05-20
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
gitprep authored on 2014-05-16
100
}
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
101

            
102
=head1 NAME
103

            
write gitprep-shell
gitprep authored on 2014-05-16
104
gitprep-shell - AuthorizedKeysCommand for sshd
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
105

            
106
=head1 USAGE
107

            
write gitprep-shell
gitprep authored on 2014-05-16
108
  ./gitprep-shell kimoto
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
109

            
110
This command return user public_key
111