Hello,
I have just developed some very simple servers (with shell scripts)
which are run via inetd. For logging purposes I wanted to have the
remote hostname, ip and local port available in those shell scripts and
have made an extension to inetd which will put this information into the
environment on the forked process. It works for my needs and now I'd
like to share my code with you. Feel free to include something like this
in a future release of the inet-utils. If you don't want this in the
official releases I hope you won't mind if I make this patch available
on my privat homepage for others.
--
---> Dirk Jagdmann ^ doj / cubic
----> http://cubic.org/~doj
-----> http://llg.cubic.org
Index: inetutils-1.4.2/inetd/ChangeLog
===================================================================
--- inetutils-1.4.2.orig/inetd/ChangeLog
+++ inetutils-1.4.2/inetd/ChangeLog
@@ -1,3 +1,9 @@
+2006-07-21 Dirk Jagdmann <[EMAIL PROTECTED]>
+
+ * inetd.c (main): set environment variables inetd_remotehost,
+ inetd_remoteip, inetd_port to corresponding values from client who
+ connected to inetd.
+
2002-04-29 Alfred M. Szmidt <[EMAIL PROTECTED]>
* inetd.c: <version.h>: Include removed.
Index: inetutils-1.4.2/inetd/inetd.c
===================================================================
--- inetutils-1.4.2.orig/inetd/inetd.c
+++ inetutils-1.4.2/inetd/inetd.c
@@ -472,6 +472,43 @@ main (int argc, char *argv[], char *envp
sep->se_service);
continue;
}
+
+ /* set inetd_... environment */
+ {
+ struct sockaddr_in sin;
+ int len = sizeof sin;
+
+ unsetenv("inetd_remotehost");
+ unsetenv("inetd_remoteip");
+ unsetenv("inetd_port");
+
+ if (getpeername(ctrl, (struct sockaddr *) &sin, &len) < 0)
+ syslog(LOG_WARNING, "getpeername:%s\n", strerror(errno));
+ else
+ {
+ struct hostent *host;
+
+ char str[16];
+
+ char *ip=inet_ntoa(sin.sin_addr);
+ if(setenv("inetd_remoteip", ip, 1) < 0)
+ syslog(LOG_WARNING, "setenv(inetd_remoteip):%s\n",
strerror(errno));
+
+ snprintf(str, sizeof(str), "%i", ntohs(sin.sin_port));
+ if(setenv("inetd_port", str, 1) < 0)
+ syslog(LOG_WARNING, "setenv(inetd_port):%s\n",
strerror(errno));
+
+ if ((host = gethostbyaddr((char *) &sin.sin_addr,
+ sizeof sin.sin_addr,
+ AF_INET)) == NULL)
+ syslog(LOG_WARNING, "gethostbyaddr:%s\n",
strerror(errno));
+ else
+ {
+ if(setenv("inetd_remotehost", host->h_name, 1) < 0)
+ syslog(LOG_WARNING,
"setenv(inetd_remotehost):%s\n", strerror(errno));
+ }
+ }
+ }
}
else
ctrl = sep->se_fd;
_______________________________________________
Bug-inetutils mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-inetutils