5

Is it enough to do a

sudo setcap cap_net_raw=eip /usr/bin/php5

to be able to use RAW SOCKETS in PHP (not CLI) in Linux ?

If yes, well it is not working (but started to work in CLI but not using Apache)

So I guess I have to give those permissions to Apache as well, but I couldn't find out how.

Can you help me?

errors from the PHP script : Warning: socket_create(): Unable to create socket [1]: Operation not permitted in

the php script :

$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
        $socket = socket_create(AF_INET, SOCK_RAW, 1);
        if ($socket !== false) {
            socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 10, 'usec' => 0));
            echo 'Creating PING Socket !';

            socket_connect($socket, gethostbyname('noczone.com'), null);
            socket_send($socket, $package, strLen($package), 0);
            if (socket_read($socket, 255)) {
                $result = microtime(true) - $ts;
            } else {
                echo 'Error Code : No PING';
            }
            socket_close($socket);
        } else {
            echo 'Failed Creating PING Socket !';
        }
4
  • First of all, you need to ensure that your PHP has been compiled with sockets php.net/manual/en/sockets.installation.php If it has been, then great. What errors do you get? Make sure you turn error reporting on: display_errors('On'); ini_set('error_reporting', 'E_ALL'); and then rerun the script. Could you update your question with the errors you get?
    – DarkMantis
    Nov 7, 2013 at 14:59
  • Warning: socket_create(): Unable to create socket [1]: Operation not permitted in
    – TeAmEr
    Nov 14, 2013 at 20:54
  • Do you have an functions disabled in your php.ini, also is safe_mode off ?
    – DarkMantis
    Nov 15, 2013 at 10:32
  • no functions disabled (i am using socket_create for a tcp connection and it's fine). safe_mode is off
    – TeAmEr
    Nov 19, 2013 at 12:41

2 Answers 2

3
+25

Have you tried: sudo setcap cap_net_raw=eip $(which httpd) ?

What's the error you're getting?

8
  • root@exclulist:/usr/sbin# sudo setcap cap_net_raw=eip $(which httpd) usage: setcap [-q] [-v] (-r|-|<caps>) <filename> [ ... (-r|-|<capsN>) <filenameN> ] Note <filename> must be a regular (non-symlink) file. root@exclulist:/usr/sbin# sudo setcap cap_net_raw=eip $(which apache2) Failed to set capabilities on file '/usr/sbin/apache2' (Invalid argument) The value of the capability argument is not permitted for a file. Or the file is not a regular (non-symlink) file
    – TeAmEr
    Nov 14, 2013 at 20:17
  • setcap cap_net_bind_service=+ep /usr/sbin/httpd What are you trying to do again?
    – dorphalsig
    Nov 15, 2013 at 17:32
  • i am trying to make a ping script in PHP and call it via apache (browser), code in the question above
    – TeAmEr
    Nov 19, 2013 at 12:39
  • And you can't use fsock_open()?
    – dorphalsig
    Nov 19, 2013 at 13:24
  • as far as i know fsock_open doesn't support raw sockets which are required to create a raw ICMP packet for ping (i don't want to connect to HTTP or any other service)
    – TeAmEr
    Nov 19, 2013 at 13:37
2

Most likely the PHP interpreter is implemented as a module in Apache (you can see this from php_sapi_name() or the PHP_SAPI constant in PHP, or from the httpd ini file).

In this case you need to give the httpd executable the capability.

1
  • it gave me apache2handler can't find it
    – TeAmEr
    Nov 14, 2013 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.