1

Here I get a querystring depending upon the value from the command shell_exec().

I am receiving a value the querystring but shell_exec() is not working.

I am using a webcam connected to Raspberry pi 3 so the command in shell_exec() is to turn the webcam ON an OFF.

$output=shell_exec('sudo /etc/init.d/motion start')

code:

<?php
$status=$_GET['status'];
if($status == 'on')
{
    $output=shell_exec('sudo /etc/init.d/motion start');
}
if($status == 'off')
{
    $output=shell_exec('sudo /etc/init.d/motion start');
}

How do I solve the execution issue?

111---
  • 4,516
  • 3
  • 30
  • 52
Ann
  • 21
  • 2
    To run sudo without a password, you will need to ensure that the user running the web daemon i.e. apache is correctly configured to run the command without a password and without a tty. – Raman Sailopal Apr 04 '18 at 12:14
  • 1
    Also, you might have a typo in your code, because both of your conditions do the same thing: send start command to /etc/init.d/motion. I guess the first one should send stop. Also, can you give us your error ? We can't help you if we don't know what the crash is. – Carpette Apr 04 '18 at 13:10

1 Answers1

0

instead of trying to give apache/http user sudo rights, usual way is to create suid wrapper binary like this:

$ sudo gcc -o suidmotion -xc - <<cEnd
#include "stdlib.h"
#include "string.h"
int main(int argc, char *argv[])
{  if (argc ==2) 
   {  if (!strcmp(argv[1], "start")) system("/etc/init.d/motion start");
      if (!strcmp(argv[1], "stop" )) system("/etc/init.d/motion stop");
   }
}
cEnd
$ sudo chmod +s suidmotion

... and than in php to call shell_exec("/path/to/suidmotion start/stop");

sudo gcc will produce output binary suidmotion owned by root:root, and chmod +s ensures that binary rights up on execution will be escalated to binary owner (root), regardless who run it.