lintian doesn't like orphan packages with uploaders...
[debian/amanda] / common-src / amcryptsimple.pl
index ef5b7d6052d699b415754819c608c92619b52fbf..f21db2bbe6b1cbe7e9bdaeabefccd45062384caa 100755 (executable)
@@ -1,10 +1,11 @@
 #!@PERL@ -w
 #
-# Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
+# Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
 #
-# This program is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License version 2 as published
-# by the Free Software Foundation.
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful, but
 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
@@ -15,7 +16,7 @@
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 #
-# Contact information: Zmanda Inc, 465 S Mathlida Ave, Suite 300
+# Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
 #
 
@@ -53,13 +54,57 @@ $ENV{'PATH'} = '/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/opt/csw/bin';
 
 $ENV{'GNUPGHOME'} = "$AMANDA_HOME/.gnupg";
 
+sub do_gpg_agent() {
+    my $path=`which gpg-agent 2>/dev/null`;
+    chomp $path;
+    if (-x $path) {
+       return "gpg-agent --daemon --";
+    }
+    return ""
+}
+
+sub which_gpg() {
+    my $path=`which gpg2 2>/dev/null`;
+    if (!$path) {
+        $path=`which gpg 2>/dev/null`;
+    }
+    if (!$path) {
+        die("no gpg or gpg2");
+    }
+    chomp $path;
+    return $path;
+}
 
 sub encrypt() {
-    system "gpg --batch --no-secmem-warning --disable-mdc --symmetric --cipher-algo AES256 --passphrase-fd 3  3<$AM_PASS";
+    my $gpg_agent_cmd = do_gpg_agent();
+    my $gpg = which_gpg();
+    system "$gpg_agent_cmd $gpg --batch -z 0 --no-secmem-warning --disable-mdc --symmetric --cipher-algo AES256 --passphrase-fd 3  3<$AM_PASS";
+    if ($? == -1) {
+       print STDERR "failed to execute gpg: $!\n";
+       exit (1);
+    } elsif ($? & 127) {
+       printf STDERR "gpg died with signal %d\n", ($? & 127);
+       exit ($?);
+    } elsif ($? >> 8) {
+       printf STDERR "gpg exited with value %d\n", ($? >> 8);
+       exit ($? >> 8);
+    }
 }
 
 sub decrypt() {
-     system "gpg --batch --quiet --no-mdc-warning --decrypt --passphrase-fd 3  3<$AM_PASS";
+    my $gpg_agent_cmd = do_gpg_agent();
+    my $gpg = which_gpg();
+    system "$gpg_agent_cmd $gpg --batch --quiet --no-mdc-warning --decrypt --passphrase-fd 3  3<$AM_PASS";
+    if ($? == -1) {
+       print STDERR "failed to execute gpg: $!\n";
+       exit (1);
+    } elsif ($? & 127) {
+       printf STDERR "gpg died with signal %d\n", ($? & 127);
+       exit ($?);
+    } elsif ($? >> 8) {
+       printf STDERR "gpg exited with value %d\n", ($? >> 8);
+       exit ($? >> 8);
+    }
 }
 
 sub int_catcher {