gstrock has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to generate a unique string using:
gpg --gen-random 1 16 | gpg --enarmor | sed -n 5p

If I put this in a perl script and run from the command line it works just fine generating something along these lines:
J7Ld1bWb56ezv3GRF7RfBg==
When I put it into a cgi script it always produces the same value of "=twTO". This happens even when I use HTML::Template and "ESCAPE HTML".

what gives?
----perl script-------------------------------------- #!/usr/bin/perl $serialno = `gpg --gen-random 1 16 | gpg --enarmor | sed -n 5p`; print $serialno;
----cgi script----------------------------------------- #!/usr/bin/perl -wT $serialno = `gpg --gen-random 1 16 | gpg --enarmor | sed -n 5p`; print "Content-type: text/html\n\n"; chomp($serialno); print "$serialno"; exit;

Replies are listed 'Best First'.
Re: works as a perl script, but not as cgi
by ikegami (Patriarch) on Dec 21, 2006 at 20:07 UTC

    That's the output one gets if the first program isn't called.

    $ gpg --enarmor < /dev/null | sed -n 5p =twTO

    Loops like gpg --gen-random 1 16 is failing. Did you check your error log?

Re: works as a perl script, but not as cgi
by kyle (Abbot) on Dec 21, 2006 at 20:51 UTC

    On my system, the www-data user does not have write permission to its own home directory (/var/www). For --gen-random (but not for --enarmor), GnuPG wants to create a ~/.gnupg directory and fails, which it considers fatal. This works:

    gpg --homedir /tmp --gen-random 1 16

    ...although it creates two files in /tmp and emits messages to that effect. Note also that it does not work if the two options are reversed—the --homedir option has to be first.

    I don't know if that's your problem or not, but it's worth checking. (As already suggested, check your error log.)

      thanks everybody. Yup. I should have checked the log files.
      ~/.gnupg: can't create directory: No such file or directory
      I was focusing on it as strictly a perl problem, when I should have read the gpg man page instead.

      my bad.
Re: works as a perl script, but not as cgi
by jdporter (Paladin) on Dec 21, 2006 at 22:57 UTC

    Except for the details of your app (the gpg stuff), this is a FAQ:
    My CGI script runs from the command line but not the browser.

    The FAQ entry itself doesn't give any direct answers, but it does refer you to the "CGI_MetaFAQ", which includes a "Troubleshooting_CGI" document.

    Unfortunately, the URLs given in perlfaq9 don't seem to work (at the moment), but if you know that brian d foy is the maintainer of the CGI_MetaFAQ, you'd eventually find it in his sourceforge area.

    We're building the house of the future together.
Re: works as a perl script, but not as cgi
by chargrill (Parson) on Dec 21, 2006 at 20:00 UTC

    Check on the path to gpg and whether or not the user the webserver runs as has permission to run it. And sed.

    Update: running with -T (taint) may also be affecting your output. Check out perlsec



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: works as a perl script, but not as cgi
by f00li5h (Chaplain) on Dec 21, 2006 at 20:48 UTC

    Did you try running your cgi script from the command line?

    Grab a shell and head to your webserver, the script may give you extra warnings etc to STDERROR (which, as ikegami points out appear in the logs)

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Re: works as a perl script, but not as cgi
by cub.uanic (Acolyte) on Dec 22, 2006 at 05:15 UTC
    Really, I don't understood - why you need gpg ?...
    TIMTOWTDI :)
    I think, you need take a look at Data::UUID
    % perl -MData::UUID -e 'print Data::UUID->new->create_b64(), "\n"' es24FXuR2xGq/duz7cNLfg==
    HTH
      Data::UUID looks like cool, but I'm back to the same old same old again, where it works at the shell level but not in cgi. By this time I've learned to check my logs and I just see things like "unitialized variable at line xx" and when the line being flagged is the print statement below:
      $ug = new Data::UUID; print $ug->create_str();
      I have to assume something is preventing UUID from doing it's thing.

      I can't figure out what's going on, but I notice in /var/tmp I have a couple files owned by apache .UUID_NODEID and .UUID_STATE that don't go away. So it must be some kind of permission thing although /var/tmp is set for drwxrwxrwt.

      At this point I give up. I have gpg working. I don't have to use gpg but I saw it in Linux Journal. I'm just trying to create a unique identifier.