Imported Upstream version 3.3.2
[debian/amanda] / common-src / amgpgcrypt.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 # Amanda has problem with gpg mdc(modification detection code) in the binary mode.
23 # This program encrypt with mdc disabled.
24 # If mdc is required, use --armor option. 
25
26
27
28 # Run perl.
29 eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
30          & eval 'exec /usr/bin/perl -S $0 $argv:q'
31                 if 0;
32
33 use Time::Local;
34
35 my $AMANDA='@CLIENT_LOGIN@';
36 my $saw_sigint = 0;
37
38 $AMANDA_HOME = (getpwnam($AMANDA) )[7] || die "Cannot find $AMANDA home directory\n" ;
39
40 #The following two ($AM_PASS, $AM_PRIV) are needed only for restore/recover
41 #They should be protected and stored away during other time.
42 $AM_PASS = "$AMANDA_HOME/.am_passphrase";
43 $AM_PRIV = "$AMANDA_HOME/.gnupg/secring.gpg";
44
45 $ENV{'PATH'} = '/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/opt/csw/bin';
46
47 $ENV{'GNUPGHOME'} = "$AMANDA_HOME/.gnupg";
48
49 sub do_gpg_agent() {
50     my $path=`which gpg-agent 2>/dev/null`;
51     chomp $path;
52     if (-x $path) {
53         return "gpg-agent --daemon --";
54     }
55     return ""
56 }
57
58 sub which_gpg() {
59     my $path=`which gpg2 2>/dev/null`;
60     if (!$path) {
61         $path=`which gpg 2>/dev/null`;
62     }
63     if (!$path) {
64         die("no gpg or gpg2");
65     }
66     chomp $path;
67     return $path;
68 }
69
70 sub encrypt() {
71     my $gpg_agent_cmd = do_gpg_agent();
72     my $gpg = which_gpg();
73     system "$gpg_agent_cmd $gpg  --batch --disable-mdc --encrypt --cipher-algo AES256 --recipient $AMANDA";
74 }
75
76 sub decrypt() {
77     my $gpg_agent_cmd = do_gpg_agent();
78     my $gpg = which_gpg();
79     system "$gpg_agent_cmd $gpg --batch --quiet --no-mdc-warning --secret-keyring $AM_PRIV --decrypt --passphrase-fd 3  3<$AM_PASS";
80 }
81
82 sub my_sig_catcher {
83         $saw_sigint = 1;
84 }
85
86 #main
87
88
89
90 $SIG{'INT'} = 'my_sig_catcher';
91
92
93 if ( $#ARGV > 0 ) {
94      die "Usage: $0 [-d]\n";
95 }
96
97 if ( $#ARGV==0 && $ARGV[0] eq "-d" ) {
98     decrypt();
99 }
100 else {
101     encrypt();
102 }
103
104 $SIG{'INT'} = 'DEFAULT';