| 1 |
#!/usr/bin/php4 -q |
| 2 |
<?php |
| 3 |
// $Id$ |
| 4 |
// fifo_server.php - fifo/internet relay |
| 5 |
|
| 6 |
|
| 7 |
/* |
| 8 |
* Copyright (C) 2004 Juha Heinanen |
| 9 |
* |
| 10 |
* This file is part of ser, a free SIP server. |
| 11 |
* |
| 12 |
* ser is free software; you can redistribute it and/or modify |
| 13 |
* it under the terms of the GNU General Public License as published by |
| 14 |
* the Free Software Foundation; either version 2 of the License, or |
| 15 |
* (at your option) any later version |
| 16 |
* |
| 17 |
* For a license to use the ser software under conditions |
| 18 |
* other than those described here, or to purchase support for this |
| 19 |
* software, please contact iptel.org by e-mail at the following addresses: |
| 20 |
* [email protected] |
| 21 |
* |
| 22 |
* ser is distributed in the hope that it will be useful, |
| 23 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 |
* GNU General Public License for more details. |
| 26 |
* |
| 27 |
* You should have received a copy of the GNU General Public License |
| 28 |
* along with this program; if not, write to the Free Software |
| 29 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 30 |
* |
| 31 |
* History: |
| 32 |
* ------- |
| 33 |
* 2004-06-11: fifo_server.php introduced as interim solution |
| 34 |
* until ser fifo natively supports tcp/ip access |
| 35 |
*/ |
| 36 |
|
| 37 |
error_reporting (E_ALL); |
| 38 |
|
| 39 |
require("/etc/ser/fifo_server.cfg"); |
| 40 |
|
| 41 |
/* Check if fifo server is needed */ |
| 42 |
if ($fifo_server_address == NULL) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
/* Allow the script to hang around waiting for connections. */ |
| 47 |
set_time_limit (0); |
| 48 |
|
| 49 |
/* Turn on implicit output flushing so we see what we're getting |
| 50 |
* as it comes in. */ |
| 51 |
ob_implicit_flush (); |
| 52 |
|
| 53 |
$fifo_clients = "/etc/ser/fifo_server.clients"; |
| 54 |
|
| 55 |
global $mtime, $clients; |
| 56 |
$mtime = 0; |
| 57 |
|
| 58 |
function fifo_allow($fifo_clients, $addr) { |
| 59 |
global $mtime, $clients; |
| 60 |
$long_addr = ip2long($addr); |
| 61 |
if (!file_exists($fifo_clients)) { |
| 62 |
echo "fifo_server.clients file does not exist!\n"; |
| 63 |
return FALSE; |
| 64 |
} |
| 65 |
clearstatcache(); |
| 66 |
$cur_mtime = filemtime($fifo_clients); |
| 67 |
if ($cur_mtime > $mtime) { |
| 68 |
$fd = fopen($fifo_clients, "r"); |
| 69 |
if ($fd == FALSE) { |
| 70 |
echo "Cannot open fifo.clients file!\n"; |
| 71 |
return FALSE; |
| 72 |
} |
| 73 |
$clients = array(); |
| 74 |
while (!feof ($fd)) { |
| 75 |
$client = ip2long(fgets($fd, 4096)); |
| 76 |
if ($client != -1) { |
| 77 |
$clients[] = $client; |
| 78 |
} |
| 79 |
} |
| 80 |
fclose ($fd); |
| 81 |
$mtime = $cur_mtime; |
| 82 |
} |
| 83 |
return in_array($long_addr, $clients, TRUE); |
| 84 |
} |
| 85 |
|
| 86 |
if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) { |
| 87 |
echo "socket_create() failed: " . socket_strerror ($sock) . "\n"; |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
if (($ret = socket_bind($sock, $fifo_server_address, $fifo_server_port)) < 0) { |
| 92 |
echo "socket_bind() failed: " . socket_strerror ($ret) . "\n"; |
| 93 |
socket_close($sock); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
if (($ret = socket_listen ($sock, 5)) < 0) { |
| 98 |
echo "socket_listen() failed: " . socket_strerror ($ret) . "\n"; |
| 99 |
socket_close($sock); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
do { |
| 104 |
if (($msgsock = socket_accept($sock)) < 0) { |
| 105 |
echo "socket_accept() failed: ".socket_strerror($msgsock)."\n"; |
| 106 |
socket_close($msgsock); |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
socket_getpeername($msgsock, $addr); |
| 111 |
|
| 112 |
if (!fifo_allow($fifo_clients, $addr)) { |
| 113 |
$msg = "403 Forbidden\n"; |
| 114 |
socket_write($msgsock, $msg, strlen($msg)); |
| 115 |
socket_shutdown($msgsock); |
| 116 |
socket_close($msgsock); |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
if (FALSE === ($fifo_cmd = socket_read ($msgsock, 8192, PHP_BINARY_READ))) { |
| 121 |
echo "socket_read() failed: ".socket_strerror(socket_last_error($msgsock))."\n"; |
| 122 |
socket_shutdown($msgsock); |
| 123 |
socket_close($msgsock); |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
$fifo_reply_file_name = "ser_fifo_reply_".rand(); |
| 128 |
$fifo_reply_file = "/tmp/".$fifo_reply_file_name; |
| 129 |
|
| 130 |
$fifo_cmd = str_replace("REPLY_FILE_NAME", $fifo_reply_file_name, $fifo_cmd); |
| 131 |
|
| 132 |
/* add command separator */ |
| 133 |
$fifo_cmd=$fifo_cmd."\n"; |
| 134 |
|
| 135 |
$fifo_handle=fopen( "/tmp/ser_fifo", "w" ); |
| 136 |
if (!$fifo_handle) { |
| 137 |
$msg = "sorry -- cannot open write fifo"; |
| 138 |
socket_write($msgsock, $msg, strlen($msg)); |
| 139 |
socket_shutdown($msgsock); |
| 140 |
socket_close($msgsock); |
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
/* create fifo for replies */ |
| 145 |
@system("mkfifo -m 666 ".$fifo_reply_file); |
| 146 |
|
| 147 |
/* write fifo command */ |
| 148 |
if (fwrite( $fifo_handle, $fifo_cmd) == -1) { |
| 149 |
@unlink($fifo_reply_file); |
| 150 |
@fclose($fifo_handle); |
| 151 |
$msg = "sorry -- fifo writing error"; |
| 152 |
socket_write($msgsock, $msg, strlen($msg)); |
| 153 |
socket_shutdown($msgsock); |
| 154 |
socket_close($msgsock); |
| 155 |
continue; |
| 156 |
} |
| 157 |
|
| 158 |
@fclose($fifo_handle); |
| 159 |
|
| 160 |
/* read output now */ |
| 161 |
@$fp = fopen($fifo_reply_file, "r"); |
| 162 |
if (!$fp) { |
| 163 |
@unlink($fifo_reply_file); |
| 164 |
$msg = "sorry -- reply fifo opening error"; |
| 165 |
socket_write($msgsock, $msg, strlen($msg)); |
| 166 |
socket_shutdown($msgsock); |
| 167 |
socket_close($msgsock); |
| 168 |
continue; |
| 169 |
} |
| 170 |
$status = fgetS($fp, 256); |
| 171 |
|
| 172 |
if (!$status) { |
| 173 |
@unlink($fifo_reply_file); |
| 174 |
$msg = "sorry -- reply fifo reading error"; |
| 175 |
socket_write($msgsock, $msg, strlen($msg)); |
| 176 |
socket_shutdown($msgsock); |
| 177 |
socket_close($msgsock); |
| 178 |
continue; |
| 179 |
} |
| 180 |
|
| 181 |
socket_write($msgsock, $status, strlen($status)); |
| 182 |
|
| 183 |
$rest = fread($fp, 8192); |
| 184 |
@unlink($fifo_reply_file); |
| 185 |
|
| 186 |
socket_write($msgsock, $rest, strlen($rest)); |
| 187 |
|
| 188 |
socket_close ($msgsock); |
| 189 |
|
| 190 |
} while (true); |
| 191 |
|
| 192 |
socket_close ($sock); |
| 193 |
|
| 194 |
?> |