Rectangle 27 2

Comment out this line from /etc/sudoers

I'v tested your case in few ways ant this one gives me success.

linux - php exec with sudo fails on CentOS - Stack Overflow

php linux apache shell sudo
Rectangle 27 1

  • In this case some security hole in your php-apps may run the script too (if they can include the calling php for example - or even bypass the restriction to your ip by using another url that also calls the script, mod_rewrite)
  • This is my prefered solution, since the link calling the php is accessible by links from your main webserver and the security can be handled seperately. You can even create a new user for this server. Some simple server does the job, there are server modules for python and perl for example. It is not even necessary, that you enable exec in your php installation at all!
  • This may be too complex and has the disadvantage, that the daemon can not check which script has generated the entry.

There is no other URL that will be using this script, see my edit above to get an idea of exactly what the script does. It's strictly for internal use, none of the client websites will have any calls to shell scripts. Will this make any difference to the security issues with option 1 (which seems the easiest).

@William Stewart: At least this is what you intended! If some php-software has filesystem read access to your php-script that is calling the shell script and some security problem in that software allows an external user to include your php file (that allowed to execute the shell script), then that script can execute it!

Great answer by urzeit. I like option 4, where the PHP script would log the pending jobs to a file, then a cron job or daemon (running with the necessary privileges) would execute the jobs. If the PHP script is set to exit immediately if it is not called from his IP (by examining $_SERVER['REMOTE_ADDR']), and no other PHP scripts on the server can write to this file, then I can't see how this could be compromised.

If I was to use a cronjob to run a list of pending jobs from a file I edit from my PHP script, how would I get the cronjob to run immediately after the job file is changed. My previous knowledge of cron is restricted to time-based scheduling, is there a way to run the cronjob when the file is changed? Surely running a cronjob every so many seconds is huge overkill and inefficient?

linux - Execute shell commands with sudo via PHP - Stack Overflow

php linux apache sudo shell-exec
Rectangle 27 4

So you do not have a php problem now, but a perl one. your include path is bad.

You either have to install the ExifTool libraries in the
standard location (ie. somewhere in the @INC directories
listed in your post), or add the location to the include path,
something like this:

Code:
#!/usr/bin/perl
BEGIN { unshift @INC, "PATH_TO_DIRECTORY_CONTAINING_LIBRARIES" }
use Image::ExifTool;

You should be able to add "Image/ExifTool.pm" to the path you add
to find the ExifTool module.

- Phil

I still think using my suggestion #3 from the previous answer will fix it. If not and you really want to know the reason, create a new perl script that just outputs the contents of @INC and run it via the shell and via php. you will see the difference, then you need to find which login script is not being honored in php and open a bug against php for shell_exec not respecting it...

though the easier solution for your problem (as it does not look like you are too interested in explanations) is to just set the PERLLIB var before calling the script.

find / -name ExifTool.pm
/example/perl/Image/ExifTool.pm
PERL5LIB=/example/perl/

"...as it does not look like you are too interested in explanations" ... not so! I'm very interested. I've been on this almost a week. This seems like the most likely answer. I'm inspecting it now. I unzipped in a temp directory and run the "make" ... assuming it would install properly. Apparently it did not install in the standard way. I've been researching that this morning based on your observation. I'm not experienced in perl at all, so this has been slower for me.

This was it - thanks a lot. I'll be awarding the bounty once it lets me (in 5 hours) --- much appreciated.

