bug-inetutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-inetutils] [patch] inetd: set environment variables for remote host


From: Dirk Jagdmann
Subject: [bug-inetutils] [patch] inetd: set environment variables for remote host, ip and local port
Date: Sun, 23 Jul 2006 12:12:48 +0200
User-agent: Thunderbird 1.5.0.4 (X11/20060516)

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  <address@hidden>
+
+       * 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  <address@hidden>
 
        * 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;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]