Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Calling external programs via cgi on win32

by jamesjyu (Acolyte)
on Jul 02, 2004 at 04:38 UTC ( #371307=perlquestion: print w/ replies, xml ) Need Help??
jamesjyu has asked for the wisdom of the Perl Monks concerning the following question:

I have been battling this problem all day. The script itself is quite simple.

I am developing a web form that will allow the user to run an external program on the server. I am using Apache on win32, and using Perl via CGI.

At the command line, the script works perfectly. I am able to run the external program with no problems. However, when I hit submit and run the script via the web browser (POST), either nothing happens, or the external program crashes.

I have tried what I think is every technique in perl to run an external program.. system, exec, Win32::Spawn(), Win32::Process::Create(). They all work at the command prompt, but none work through the post method.

I am running under windows XP professional, and have set Everyone to have full control of the directory with the external program. The fact that it works via the command prompt and not the post method leads me to believe that there must be something different in the environment when perl is run through the post method.

Can someone enlighten me about what the difference is? And if you've had similar issues, and how you solved them..?

Comment on Calling external programs via cgi on win32
Download Code
Re: Calling external programs via cgi on win32
by Gerard (Pilgrim) on Jul 02, 2004 at 04:48 UTC
    It is possible that Apache being run as a service does not have the correct permissons to do something.

    The default user running the service will be "Local System Account". You can change this on the Log On tab of the Service properties dialog.
    For example try changing this to the same user that you are logged in as when running from the command prompt. You will need to restart apache, for this to take affect.

    Hope this helps, Gerard.
    Update: It really all depends on what your external program is trying to do, and how things are set up there... I haven't had similar problems, but have avioded them, when scripts are trying to access the local network for example.
      I tried your suggestion, and changed the log in to myself (administrator level) and still doesn't help. In terms of the external program, I have tried just starting a simple program like notepad.exe, and this doesn't even work (but works on the command line of course). There must be something fundamentally wrong that I'm not aware of..
        It turns out you cannot invoke a gui based program from a cgi script. This would have worked in win98, but not in any newer version of windows...
Re: Calling external programs via cgi on win32
by Wassercrats on Jul 02, 2004 at 06:13 UTC
    Are you sure your form is passing data to your script? I have that same problem and have to configure my script manually, bypassing the form, because I'm not running a server. I might use VBScript to save the form data to a file and have my script read it from the file.

    There's some talk about server emulators in Perl web browsers.

Re: Calling external programs via cgi on win32
by arden (Curate) on Jul 02, 2004 at 06:13 UTC
    What does your external program do? Does it need to interact with some directory, files, or another executable to which it doesn't have permission to? Does it need to read something from the registry? What security lock-downs have you applied to your XP-Pro system?

    This really looks like a windows' permissions problem to me. Try to figure out exactly what that external program is doing, I'll wager your answer will be found there.

    - - arden.

Re: Calling external programs via cgi on win32
by JamesNC (Chaplain) on Jul 02, 2004 at 12:42 UTC
    In addition to the other comments( of which I tend to agree that it is a permissions issue for sure!). Check Apache's logs for errors. Also, I don't know if you are use CGI::Carp or not, but that helps to spit errors out to the browser when things go wrong.
    I would also try writing a simple test case:
    use CGI qw(:standard); my $cgi = CGI->new(); print $cgi->header; print $cgi->start_html; my $rv = `set `; # see your environment print $cgi->p( $rv ); print $cgi->end_html;

    JamesNC
Re: Calling external programs via cgi on win32
by skyknight (Hermit) on Jul 02, 2004 at 13:25 UTC

    Question your assumptions. Specifically, validate the input to your script to make sure that it's getting from the web browser the same stuff it seems to be getting from the command line. Validate, validate, validate. This is good pratice even if you're not jumping between environments. Also, try dumping out the %ENV hash both from the command line and script. Check which user Apache is running as, as well as the group. A good test to see if permissions are causing problems would be (assuming you have the ability to do so) to create another user account, and try invoking your CGI script from it. This could be telling since this other account will probably have permissions (with respect to executing your CGI script) similar to Apache.

Re: Calling external programs via cgi on win32
by paulbort (Hermit) on Jul 02, 2004 at 18:34 UTC
    I've said before, and I'll say again: this is a dangerous design and should not be used.

    This kind of code gives anyone with a web browser complete access to your system. If you want users to be able to run specific programs, have the CGI write commands to a queue somewhere (files in a directory, registry entries, database rows, whatever), then have a second program that runs in the background (as a service maybe) pick up the queue entries and execute them.

    This gives you two big wins right off the bat: First, your web server doesn't need any special privileges, it just needs to be able to write to your queue. Second, when you later get hacked and decide you want to limit the commands that can be run, the back-end script is the only part you need to change.

    Even if you're doing this on an isolated system, that no one can acces from a network, it's still worthwhile to think about security and isolation in every program. These are good programming habits that will serve you well all of your days.

    --
    Spring: Forces, Coiled Again!
      Thank you paulbort, I took your advice, and all is well now.
Re: Calling external programs via cgi on win32
by Anonymous Monk on Jul 02, 2004 at 19:31 UTC
    The problem seems to be that your external program wants to open a window on the desktop. That is only possible if either the program inherits an open desktop (which most programs assume) or if it runs as the same user that owns the desktop and dances around some syscalls in this case (I think this is what the Explorer does). So your external program should be explorer.exe. I don't know the command line to make it start another program (to inherit the desktop from the explorer).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-12-28 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (181 votes), past polls