asterisk CLI with non root user

1.2k Views Asked by At

I'm developing a web application to manages my asterisk server ( adding extension, adding contexts managing clients, etc.. ) I'm using PHP, Mysql to do that, I used the database to add clients but for an extension, I still use the file extensions.conf. let's suppose that I added many extensions to the extensions.conf file from the web app, next I should reload the dialplan, the only way I know is by typing this command into the Asterisk CLI:

dialplan reload

so if I want to reload the dialplan from the web app I should run somewhere :

shell_exec ( "asterisk -rx 'dialplan reload'" );

the problem is this command asterisk -rx needs root privileges. is there a way to let the apache user run this command.

Edite :

this is the content of my /etc/asterisk/asterisk.conf file

[directories](!)
astetcdir => /etc/asterisk
astmoddir => /usr/lib/asterisk/modules
astvarlibdir => /var/lib/asterisk
astdbdir => /var/lib/asterisk
astkeydir => /var/lib/asterisk
astdatadir => /var/lib/asterisk
astagidir => /var/lib/asterisk/agi-bin
astspooldir => /var/spool/asterisk
[options]
astrundir => /var/run/asterisk
astlogdir => /var/log/asterisk
astsbindir => /usr/sbin
runuser = asterisk ; The user to run as. The default is root.
rungroup = asterisk ; The group to run as. The default is root
2

There are 2 best solutions below

6
arheops On BEST ANSWER

Your command NOT need root privileges.

Check /etc/asterisk/asterisk.conf for file and permissions used.

1
Ron On

You could use sudoers (not recommended) to allow for execution of specific user to run:

sudo asterisk -rx 'dialplan reload'

and you can call that from your user, although this is dangerous if that is your web user...

Or you can use AMI

To execute a command via Asterisk Manager Interface (AMI) you can create yourself a user in manager.conf and then from PHP use a script to auth and execute a command. Example:

<?php
    $socket = fsockopen("127.0.0.1","5038", $errno, $errstr, 10);
    if (!$socket){
        echo "$errstr ($errno)\n";
    }else{
        fputs($socket, "Action: Login\r\n");
        fputs($socket, "UserName: myusername\r\n");
        fputs($socket, "Secret: mypassword\r\n\r\n");

        fputs($socket, "Action: Command\r\n");
        fputs($socket, "Command: dialplan reload\r\n\r\n");

        fputs($socket, "Action: Logoff\r\n\r\n");
        while (!feof($socket)){
            echo fgets($socket).'<br>';
        }
        fclose($socket);
    }
?>