- * MAPHOSTNAME -- turn a hostname into canonical form
- *
- * Parameters:
- * hbuf -- a buffer containing a hostname.
- * hbsize -- the size of hbuf.
- *
- * Returns:
- * TRUE if the host name was mapped.
- * FALSE otherwise.
- *
- * Side Effects:
- * Looks up the host specified in hbuf. If it is not
- * the canonical name for that host, replace it with
- * the canonical name. If the name is unknown, or it
- * is already the canonical name, leave it unchanged.
- */
+** GETAUTHINFO -- get the real host name asociated with a file descriptor
+**
+** Uses RFC1413 protocol to try to get info from the other end.
+**
+** Parameters:
+** fd -- the descriptor
+**
+** Returns:
+** The user@host information associated with this descriptor.
+**
+** Side Effects:
+** Sets RealHostName to the name of the host at the other end.
+*/
+
+#ifdef IDENTPROTO
+
+static jmp_buf CtxAuthTimeout;
+
+static
+authtimeout()
+{
+ longjmp(CtxAuthTimeout, 1);
+}
+
+#endif
+
+char *
+getauthinfo(fd)
+ int fd;
+{
+ SOCKADDR fa;
+ int falen;
+ register char *p;
+#ifdef IDENTPROTO
+ SOCKADDR la;
+ int lalen;
+ register struct servent *sp;
+ int s;
+ int i;
+ EVENT *ev;
+#endif
+ static char hbuf[MAXNAME * 2 + 2];
+ extern char *hostnamebyanyaddr();
+ extern char RealUserName[]; /* main.c */
+
+ falen = sizeof fa;
+ if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0)
+ {
+ RealHostName = "localhost";
+ (void) sprintf(hbuf, "%s@localhost", RealUserName);
+ if (tTd(9, 1))
+ printf("getauthinfo: %s\n", hbuf);
+ return hbuf;
+ }
+
+ RealHostName = newstr(hostnamebyanyaddr(&fa));
+ RealHostAddr = fa;
+
+#ifdef IDENTPROTO
+ lalen = sizeof la;
+ if (fa.sa.sa_family != AF_INET ||
+ getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
+ la.sa.sa_family != AF_INET)
+ {
+ /* no ident info */
+ goto noident;
+ }
+
+ /* create ident query */
+ (void) sprintf(hbuf, "%d,%d\r\n",
+ ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port));
+
+ /* create local address */
+ bzero(&la, sizeof la);
+
+ /* create foreign address */
+ sp = getservbyname("auth", "tcp");
+ if (sp != NULL)
+ fa.sin.sin_port = sp->s_port;
+ else
+ fa.sin.sin_port = htons(113);
+
+ s = -1;
+ if (setjmp(CtxAuthTimeout) != 0)
+ {
+ if (s >= 0)
+ (void) close(s);
+ goto noident;
+ }
+
+ /* put a timeout around the whole thing */
+ ev = setevent((time_t) 30, authtimeout, 0);
+
+ /* connect to foreign IDENT server */
+ s = socket(AF_INET, SOCK_STREAM, 0);
+ if (s < 0)
+ {
+ clrevent(ev);
+ goto noident;
+ }
+ if (connect(s, &fa.sa, sizeof fa.sin) < 0)
+ {
+closeident:
+ (void) close(s);
+ clrevent(ev);
+ goto noident;
+ }
+
+ if (tTd(9, 10))
+ printf("getauthinfo: sent %s", hbuf);
+
+ /* send query */
+ if (write(s, hbuf, strlen(hbuf)) < 0)
+ goto closeident;
+
+ /* get result */
+ i = read(s, hbuf, sizeof hbuf);
+ (void) close(s);
+ clrevent(ev);
+ if (i <= 0)
+ goto noident;
+ if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
+ i--;
+ hbuf[++i] = '\0';