@Orangeman555 thanks. about learning, i honestly do not think you should have to learn perl to solve this. if you want to improve, learn linux (file system structure, file permission, stdout vs stdout i.e. buffers and streams, and about one shell or another, i suggest bash) and learn about your chosen work environment (php, apache, i'm assuming) learn where to search for error logs, etc. eventually you will see that everything follows the same pattern and it gets easy :) cheers.

linux - Cannot run shell command through php exec, but can as user on ...

linux perl bash shell exiftool
Rectangle 27 9

Both performance and security wise it's better not to call a shell at all between web server process and executable.

About performances, well, yes, php internals forks, and the shell itself forks too so that's a bit heavy. But you really need to execute a lot of processes to consider those performances issues.

About security, I do not see any issue here. PHP has the escapeshellarg function to sanitize arguments.

The only real problem I met with exec without pcntl is not a resource nor security issue : it is really difficult to create real deamons (without any attachment to its parent, particularily Apache). I solved this by using at, after double-escaping my command:

$arg1 = escapeshellarg($arg1);
$arg2 = escapeshellarg($arg2);
$command = escapeshellarg("/some/bin $arg1 $arg2 > /dev/null 2>&1 &");
exec("$command | at now -M");

To get back to your question, the only way I know to execute programs in a standard (fork+exec) way is to use the PCNTL extension (as already mentionned). Anyway, good luck!

To complete my answer, you can create an exec function yourself that does the same thing as pcntl_fork+pcntl_exec.

I made a my_exec extension that does a classic exec+fork, but actually, I do not think it will solve your issues if you're running this function under apache, because the same behaviour as pcntl_fork will apply (apache2 will be forked and there may be unexpected behaviours with signal catching and so on when execv does not succeed).

phpize
PHP_ARG_ENABLE(my_exec_extension, whether to enable my extension,
[ --enable-my-extension   Enable my extension])

if test "$PHP_MY_EXEC_EXTENSION" = "yes"; then
  AC_DEFINE(HAVE_MY_EXEC_EXTENSION, 1, [Whether you have my extension])
  PHP_NEW_EXTENSION(my_exec_extension, my_exec_extension.c, $ext_shared)
fi
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define PHP_MY_EXEC_EXTENSION_VERSION "1.0"
#define PHP_MY_EXEC_EXTENSION_EXTNAME "my_exec_extension"

extern zend_module_entry my_exec_extension_module_entry;
#define phpext_my_exec_extension_ptr &my_exec_extension_module_entry

// declaration of a custom my_exec()
PHP_FUNCTION(my_exec);

// list of custom PHP functions provided by this extension
// set {NULL, NULL, NULL} as the last record to mark the end of list
static function_entry my_functions[] = {
    PHP_FE(my_exec, NULL)
    {NULL, NULL, NULL}
};

// the following code creates an entry for the module and registers it with Zend.
zend_module_entry my_exec_extension_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    PHP_MY_EXEC_EXTENSION_EXTNAME,
    my_functions,
    NULL, // name of the MINIT function or NULL if not applicable
    NULL, // name of the MSHUTDOWN function or NULL if not applicable
    NULL, // name of the RINIT function or NULL if not applicable
    NULL, // name of the RSHUTDOWN function or NULL if not applicable
    NULL, // name of the MINFO function or NULL if not applicable
#if ZEND_MODULE_API_NO >= 20010901
    PHP_MY_EXEC_EXTENSION_VERSION,
#endif
    STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE(my_exec_extension)

char *concat(char *old, char *buf, int buf_len)
{
    int str_size = strlen(old) + buf_len;
    char *str = malloc((str_size + 1) * sizeof(char));
    snprintf(str, str_size, "%s%s", old, buf);
    str[str_size] = '\0';
    free(old);
    return str;
}

char *exec_and_return(char *command, char **argv)
{
    int link[2], readlen;
    pid_t pid;
    char buffer[4096];
    char *output;

    output = strdup("");

    if (pipe(link) < 0)
    {
        return strdup("Could not pipe!");
    }

    if ((pid = fork()) < 0)
    {
        return strdup("Could not fork!");
    }

    if (pid == 0)
    {
        dup2(link[1], STDOUT_FILENO);
        close(link[0]);
        if (execv(command, argv) < 0)
        {
            printf("Command not found or access denied: %s\n", command);
            exit(1);
        }
    }
    else
    {
        close(link[1]);

        while ((readlen = read(link[0], buffer, sizeof(buffer))) > 0)
        {
            output = concat(output, buffer, readlen);
        }

        wait(NULL);
    }
    return output;
}

PHP_FUNCTION(my_exec)
{
    char *command;
    int command_len, argc, i;
    zval *arguments, **data;
    HashTable *arr_hash;
    HashPosition pointer;
    char **argv;

    // recovers a string (s) and an array (a) from arguments
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &command, &command_len, &arguments) == FAILURE) {
        RETURN_NULL();
    }

    arr_hash = Z_ARRVAL_P(arguments);

    // creating argc and argv from our argument array
    argc = zend_hash_num_elements(arr_hash);
    argv = malloc((argc + 1) * sizeof(char *));
    argv[argc] = NULL;

    for (
            i = 0, zend_hash_internal_pointer_reset_ex(arr_hash, &pointer);
            zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS;
            zend_hash_move_forward_ex(arr_hash, &pointer)
        )
    {
        if (Z_TYPE_PP(data) == IS_STRING) {
            argv[i] = malloc((Z_STRLEN_PP(data) + 1) * sizeof(char));
            argv[i][Z_STRLEN_PP(data)] = '\0';
            strncpy(argv[i], Z_STRVAL_PP(data), Z_STRLEN_PP(data));
            i++;
        }
    }

    char *output = exec_and_return(command, argv);

    // freeing allocated memory
    for (i = 0; (i < argc); i++)
    {
        free(argv[i]);
    }
    free(argv);

    // WARNING! I guess there is a memory leak here.
    // Second arguemnt to 1 means to PHP: do not free memory
    // But if I put 0, I get a segmentation fault
    // So I think I do not malloc correctly for a PHP extension.
    RETURN_STRING(output, 1);
}

