Imported Upstream version 3.3.2
[debian/amanda] / common-src / amcryptsimple.pl
index cf5306bf2b3698fc7784246c7005892bc9d049ae..dd8760f91d0e0461dbce95e305c0224706201f03 100755 (executable)
@@ -1,6 +1,6 @@
 #!@PERL@ -w
 #
-# Copyright (c) 2007,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
@@ -53,9 +53,31 @@ $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 --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);
@@ -69,7 +91,9 @@ sub encrypt() {
 }
 
 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);