📄 daemon.c
字号:
case 'A': /* address */ switch (DaemonAddr.sa.sa_family) {#ifdef NETINET case AF_INET: if (isascii(*v) && isdigit(*v)) DaemonAddr.sin.sin_addr.s_addr = inet_network(v); else { register struct netent *np; np = getnetbyname(v); if (np == NULL) syserr("554 network \"%s\" unknown", v); else DaemonAddr.sin.sin_addr.s_addr = np->n_net; } break;#endif default: syserr("554 Address= option unsupported for family %d", DaemonAddr.sa.sa_family); break; } break; case 'P': /* port */ switch (DaemonAddr.sa.sa_family) { short port;#ifdef NETINET case AF_INET: if (isascii(*v) && isdigit(*v)) DaemonAddr.sin.sin_port = htons(atoi(v)); else { register struct servent *sp; sp = getservbyname(v, "tcp"); if (sp == NULL) syserr("554 service \"%s\" unknown", v); else DaemonAddr.sin.sin_port = sp->s_port; } break;#endif#ifdef NETISO case AF_ISO: /* assume two byte transport selector */ if (isascii(*v) && isdigit(*v)) port = htons(atoi(v)); else { register struct servent *sp; sp = getservbyname(v, "tcp"); if (sp == NULL) syserr("554 service \"%s\" unknown", v); else port = sp->s_port; } bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2); break;#endif default: syserr("554 Port= option unsupported for family %d", DaemonAddr.sa.sa_family); break; } break; case 'L': /* listen queue size */ ListenQueueSize = atoi(v); break; case 'S': /* send buffer size */ TcpSndBufferSize = atoi(v); break; case 'R': /* receive buffer size */ TcpRcvBufferSize = atoi(v); break; } }}/*** MAKECONNECTION -- make a connection to an SMTP socket on another machine.**** Parameters:** host -- the name of the host.** port -- the port number to connect to.** mci -- a pointer to the mail connection information** structure to be filled in.** usesecureport -- if set, use a low numbered (reserved)** port to provide some rudimentary authentication.**** Returns:** An exit code telling whether the connection could be** made and if not why not.**** Side Effects:** none.*/SOCKADDR CurHostAddr; /* address of current host */intmakeconnection(host, port, mci, usesecureport) char *host; u_short port; register MCI *mci; bool usesecureport;{ register int i, s; register struct hostent *hp = (struct hostent *)NULL; SOCKADDR addr; int sav_errno; int addrlen;#if NAMED_BIND extern int h_errno;#endif /* ** Set up the address for the mailer. ** Accept "[a.b.c.d]" syntax for host name. */#if NAMED_BIND h_errno = 0;#endif errno = 0; bzero(&CurHostAddr, sizeof CurHostAddr); SmtpPhase = mci->mci_phase = "initial connection"; CurHostName = host; if (host[0] == '[') { long hid; register char *p = strchr(host, ']'); if (p != NULL) { *p = '\0';#ifdef NETINET hid = inet_addr(&host[1]); if (hid == -1)#endif { /* try it as a host name (avoid MX lookup) */ hp = gethostbyname(&host[1]); if (hp == NULL && p[-1] == '.') { p[-1] = '\0'; hp = gethostbyname(&host[1]); p[-1] = '.'; } *p = ']'; goto gothostent; } *p = ']'; } if (p == NULL) { usrerr("553 Invalid numeric domain spec \"%s\"", host); return (EX_NOHOST); }#ifdef NETINET addr.sin.sin_family = AF_INET; /*XXX*/ addr.sin.sin_addr.s_addr = hid;#endif } else { register char *p = &host[strlen(host) - 1]; hp = gethostbyname(host); if (hp == NULL && *p == '.') { *p = '\0'; hp = gethostbyname(host); *p = '.'; }gothostent: if (hp == NULL) {#if NAMED_BIND if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) return (EX_TEMPFAIL); /* if name server is specified, assume temp fail */ if (errno == ECONNREFUSED && UseNameServer) return (EX_TEMPFAIL);#endif return (EX_NOHOST); } addr.sa.sa_family = hp->h_addrtype; switch (hp->h_addrtype) {#ifdef NETINET case AF_INET: bcopy(hp->h_addr, &addr.sin.sin_addr, sizeof addr.sin.sin_addr); break;#endif default: bcopy(hp->h_addr, addr.sa.sa_data, hp->h_length); break; } i = 1; } /* ** Determine the port number. */ if (port != 0) port = htons(port); else { register struct servent *sp = getservbyname("smtp", "tcp"); if (sp == NULL) { syserr("554 makeconnection: service \"smtp\" unknown"); port = htons(25); } else port = sp->s_port; } switch (addr.sa.sa_family) {#ifdef NETINET case AF_INET: addr.sin.sin_port = port; addrlen = sizeof (struct sockaddr_in); break;#endif#ifdef NETISO case AF_ISO: /* assume two byte transport selector */ bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2); addrlen = sizeof (struct sockaddr_iso); break;#endif default: syserr("Can't connect to address family %d", addr.sa.sa_family); return (EX_NOHOST); } /* ** Try to actually open the connection. */#ifdef XLA /* if too many connections, don't bother trying */ if (!xla_noqueue_ok(host)) return EX_TEMPFAIL;#endif for (;;) { if (tTd(16, 1)) printf("makeconnection (%s [%s])\n", host, anynet_ntoa(&addr)); /* save for logging */ CurHostAddr = addr; if (usesecureport) { int rport = IPPORT_RESERVED - 1; s = rresvport(&rport); } else { s = socket(AF_INET, SOCK_STREAM, 0); } if (s < 0) { sav_errno = errno; syserr("makeconnection: no socket"); goto failure; }#ifdef SO_SNDBUF if (TcpSndBufferSize > 0) { if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *) &TcpSndBufferSize, sizeof(TcpSndBufferSize)) < 0) syserr("makeconnection: setsockopt(SO_SNDBUF)"); }#endif if (tTd(16, 1)) printf("makeconnection: fd=%d\n", s); /* turn on network debugging? */ if (tTd(16, 101)) { int on = 1; (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); } if (CurEnv->e_xfp != NULL) (void) fflush(CurEnv->e_xfp); /* for debugging */ errno = 0; /* for debugging */ if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0) break; /* couldn't connect.... figure out why */ sav_errno = errno; (void) close(s); if (hp && hp->h_addr_list[i]) { if (tTd(16, 1)) printf("Connect failed (%s); trying new address....\n", errstring(sav_errno)); switch (addr.sa.sa_family) {#ifdef NETINET case AF_INET: bcopy(hp->h_addr_list[i++], &addr.sin.sin_addr, sizeof addr.sin.sin_addr); break;#endif default: bcopy(hp->h_addr_list[i++], addr.sa.sa_data, hp->h_length); break; } continue; } /* failure, decide if temporary or not */ failure:#ifdef XLA xla_host_end(host);#endif if (transienterror(sav_errno)) return EX_TEMPFAIL; else { message("%s", errstring(sav_errno)); return (EX_UNAVAILABLE); } } /* connection ok, put it into canonical form */ if ((mci->mci_out = fdopen(s, "w")) == NULL || (s = dup(s)) < 0 || (mci->mci_in = fdopen(s, "r")) == NULL) { syserr("cannot open SMTP client channel, fd=%d", s); return EX_TEMPFAIL; } return (EX_OK);}/*** MYHOSTNAME -- return the name of this host.**** Parameters:** hostbuf -- a place to return the name of this host.** size -- the size of hostbuf.**** Returns:** A list of aliases for this host.**** Side Effects:** Adds numeric codes to $=w.*/char **myhostname(hostbuf, size) char hostbuf[]; int size;{ register struct hostent *hp; extern struct hostent *gethostbyname(); if (gethostname(hostbuf, size) < 0) { (void) strcpy(hostbuf, "localhost"); } hp = gethostbyname(hostbuf); if (hp == NULL) { syserr("!My host name (%s) does not seem to exist!", hostbuf); } (void) strncpy(hostbuf, hp->h_name, size - 1); hostbuf[size - 1] = '\0';#if NAMED_BIND /* if still no dot, try DNS directly (i.e., avoid NIS problems) */ if (strchr(hostbuf, '.') == NULL) { extern bool getcanonname(); extern int h_errno; /* try twice in case name server not yet started up */ if (!getcanonname(hostbuf, size, TRUE) && UseNameServer && (h_errno != TRY_AGAIN || (sleep(30), !getcanonname(hostbuf, size, TRUE)))) { errno = h_errno + E_DNSBASE; syserr("!My host name (%s) not known to DNS", hostbuf); } }#endif if (hp->h_addrtype == AF_INET && hp->h_length == 4) { register int i; for (i = 0; hp->h_addr_list[i] != NULL; i++) { char ipbuf[100]; sprintf(ipbuf, "[%s]", inet_ntoa(*((struct in_addr *) hp->h_addr_list[i]))); setclass('w', ipbuf); } } return (hp->h_aliases);}/*** 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.*/#if IDENTPROTOstatic jmp_buf CtxAuthTimeout;staticauthtimeout(){ longjmp(CtxAuthTimeout, 1);}#endifchar *getauthinfo(fd) int fd;{ int falen; register char *p;#if 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 RealHostAddr; if (getpeername(fd, &RealHostAddr.sa, &falen) < 0 || falen <= 0 || RealHostAddr.sa.sa_family == 0) { (void) sprintf(hbuf, "%s@localhost", RealUserName); if (tTd(9, 1)) printf("getauthinfo: %s\n", hbuf); return hbuf; } if (RealHostName == NULL) { /* translate that to a host name */ RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr)); }#if IDENTPROTO if (TimeOuts.to_ident == 0) goto noident; lalen = sizeof la; if (RealHostAddr.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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -