# FastCGI unix sockets support patch

- Thread: https://externals.io/message/22531
- Original archive: https://news-web.php.net/php.internals/22531
- Started by Rostislav Krasny on 2006-03-20T08:46:02+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #22531 — Rostislav Krasny — 2006-03-20T08:46:02+00:00

URL: https://externals.io/message/22531#22531

Hi,

According to a 'php -h' output of php-cgi 5.1.2 the FastCGI can be used
only by TCP/IP:

  -b <address:port>|<port> Bind Path for external FASTCGI Server mode

But according to the code FastCGI could also be used by UNIX sockets.
According to the code if the "-b <address:port>" option is used and the
port number is illegal or omitted then all the "address:port" string
assumed as a UNIX socket name. That means that the UNIX socket should
allways include a ":" in its filename. My patch eliminate this limitation.
After applying it one should use "-b socket:" for UNIX sockets and
"-b address:port" or "-b port" for TCP/IP. And if the port number is
illegal, an error message is printed. If the "-b socket:" format is used
the ":" is not added to the UNIX socket filename.

What do you think about following patch?

--- sapi/cgi/libfcgi/os_unix.c.orig	Sun Dec  7 14:59:54 2003
+++ sapi/cgi/libfcgi/os_unix.c	Mon Mar 20 09:28:28 2006
@@ -299,13 +299,15 @@
     char    host[MAXPATHLEN];
 
     strlcpy(host, bindPath, MAXPATHLEN-1);
-    if((tp = strchr(host, ':')) != 0) {
+    if((tp = strrchr(host, ':')) != 0) {
 	*tp++ = 0;
-	if((port = atoi(tp)) == 0) {
-	    *--tp = ':';
-	 } else {
+	if (*tp != '\0') {
+	    if((port = atoi(tp)) == 0) {
+		fprintf(stderr, "%s is illegal port number!\n", tp);
+		return -1;
+	    }
 	    tcp = TRUE;
-	 }
+	}
     }
     if(tcp) {
       if (!*host || !strcmp(host,"*")) {
@@ -357,7 +359,7 @@
 	servLen = sizeof(sa.inetVariant);
     } else {
 	unlink(bindPath);
-	if(OS_BuildSockAddrUn(bindPath, &sa.unixVariant, &servLen)) {
+	if(OS_BuildSockAddrUn(host, &sa.unixVariant, &servLen)) {
 	    fprintf(stderr, "Listening socket's path name is too long.\n");
 	    return -1;
 	}


This patch changes an OS_CreateLocalIpcFd() function. The same change
of OS_FcgiConnect() should probably be done as well. I didn't because
the OS_FcgiConnect() isn't used anywhere by php.

---

## #22535 — Dmitry Stogov — 2006-03-20T10:44:30+00:00

URL: https://externals.io/message/22531#22535 · In reply to: #22531

Hi Rostislav,

PHP HEAD and PHP_5_1 (5.1.3) don't use libfcgi any more.
They use simplest and smallest replacement library.

Please look into CVS code and provide a patch for it (if necessary).

Thanks. Dmitry.

> -----Original Message-----
> From: Rostislav Krasny [mailto:rosti.bsd@gmail.com] 
> Sent: Monday, March 20, 2006 11:46 AM
> To: internals@lists.php.net
> Subject: [PHP-DEV] FastCGI unix sockets support patch
> 
> 
> Hi,
> 
> According to a 'php -h' output of php-cgi 5.1.2 the FastCGI 
> can be used only by TCP/IP:
> 
>   -b <address:port>|<port> Bind Path for external FASTCGI Server mode
> 
> But according to the code FastCGI could also be used by UNIX 
> sockets. According to the code if the "-b <address:port>" 
> option is used and the port number is illegal or omitted then 
> all the "address:port" string assumed as a UNIX socket name. 
> That means that the UNIX socket should allways include a ":" 
> in its filename. My patch eliminate this limitation. After 
> applying it one should use "-b socket:" for UNIX sockets and 
> "-b address:port" or "-b port" for TCP/IP. And if the port 
> number is illegal, an error message is printed. If the "-b 
> socket:" format is used the ":" is not added to the UNIX 
> socket filename.
> 
> What do you think about following patch?
> 
> --- sapi/cgi/libfcgi/os_unix.c.orig	Sun Dec  7 14:59:54 2003
> +++ sapi/cgi/libfcgi/os_unix.c	Mon Mar 20 09:28:28 2006
> @@ -299,13 +299,15 @@
>      char    host[MAXPATHLEN];
>  
>      strlcpy(host, bindPath, MAXPATHLEN-1);
> -    if((tp = strchr(host, ':')) != 0) {
> +    if((tp = strrchr(host, ':')) != 0) {
>  	*tp++ = 0;
> -	if((port = atoi(tp)) == 0) {
> -	    *--tp = ':';
> -	 } else {
> +	if (*tp != '\0') {
> +	    if((port = atoi(tp)) == 0) {
> +		fprintf(stderr, "%s is illegal port number!\n", tp);
> +		return -1;
> +	    }
>  	    tcp = TRUE;
> -	 }
> +	}
>      }
>      if(tcp) {
>        if (!*host || !strcmp(host,"*")) {
> @@ -357,7 +359,7 @@
>  	servLen = sizeof(sa.inetVariant);
>      } else {
>  	unlink(bindPath);
> -	if(OS_BuildSockAddrUn(bindPath, &sa.unixVariant, &servLen)) {
> +	if(OS_BuildSockAddrUn(host, &sa.unixVariant, &servLen)) {
>  	    fprintf(stderr, "Listening socket's path name is 
> too long.\n");
>  	    return -1;
>  	}
> 
> 
> This patch changes an OS_CreateLocalIpcFd() function. The 
> same change of OS_FcgiConnect() should probably be done as 
> well. I didn't because the OS_FcgiConnect() isn't used 
> anywhere by php.

---

## #22542 — Rostislav Krasny — 2006-03-20T19:41:39+00:00

URL: https://externals.io/message/22531#22542 · In reply to: #22535

On Mon, 20 Mar 2006 13:44:30 +0300
dmitry@zend.com ("Dmitry Stogov") wrote:

> Hi Rostislav,
> 
> PHP HEAD and PHP_5_1 (5.1.3) don't use libfcgi any more.
> They use simplest and smallest replacement library.
> 
> Please look into CVS code and provide a patch for it (if necessary).
> 
> Thanks. Dmitry.

Hello Dmitry,

I've tried the latest php5.1-200603200930 snapshot. It has the same
problem. During fixing it in sapi/cgi/fastcgi.c I reworked an fcgi_listen()
function. The new version of this function only is attached to this message
instead of a patch that may be less handy in this case. There are two
additional things in the function that could still be improved:

1. use gethostbyname2(host, AF_INET) instead of gethostbyname(host)
   it will eliminate IPv6 addresses from resolving
2. use 0770 socket mode instead of 0777
   it will make the UNIX socket more secure

What do you think about that new code?

---

## #22544 — Rostislav Krasny — 2006-03-21T06:52:57+00:00

URL: https://externals.io/message/22531#22544 · In reply to: #22542

On Mon, 20 Mar 2006 21:41:39 +0200
rosti.bsd@gmail.com (Rostislav Krasny) wrote:

> 1. use gethostbyname2(host, AF_INET) instead of gethostbyname(host)
>    it will eliminate IPv6 addresses from resolving

The `gethostbyname()` doesn't actually request IPv6 addresses. Sorry for
that separate false statement.
