+#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)
+ {
+#ifdef _SCO_unix_
+ fa.sin.sin_port = htons(sp->s_port);
+#else
+ fa.sin.sin_port = sp->s_port;
+#endif
+ }
+ 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';
+
+ if (tTd(9, 3))
+ printf("getauthinfo: got %s\n", hbuf);
+
+ /* parse result */
+ p = strchr(hbuf, ':');
+ if (p == NULL)
+ {
+ /* malformed response */
+ goto noident;
+ }
+ while (isascii(*++p) && isspace(*p))
+ continue;
+ if (strncasecmp(p, "userid", 6) != 0)
+ {
+ /* presumably an error string */
+ goto noident;
+ }
+ p += 6;
+ while (isascii(*p) && isspace(*p))
+ p++;
+ if (*p++ != ':')
+ {
+ /* either useridxx or malformed response */
+ goto noident;
+ }
+
+ /* p now points to the OSTYPE field */
+ p = strchr(p, ':');
+ if (p == NULL)
+ {
+ /* malformed response */
+ goto noident;
+ }
+
+ /* 1413 says don't do this -- but it's broken otherwise */
+ while (isascii(*++p) && isspace(*p))
+ continue;
+
+ /* p now points to the authenticated name */
+ (void) sprintf(hbuf, "%s@%s", p, RealHostName);
+ goto finish;
+
+#endif /* IDENTPROTO */
+
+noident:
+ (void) strcpy(hbuf, RealHostName);
+
+finish:
+ if (RealHostName[0] != '[')
+ {
+ p = &hbuf[strlen(hbuf)];
+ (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
+ }
+ if (tTd(9, 1))
+ printf("getauthinfo: %s\n", hbuf);
+ return hbuf;
+}
+\f/*
+** HOST_MAP_LOOKUP -- turn a hostname into canonical form
+**
+** Parameters:
+** map -- a pointer to this map (unused).
+** name -- the (presumably unqualified) hostname.
+** av -- unused -- for compatibility with other mapping
+** functions.
+** statp -- an exit status (out parameter) -- set to
+** EX_TEMPFAIL if the name server is unavailable.
+**
+** Returns:
+** The mapping, if found.
+** NULL if no mapping found.
+**
+** Side Effects:
+** Looks up the host specified in hbuf. If it is not
+** the canonical name for that host, return the canonical
+** name.
+*/
+
+char *
+host_map_lookup(map, name, av, statp)
+ MAP *map;
+ char *name;
+ char **av;
+ int *statp;