...

View Full Version : call perl script from php



mehaj
02-18-2010, 03:27 PM
hi,
i'm trying to call a perl script from php without success.
my php code :


<?php
$tel = $_POST['Tel'] ;
$message = $_POST['message'] ;
//$tel = 'xxxxxxx';
//$message = 'viwwwww';
print $tel;
print $message;
$outpout = exec("perl /var/www/htdocs/msg.pl $tel $message");
print ($outpout);

?>



however, when i enter the following command in the command prompt it works

php /var/www/htdocs/sendsms.php
can someone help me please !!
thank's in advance

Fou-Lu
02-18-2010, 03:43 PM
Do you have execute privileges on you're server for php?

Test with a simple:


<?php

echo exec('whoami');
?>


Also, you'll be wanting to search a little bit on how to correctly handle exec calls in PHP. What you have there is a huge risk since you're completely trusting you're users.

mehaj
02-18-2010, 05:43 PM
Do you have execute privileges on you're server for php? yes i do

Test with a simple:


<?php

echo exec('whoami');
?>

i have tested your code and it return //

Fou-Lu
02-18-2010, 08:50 PM
Thats an odd output, I would have expected a id of some sorts to show up. Oh well.

You said that you can execute /var/www/htdocs/sendsms.php from the cli correct? I presume thats either as you're user or a root user.
Can you check the file permissions on /var/www/htdocs/msg.pl? If executing the entire php script from the cli works, but not from apache, that leads me to believe there is a permission problem when executing /var/www/htdocs/msg.pl. It needs to have execute for world or be owned/grouped by you're apache user (nobody maybe?) and executable by them for it to work in exec.
If you give you're exec an array variable (its by ref so make sure its an actual variable), you can capture the entire output into the array line by line, and a third parameter should have the return result (assuming 0 is what you're wanting). That may give you some direction when it comes to what the issue is.

You can also check if the file is executable with the is_executable() function.

Aside from that, I won't be able to give you much help I'm afraid. Hopefully it lies with either having difficulty finding the perl command (which I doubt), or an issue with permissions.

mehaj
02-22-2010, 12:02 PM
hi Fou-Lu
I'm the root. and as i've mentioned before i can execute /var/www/htdocs/sendsms.php from the cli but not from the web
The file permissions on /var/www/htdocs/msg.pl are 755 .

i've tried this code, from the cli the output is /var/www/htdocs/sendsms.php is executable, from the web i have /var/www/htdocs/sendsms.php is not executable



<?php

$file = '/var/www/htdocs/sendsms.php';

if (is_executable($file))
{
echo $file.' is executable';
}
else
{
echo $file.' is not executable';
}

?>



i've also tried this code,from the cli the output is root from the web the output is "OS Error: 127."


<?php

exec ("whoami",$output,$error);
if($error) echo ("OS Error :$error.<br/>\n");
echo implode("<br/>",$output);
?>




Concerning my server (apache), in the httpd.conf i have
User www
Group www
maybe that switching to apache so changing the user cause problems ?

thank you anyway !

Fou-Lu
02-22-2010, 03:05 PM
yes, I'd suspect it is related directly to the use of apache or ini configurations.

The permissions are fine, so we know thats not the issue. With the web based envorinment and what error's its tossing at you (fnf I believe), my assumption at this point is either safe_mode is enabled (check with printf("Safe mode enabled: %d\n", ini_get('safe_mode'));), or that the path is not accessible to to a chroot jailed user. I believe you'll need to change the www account to /bin/sh for its shell.

mehaj
02-23-2010, 10:33 AM
:thumbsup: the probleme was coming from the configuration of apache in Openbsd. . the server is chrooted by default so he doesn’t run any programs.I just disabled the chroot and it worked.
I still have another question, my perl script doesn’t work entirly, some commands in the script need root user to be executed , but i’m www user , can i change the user using a command or script in php.

Fou-Lu
02-23-2010, 04:36 PM
Using PHP, I don't think that will work. You're perl script itself can probably select a new user to run with, but if you're requiring a root user to execute the script, I would not be public fronting the script to start with. That will just open you up to potentially dangerous security holes.

I suppose you can su from you're perl script, but I still think you're really opening it up to problems. PHP should be also able to su you're script prior to calling the pl. I assume that would also work.
What is it you're requiring root privilege for?

mehaj
02-24-2010, 10:30 AM
hi Fou-lu
i wich to sendsms,the connection to gsm need root privilège.
i have tried calling the comand sudo in my php code, but it doesn't work

exec("sudo perl /var/www/htdocs/msg.pl $tel $message ");

i’ve also modify the sudoers file ( incomment the line %wheel ALL=(ALL) NOPASSWD:ALL) and i’ve added the user www to the group ‘wheel’

:confused: I must have forgotten something!! I'll try again with php before switching to perl

thank you again for your help

Fou-Lu
02-24-2010, 02:40 PM
Yeah, let me know how it all turns out.
I've never needed to su to execute any external script before, so I'm not certain what the issue is.

mehaj
02-25-2010, 10:54 AM
Fou-lu,
Great thanks for your contribution to this thread,i wouldn't have been able to solve it without your help and your suggestion.
i have solved my problem using command sudo in my php code.

exec("sudo perl /var/www/htdocs/msg.pl $tel $message");
i just gave to 'www' user root privilege and his own shell (this is what was missing,because,in openbsd 'www' doesn't have shell)
before :

# userinfo www
login www
passwd *
uid 67
groups www wheel
change NEVER
class
gecos HTTP Server
dir /var/www
shell /sbin/nologin
expire NEVER

after


# userinfo www
login www
passwd *
uid 67
groups www wheel
change NEVER
class
gecos HTTP Server,,,
dir /var/www
shell /bin/sh
expire NEVER

for more details " http://www.daemonforums.org/showthread.php?p=30383 "

Fou-Lu
02-25-2010, 02:36 PM
Great, I figured the shell was a big issue for sure (suggested that a few posts up hah).

Once you change the shell, do you still need to sudo for it?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum