📄 daemon.c
字号:
(void) sprintf(hbuf, "%d,%d\r\n", ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port)); /* create local address */ la.sin.sin_port = 0; /* create foreign address */ sp = getservbyname("auth", "tcp"); if (sp != NULL) RealHostAddr.sin.sin_port = sp->s_port; else RealHostAddr.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(TimeOuts.to_ident, authtimeout, 0); /* connect to foreign IDENT server using same address as SMTP socket */ s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) { clrevent(ev); goto noident; } if (bind(s, &la.sa, sizeof la.sin) < 0 || connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0) { goto closeident; } 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 == NULL ? "localhost" : RealHostName); goto finish;closeident: (void) close(s); clrevent(ev);#endif /* IDENTPROTO */noident: if (RealHostName == NULL) { if (tTd(9, 1)) printf("getauthinfo: NULL\n"); return NULL; } (void) strcpy(hbuf, RealHostName);finish: if (RealHostName != NULL && RealHostName[0] != '[') { p = &hbuf[strlen(hbuf)]; (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr)); } if (tTd(9, 1)) printf("getauthinfo: %s\n", hbuf); return hbuf;}/*** 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;{ register struct hostent *hp; u_long in_addr; char *cp; int i; register STAB *s; char hbuf[MAXNAME]; extern struct hostent *gethostbyaddr();#if NAMED_BIND extern int h_errno;#endif /* ** See if we have already looked up this name. If so, just ** return it. */ s = stab(name, ST_NAMECANON, ST_ENTER); if (bitset(NCF_VALID, s->s_namecanon.nc_flags)) { if (tTd(9, 1)) printf("host_map_lookup(%s) => CACHE %s\n", name, s->s_namecanon.nc_cname); errno = s->s_namecanon.nc_errno;#if NAMED_BIND h_errno = s->s_namecanon.nc_herrno;#endif *statp = s->s_namecanon.nc_stat; if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL) { sprintf(hbuf, "%s: Name server timeout", shortenstring(name, 33)); CurEnv->e_message = newstr(hbuf); } return s->s_namecanon.nc_cname; } /* ** If first character is a bracket, then it is an address ** lookup. Address is copied into a temporary buffer to ** strip the brackets and to preserve name if address is ** unknown. */ if (*name != '[') { extern bool getcanonname(); if (tTd(9, 1)) printf("host_map_lookup(%s) => ", name); s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ (void) strcpy(hbuf, name); if (getcanonname(hbuf, sizeof hbuf - 1, TRUE)) { if (tTd(9, 1)) printf("%s\n", hbuf); cp = map_rewrite(map, hbuf, strlen(hbuf), av); s->s_namecanon.nc_cname = newstr(cp); return cp; } else { register struct hostent *hp; s->s_namecanon.nc_errno = errno;#if NAMED_BIND s->s_namecanon.nc_herrno = h_errno; if (tTd(9, 1)) printf("FAIL (%d)\n", h_errno); switch (h_errno) { case TRY_AGAIN: if (UseNameServer) { sprintf(hbuf, "%s: Name server timeout", shortenstring(name, 33)); message("%s", hbuf); if (CurEnv->e_message == NULL) CurEnv->e_message = newstr(hbuf); } *statp = EX_TEMPFAIL; break; case HOST_NOT_FOUND: *statp = EX_NOHOST; break; case NO_RECOVERY: *statp = EX_SOFTWARE; break; default: *statp = EX_UNAVAILABLE; break; }#else if (tTd(9, 1)) printf("FAIL\n"); *statp = EX_NOHOST;#endif s->s_namecanon.nc_stat = *statp; if (*statp != EX_TEMPFAIL || UseNameServer) return NULL; /* ** Try to look it up in /etc/hosts */ hp = gethostbyname(name); if (hp == NULL) { /* no dice there either */ s->s_namecanon.nc_stat = *statp = EX_NOHOST; return NULL; } s->s_namecanon.nc_stat = *statp = EX_OK; cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); s->s_namecanon.nc_cname = newstr(cp); return cp; } } if ((cp = strchr(name, ']')) == NULL) return (NULL); *cp = '\0'; in_addr = inet_addr(&name[1]); /* nope -- ask the name server */ hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); s->s_namecanon.nc_errno = errno;#if NAMED_BIND s->s_namecanon.nc_herrno = h_errno;#endif s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ if (hp == NULL) { s->s_namecanon.nc_stat = *statp = EX_NOHOST; return (NULL); } /* found a match -- copy out */ cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); s->s_namecanon.nc_stat = *statp = EX_OK; s->s_namecanon.nc_cname = newstr(cp); return cp;}/*** ANYNET_NTOA -- convert a network address to printable form.**** Parameters:** sap -- a pointer to a sockaddr structure.**** Returns:** A printable version of that sockaddr.*/char *anynet_ntoa(sap) register SOCKADDR *sap;{ register char *bp; register char *ap; int l; static char buf[100]; /* check for null/zero family */ if (sap == NULL) return "NULLADDR"; if (sap->sa.sa_family == 0) return "0"; switch (sap->sa.sa_family) {#ifdef MAYBENEXTRELEASE /*** UNTESTED *** UNTESTED *** UNTESTED ***/#ifdef NETUNIX case AF_UNIX: if (sap->sunix.sun_path[0] != '\0') sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path); else sprintf(buf, "[UNIX: localhost]"); return buf;#endif#endif#ifdef NETINET case AF_INET: return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);#endif default: /* this case is only to ensure syntactic correctness */ break; } /* unknown family -- just dump bytes */ (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); bp = &buf[strlen(buf)]; ap = sap->sa.sa_data; for (l = sizeof sap->sa.sa_data; --l >= 0; ) { (void) sprintf(bp, "%02x:", *ap++ & 0377); bp += 3; } *--bp = '\0'; return buf;}/*** HOSTNAMEBYANYADDR -- return name of host based on address**** Parameters:** sap -- SOCKADDR pointer**** Returns:** text representation of host name.**** Side Effects:** none.*/char *hostnamebyanyaddr(sap) register SOCKADDR *sap;{ register struct hostent *hp; int saveretry;#if NAMED_BIND /* shorten name server timeout to avoid higher level timeouts */ saveretry = _res.retry; _res.retry = 3;#endif /* NAMED_BIND */ switch (sap->sa.sa_family) {#ifdef NETINET case AF_INET: hp = gethostbyaddr((char *) &sap->sin.sin_addr, sizeof sap->sin.sin_addr, AF_INET); break;#endif#ifdef NETISO case AF_ISO: hp = gethostbyaddr((char *) &sap->siso.siso_addr, sizeof sap->siso.siso_addr, AF_ISO); break;#endif#ifdef MAYBENEXTRELEASE /*** UNTESTED *** UNTESTED *** UNTESTED ***/ case AF_UNIX: hp = NULL; break;#endif default: hp = gethostbyaddr(sap->sa.sa_data, sizeof sap->sa.sa_data, sap->sa.sa_family); break; }#if NAMED_BIND _res.retry = saveretry;#endif /* NAMED_BIND */ if (hp != NULL) return hp->h_name; else { /* produce a dotted quad */ static char buf[512]; (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); return buf; }}# else /* DAEMON *//* code for systems without sophisticated networking *//*** MYHOSTNAME -- stub version for case of no daemon code.**** Can't convert to upper case here because might be a UUCP name.**** Mark, you can change this to be anything you want......*/char **myhostname(hostbuf, size) char hostbuf[]; int size;{ register FILE *f; hostbuf[0] = '\0'; f = fopen("/usr/include/whoami", "r"); if (f != NULL) { (void) fgets(hostbuf, size, f); fixcrlf(hostbuf, TRUE); (void) fclose(f); } return (NULL);}/*** GETAUTHINFO -- get the real host name asociated with a file descriptor**** Parameters:** fd -- the descriptor**** Returns:** The host name associated with this descriptor, if it can** be determined.** NULL otherwise.**** Side Effects:** none*/char *getauthinfo(fd) int fd;{ return NULL;}/*** MAPHOSTNAME -- turn a hostname into canonical form**** Parameters:** map -- a pointer to the database map.** name -- a buffer containing a hostname.** avp -- a pointer to a (cf file defined) argument vector.** statp -- an exit status (out parameter).**** Returns:** mapped host name** FALSE otherwise.**** Side Effects:** Looks up the host specified in name. 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.*//*ARGSUSED*/char *host_map_lookup(map, name, avp, statp) MAP *map; char *name; char **avp; char *statp;{ register struct hostent *hp; hp = gethostbyname(name); if (hp != NULL) return hp->h_name; *statp = EX_NOHOST; return NULL;}#endif /* DAEMON */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -