cankko
cankko

Reputation: 29

Permission error by execute bash script via php shell_exec

I try to insert line to /etc/ppp/chap-secret file via bash script what should run with php shell_exec.

I hope that I am on right way or is there a better way? whatever my work is like below,

/var/www/test.php:

<?php echo shell_exec("cd /etc/ppp; bash test.sh"); ?>

/etc/ppp/test.sh:

#!/bin/bash
sed -i "/IP addresses/a client123* pw123123 192.168.0.101" chap-secrets

I also added www-data ALL=NOPASSWD: /etc/ppp/test.sh to sudoers.

I get this error:

sed: couldn't open temporary file ./sedXym2Nn: Permission denied

from terminal all works fine, but I need it from admin web via button click.

How to fix permissions error and get this process?

Upvotes: 2

Views: 640

Answers (1)

gortsu
gortsu

Reputation: 126

Granting a web server access to system files sure seems reckless, but at least you are using a wrapper script to prevent the server from running arbitrary commands with super user privileges. That being said:

You have given www-data sudo access to /etc/ppp/test.sh without password, but you are not executing the command with sudo from your shell_exec function.

Calling shell_exec("cd /etc/ppp; sudo bash test.sh"); should do the trick.

Upvotes: 1

Related Questions