chroot into slice dir to limit security exposure
[cloudstore.git] / gearman / send_file.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Data::Dump qw(dump);
6 use File::Path qw(make_path remove_tree);
7
8 sub home_dir {
9         my $login = shift;
10         my ( undef, undef, $uid, $gid, undef, undef, $email, $dir, $shell ) = getpwnam $login;
11         return $dir;
12 }
13
14 use Gearman::Worker;
15 my $worker = Gearman::Worker->new;
16 $worker->job_servers('127.0.0.1:4730');
17
18 open(my $log, '>>', '/rsync1/s1/log/send_file.log');
19 select($log); $|++;
20
21 $worker->register_function( send_file => sub {
22         my ($job) = @_;
23
24         my $work = $job->arg;
25         chomp $work;
26         warn "# send_file [$work]\n";
27
28         my ( $from, $to ) = split(/#/,$work,2);
29
30         my $f_dir = home_dir($1) if $from =~ s/~(\w+)//;
31         my $t_dir = home_dir($1) if $to   =~ s/~(\w+)//;
32
33         warn "send_file $f_dir $from -> $t_dir $to\n";
34
35         my $t_basedir = $t_dir . $to;
36         $t_basedir =~ s{/[^/]+$}{};
37         make_path $t_basedir unless -d $t_basedir;
38         link $f_dir . $from => $t_dir . $to;
39
40         print "send#$work#", -s $t_dir.$to, "#$!\n";
41
42         die "ERROR: $!" if $!;
43
44         return "send_file $f_dir $from -> $t_dir $to";
45 });
46
47
48 $worker->register_function( delete => sub {
49         my ($job) = @_;
50         my $work = $job->arg;
51         chomp $work;
52         warn "# delete [$work]\n";
53
54         my $dir = home_dir($1) if $work =~ s/~(\w+)//;
55         my $login = $1;
56         my $full = $dir . $work;
57
58         if ( -d $full ) {
59                 print "delete_tree#$work\n";
60                 warn "remove_tree $full\n";
61                 remove_tree $full;
62         } else {
63                 print "delete_file#$work#", -s $full, "\n";
64                 warn "unlink $full\n";
65                 unlink $full;
66         }
67         if ( $! ) {
68                 warn "ERROR: $!";
69                 return "$full: $!\n";
70         } else {
71                 return "$full: OK\n";
72         }
73
74 });
75
76 warn "$0 pid $$ waitng for jobs\n";
77
78 if ( $ENV{SLICE} ) {
79         chroot $ENV{SLICE} || die "can't chroot $ENV{SLICE}: $!";
80 } else {
81         warn "WARNING: not running under chroot SLICE=/hostname/sx\n";
82 }
83
84 $worker->work while 1;
85