[Rt-commit] r12819 - in rt/branches/3.8-TESTING/lib/RT: Crypt

sunnavy at bestpractical.com sunnavy at bestpractical.com
Mon Jun 2 18:24:49 EDT 2008


Author: sunnavy
Date: Mon Jun  2 18:24:48 2008
New Revision: 12819

Added:
   rt/branches/3.8-TESTING/lib/RT/Util.pm
Modified:
   rt/branches/3.8-TESTING/lib/RT/Crypt/GnuPG.pm

Log:
lift safe_run_child out to Util.pm

Modified: rt/branches/3.8-TESTING/lib/RT/Crypt/GnuPG.pm
==============================================================================
--- rt/branches/3.8-TESTING/lib/RT/Crypt/GnuPG.pm	(original)
+++ rt/branches/3.8-TESTING/lib/RT/Crypt/GnuPG.pm	Mon Jun  2 18:24:48 2008
@@ -54,6 +54,7 @@
 use IO::Handle;
 use GnuPG::Interface;
 use RT::EmailParser ();
+use RT::Util 'safe_run_child';
 
 =head1 NAME
 
@@ -351,7 +352,7 @@
 );
 
 # DEV WARNING: always pass all STD* handles to GnuPG interface even if we don't
-# need them, just pass 'new IO::Handle' and then close it after _safe_run_child.
+# need them, just pass 'new IO::Handle' and then close it after safe_run_child.
 # we don't want to leak anything into FCGI/Apache/MP handles, this break things.
 # So code should look like:
 #        my $handles = GnuPG::Handles->new(
@@ -361,29 +362,6 @@
 #            ...
 #        );
 
-sub _safe_run_child (&) {
-    local @ENV{'LANG', 'LC_ALL'} = ('C', 'C');
-
-    return shift->() if $ENV{'MOD_PERL'};
-
-    # We need to reopen stdout temporarily, because in FCGI
-    # environment, stdout is tied to FCGI::Stream, and the child
-    # of the run3 wouldn't be able to reopen STDOUT properly.
-    my $stdin = IO::Handle->new;
-    $stdin->fdopen( 0, 'r' );
-    local *STDIN = $stdin;
-
-    my $stdout = IO::Handle->new;
-    $stdout->fdopen( 1, 'w' );
-    local *STDOUT = $stdout;
-
-    my $stderr = IO::Handle->new;
-    $stderr->fdopen( 2, 'w' );
-    local *STDERR = $stderr;
-
-    return shift->();
-}
-
 =head2 SignEncrypt Entity => MIME::Entity, [ Encrypt => 1, Sign => 1, ... ]
 
 Signs and/or encrypts an email message with GnuPG utility.
@@ -500,7 +478,7 @@
 
         eval {
             local $SIG{'CHLD'} = 'DEFAULT';
-            my $pid = _safe_run_child { $gnupg->detach_sign( handles => $handles ) };
+            my $pid = safe_run_child { $gnupg->detach_sign( handles => $handles ) };
             $entity->make_multipart( 'mixed', Force => 1 );
             $entity->parts(0)->print( $handle{'input'} );
             close $handle{'input'};
@@ -560,7 +538,7 @@
 
         eval {
             local $SIG{'CHLD'} = 'DEFAULT';
-            my $pid = _safe_run_child { $args{'Sign'}
+            my $pid = safe_run_child { $args{'Sign'}
                 ? $gnupg->sign_and_encrypt( handles => $handles )
                 : $gnupg->encrypt( handles => $handles ) };
             $entity->make_multipart( 'mixed', Force => 1 );
@@ -688,7 +666,7 @@
         my $method = $args{'Sign'} && $args{'Encrypt'}
             ? 'sign_and_encrypt'
             : ($args{'Sign'}? 'clearsign': 'encrypt');
-        my $pid = _safe_run_child { $gnupg->$method( handles => $handles ) };
+        my $pid = safe_run_child { $gnupg->$method( handles => $handles ) };
         $entity->bodyhandle->print( $handle{'input'} );
         close $handle{'input'};
         waitpid $pid, 0;
@@ -777,7 +755,7 @@
         my $method = $args{'Sign'} && $args{'Encrypt'}
             ? 'sign_and_encrypt'
             : ($args{'Sign'}? 'detach_sign': 'encrypt');
-        my $pid = _safe_run_child { $gnupg->$method( handles => $handles ) };
+        my $pid = safe_run_child { $gnupg->$method( handles => $handles ) };
         $entity->bodyhandle->print( $handle{'input'} );
         close $handle{'input'};
         waitpid $pid, 0;
@@ -880,7 +858,7 @@
         my $method = $args{'Sign'} && $args{'Encrypt'}
             ? 'sign_and_encrypt'
             : ($args{'Sign'}? 'clearsign': 'encrypt');
-        my $pid = _safe_run_child { $gnupg->$method( handles => $handles ) };
+        my $pid = safe_run_child { $gnupg->$method( handles => $handles ) };
         $handle{'input'}->print( ${ $args{'Content'} } );
         close $handle{'input'};
         waitpid $pid, 0;
@@ -1112,7 +1090,7 @@
     my %res;
     eval {
         local $SIG{'CHLD'} = 'DEFAULT';
-        my $pid = _safe_run_child { $gnupg->verify( handles => $handles, command_args => [ '-', $tmp_fn ] ) };
+        my $pid = safe_run_child { $gnupg->verify( handles => $handles, command_args => [ '-', $tmp_fn ] ) };
         $args{'Signature'}->bodyhandle->print( $handle{'input'} );
         close $handle{'input'};
 
@@ -1161,7 +1139,7 @@
     my %res;
     eval {
         local $SIG{'CHLD'} = 'DEFAULT';
-        my $pid = _safe_run_child { $gnupg->verify( handles => $handles, command_args => [ '-', $tmp_fn ] ) };
+        my $pid = safe_run_child { $gnupg->verify( handles => $handles, command_args => [ '-', $tmp_fn ] ) };
         $args{'Signature'}->bodyhandle->print( $handle{'input'} );
         close $handle{'input'};
 
@@ -1228,7 +1206,7 @@
     eval {
         local $SIG{'CHLD'} = 'DEFAULT';
         $gnupg->passphrase( $args{'Passphrase'} );
-        my $pid = _safe_run_child { $gnupg->decrypt( handles => $handles ) };
+        my $pid = safe_run_child { $gnupg->decrypt( handles => $handles ) };
         $args{'Data'}->bodyhandle->print( $handle{'input'} );
         close $handle{'input'};
 
@@ -1371,7 +1349,7 @@
     eval {
         local $SIG{'CHLD'} = 'DEFAULT';
         $gnupg->passphrase( $args{'Passphrase'} );
-        my $pid = _safe_run_child { $gnupg->decrypt( handles => $handles ) };
+        my $pid = safe_run_child { $gnupg->decrypt( handles => $handles ) };
         waitpid $pid, 0;
     };
     $res{'exit_code'} = $?;
@@ -1488,7 +1466,7 @@
     eval {
         local $SIG{'CHLD'} = 'DEFAULT';
         $gnupg->passphrase( $args{'Passphrase'} );
-        my $pid = _safe_run_child { $gnupg->decrypt( handles => $handles ) };
+        my $pid = safe_run_child { $gnupg->decrypt( handles => $handles ) };
         print { $handle{'input'} } ${ $args{'Content'} };
         close $handle{'input'};
 
@@ -2072,7 +2050,7 @@
     eval {
         local $SIG{'CHLD'} = 'DEFAULT';
         my $method = $type eq 'private'? 'list_secret_keys': 'list_public_keys';
-        my $pid = _safe_run_child { $gnupg->$method( handles => $handles, $email? (command_args => $email) : () ) };
+        my $pid = safe_run_child { $gnupg->$method( handles => $handles, $email? (command_args => $email) : () ) };
         close $handle{'input'};
         waitpid $pid, 0;
     };
@@ -2271,7 +2249,7 @@
     eval {
         local $SIG{'CHLD'} = 'DEFAULT';
         local @ENV{'LANG', 'LC_ALL'} = ('C', 'C');
-        my $pid = _safe_run_child { $gnupg->wrap_call(
+        my $pid = safe_run_child { $gnupg->wrap_call(
             handles => $handles,
             commands => ['--delete-secret-and-public-key'],
             command_args => [$key],
@@ -2326,7 +2304,7 @@
     eval {
         local $SIG{'CHLD'} = 'DEFAULT';
         local @ENV{'LANG', 'LC_ALL'} = ('C', 'C');
-        my $pid = _safe_run_child { $gnupg->wrap_call(
+        my $pid = safe_run_child { $gnupg->wrap_call(
             handles => $handles,
             commands => ['--import'],
         ) };

Added: rt/branches/3.8-TESTING/lib/RT/Util.pm
==============================================================================
--- (empty file)
+++ rt/branches/3.8-TESTING/lib/RT/Util.pm	Mon Jun  2 18:24:48 2008
@@ -0,0 +1,79 @@
+# BEGIN BPS TAGGED BLOCK {{{
+#
+# COPYRIGHT:
+#
+# This software is Copyright (c) 1996-2008 Best Practical Solutions, LLC
+#                                          <jesse at bestpractical.com>
+#
+# (Except where explicitly superseded by other copyright notices)
+#
+#
+# LICENSE:
+#
+# This work is made available to you under the terms of Version 2 of
+# the GNU General Public License. A copy of that license should have
+# been provided with this software, but in any event can be snarfed
+# from www.gnu.org.
+#
+# This work is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 or visit their web page on the internet at
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+#
+#
+# CONTRIBUTION SUBMISSION POLICY:
+#
+# (The following paragraph is not intended to limit the rights granted
+# to you to modify and distribute this software under the terms of
+# the GNU General Public License and is only of importance to you if
+# you choose to contribute your changes and enhancements to the
+# community by submitting them to Best Practical Solutions, LLC.)
+#
+# By intentionally submitting any modifications, corrections or
+# derivatives to this work, or any other work intended for use with
+# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# royalty-free, perpetual, license to use, copy, create derivative
+# works based on those contributions, and sublicense and distribute
+# those contributions and any derivatives thereof.
+#
+# END BPS TAGGED BLOCK }}}
+
+package RT::Util;
+use strict;
+use warnings;
+
+use base 'Exporter';
+our @EXPORT = qw/safe_run_child/;
+
+sub safe_run_child (&) {
+    local @ENV{ 'LANG', 'LC_ALL' } = ( 'C', 'C' );
+
+    return shift->() if $ENV{'MOD_PERL'};
+
+    # We need to reopen stdout temporarily, because in FCGI
+    # environment, stdout is tied to FCGI::Stream, and the child
+    # of the run3 wouldn't be able to reopen STDOUT properly.
+    my $stdin = IO::Handle->new;
+    $stdin->fdopen( 0, 'r' );
+    local *STDIN = $stdin;
+
+    my $stdout = IO::Handle->new;
+    $stdout->fdopen( 1, 'w' );
+    local *STDOUT = $stdout;
+
+    my $stderr = IO::Handle->new;
+    $stderr->fdopen( 2, 'w' );
+    local *STDERR = $stderr;
+
+    return shift->();
+}
+
+1;


More information about the Rt-commit mailing list