Restore hostname logging in inetd mode

3 messages, 2 authors, 2016-06-15 · open the first message on its own page

Restore hostname logging in inetd mode

From: Jan Engelhardt <hidden>
Date: 2016-06-15 22:54:42

The following changes since commit 0ce986446163b37c7f663ce7a408e7f94c31ba63:

  The fourth batch for 1.8.0 (2012-09-07 11:25:22 -0700)

are available in the git repository at:

  git://git.inai.de/git master

for you to fetch changes up to 864633738f6432574402afc43b6bd83c83fc8916:

  daemon: restore getpeername(0,...) use (2012-09-08 19:00:35 +0200)

----------------------------------------------------------------
Jan Engelhardt (1):
      daemon: restore getpeername(0,...) use

 daemon.c |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 51 insertions(+), 4 deletions(-)

[PATCH] daemon: restore getpeername(0,...) use

From: Jan Engelhardt <hidden>
Date: 2016-06-15 22:54:42

This reverts f9c87be6b42dd0f8b31a4bb8c6a44326879fdd1a, in a sense,
because that commit broke logging of "Connection from ..." when
git-daemon is run under xinetd.

This patch here computes the text representation of the peer and then
copies that to environment variables such that the code in execute()
and subfunctions can stay as-is.

Signed-off-by: Jan Engelhardt <redacted>
---
 daemon.c |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 51 insertions(+), 4 deletions(-)
diff --git a/daemon.c b/daemon.c
index 4602b46..eaf08c2 100644
--- a/daemon.c
+++ b/daemon.c
@@ -1,3 +1,4 @@
+#include <stdbool.h>
 #include "cache.h"
 #include "pkt-line.h"
 #include "exec_cmd.h"
@@ -1164,6 +1165,54 @@ static int serve(struct string_list *listen_addr, int listen_port,
 	return service_loop(&socklist);
 }
 
+static void inetd_mode_prepare(void)
+{
+	struct sockaddr_storage ss;
+	struct sockaddr *addr = (void *)&ss;
+	socklen_t slen = sizeof(ss);
+	char addrbuf[256], portbuf[6] = "";
+
+	if (!freopen("/dev/null", "w", stderr))
+		die_errno("failed to redirect stderr to /dev/null");
+
+	/*
+	 * Windows is said to not be able to handle this, so we will simply
+	 * ignore failure here. (It only affects a log message anyway.)
+	 */
+	if (getpeername(0, addr, &slen) < 0)
+		return;
+
+	if (addr->sa_family == AF_INET) {
+		const struct sockaddr_in *sin_addr = (void *)addr;
+
+		if (inet_ntop(addr->sa_family, &sin_addr->sin_addr,
+			      addrbuf, sizeof(addrbuf)) == NULL)
+			return;
+		snprintf(portbuf, sizeof(portbuf), "%hu",
+			 ntohs(sin_addr->sin_port));
+#ifndef NO_IPV6
+	} else if (addr->sa_family == AF_INET6) {
+		const struct sockaddr_in6 *sin6_addr = (void *)addr;
+
+		addrbuf[0] = '[';
+		addrbuf[1] = '\0';
+		if (inet_ntop(AF_INET6, &sin6_addr->sin6_addr, addrbuf + 1,
+			      sizeof(addrbuf) - 2) == NULL)
+			return;
+		strcat(addrbuf, "]");
+
+		snprintf(portbuf, sizeof(portbuf), "%hu",
+			 ntohs(sin6_addr->sin6_port));
+#endif
+	} else {
+		snprintf(addrbuf, sizeof(addrbuf), "<AF %d>",
+			 addr->sa_family);
+	}
+	if (setenv("REMOTE_ADDR", addrbuf, true) < 0)
+		return;
+	setenv("REMOTE_PORT", portbuf, true);
+}
+
 int main(int argc, char **argv)
 {
 	int listen_port = 0;
@@ -1341,10 +1390,8 @@ int main(int argc, char **argv)
 		die("base-path '%s' does not exist or is not a directory",
 		    base_path);
 
-	if (inetd_mode) {
-		if (!freopen("/dev/null", "w", stderr))
-			die_errno("failed to redirect stderr to /dev/null");
-	}
+	if (inetd_mode)
+		inetd_mode_prepare();
 
 	if (inetd_mode || serve_mode)
 		return execute();
-- 
1.7.10.4

Re: [PATCH] daemon: restore getpeername(0,...) use

From: Joachim Schmitz <hidden>
Date: 2016-06-15 22:54:42

Jan Engelhardt wrote:
quoted hunk
This reverts f9c87be6b42dd0f8b31a4bb8c6a44326879fdd1a, in a sense,
because that commit broke logging of "Connection from ..." when
git-daemon is run under xinetd.

This patch here computes the text representation of the peer and then
copies that to environment variables such that the code in execute()
and subfunctions can stay as-is.

Signed-off-by: Jan Engelhardt <redacted>
---
daemon.c |   55
+++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file
changed, 51 insertions(+), 4 deletions(-) 
diff --git a/daemon.c b/daemon.c
index 4602b46..eaf08c2 100644
--- a/daemon.c
+++ b/daemon.c
@@ -1,3 +1,4 @@
+#include <stdbool.h>
#include "cache.h"
#include "pkt-line.h"
#include "exec_cmd.h"
@@ -1164,6 +1165,54 @@ static int serve(struct string_list
 *listen_addr, int listen_port, return service_loop(&socklist);
}

+static void inetd_mode_prepare(void)
+{
+ struct sockaddr_storage ss;
+ struct sockaddr *addr = (void *)&ss;
+ socklen_t slen = sizeof(ss);
+ char addrbuf[256], portbuf[6] = "";
+
+ if (!freopen("/dev/null", "w", stderr))
+ die_errno("failed to redirect stderr to /dev/null");
+
+ /*
+ * Windows is said to not be able to handle this, so we will simply
+ * ignore failure here. (It only affects a log message anyway.)
+ */
+ if (getpeername(0, addr, &slen) < 0)
+ return;
+
+ if (addr->sa_family == AF_INET) {
+ const struct sockaddr_in *sin_addr = (void *)addr;
+
+ if (inet_ntop(addr->sa_family, &sin_addr->sin_addr,
+       addrbuf, sizeof(addrbuf)) == NULL)
+ return;
+ snprintf(portbuf, sizeof(portbuf), "%hu",
+ ntohs(sin_addr->sin_port));
+#ifndef NO_IPV6
+ } else if (addr->sa_family == AF_INET6) {
+ const struct sockaddr_in6 *sin6_addr = (void *)addr;
+
+ addrbuf[0] = '[';
+ addrbuf[1] = '\0';
+ if (inet_ntop(AF_INET6, &sin6_addr->sin6_addr, addrbuf + 1,
+       sizeof(addrbuf) - 2) == NULL)
+ return;
+ strcat(addrbuf, "]");
+
+ snprintf(portbuf, sizeof(portbuf), "%hu",
+ ntohs(sin6_addr->sin6_port));
+#endif
+ } else {
+ snprintf(addrbuf, sizeof(addrbuf), "<AF %d>",
+ addr->sa_family);
+ }
+ if (setenv("REMOTE_ADDR", addrbuf, true) < 0)
+ return;
+ setenv("REMOTE_PORT", portbuf, true);
setenv() is not a function available on all plattfomrs.

Bye, Jojo
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help