test.php a usage sample

<?php

dl("my_exec.so");

$output = my_exec("/bin/ls", array("-l", "/"));
var_dump($output);

shell script run those commands, of course use your own module directory

phpize
./configure
make
sudo cp modules/my_exec_extension.so /opt/local/lib/php/extensions/no-debug-non-zts-20090626/my_exec.so
KolyMac:my_fork ninsuo$ php test.php
string(329) ".DS_Store
.Spotlight-V100
.Trashes
.file
.fseventsd
.hidden
.hotfiles.btree
.vol
AppleScript
Applications
Developer
Installer Log File
Library
Microsoft Excel Documents
Microsoft Word Documents
Network
System
Users
Volumes
bin
cores
dev
etc
home
lost+found
mach_kernel
net
opt
private
sbin
tmp
usr
var
vc_command.txt
vidotask.txt"

I am not a C dev, so I think there are cleaner ways to achieve this. But you get the idea.

Thank you for this informative answer. It spans two topics about PHP : first, accept to fork a useless shell and work around security (escapeshellargs), and second how to create daemons (that part steps off the question asked). See George Cummins's bounty.

Security is a problem with any shell exec in any programming language, because most shells (sh, ash, bash, ksh, csh, tcsh, etc.) are slightly different and have different reserved characters with special meaning. Escapeshellarg() is a dangerous illusion of security. Not to mention that historically, escapeshellarg() has itself had vulnerabilities. If you don't need a shell, it's far safer not to use one. This is security 101: reduce attack surface area.

You mentioned escapeshellarg. Now we know about the 22-year-old (ref ) shellshock bug, news.ycombinator.com/item?id=8371357 says: "Sanitization is an orthogonal issue; no amount of escapeshellarg() would save someone from this issue (...)" Security requires to not introduce unneeded security holes. Calling a shell when not technically needed does just that.

performance - Run executable from php without spawning a shell - Stack...

php performance security shell
Rectangle 27 9

Both performance and security wise it's better not to call a shell at all between web server process and executable.

About performances, well, yes, php internals forks, and the shell itself forks too so that's a bit heavy. But you really need to execute a lot of processes to consider those performances issues.

About security, I do not see any issue here. PHP has the escapeshellarg function to sanitize arguments.

The only real problem I met with exec without pcntl is not a resource nor security issue : it is really difficult to create real deamons (without any attachment to its parent, particularily Apache). I solved this by using at, after double-escaping my command:

$arg1 = escapeshellarg($arg1);
$arg2 = escapeshellarg($arg2);
$command = escapeshellarg("/some/bin $arg1 $arg2 > /dev/null 2>&1 &");
exec("$command | at now -M");

To get back to your question, the only way I know to execute programs in a standard (fork+exec) way is to use the PCNTL extension (as already mentionned). Anyway, good luck!

To complete my answer, you can create an exec function yourself that does the same thing as pcntl_fork+pcntl_exec.

I made a my_exec extension that does a classic exec+fork, but actually, I do not think it will solve your issues if you're running this function under apache, because the same behaviour as pcntl_fork will apply (apache2 will be forked and there may be unexpected behaviours with signal catching and so on when execv does not succeed).

phpize
PHP_ARG_ENABLE(my_exec_extension, whether to enable my extension,
[ --enable-my-extension   Enable my extension])

