fifo_server.php

00001 #!/usr/bin/php4 -q
00002 <?php
00003 // $Id$
00004 // fifo_server.php - fifo/internet relay
00005 
00006 
00007 /*
00008  * Copyright (C) 2004 Juha Heinanen
00009  *
00010  * This file is part of ser, a free SIP server.
00011  *
00012  * ser is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version
00016  *
00017  * For a license to use the ser software under conditions
00018  * other than those described here, or to purchase support for this
00019  * software, please contact iptel.org by e-mail at the following addresses:
00020  *    info@iptel.org
00021  *
00022  * ser is distributed in the hope that it will be useful,
00023  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025  * GNU General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License 
00028  * along with this program; if not, write to the Free Software 
00029  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00030  *
00031  * History:
00032  * -------
00033  * 2004-06-11: fifo_server.php introduced as interim solution
00034  *             until ser fifo natively supports tcp/ip access
00035  */
00036 
00037 error_reporting (E_ALL);
00038 
00039 require("/etc/ser/fifo_server.cfg");
00040 
00041 /* Check if fifo server is needed */
00042 if ($fifo_server_address == NULL) {
00043   return;
00044 }
00045 
00046 /* Allow the script to hang around waiting for connections. */
00047 set_time_limit (0);
00048 
00049 /* Turn on implicit output flushing so we see what we're getting
00050  * as it comes in. */
00051 ob_implicit_flush ();
00052 
00053 $fifo_clients = "/etc/ser/fifo_server.clients";
00054 
00055 global $mtime, $clients;
00056 $mtime = 0;
00057 
00058 function fifo_allow($fifo_clients, $addr) {
00059   global $mtime, $clients;
00060   $long_addr = ip2long($addr);
00061   if (!file_exists($fifo_clients)) {
00062     echo "fifo_server.clients file does not exist!\n";
00063     return FALSE;
00064   }
00065   clearstatcache();
00066   $cur_mtime = filemtime($fifo_clients);
00067   if ($cur_mtime > $mtime) {
00068     $fd = fopen($fifo_clients, "r");
00069     if ($fd == FALSE) {
00070       echo "Cannot open fifo.clients file!\n";
00071       return FALSE;
00072     }
00073     $clients = array();
00074     while (!feof ($fd)) {
00075       $client = ip2long(fgets($fd, 4096));
00076       if ($client != -1) {
00077         $clients[] = $client;
00078       }
00079     }
00080     fclose ($fd);
00081     $mtime = $cur_mtime;
00082   }
00083   return in_array($long_addr, $clients, TRUE);
00084 }
00085 
00086 if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
00087     echo "socket_create() failed: " . socket_strerror ($sock) . "\n";
00088     return;
00089 }
00090 
00091 if (($ret = socket_bind($sock, $fifo_server_address, $fifo_server_port)) < 0) {
00092   echo "socket_bind() failed: " . socket_strerror ($ret) . "\n";
00093   socket_close($sock);
00094   return;
00095 }
00096 
00097 if (($ret = socket_listen ($sock, 5)) < 0) {
00098   echo "socket_listen() failed: " . socket_strerror ($ret) . "\n";
00099   socket_close($sock);
00100   return;
00101 }
00102 
00103 do {
00104   if (($msgsock = socket_accept($sock)) < 0) {
00105     echo "socket_accept() failed: ".socket_strerror($msgsock)."\n";
00106     socket_close($msgsock);
00107     continue;
00108   }
00109 
00110   socket_getpeername($msgsock, $addr);
00111 
00112   if (!fifo_allow($fifo_clients, $addr)) {
00113     $msg = "403 Forbidden\n";
00114     socket_write($msgsock, $msg, strlen($msg));
00115     socket_shutdown($msgsock);
00116     socket_close($msgsock);
00117     continue;
00118   }
00119       
00120   if (FALSE === ($fifo_cmd = socket_read ($msgsock, 8192, PHP_BINARY_READ))) {
00121     echo "socket_read() failed: ".socket_strerror(socket_last_error($msgsock))."\n";
00122     socket_shutdown($msgsock);
00123     socket_close($msgsock);
00124     continue;
00125   }
00126 
00127   $fifo_reply_file_name = "ser_fifo_reply_".rand();
00128   $fifo_reply_file = "/tmp/".$fifo_reply_file_name;
00129 
00130   $fifo_cmd = str_replace("REPLY_FILE_NAME", $fifo_reply_file_name, $fifo_cmd);
00131   
00132   /* add command separator */
00133   $fifo_cmd=$fifo_cmd."\n";
00134   
00135   $fifo_handle=fopen( "/tmp/ser_fifo", "w" );
00136   if (!$fifo_handle) {
00137     $msg = "sorry -- cannot open write fifo";
00138     socket_write($msgsock, $msg, strlen($msg));
00139     socket_shutdown($msgsock);
00140     socket_close($msgsock);
00141     continue;
00142   }
00143   
00144   /* create fifo for replies */
00145   @system("mkfifo -m 666 ".$fifo_reply_file);
00146 
00147   /* write fifo command */
00148   if (fwrite( $fifo_handle, $fifo_cmd) == -1) {
00149     @unlink($fifo_reply_file);
00150     @fclose($fifo_handle);
00151     $msg = "sorry -- fifo writing error";
00152     socket_write($msgsock, $msg, strlen($msg));
00153     socket_shutdown($msgsock);
00154     socket_close($msgsock);
00155     continue;
00156   }
00157 
00158   @fclose($fifo_handle);
00159   
00160   /* read output now */
00161   @$fp = fopen($fifo_reply_file, "r");
00162   if (!$fp) {
00163     @unlink($fifo_reply_file);
00164     $msg = "sorry -- reply fifo opening error";
00165     socket_write($msgsock, $msg, strlen($msg));
00166     socket_shutdown($msgsock);
00167     socket_close($msgsock);
00168     continue;
00169   }
00170   $status = fgetS($fp, 256);
00171   
00172   if (!$status) {
00173     @unlink($fifo_reply_file);
00174     $msg = "sorry -- reply fifo reading error";
00175     socket_write($msgsock, $msg, strlen($msg));
00176     socket_shutdown($msgsock);
00177     socket_close($msgsock);
00178     continue;
00179   }
00180   
00181   socket_write($msgsock, $status, strlen($status));
00182   
00183   $rest = fread($fp, 8192);
00184   @unlink($fifo_reply_file);
00185 
00186   socket_write($msgsock, $rest, strlen($rest));
00187 
00188   socket_close ($msgsock);
00189 
00190 } while (true);
00191 
00192 socket_close ($sock);
00193 
00194 ?>