Thread (9 messages) 9 messages, 4 authors, 2016-06-15

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

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:54:42

Jan Engelhardt [off-list ref] writes:
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"
Platform agnostic parts of the code that use "git-compat-util.h"
(users of "cache.h" are indirectly users of it) are not allowed to
do platform specific include like this at their beginning.

This is the first use of stdbool.h; what do you need it for?
quoted hunk
@@ -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();
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help