if test "$PHP_MY_EXEC_EXTENSION" = "yes"; then
  AC_DEFINE(HAVE_MY_EXEC_EXTENSION, 1, [Whether you have my extension])
  PHP_NEW_EXTENSION(my_exec_extension, my_exec_extension.c, $ext_shared)
fi
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define PHP_MY_EXEC_EXTENSION_VERSION "1.0"
#define PHP_MY_EXEC_EXTENSION_EXTNAME "my_exec_extension"

extern zend_module_entry my_exec_extension_module_entry;
#define phpext_my_exec_extension_ptr &my_exec_extension_module_entry

// declaration of a custom my_exec()
PHP_FUNCTION(my_exec);

// list of custom PHP functions provided by this extension
// set {NULL, NULL, NULL} as the last record to mark the end of list
static function_entry my_functions[] = {
    PHP_FE(my_exec, NULL)
    {NULL, NULL, NULL}
};

// the following code creates an entry for the module and registers it with Zend.
zend_module_entry my_exec_extension_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    PHP_MY_EXEC_EXTENSION_EXTNAME,
    my_functions,
    NULL, // name of the MINIT function or NULL if not applicable
    NULL, // name of the MSHUTDOWN function or NULL if not applicable
    NULL, // name of the RINIT function or NULL if not applicable
    NULL, // name of the RSHUTDOWN function or NULL if not applicable
    NULL, // name of the MINFO function or NULL if not applicable
#if ZEND_MODULE_API_NO >= 20010901
    PHP_MY_EXEC_EXTENSION_VERSION,
#endif
    STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE(my_exec_extension)

char *concat(char *old, char *buf, int buf_len)
{
    int str_size = strlen(old) + buf_len;
    char *str = malloc((str_size + 1) * sizeof(char));
    snprintf(str, str_size, "%s%s", old, buf);
    str[str_size] = '\0';
    free(old);
    return str;
}

char *exec_and_return(char *command, char **argv)
{
    int link[2], readlen;
    pid_t pid;
    char buffer[4096];
    char *output;

    output = strdup("");

    if (pipe(link) < 0)
    {
        return strdup("Could not pipe!");
    }

    if ((pid = fork()) < 0)
    {
        return strdup("Could not fork!");
    }

    if (pid == 0)
    {
        dup2(link[1], STDOUT_FILENO);
        close(link[0]);
        if (execv(command, argv) < 0)
        {
            printf("Command not found or access denied: %s\n", command);
            exit(1);
        }
    }
    else
    {
        close(link[1]);

        while ((readlen = read(link[0], buffer, sizeof(buffer))) > 0)
        {
            output = concat(output, buffer, readlen);
        }

        wait(NULL);
    }
    return output;
}

PHP_FUNCTION(my_exec)
{
    char *command;
    int command_len, argc, i;
    zval *arguments, **data;
    HashTable *arr_hash;
    HashPosition pointer;
    char **argv;

    // recovers a string (s) and an array (a) from arguments
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &command, &command_len, &arguments) == FAILURE) {
        RETURN_NULL();
    }

    arr_hash = Z_ARRVAL_P(arguments);

    // creating argc and argv from our argument array
    argc = zend_hash_num_elements(arr_hash);
    argv = malloc((argc + 1) * sizeof(char *));
    argv[argc] = NULL;

    for (
            i = 0, zend_hash_internal_pointer_reset_ex(arr_hash, &pointer);
            zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS;
            zend_hash_move_forward_ex(arr_hash, &pointer)
        )
    {
        if (Z_TYPE_PP(data) == IS_STRING) {
            argv[i] = malloc((Z_STRLEN_PP(data) + 1) * sizeof(char));
            argv[i][Z_STRLEN_PP(data)] = '\0';
            strncpy(argv[i], Z_STRVAL_PP(data), Z_STRLEN_PP(data));
            i++;
        }
    }

    char *output = exec_and_return(command, argv);

    // freeing allocated memory
    for (i = 0; (i < argc); i++)
    {
        free(argv[i]);
    }
    free(argv);

    // WARNING! I guess there is a memory leak here.
    // Second arguemnt to 1 means to PHP: do not free memory
    // But if I put 0, I get a segmentation fault
    // So I think I do not malloc correctly for a PHP extension.
    RETURN_STRING(output, 1);
}

