Reputation: 203
Basically, I have written a CGI script in C, which contains a single system() function which is supposed to call a a batch file on the server where the EXE resides. A sample code I have written is as follows:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
//system("ROBOCOPY \\pdffiles \\\\10.6.202.88\\d$\\pdffiles /mov /ZB /XJ /FFT /MT /R:2 /W:5 /V");
int t = 0;
t= system("PDF_MOVE.bat");
if(t == -1)
{
printf("file copy failure");
}
else
{
printf("file copy successfull");
}
return 0;
}
After I compile this code and put the generated .exe file in a folder in the Apache server.
Then when I try to hit this exe from a web browser by entering the full path of the exe in the browser(i.e http://10.6.202.111/path/to/file_copy_test.exe). This either tries to download the .exe file or gives me a Internal server error (500). The batch file contains the robocopy command that I have commented in the code. Also if I directly try to run the robocopy command through the system commmand, it fails as well.
What can I do to fix this, any Apache setting needs to be changed?. do the path to the folders needs to be changed?. I have tried putting ./ in front of the batch file name but then it fails on the command line as '.' is not recognised.
The Apache error log shows:
[cgi:error] [pid 10604:tid 1512] [client 172.24.2.82:56507] AH01215: operable program or batch file.\r: D:/itrade/app/user/file_copy_test.exe
After making the suggested changes, I realised that the source and destination paths provided for the Robocopy command were incorrect. Fixing that finally made it work.
The batch file I used:-
@echo off
(
net use \\10.6.202.88\d$ /user:Username password
ROBOCOPY D:\path\to\pdffiles \\10.6.202.88\d$\pdffiles /ZB /COPYALL /mov /XJ /FFT /MT /R:2 /W:5 /v /tee /log:robocopy_log.txt
EXIT
) >output.txt 2>&1
Upvotes: 0
Views: 1834
Reputation: 3451
Make sure you allow cgi execution by adding this to the VistualHost file or .htaccess file (see: https://www.devside.net/wamp-server/running-exe-and-batch-files-as-cgi-scripts-in-apache-under-windows)
AddHandler cgi-script .bat # add .exe if you wish
Options +ExecCGI +FollowSymlinks
And why use a C program / .exe to execute a batch file? This batch file should also do the trick (to be honest I did not test this):
@echo off
echo Content-Type: text/html
echo.
CALL PDF_MOVE.bat || goto error
echo file copy successfull
goto :EOF
:error
echo file copy failure
Upvotes: 2
Reputation: 8142
We don't know what the batch file outputs, but your C code is missing one vital important thing to conform to the CGI standards - a HTTP header.
The bare minimum you need to output is the Content-Type which since you're not expecting to output any HTML, might as well be "text/plain". The header is terminated with a blank line.
printf("Content-Type: text/plain\n\n");
Without that, Apache will treat anything you output as the header and return an error 500 if it doesn't understand it.
Configuration-wise, Apache needs to be told that CGI "scripts" exist in the folder you're trying to run it from and what extension they have.
Options +ExecCGI
AddHandler cgi-script .exe
That might need to be in your main apache configuration inside a <Directory ...>
block or in a ".htaccess" if you have allowed "users" to override the Options
directive with AllowOverride Options
The executable also needs permissions to be read and executed by Apache.
Upvotes: 1