Imported Upstream version 3.3.3
[debian/amanda] / common-src / amcryptsimple.pl
1 #!@PERL@ -w
2 #
3 # Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
20 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
21 #
22
23
24
25 # Run perl.
26 eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
27          & eval 'exec /usr/bin/perl -S $0 $argv:q'
28                 if 0;
29
30 use Time::Local;
31
32 my $AMANDA='@CLIENT_LOGIN@';
33
34 my $ddebug = 1;   # set to 1 to print signal debug to stderr
35
36 my $sigint_seen   = 0;
37 my $sigpipe_seen  = 0;
38 my $sighup_seen   = 0;
39 my $sigill_seen   = 0;
40 my $sigterm_seen  = 0;
41 my $sigsegv_seen  = 0;
42 my $sigquit_seen  = 0;
43 my $sigfpe_seen   = 0;
44
45 $AMANDA_HOME = (getpwnam($AMANDA) )[7] || die "Cannot find $AMANDA home directory\n" ;
46 $AM_PASS = "$AMANDA_HOME/.am_passphrase";
47
48 unless ( -e $AM_PASS ) {
49   die "secret key $AM_PASS not found\n";
50 }
51
52
53 $ENV{'PATH'} = '/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/opt/csw/bin';
54
55 $ENV{'GNUPGHOME'} = "$AMANDA_HOME/.gnupg";
56
57 sub do_gpg_agent() {
58     my $path=`which gpg-agent 2>/dev/null`;
59     chomp $path;
60     if (-x $path) {
61         return "gpg-agent --daemon --";
62     }
63     return ""
64 }
65
66 sub which_gpg() {
67     my $path=`which gpg2 2>/dev/null`;
68     if (!$path) {
69         $path=`which gpg 2>/dev/null`;
70     }
71     if (!$path) {
72         die("no gpg or gpg2");
73     }
74     chomp $path;
75     return $path;
76 }
77
78 sub encrypt() {
79     my $gpg_agent_cmd = do_gpg_agent();
80     my $gpg = which_gpg();
81     system "$gpg_agent_cmd $gpg --batch -z 0 --no-secmem-warning --disable-mdc --symmetric --cipher-algo AES256 --passphrase-fd 3  3<$AM_PASS";
82     if ($? == -1) {
83         print STDERR "failed to execute gpg: $!\n";
84         exit (1);
85     } elsif ($? & 127) {
86         printf STDERR "gpg died with signal %d\n", ($? & 127);
87         exit ($?);
88     } elsif ($? >> 8) {
89         printf STDERR "gpg exited with value %d\n", ($? >> 8);
90         exit ($? >> 8);
91     }
92 }
93
94 sub decrypt() {
95     my $gpg_agent_cmd = do_gpg_agent();
96     my $gpg = which_gpg();
97     system "$gpg_agent_cmd $gpg --batch --quiet --no-mdc-warning --decrypt --passphrase-fd 3  3<$AM_PASS";
98     if ($? == -1) {
99         print STDERR "failed to execute gpg: $!\n";
100         exit (1);
101     } elsif ($? & 127) {
102         printf STDERR "gpg died with signal %d\n", ($? & 127);
103         exit ($?);
104     } elsif ($? >> 8) {
105         printf STDERR "gpg exited with value %d\n", ($? >> 8);
106         exit ($? >> 8);
107     }
108 }
109
110 sub int_catcher {
111     $sigint_seen = 1;
112 }
113
114 sub pipe_catcher {
115     $sigpipe_seen = 1;
116 }
117
118 sub hup_catcher {
119     $sighup_seen = 1;
120 }
121
122 sub ill_catcher {
123     $sigill_seen = 1;
124 }
125
126 sub term_catcher {
127     $sigterm_seen = 1;
128 }
129
130 sub segv_catcher {
131     $sigsegv_seen = 1;
132 }
133
134 sub quit_catcher {
135     $sigquit_seen = 1;
136 }
137
138 sub fpe_catcher {
139     $sigfpe_seen = 1;
140 }
141
142 #main
143
144 $SIG{'INT'}   = 'int_catcher';
145 $SIG{'PIPE'}  = 'pipe_catcher';
146 $SIG{'HUP'}   = 'hup_catcher';
147 $SIG{'ILL'}   = 'ill_catcher';
148 $SIG{'TERM'}  = 'term_catcher';
149 $SIG{'SEGV'}  = 'segv_catcher';
150 $SIG{'QUIT'}  = 'quit_catcher';
151 $SIG{'FPE'}   = 'FPE_catcher';
152
153
154 if ( $#ARGV > 0 ) {
155      die "Usage: $0 [-d]\n";
156 }
157
158 if ( $#ARGV==0 && $ARGV[0] eq "-d" ) {
159     decrypt();
160 }
161 else {
162     encrypt();
163 }
164
165 if ( $ddebug  ) {
166     if ( $sigint_seen )  { print STDERR "strange sigint seen = $sigint_seen\n"; }
167     if ( $sigpipe_seen ) { print STDERR "strange sigpipe seen = $sigpipe_seen\n"; }
168     if ( $sighup_seen )  { print STDERR "strange sighup seen = $sighup_seen\n"; }
169     if ( $sigill_seen )  { print STDERR "strange sigill seen = $sigill_seen\n"; }
170     
171     if ( $sigterm_seen ) { print STDERR "strange sigterm seen = $sigterm_seen\n"; }
172     if ( $sigsegv_seen ) { print STDERR "strange sigsegv seen = $sigsegv_seen\n"; }
173     if ( $sigquit_seen ) { print STDERR "strange sigquit seen = $sigquit_seen\n"; }
174     if ( $sigfpe_seen )  { print STDERR "strange sigfpe seen = $sigfpe_seen\n"; }
175     
176 }
177
178 $SIG{'INT'}  = 'DEFAULT';
179 $SIG{'PIPE'} = 'DEFAULT';
180 $SIG{'HUP'}  = 'DEFAULT';
181 $SIG{'ILL'}  = 'DEFAULT';
182 $SIG{'TERM'} = 'DEFAULT';
183 $SIG{'SEGV'} = 'DEFAULT';
184 $SIG{'QUIT'} = 'DEFAULT';
185 $SIG{'FPE'}  = 'DEFAULT';
186