test.php a usage sample

<?php

dl("my_exec.so");

$output = my_exec("/bin/ls", array("-l", "/"));
var_dump($output);

shell script run those commands, of course use your own module directory

phpize
./configure
make
sudo cp modules/my_exec_extension.so /opt/local/lib/php/extensions/no-debug-non-zts-20090626/my_exec.so
KolyMac:my_fork ninsuo$ php test.php
string(329) ".DS_Store
.Spotlight-V100
.Trashes
.file
.fseventsd
.hidden
.hotfiles.btree
.vol
AppleScript
Applications
Developer
Installer Log File
Library
Microsoft Excel Documents
Microsoft Word Documents
Network
System
Users
Volumes
bin
cores
dev
etc
home
lost+found
mach_kernel
net
opt
private
sbin
tmp
usr
var
vc_command.txt
vidotask.txt"

I am not a C dev, so I think there are cleaner ways to achieve this. But you get the idea.

Thank you for this informative answer. It spans two topics about PHP : first, accept to fork a useless shell and work around security (escapeshellargs), and second how to create daemons (that part steps off the question asked). See George Cummins's bounty.

Security is a problem with any shell exec in any programming language, because most shells (sh, ash, bash, ksh, csh, tcsh, etc.) are slightly different and have different reserved characters with special meaning. Escapeshellarg() is a dangerous illusion of security. Not to mention that historically, escapeshellarg() has itself had vulnerabilities. If you don't need a shell, it's far safer not to use one. This is security 101: reduce attack surface area.

You mentioned escapeshellarg. Now we know about the 22-year-old (ref ) shellshock bug, news.ycombinator.com/item?id=8371357 says: "Sanitization is an orthogonal issue; no amount of escapeshellarg() would save someone from this issue (...)" Security requires to not introduce unneeded security holes. Calling a shell when not technically needed does just that.

performance - Run executable from php without spawning a shell - Stack...

php performance security shell
Rectangle 27 2

To run a shell command with PHP, use shell_exec. It will run as the web user (say, www for Apache), so that user will need to have appropriate permissions. You could, of course, use sudo to get around that - but that is extremely insecure and a very, very bad idea.

So if you had, e.g. a shell script called shellscript.sh sitting on yoru server, and you've written it to do whatever you need it to do, you could run it in php with:

shell_exec('sh path/to/shellscript.sh')

However - I'm not sure I correctly understand what you're trying to do, but it seems to me this is a hacky, very inefficient way to go about things, and it's very likely there is a much smoother solution. Even so, good luck.

How to execute a terminal script in Linux from a php file - Stack Over...

php linux terminal raspberry-pi
Rectangle 27 2

If you are using Z Shell, just do the following:

nano ~/.zshrc
export PATH=/Applications/MAMP/bin/php/php5.6.10/bin:$PATH
source ~/.zshrc
  • Run which php - you should get the MAMP 5.6.10 path

Laravel requires the Mcrypt PHP extension - Stack Overflow

php laravel laravel-4 mcrypt
Rectangle 27 1

First, you need to check if you need to be a root or (sudo user) for running rsync.

If yes then exec() command will only work if it is run by same user on php-cli (not on browser by Apache user). i.e. Which user you are loggined into shell for run rsync.

If it is root or any elavated permission user with sudo permission then, This rsync command may not be available to apache/www-data user which is working when php script run from browser.

So You try to make a normal user and login through it, Then try rsync if you are successful then it may be interesting to see what are other problems can be, But if you getting access/permission denied then obviously you can not run this script at-least on browser.

Besides this One more thing permission may not be directly related to rsync command itself but with folder /etc/test/ which is owned by root user in normal scenario.

Thanks for the quick reply. I guess you are right, the rsync command may not be available to apache/www-data. I try to trigger my php script from command line (e.g: php myscript.php) and this works. But when i run the php on browser, rsync don't work. Anyways thanks, i will run this with crontab and i think it will work.

php exec() rsync ssh to remote server not working - Stack Overflow

php rsync
Rectangle 27 2

To run your command as if you were the apache user, just try this in a shell:

# switch to superuser
sudo su -
# then switch to the apache user
su - www-data

