gitprep / script / gitprep-shell-raw /
Newer Older
108 lines | 2.905kb
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
fix gitprep-shell
Yuki Kimoto authored on 2016-04-22
17
my $session_user_id = shift;
18
die "User not specifed" unless defined $session_user_id;
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
cleanup ->rep_home
Yuki Kimoto authored on 2016-04-14
21
my $app = Mojo::Server->new->load_app("$FindBin::Bin/gitprep");
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
fix gitprep-shell
Yuki Kimoto authored on 2016-04-22
50
my ($verb, $user_id, $project_id) = parse_ssh_original_command($ssh_original_command);
51
sanity($project_id);
write gitprep-shell
gitprep authored on 2014-05-16
52

            
add collaboration feature to...
gitprep authored on 2014-05-20
53
# Can access
54
my $can_access;
fix gitprep-shell
Yuki Kimoto authored on 2016-04-22
55
if ($session_user_id eq $user_id) {
add collaboration feature to...
gitprep authored on 2014-05-20
56
  $can_access = 1;
57
}
58
else {
change is_collaborator argum...
Yuki Kimoto authored on 2016-04-23
59
  $can_access = $app->gitprep_api->is_collaborator($session_user_id, $user_id, $project_id);
add collaboration feature to...
gitprep authored on 2014-05-20
60
}
fix gitprep-shell
Yuki Kimoto authored on 2016-04-22
61
die qq|User "$session_user_id" can't access repository "$user_id/$project_id.git"\n|
add collaboration feature to...
gitprep authored on 2014-05-20
62
  unless $can_access; 
63

            
64
# Command
fix gitprep-shell
Yuki Kimoto authored on 2016-04-22
65
my $rep_info = $app->rep_info($user_id, $project_id);
remove rep_path
Yuki Kimoto authored on 2016-04-16
66
my $rep_git_dir = $rep_info->{git_dir};
67
my $repository = "'$rep_git_dir'";
write gitprep-shell
gitprep authored on 2014-05-16
68
my @git_shell_cmd = ("git", "shell", "-c", "$verb $repository");
Complete gitprep-shell
gitprep authored on 2014-05-16
69
warn "@git_shell_cmd" if $debug;
70
unless ($debug) {
71
  system(@git_shell_cmd) == 0
add collaboration feature to...
gitprep authored on 2014-05-20
72
    or die "Can't execute command: @git_shell_cmd\n" ;
Complete gitprep-shell
gitprep authored on 2014-05-16
73
}
write gitprep-shell
gitprep authored on 2014-05-16
74

            
75
sub parse_ssh_original_command {
76
  my $ssh_original_command = shift;
77

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

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

            
92
sub sanity {
fix gitprep-shell
Yuki Kimoto authored on 2016-04-22
93
  my $project_id = shift;
94
  die "'$project_id' contains bad characters\n" if $project_id !~ $project_re;
95
  die "'$project_id' ends with a '/'\n"         if $project_id =~ m(/$);
96
  die "'$project_id' contains '..'\n"           if $project_id =~ m(\.\.);
write gitprep-shell
gitprep authored on 2014-05-16
97
}
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
98

            
99
=head1 NAME
100

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

            
103
=head1 USAGE
104

            
add gitprep-shell wrappter s...
Yuki Kimoto authored on 2016-05-04
105
  ./gitprep-shell-raw kimoto
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
106

            
107
This command return user public_key
108