Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    call perl script from php

    hi,
    i'm trying to call a perl script from php without success.
    my php code :
    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
    Code:
     php  /var/www/htdocs/sendsms.php
    can someone help me please !!
    thank's in advance

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Do you have execute privileges on you're server for php?

    Test with a simple:
    PHP Code:
    <?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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    mehaj (02-25-2010)

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Do you have execute privileges on you're server for php? yes i do

    Test with a simple:
    PHP Code:
    <?php

    echo exec('whoami');
    ?>
    i have tested your code and it return //

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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 Code:
    <?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 !
    Last edited by mehaj; 02-22-2010 at 12:22 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    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’

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

    thank you again for your help

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.
    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 :
    Code:
    # 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
    Code:
    # 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 "

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •