Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Running gpg thru IPC, fileno problem

by zentara (Archbishop)
on Jun 09, 2005 at 14:30 UTC ( #465144=perlquestion: print w/ replies, xml ) Need Help??
zentara has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Someone asked on the perl.beginners maillist, a question about running gpg thru a script. Well I thought It could be run thru IPC::Open3, but there is a complication. Gpg grabs the tty of the perlscript, preventing IPC from operating.

If you run the script below(modified to edit a suitable name for your system), gpg, grabs the tty. If you enter "trust" followed by Enter, a new menu appears. That is the functionality I'm trying to perform thru the script.

If I print to the IN filehandle, it does nothing. I've noticed that the Crypt::GPG module uses IPC::Run and an undocumented gpg option called "--no-tty", but that aside, I just want to know how to access the STDIN to gpg.

On my system gpg is shown using /dev/pts/5, and I can't seem to be able to print to it. Is it possible thru some sort of fileno magic?

#!/usr/bin/perl use warnings; use strict; use IPC::Open3; use POSIX; my $childpid = open3(\*IN, \*OUT, \*ERR, 'gpg --edit zentara'); #gpg grabs control of the tty here my $tty0 = ttyname(0); #STDIN my $tty1 = ttyname(1); #STDOUT print "$tty0 $tty1\n"; # they are /dev/pts/5 on my machine #how do I print to STDIN of /dev/pts/5 ? print IN "trust\n"; chomp(my $answer = <OUT>); print $answer; if ($answer =~ /Your decision/) { print IN "5\n"; }else { print "decision error $!\n"; exit; }

I'm not really a human, but I play one on earth. flash japh

Comment on Running gpg thru IPC, fileno problem
Download Code
Re: Running gpg thru IPC, fileno problem
by Fletch (Chancellor) on Jun 09, 2005 at 14:36 UTC

    You need to look at either IPC::Run or Expect as they'll let you run a child on a pty.

    --
    We're looking for people in ATL

Re: Running gpg thru IPC, fileno problem
by PodMaster (Abbot) on Jun 09, 2005 at 15:18 UTC
      I explored CPAN's GPG/PGP modules a while back. Most of them either don't compile or fail their tests on Fedora Core 3. The GPG interfaces on CPAN seem to have been left to rot, which means doing your own IPC to the executable is probably the safest choice at the moment. (Or put in the effort to clean one of them up, if you have the time and interest.)
        Most of them either don't compile or fail their tests on Fedora Core 3.
        Seeing how most everything fails on fedora core :)(that locale issue) and there is a few passes, I wouldn't discount them without good examination (i have successfully used at least 2 of those).

        update: also, a lot of those FAILS are a case of Can't exec "gpg": No such file or directory , which means the test suite isn't the best (assumes you have gpg installed), but doesn't indicate an actual failure.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

      A recent thread suggests that you should not necessarily trust GnuPG, but at least the thread provides a fix so that the package actually compiles.

Re: Running gpg thru IPC, fileno problem
by zentara (Archbishop) on Jun 09, 2005 at 20:33 UTC
    Just in case anyone is interested I found an answer. After muddling thru all the modules, and seeing how it was done, I decided to get the latest version of gpg ( 1.4.1) and look thru the docs for a new option called --command-fd which is a file descriptor which gpg will use to accept commands in a --no-tty mode. It works. :-)
    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; local $SIG{CHLD} = 'IGNORE'; local $SIG{PIPE} = 'IGNORE'; my $childpid = open3(\*IN, \*OUT, \*ERR, 'gpg -v --no-greeting --no-tty --command-fd 0 --status-fd 1 --e +dit zentara'); print IN "trust\n5\ny\n"; close IN; my(@answer,@err); @answer = <OUT>; print "out->@answer\n"; @err = <ERR>; print "err->@err\n";

    I'm not really a human, but I play one on earth. flash japh
      I'm interested :)

      I played around with the file descriptor option with gpg a while ago and never got it to work. Thanks for posting your snippet of working code!

        Hi, I just updated the example to use '-v' , and it will give alot more useful output, like if keys are expired.

        I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://465144]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-08-28 11:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls