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