]> code.citadel.org Git - citadel.git/blobdiff - ctdlphp/sessionproxy.php
* Allow the session proxy to time out after 15 minutes.
[citadel.git] / ctdlphp / sessionproxy.php
index d9977a636a44bfe3cf7521607d2eea8829f93d37..a9e627e8770ead45575da98f20a266d059d3dbdf 100755 (executable)
@@ -58,26 +58,29 @@ if ($sockname == "/tmp/") {
 system("/bin/rm -f " . $sockname);
 
 $sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
-if ($sock < 0) {
+if (!$sock) {
        echo "socket_create() failed: ", socket_strerror($sock), "\n";
        system("/bin/rm -f " . $sockname);
        exit(2);
 }
 
 $ret = socket_bind($sock, $sockname);
-if ($ret < 0) {
+if (!$ret) {
        echo "socket_bind() failed: ", socket_strerror($ret), "\n";
        system("/bin/rm -f " . $sockname);
        exit(3);
 }
 
 $ret = socket_listen($sock, 5);
-if ($ret < 0) {
+if (!$ret) {
        echo "socket_listen() failed: ", socket_strerror($ret), "\n";
        system("/bin/rm -f " . $sockname);
        exit(4);
 }
 
+// Set the permissions so someone else doesn't jump into our connection.
+chmod($sockname, 0600);
+
 // We need to get a connection to the Citadel server going now.
 
 $ctdlsock = fsockopen("uncensored.citadel.org", 504, $errno, $errstr, 30);
@@ -103,13 +106,49 @@ if (substr($buf, 0, 1) != "2") {
 }
 
 do {
+       // Wait for connections, but time out after 15 minutes.
+       if (socket_select($readsock = array($sock),
+                       $writesock = NULL,
+                       $exceptsock = NULL,
+                       900, 0
+       ) == 0) {
+               // Timing out.
+               socket_close ($sock);
+               system("/bin/rm -f " . $sockname);
+               exit(8);
+       }
+
+       // Ok, there's a valid connection coming in.  Accept it.
        $msgsock = socket_accept($sock);
        if ($msgsock >= 0) do {
                $buf = sock_gets($msgsock);
                if ($buf !== false) {
-                       fwrite($ctdlsock, $buf . "\n", (strlen($buf)+1) );
+                       if (!fwrite($ctdlsock, $buf . "\n")) {
+                               fclose($ctdlsock);
+                               socket_close($sock);
+                               system("/bin/rm -f " . $sockname);
+                               exit(9);
+                       }
                        $talkback = fgets($ctdlsock, 4096);
+                       if (!$talkback) {
+                               fclose($ctdlsock);
+                               socket_close($sock);
+                               system("/bin/rm -f " . $sockname);
+                               exit(10);
+                       }
                        socket_write($msgsock, $talkback, strlen($talkback));
+
+                       if (substr($talkback, 0, 1) == "1") do {
+                               $buf = fgets($ctdlsock, 4096);
+                               if (!$buf) {
+                                       $buf = "000\n" ;
+                               }
+                               else {
+                                       socket_write($msgsock, $buf,
+                                               strlen($buf));
+                               }
+                       } while ($buf != "000\n");
+
                }
        } while($buf !== false);
 
@@ -118,7 +157,7 @@ do {
 } while (true);
 
 socket_close($sock);
-socket_close($ctdlsock);
+fclose($ctdlsock);
 system("/bin/rm -f " . $sockname);
 exit(0);