You will find yourself in a quite restricted shell, from which it is usually not possible to start openoffice. Indeed, it requires a lot of environment, that would be unsafe to completely set up for apache anyway.

AFAIK, better create a dedicated user that is allowed to run your command (eg a regular "www-runner" user), then "su" to it from PHP. Other security measures include chroot'ing the dedidacted user, or using apparmor to limit what and where it is allowed to run. In any case, never let www-data run something as root by adding www-data to the sudoers: this is way too dangerous!

You can also have a look at libapache2-mod-suphp (a suid apache module to run php scripts with the owner permissions).It is easier to use than the dedicated suEXEC apache beast (http://httpd.apache.org/docs/2.0/suexec.html). The latter really is not for a quick fix ;)

PHP command not executed system(), exec() or passthru() - Stack Overfl...

php system exec passthru sudoers
Rectangle 27 0

I just Google'd for php sudo shell_exec and this came up as the #1 match:

system('echo "PASS" | sudo -u root -S COMMAND');
system('echo "mypass" | sudo -u root -S mkdir /testdir');
/testdir

@Kevin: You may have to wrap COMMAND in quotes, or (if i'm right with my *nix knowledge) you can't create a folder in /.

you cannot and should never be able to pipe a string into an interactive password prompt

shell - PHP sudo in shell_exec - Stack Overflow

php shell root sudo shell-exec
Rectangle 27 0

I have to slightly question why you are making a potentially dangerous system shell call when there are a number of good PHP zipping classes around. The tutorial here: http://www.granthinkson.com/2005/07/01/create-zip-files-dynamically-using-php/ shows how to easily create a class that will output a zip file to the browser. There is also a number of classes on phpclasses.org, this seems to be the best one: http://www.phpclasses.org/browse/package/2322.html.

If you have to do it with a system call my suggestions are:

To truncate the path for the file you could use symbolic links - See: http://en.wikipedia.org/wiki/Ln_(Unix).

Can you not increase the permissions of the zip executable to mean that apache can use it without using sudo?

shell - Zipping files through shell_exec in PHP: problem with paths - ...

php shell zip sudo shell-exec
Rectangle 27 0

chdir()
chdir('/home/backups/test1/');

PHP uses the apache user which doesn't have access to the directory with the user's files. I use "sudo zip ..." to perform the compression, but "sudo cd /path/to/folder" can't be done.

shell - Zipping files through shell_exec in PHP: problem with paths - ...

php shell zip sudo shell-exec
Rectangle 27 0

It seems like a simple option, but according to man it's just not there. You could of course symlink the stuff you want to archive in the location where you'll create the zip file (I assume you can actually cd to that directory). With the proper options, zip will not archive the symlinks themselves but the symlinked files instead, using the names of the symlink.

E.g. create symlinks /tmp/$PID/my pics/pic1.jpg etc, and then zip everything in /tmp/$PID.

shell - Zipping files through shell_exec in PHP: problem with paths - ...

php shell zip sudo shell-exec
Rectangle 27 0

First of all, the password must be cryptographed, like this:

/etc/newserver.sh /home/testuser testuser "$6$VP9.GI9D$nVjpXIlgoTCLYICNW9ijPqg07opPrjTU2ilYULaT4rut8S9CAmWggMXuOhJ27C5ltwCRfzSxEVgSlReA2i/rH1"

And I think, you should use "-c" as argument.

shell_exec("sudo sh -c \"/etc/newserver.sh /home/testuser testuser testpass\"");

I know about the cryptographic passwords, the command should still run and the account should still be valid apart from not being able to access it through its password, im using this as nothing but a test bed, i have a different set of parameters to be passed from the PHP including a encrypted password. ill try using -c now.

You checked the permission of execution of file? chmod u+x /etc/newserver.sh

Executing in a web environment does not provide a standard input stream to requests (HTTP data being not-included of course). sudo naturally prompts for a password, which you fail to provide 3 times automatically : permission denied.

linux - Run shell script with sudo from PHP - Stack Overflow

php linux bash shell
Rectangle 27 0

Sudo normally requires an interactive shell to enter your password. That's obviously not going to happen in a PHP script. If you're sure you know what you're doing and you've got your security issues covered, try allowing the Apache user to run sudo without a password, but only for certain commands.

For example, adding the following line in your sudoers file will allow Apache to run sudo without a password, only for the gzip command.

nobody ALL=NOPASSWD: gzip
  • There might still be complications due to the way PHP calls shell commands.
  • Remember that it's very risky to allow the web server to run commands as root!

Write a shell script with the suid bit to make it run as root no matter who calls it.

Probably a better alternative:

Write the commands to a queue and have cron pick them up, validate them (only allow known good requests), and run them, then mark that queue complete with the date and result.

Your end-user can then click/wait for update using ajax.

@MahavirMunot which option did you tried? i think last option will be better option. where you will be running the shell script which can do all the GZIPs for you

yes shell script also generated the files with user as nobody. still no progress

did you run shell script as desert user ?

:Yes, I tried it for temporary purpose but it still did not work.

Don't want php script to execute the shell command as apache user when...

php apache shell unix webserver
Rectangle 27 0

Edit the sudoers file (with visudo) and add a rule that allows the web server user to run the command without a password. For example:

www-data ALL=NOPASSWD: /path/to/script

bash - How to call shell script from php that requires SUDO? - Stack O...

php bash sudo shellexecute
Rectangle 27 0

sudoers
username ALL=NOPASSWD: /path/to/script

This will allow that particular user to call sudo on that particular script without being prompted for a password.

This makes sense but one part confuses me... Where exactly do I put the line username ALL=NOPASSWD: /path/to/script What is my sudoers file?

In /etc/sudoers, though you usually use the visudo command to edit that file.

bash - How to call shell script from php that requires SUDO? - Stack O...

php bash sudo shellexecute
Rectangle 27 0

As requested, here's the python server...

#!/usr/bin/python
import os 
import socket
print "  Loading Bindings..."
settings = {}
line = 0 
for each in open('/path/to/actions.txt', 'r'):
 line = line + 1
  each = each.rstrip()
  if each <> "":
    if each[0] <> '#':
      a = each.partition(':')
      if a[2]:
        settings[a[0]] = a[2]
      else:
        print "    Err @ line",line,":",each
print "  Starting Server...",
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", port))
print "OK."
print "  Listening on port:", port
while True:
    datagram = s.recv(1024)
    if not datagram:
        break
    print "Rx Cmd:", datagram
    if settings.has_key(datagram):
      print "Launch:", settings[datagram]
      os.system(settings[datagram]+" &")
s.close()

The config file "actions.txt" uses the format "action-name:corresponding-shell-command" i.e.

# Hash denotes a comment
webroot:nautilus /var/www
ftp:filezilla
edit_homepage:gedit /var/www/homepage/htdocs/index.php

This code doesn't check the originating IP of the incoming UDP packets as I have it running on localhost, I am firewalled of from anyone else and checking would provide no protection against spoofing anyway.

I don't have time to rewrite it to use TCP/IP but Python is a language that's worth getting to know so if you really want that functionality I'll leave it to you to have a google for 'Python' and 'SOCK_STREAM'. It's probably not worth your trouble though, it's easier to configure your firewall so that no spoofed localhost packets can get through and modify the code to make sure it only listens to packets from the loopback.

scripting - How can I have a PHP script run a shell script as root? - ...

php scripting permissions shell sudo
Rectangle 27 0

The problem is, that you run the script on a terminal as a user that probably has the sudo rights, whereas the apache/webserver user doesn't, so the $ret_val (which is actually just a status code) is set to 1 (means error).

try var_dump($out); in both cases to see the results of your exec call. To do this kind of thing from the browser, you might want to look into proc_open and family, or have a script that is chmod'ed to 777, so the apache user can run it, too. Let that script then call the actual shell script and return it's output back. This is, however very dangerous, and should only be used for testing environments on your own machine. Never do this in production environments!

I have posted a couple of questions here, too that might prove informative:

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

php to call a shell script - Stack Overflow

php shell sudo minecraft
Rectangle 27 0

You don't want to give Apache root.

There's another solution to your problem. The problem is that Apache cannot kill the process because it is owned by root. What you can do is change the owner to 'www-data' which is what Apache is identified as.

If the process is a service and starts up on boot you can add the

sudo -u www-data <start-up script>

so that www-data would be the owner of that process and hence running the shut-down script would work.

scripting - How can I have a PHP script run a shell script as root? - ...

php scripting permissions shell sudo