note 74922 added to function.socket-create

From: Date: Fri, 04 May 2007 05:45:01 +0000
Subject: note 74922 added to function.socket-create
Groups: php.notes 
Request: Send a blank email to php-notes+get-126189@lists.php.net to get a copy of this message
When running these types of sockets, typically you need to be root or else they fail (not the case with stream sockets: http://us.php.net/manual/en/function.stream-socket-server.php) This is an example for Linux/Unix type systems to use sockets without being root. (tested on Debian and CentOS) <?php $user = "daemon"; $script_name = "uid"; //the name of this script ///////////////////////////////////////////// //try creating a socket as a user other than root echo "\n__________________________________________\n"; echo "Trying to start a socket as user $user\n"; $uid_name = posix_getpwnam($user); $uid_name = $uid_name['uid']; if(posix_seteuid($uid_name)) { echo "SUCCESS: You are now $user!\n"; if($socket = @socket_create(AF_INET, SOCK_RAW, 1)) { echo "SUCCESS: You are NOT root and created a socket! This should not happen!\n"; } else { echo "ERROR: socket_create() failed because you're not root!\n"; } $show_process = shell_exec("ps aux | grep -v grep | grep $script_name"); echo "Current process stats::-->\t $show_process"; } else { exit("ERROR: seteuid($uid_name) failed!\n"); } ///////////////////////////////////////////// //no try creating a socket as root echo "\n__________________________________________\n"; echo "Trying to start a socket as user 'root'\n"; if(posix_seteuid(0)) { echo "SUCCESS: You are now root!\n"; $show_process = shell_exec("ps aux | grep -v grep | grep $script_name"); echo "Current process stats::-->\t $show_process"; if($socket = @socket_create(AF_INET, SOCK_RAW, 1)) { echo "SUCCESS: You created a socket as root and now should seteuid() to another user\n"; ///////////////////////////////////////// //now modify the socket as another user echo "\n__________________________________________\n"; echo "Switching to user $user\n"; if(posix_seteuid($uid_name)) { echo "SUCCESS: You are now $user!\n"; if(socket_bind($socket, 0, 8000)) { echo "SUCCESS: socket_bind() worked as $user!\n"; } else { echo "ERROR: Must be root to user socket_bind()\n"; } $show_process = shell_exec("ps aux | grep -v grep | grep $script_name"); echo "Current process stats::-->\t $show_process"; socket_close($socket); //hard to error check but it does close as this user echo "SUCCESS: You closed the socket as user $user!\n"; } else { echo "ERROR: seteuid($uid_name) failed while socket was open!\n"; } } else { echo "ERROR: Socket failed for some reason!\n"; } } else { exit("ERROR: Changing to root failed!\n"); } ?> ---- Server IP: 69.39.81.135 Probable Submitter: 69.239.205.208 ---- Manual Page -- http://www.php.net/manual/en/function.socket-create.php Edit -- https://master.php.net/note/edit/74922 Del: integrated -- https://master.php.net/note/delete/74922/integrated Del: useless -- https://master.php.net/note/delete/74922/useless Del: bad code -- https://master.php.net/note/delete/74922/bad+code Del: spam -- https://master.php.net/note/delete/74922/spam Del: non-english -- https://master.php.net/note/delete/74922/non-english Del: in docs -- https://master.php.net/note/delete/74922/in+docs Del: other reasons-- https://master.php.net/note/delete/74922 Reject -- https://master.php.net/note/reject/74922 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126189) next »