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

I'm writing a build utility for a large Java application in Perl.

One of the requirements is that we be able to register EJBs with
our LDAP server. I have a script that builds a registration command
and then executes it using qx($cmd);.

The odd thing is, this works fine from a command line (i.e., registers
the EJB, generates and compiles the stubs), but from the same script
running through CGI I get a "Error creating context" error, which basically
means to me "something ain't right".

Now, I don't expect anyone to answer any questions about Java, but I
have to wonder what might be causing the problem. I'm running the exact
same script from cgi and from command-line:

from command-line:
C:\> perl ejbreg.pl

in the cgi script:
open(PS, "ejbreg.pl |") || die "Can't open script"; print "<DIV>"; while ($line=<PS>) { if($line=~/.*\n$/) { print "$line<BR>\n"; } else { print "$line"; } print ""; } print "</DIV>"; close(PS);



Are there are any differences between the runtime environments of
command-line scripts and scripts called from cgi?

Could it be that there are additional restrictions on the CGI script?
Could something restrict it from being able to contact the LDAP server?
(incidentally, the LDAP server resides on a separate machine from the
web server serving up the cgi).

Thanks for any help with this issue. I can provide additional code if needed

jbwiv

Replies are listed 'Best First'.
Re: Differences between CGI and command-line versions of scripts?!?
by arturo (Vicar) on Apr 03, 2001 at 21:31 UTC

    Apart from the environment variables that will be different, the biggest one is that CGI scripts typically run with the same permissions as the web server process, which are usually set to be quite restricted.

    You could help yourself out a lot by printing out the error returned by the operating system instead of simply dying with "something went wrong" messages; open or die "Ack!@: $!\n" ought to give you some insight into whether this is a permissions problem.

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      Good point. I typically do provide more error output but for this post decided to be brief.

      My problem is...the script isn't dying. The script runs just fine. The only problem is when I try to qx() and pass off the java registration commands to Dos. When I run the full script from a command-line, it works just fine. But when I run it from CGI, I get errors returned from the Java registration command.

      That leaves me to believe that it's something to do with the environment the script is running in from the web server (in this case M$ PWS).

      thanks!
      jbwiv
Re: Differences between CGI and command-line versions of scripts?!?
by dws (Chancellor) on Apr 04, 2001 at 22:43 UTC
    Based on some CB exchanges, I know you're using IIS. What you're probably running into is this: By default, IIS uses an "anonymous user" context when servicing requests. The anonymous user doesn't have access to everything that an real user does.

    To change the user context,

    1. Start the "Internet Service Manager"
    2. Right-click on either the "Default Web Site" or some virtual directory within it, then select "Properties..." from the pop-up menu.
    3. Select the "Directory Security" panel.
    4. In the "Anonymous Access and Authentication Control" area, click "Edit..."
    5. In the dialog, make sure "Allow Anonymous Access" is checked.
    6. Click "Edit..." for "Account used for Anonymous Access". You'll see a dialog that shows which "user" is used when serving anonymous requests.
    7. Click "Browse" and then select a valid user.
    This assumes that you have administrative access to IIS.

    I recommend that you make changes on a virtual directory basis, and not for the entire website, until you've had a chance to read up on the implications.

      Exactly! Switched the "Anonymous User" to my id, and it worked like a charm.

      Thanks so much for your help!!!

Re: Differences between CGI and command-line versions of scripts?!?
by runrig (Abbot) on Apr 03, 2001 at 21:30 UTC
    What's the error in the error logs? Maybe you need to specify the full path to the command (in CGI the PATH is probably not the same). Since you are opening a pipe you should also check the status of the close() command (And as others have suggested, include '$!' in the error message).

    Also I don't get why you are checking for a newline at the end of every line read in. There should be a newline at the end of every line read in (except maybe the last one, is that what that's for?).

      (in CGI the PATH is probably not the same)

      Exactly. Unless "." is in $ENV{PATH}, (which is probably won't be when the CGI is invoked), your open() will fail. Replace   open(PS, "ejbreg.pl |") || die "Can't open script"; with   open(PS, "./ejbreg.pl |" ) or die "Can't open script: $!"; And depending on the webserver, you may need to chdir() to get where you need to be.

        Sorry, this seems to have caused some confusion. My actual script looks like:

        my $process='perl c:\winnt\profiles\jbw\builder\' . "guibuilder.pm $NAME $PASS $BOPT $IWEB $INAS $IAPP " . "$IHLP $VERB |"; open(PH, "$process") || die "Can't open process: $process -- $! ";

        Again, the script runs fine from both command line and cgi. It just can't seem to access the LDAP server from cgi...and I can't seem to figure out why.

        Thanks for the tip anyway!

make++ -- A better, safer make implementation
by princepawn (Parson) on Apr 04, 2001 at 01:04 UTC
    Perhaps as part of your build utility, you could use the Perl drop-in replacement for GNU make with Perl extensions: make++