122700Sdist /*
268839Seric * Copyright (c) 1983, 1995 Eric P. Allman
362522Sbostic * Copyright (c) 1988, 1993
462522Sbostic * The Regents of the University of California. All rights reserved.
533780Sbostic *
642825Sbostic * %sccs.include.redist.c%
733780Sbostic */
822700Sdist
933932Sbostic #include <errno.h>
1040962Sbostic #include "sendmail.h"
114535Seric
1233780Sbostic #ifndef lint
1333780Sbostic #ifdef DAEMON
14*69954Seric static char sccsid[] = "@(#)daemon.c 8.105 (Berkeley) 06/20/95 (with daemon mode)";
1533780Sbostic #else
16*69954Seric static char sccsid[] = "@(#)daemon.c 8.105 (Berkeley) 06/20/95 (without daemon mode)";
1733780Sbostic #endif
1833780Sbostic #endif /* not lint */
194535Seric
2033780Sbostic #ifdef DAEMON
2133780Sbostic
2264338Seric # include <arpa/inet.h>
235978Seric
2466334Seric #if NAMED_BIND
2559042Seric # include <resolv.h>
2659042Seric #endif
2759042Seric
2869601Seric #if IP_SRCROUTE
2969601Seric # include <netinet/in_systm.h>
3069601Seric # include <netinet/ip.h>
3169601Seric # include <netinet/ip_var.h>
3269601Seric #endif
3369601Seric
344535Seric /*
354535Seric ** DAEMON.C -- routines to use when running as a daemon.
367556Seric **
377556Seric ** This entire file is highly dependent on the 4.2 BSD
387556Seric ** interprocess communication primitives. No attempt has
397556Seric ** been made to make this file portable to Version 7,
407556Seric ** Version 6, MPX files, etc. If you should try such a
417556Seric ** thing yourself, I recommend chucking the entire file
427556Seric ** and starting from scratch. Basic semantics are:
437556Seric **
447556Seric ** getrequests()
457556Seric ** Opens a port and initiates a connection.
467556Seric ** Returns in a child. Must set InChannel and
477556Seric ** OutChannel appropriately.
4810206Seric ** clrdaemon()
4910206Seric ** Close any open files associated with getting
5010206Seric ** the connection; this is used when running the queue,
5110206Seric ** etc., to avoid having extra file descriptors during
5210206Seric ** the queue run and to avoid confusing the network
5310206Seric ** code (if it cares).
5452106Seric ** makeconnection(host, port, outfile, infile, usesecureport)
557556Seric ** Make a connection to the named host on the given
567556Seric ** port. Set *outfile and *infile to the files
577556Seric ** appropriate for communication. Returns zero on
587556Seric ** success, else an exit status describing the
597556Seric ** error.
6060089Seric ** host_map_lookup(map, hbuf, avp, pstat)
6156823Seric ** Convert the entry in hbuf into a canonical form.
624535Seric */
634535Seric /*
644535Seric ** GETREQUESTS -- open mail IPC port and get requests.
654535Seric **
664535Seric ** Parameters:
674535Seric ** none.
684535Seric **
694535Seric ** Returns:
704535Seric ** none.
714535Seric **
724535Seric ** Side Effects:
734535Seric ** Waits until some interesting activity occurs. When
744535Seric ** it does, a child is created to process it, and the
754535Seric ** parent waits for completion. Return from this
769886Seric ** routine is always in the child. The file pointers
779886Seric ** "InChannel" and "OutChannel" should be set to point
789886Seric ** to the communication channel.
794535Seric */
804535Seric
8158849Seric int DaemonSocket = -1; /* fd describing socket */
8258849Seric SOCKADDR DaemonAddr; /* socket for incoming */
8359783Seric int ListenQueueSize = 10; /* size of listen queue */
8464381Seric int TcpRcvBufferSize = 0; /* size of TCP receive buffer */
8564381Seric int TcpSndBufferSize = 0; /* size of TCP send buffer */
8616144Seric
8768693Seric void
getrequests()884535Seric getrequests()
894535Seric {
909610Seric int t;
9153751Seric bool refusingconnections = TRUE;
9258419Seric FILE *pidf;
9364828Seric int socksize;
9469881Seric #if XDEBUG
9566793Seric bool j_has_dot;
9666793Seric #endif
9746928Sbostic extern void reapchild();
987117Seric
999610Seric /*
1009610Seric ** Set up the address for the mailer.
1019610Seric */
1029610Seric
10358849Seric if (DaemonAddr.sin.sin_family == 0)
10458849Seric DaemonAddr.sin.sin_family = AF_INET;
10558849Seric if (DaemonAddr.sin.sin_addr.s_addr == 0)
10658849Seric DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
10758849Seric if (DaemonAddr.sin.sin_port == 0)
1089610Seric {
10965169Seric register struct servent *sp;
11065169Seric
11158849Seric sp = getservbyname("smtp", "tcp");
11258849Seric if (sp == NULL)
11358849Seric {
11458909Seric syserr("554 service \"smtp\" unknown");
11565169Seric DaemonAddr.sin.sin_port = htons(25);
11658849Seric }
11765169Seric else
11865169Seric DaemonAddr.sin.sin_port = sp->s_port;
1199610Seric }
1209610Seric
1219610Seric /*
1229610Seric ** Try to actually open the connection.
1239610Seric */
1249610Seric
1259610Seric if (tTd(15, 1))
12658849Seric printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1279610Seric
1289610Seric /* get a socket for the SMTP connection */
12966854Seric socksize = opendaemonsocket(TRUE);
13010347Seric
13164035Seric (void) setsignal(SIGCHLD, reapchild);
13224945Seric
13358419Seric /* write the pid to the log file for posterity */
13458419Seric pidf = fopen(PidFile, "w");
13558419Seric if (pidf != NULL)
13658419Seric {
13763863Seric extern char *CommandLineArgs;
13863863Seric
13963863Seric /* write the process id on line 1 */
14058419Seric fprintf(pidf, "%d\n", getpid());
14163863Seric
14263863Seric /* line 2 contains all command line flags */
14363863Seric fprintf(pidf, "%s\n", CommandLineArgs);
14463863Seric
14563863Seric /* flush and close */
14658419Seric fclose(pidf);
14758419Seric }
14858419Seric
14969881Seric #if XDEBUG
15066793Seric {
15166812Seric char jbuf[MAXHOSTNAMELEN];
15258419Seric
15368693Seric expand("\201j", jbuf, sizeof jbuf, CurEnv);
15466812Seric j_has_dot = strchr(jbuf, '.') != NULL;
15566793Seric }
15666793Seric #endif
15766793Seric
1589610Seric if (tTd(15, 1))
15910206Seric printf("getrequests: %d\n", DaemonSocket);
1609610Seric
1614631Seric for (;;)
1624631Seric {
16314875Seric register int pid;
16411147Seric auto int lotherend;
16553751Seric extern bool refuseconnections();
16668693Seric extern int getla();
16711147Seric
16814875Seric /* see if we are rejecting connections */
16953751Seric CurrentLA = getla();
17053751Seric if (refuseconnections())
17136584Sbostic {
17266845Seric if (DaemonSocket >= 0)
17353751Seric {
17466845Seric /* close socket so peer will fail quickly */
17566845Seric (void) close(DaemonSocket);
17666845Seric DaemonSocket = -1;
17753751Seric }
17866845Seric refusingconnections = TRUE;
17966845Seric sleep(15);
18053751Seric continue;
18136584Sbostic }
18214875Seric
18368693Seric /* arrange to (re)open the socket if necessary */
18453751Seric if (refusingconnections)
18553751Seric {
18667690Seric (void) opendaemonsocket(FALSE);
18753751Seric refusingconnections = FALSE;
18853751Seric }
18953751Seric
19069881Seric #if XDEBUG
19166793Seric /* check for disaster */
19266793Seric {
19366812Seric char jbuf[MAXHOSTNAMELEN];
19466793Seric
19568693Seric expand("\201j", jbuf, sizeof jbuf, CurEnv);
19668693Seric if (!wordinclass(jbuf, 'w'))
19766793Seric {
19866793Seric dumpstate("daemon lost $j");
19966793Seric syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog");
20066793Seric abort();
20166793Seric }
20266812Seric else if (j_has_dot && strchr(jbuf, '.') == NULL)
20366793Seric {
20466793Seric dumpstate("daemon $j lost dot");
20566793Seric syslog(LOG_ALERT, "daemon process $j lost dot; see syslog");
20666793Seric abort();
20766793Seric }
20866793Seric }
20966793Seric #endif
21066793Seric
2119610Seric /* wait for a connection */
21269781Seric setproctitle("accepting connections");
2139610Seric do
2149610Seric {
2159610Seric errno = 0;
21664828Seric lotherend = socksize;
21746928Sbostic t = accept(DaemonSocket,
21846928Sbostic (struct sockaddr *)&RealHostAddr, &lotherend);
2199610Seric } while (t < 0 && errno == EINTR);
2209610Seric if (t < 0)
2215978Seric {
2229610Seric syserr("getrequests: accept");
22368693Seric
22468693Seric /* arrange to re-open the socket next time around */
22568693Seric (void) close(DaemonSocket);
22668693Seric DaemonSocket = -1;
2279610Seric sleep(5);
2289610Seric continue;
2295978Seric }
2304631Seric
2315978Seric /*
2325978Seric ** Create a subprocess to process the mail.
2335978Seric */
2345978Seric
2357677Seric if (tTd(15, 2))
2369610Seric printf("getrequests: forking (fd = %d)\n", t);
2375978Seric
2384636Seric pid = fork();
2394636Seric if (pid < 0)
2404631Seric {
2414636Seric syserr("daemon: cannot fork");
2424636Seric sleep(10);
2439610Seric (void) close(t);
2444636Seric continue;
2454631Seric }
2464631Seric
2474636Seric if (pid == 0)
2484631Seric {
24964086Seric char *p;
25058951Seric extern char *hostnamebyanyaddr();
25168693Seric extern void intsig();
25211147Seric
2534636Seric /*
2544636Seric ** CHILD -- return to caller.
25511147Seric ** Collect verified idea of sending host.
2564636Seric ** Verify calling user id if possible here.
2574636Seric */
2584631Seric
25964035Seric (void) setsignal(SIGCHLD, SIG_DFL);
26068693Seric (void) setsignal(SIGHUP, intsig);
26168693Seric (void) close(DaemonSocket);
26266017Seric DisConnected = FALSE;
26324950Seric
26466032Seric setproctitle("startup with %s",
26566032Seric anynet_ntoa(&RealHostAddr));
26666032Seric
26711147Seric /* determine host name */
26864086Seric p = hostnamebyanyaddr(&RealHostAddr);
26969471Seric if (strlen(p) > MAXNAME)
27069471Seric p[MAXNAME] = '\0';
27164086Seric RealHostName = newstr(p);
27266032Seric setproctitle("startup with %s", p);
27358778Seric
27464724Seric if ((InChannel = fdopen(t, "r")) == NULL ||
27564724Seric (t = dup(t)) < 0 ||
27664724Seric (OutChannel = fdopen(t, "w")) == NULL)
27764724Seric {
27864724Seric syserr("cannot open SMTP server channel, fd=%d", t);
27964724Seric exit(0);
28064724Seric }
28159254Seric
28216884Seric /* should we check for illegal connection here? XXX */
28359156Seric #ifdef XLA
28459156Seric if (!xla_host_ok(RealHostName))
28559156Seric {
28659254Seric message("421 Too many SMTP sessions for this host");
28759156Seric exit(0);
28859156Seric }
28959156Seric #endif
29016884Seric
2917677Seric if (tTd(15, 2))
2925978Seric printf("getreq: returning\n");
2934636Seric return;
2944631Seric }
2954631Seric
29669839Seric CurChildren++;
29769839Seric
2987117Seric /* close the port so that others will hang (for a while) */
2999610Seric (void) close(t);
3004631Seric }
3019886Seric /*NOTREACHED*/
3024631Seric }
3035978Seric /*
30466845Seric ** OPENDAEMONSOCKET -- open the SMTP socket
30566845Seric **
30666845Seric ** Deals with setting all appropriate options. DaemonAddr must
30766845Seric ** be set up in advance.
30866845Seric **
30966845Seric ** Parameters:
31066854Seric ** firsttime -- set if this is the initial open.
31166845Seric **
31266845Seric ** Returns:
31366845Seric ** Size in bytes of the daemon socket addr.
31466845Seric **
31566845Seric ** Side Effects:
31666845Seric ** Leaves DaemonSocket set to the open socket.
31766845Seric ** Exits if the socket cannot be created.
31866845Seric */
31966845Seric
32066861Seric #define MAXOPENTRIES 10 /* maximum number of tries to open connection */
32166861Seric
32266845Seric int
opendaemonsocket(firsttime)32366854Seric opendaemonsocket(firsttime)
32466854Seric bool firsttime;
32566845Seric {
32666845Seric int on = 1;
32768693Seric int socksize = 0;
32866861Seric int ntries = 0;
32966861Seric int saveerrno;
33066845Seric
33166845Seric if (tTd(15, 2))
33266845Seric printf("opendaemonsocket()\n");
33366845Seric
33466861Seric do
33566845Seric {
33666862Seric if (ntries > 0)
33766862Seric sleep(5);
33866861Seric if (firsttime || DaemonSocket < 0)
33966854Seric {
34066861Seric DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
34166861Seric if (DaemonSocket < 0)
34266861Seric {
34366861Seric /* probably another daemon already */
34466861Seric saveerrno = errno;
34566861Seric syserr("opendaemonsocket: can't create server SMTP socket");
34666861Seric severe:
34766845Seric # ifdef LOG
34866861Seric if (LogLevel > 0)
34966861Seric syslog(LOG_ALERT, "problem creating SMTP socket");
35066845Seric # endif /* LOG */
35166861Seric DaemonSocket = -1;
35266861Seric continue;
35366861Seric }
35466845Seric
35566861Seric /* turn on network debugging? */
35666861Seric if (tTd(15, 101))
35766861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET,
35866861Seric SO_DEBUG, (char *)&on,
35966861Seric sizeof on);
36066845Seric
36166861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET,
36266861Seric SO_REUSEADDR, (char *)&on, sizeof on);
36366861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET,
36466861Seric SO_KEEPALIVE, (char *)&on, sizeof on);
36566845Seric
36666845Seric #ifdef SO_RCVBUF
36766861Seric if (TcpRcvBufferSize > 0)
36866861Seric {
36966861Seric if (setsockopt(DaemonSocket, SOL_SOCKET,
37066861Seric SO_RCVBUF,
37166861Seric (char *) &TcpRcvBufferSize,
37266861Seric sizeof(TcpRcvBufferSize)) < 0)
37366861Seric syserr("getrequests: setsockopt(SO_RCVBUF)");
37466861Seric }
37566845Seric #endif
37666845Seric
37766861Seric switch (DaemonAddr.sa.sa_family)
37866861Seric {
37969881Seric # if NETINET
38066861Seric case AF_INET:
38166861Seric socksize = sizeof DaemonAddr.sin;
38266861Seric break;
38366845Seric # endif
38466845Seric
38569881Seric # if NETISO
38666861Seric case AF_ISO:
38766861Seric socksize = sizeof DaemonAddr.siso;
38866861Seric break;
38966845Seric # endif
39066845Seric
39166861Seric default:
39266861Seric socksize = sizeof DaemonAddr;
39366861Seric break;
39466861Seric }
39566861Seric
39666861Seric if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
39766861Seric {
39866861Seric saveerrno = errno;
39966861Seric syserr("getrequests: cannot bind");
40066861Seric (void) close(DaemonSocket);
40166861Seric goto severe;
40266861Seric }
40366854Seric }
40466861Seric if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0)
40566854Seric {
40666861Seric saveerrno = errno;
40766861Seric syserr("getrequests: cannot listen");
40866854Seric (void) close(DaemonSocket);
40966854Seric goto severe;
41066854Seric }
41166861Seric return socksize;
41266861Seric } while (ntries++ < MAXOPENTRIES && transienterror(saveerrno));
41368693Seric syserr("!opendaemonsocket: server SMTP socket wedged: exiting");
41466861Seric finis();
41566845Seric }
41666845Seric /*
41710206Seric ** CLRDAEMON -- reset the daemon connection
41810206Seric **
41910206Seric ** Parameters:
42010206Seric ** none.
42110206Seric **
42210206Seric ** Returns:
42310206Seric ** none.
42410206Seric **
42510206Seric ** Side Effects:
42610206Seric ** releases any resources used by the passive daemon.
42710206Seric */
42810206Seric
42968693Seric void
clrdaemon()43010206Seric clrdaemon()
43110206Seric {
43210206Seric if (DaemonSocket >= 0)
43310206Seric (void) close(DaemonSocket);
43410206Seric DaemonSocket = -1;
43510206Seric }
43610206Seric /*
43758849Seric ** SETDAEMONOPTIONS -- set options for running the daemon
43858849Seric **
43958849Seric ** Parameters:
44058849Seric ** p -- the options line.
44158849Seric **
44258849Seric ** Returns:
44358849Seric ** none.
44458849Seric */
44558849Seric
44668693Seric void
setdaemonoptions(p)44758849Seric setdaemonoptions(p)
44858849Seric register char *p;
44958849Seric {
45058873Seric if (DaemonAddr.sa.sa_family == AF_UNSPEC)
45158873Seric DaemonAddr.sa.sa_family = AF_INET;
45258873Seric
45358849Seric while (p != NULL)
45458849Seric {
45558849Seric register char *f;
45658849Seric register char *v;
45758849Seric
45858849Seric while (isascii(*p) && isspace(*p))
45958849Seric p++;
46058849Seric if (*p == '\0')
46158849Seric break;
46258849Seric f = p;
46358849Seric p = strchr(p, ',');
46458849Seric if (p != NULL)
46558849Seric *p++ = '\0';
46658849Seric v = strchr(f, '=');
46758849Seric if (v == NULL)
46858849Seric continue;
46958849Seric while (isascii(*++v) && isspace(*v))
47058849Seric continue;
47169397Seric if (isascii(*f) && isupper(*f))
47269397Seric *f = tolower(*f);
47358849Seric
47458849Seric switch (*f)
47558849Seric {
47658873Seric case 'F': /* address family */
47758849Seric if (isascii(*v) && isdigit(*v))
47858873Seric DaemonAddr.sa.sa_family = atoi(v);
47969881Seric #if NETINET
48058873Seric else if (strcasecmp(v, "inet") == 0)
48158873Seric DaemonAddr.sa.sa_family = AF_INET;
48258873Seric #endif
48369881Seric #if NETISO
48458873Seric else if (strcasecmp(v, "iso") == 0)
48558873Seric DaemonAddr.sa.sa_family = AF_ISO;
48658873Seric #endif
48769881Seric #if NETNS
48858873Seric else if (strcasecmp(v, "ns") == 0)
48958873Seric DaemonAddr.sa.sa_family = AF_NS;
49058873Seric #endif
49169881Seric #if NETX25
49258873Seric else if (strcasecmp(v, "x.25") == 0)
49358873Seric DaemonAddr.sa.sa_family = AF_CCITT;
49458873Seric #endif
49558849Seric else
49658873Seric syserr("554 Unknown address family %s in Family=option", v);
49758873Seric break;
49858873Seric
49958873Seric case 'A': /* address */
50058873Seric switch (DaemonAddr.sa.sa_family)
50158849Seric {
50269881Seric #if NETINET
50358873Seric case AF_INET:
50458873Seric if (isascii(*v) && isdigit(*v))
50568693Seric DaemonAddr.sin.sin_addr.s_addr = htonl(inet_network(v));
50658873Seric else
50758873Seric {
50858873Seric register struct netent *np;
50958849Seric
51058873Seric np = getnetbyname(v);
51158873Seric if (np == NULL)
51258873Seric syserr("554 network \"%s\" unknown", v);
51358873Seric else
51458873Seric DaemonAddr.sin.sin_addr.s_addr = np->n_net;
51558873Seric }
51658873Seric break;
51758873Seric #endif
51858873Seric
51958873Seric default:
52058873Seric syserr("554 Address= option unsupported for family %d",
52158873Seric DaemonAddr.sa.sa_family);
52258873Seric break;
52358849Seric }
52458849Seric break;
52558849Seric
52658873Seric case 'P': /* port */
52758873Seric switch (DaemonAddr.sa.sa_family)
52858849Seric {
52958873Seric short port;
53058849Seric
53169881Seric #if NETINET
53258873Seric case AF_INET:
53358873Seric if (isascii(*v) && isdigit(*v))
53464366Seric DaemonAddr.sin.sin_port = htons(atoi(v));
53558849Seric else
53658873Seric {
53758873Seric register struct servent *sp;
53858873Seric
53958873Seric sp = getservbyname(v, "tcp");
54058873Seric if (sp == NULL)
54158909Seric syserr("554 service \"%s\" unknown", v);
54258873Seric else
54358873Seric DaemonAddr.sin.sin_port = sp->s_port;
54458873Seric }
54558873Seric break;
54658873Seric #endif
54758873Seric
54869881Seric #if NETISO
54958873Seric case AF_ISO:
55058873Seric /* assume two byte transport selector */
55158873Seric if (isascii(*v) && isdigit(*v))
55264366Seric port = htons(atoi(v));
55358873Seric else
55458873Seric {
55558873Seric register struct servent *sp;
55658873Seric
55758873Seric sp = getservbyname(v, "tcp");
55858873Seric if (sp == NULL)
55958909Seric syserr("554 service \"%s\" unknown", v);
56058873Seric else
56158873Seric port = sp->s_port;
56258873Seric }
56358873Seric bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
56458873Seric break;
56558873Seric #endif
56658873Seric
56758873Seric default:
56858873Seric syserr("554 Port= option unsupported for family %d",
56958873Seric DaemonAddr.sa.sa_family);
57058873Seric break;
57158849Seric }
57258849Seric break;
57359783Seric
57459783Seric case 'L': /* listen queue size */
57559783Seric ListenQueueSize = atoi(v);
57659783Seric break;
57764381Seric
57864381Seric case 'S': /* send buffer size */
57964381Seric TcpSndBufferSize = atoi(v);
58064381Seric break;
58164381Seric
58264381Seric case 'R': /* receive buffer size */
58364381Seric TcpRcvBufferSize = atoi(v);
58464381Seric break;
58569397Seric
58669397Seric default:
58769397Seric syserr("554 DaemonPortOptions parameter \"%s\" unknown", f);
58858849Seric }
58958849Seric }
59058849Seric }
59158849Seric /*
5926039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine.
5936039Seric **
5946039Seric ** Parameters:
5956039Seric ** host -- the name of the host.
5966633Seric ** port -- the port number to connect to.
59753739Seric ** mci -- a pointer to the mail connection information
59853739Seric ** structure to be filled in.
59952106Seric ** usesecureport -- if set, use a low numbered (reserved)
60052106Seric ** port to provide some rudimentary authentication.
6016039Seric **
6026039Seric ** Returns:
6036039Seric ** An exit code telling whether the connection could be
6046039Seric ** made and if not why not.
6056039Seric **
6066039Seric ** Side Effects:
6076039Seric ** none.
6086039Seric */
6095978Seric
61058755Seric SOCKADDR CurHostAddr; /* address of current host */
61158305Seric
61254967Seric int
makeconnection(host,port,mci,usesecureport)61353739Seric makeconnection(host, port, mci, usesecureport)
6146039Seric char *host;
6157286Seric u_short port;
61654967Seric register MCI *mci;
61752106Seric bool usesecureport;
6186039Seric {
61968693Seric register int i = 0;
62068693Seric register int s;
62129430Sbloom register struct hostent *hp = (struct hostent *)NULL;
62258755Seric SOCKADDR addr;
62352106Seric int sav_errno;
62458755Seric int addrlen;
62568693Seric bool firstconnect;
6266039Seric
6276039Seric /*
6286039Seric ** Set up the address for the mailer.
6299308Seric ** Accept "[a.b.c.d]" syntax for host name.
6306039Seric */
6316039Seric
63266334Seric #if NAMED_BIND
63325475Smiriam h_errno = 0;
63435651Seric #endif
63525475Smiriam errno = 0;
63658864Seric bzero(&CurHostAddr, sizeof CurHostAddr);
63764334Seric SmtpPhase = mci->mci_phase = "initial connection";
63858906Seric CurHostName = host;
63925475Smiriam
6409308Seric if (host[0] == '[')
6419308Seric {
64211147Seric long hid;
64356795Seric register char *p = strchr(host, ']');
6449308Seric
64511147Seric if (p != NULL)
6469308Seric {
64711147Seric *p = '\0';
64869881Seric #if NETINET
64911147Seric hid = inet_addr(&host[1]);
65058360Seric if (hid == -1)
65159884Seric #endif
65258360Seric {
65358360Seric /* try it as a host name (avoid MX lookup) */
65468693Seric hp = sm_gethostbyname(&host[1]);
65566349Seric if (hp == NULL && p[-1] == '.')
65666349Seric {
65768693Seric #if NAMED_BIND
65868693Seric int oldopts = _res.options;
65968693Seric
66068693Seric _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
66168693Seric #endif
66266349Seric p[-1] = '\0';
66368693Seric hp = sm_gethostbyname(&host[1]);
66466349Seric p[-1] = '.';
66568693Seric #if NAMED_BIND
66668693Seric _res.options = oldopts;
66768693Seric #endif
66866349Seric }
66958360Seric *p = ']';
67058360Seric goto gothostent;
67158360Seric }
67211147Seric *p = ']';
6739308Seric }
67458360Seric if (p == NULL)
6759308Seric {
67658151Seric usrerr("553 Invalid numeric domain spec \"%s\"", host);
67768857Seric mci->mci_status = "5.1.2";
6789308Seric return (EX_NOHOST);
6799308Seric }
68069881Seric #if NETINET
68159884Seric addr.sin.sin_family = AF_INET; /*XXX*/
68258778Seric addr.sin.sin_addr.s_addr = hid;
68359884Seric #endif
6849308Seric }
6859610Seric else
6869610Seric {
68766349Seric register char *p = &host[strlen(host) - 1];
68866349Seric
68968693Seric hp = sm_gethostbyname(host);
69066349Seric if (hp == NULL && *p == '.')
69166349Seric {
69268693Seric #if NAMED_BIND
69368693Seric int oldopts = _res.options;
69468693Seric
69568693Seric _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
69668693Seric #endif
69766349Seric *p = '\0';
69868693Seric hp = sm_gethostbyname(host);
69966349Seric *p = '.';
70068693Seric #if NAMED_BIND
70168693Seric _res.options = oldopts;
70268693Seric #endif
70366349Seric }
70458360Seric gothostent:
70525475Smiriam if (hp == NULL)
70624945Seric {
70766334Seric #if NAMED_BIND
70868693Seric /* check for name server timeouts */
70968693Seric if (errno == ETIMEDOUT || h_errno == TRY_AGAIN ||
71068693Seric (errno == ECONNREFUSED && UseNameServer))
71168693Seric {
71268693Seric mci->mci_status = "4.4.3";
71325475Smiriam return (EX_TEMPFAIL);
71468693Seric }
71535651Seric #endif
71625475Smiriam return (EX_NOHOST);
71724945Seric }
71858778Seric addr.sa.sa_family = hp->h_addrtype;
71958778Seric switch (hp->h_addrtype)
72058778Seric {
72169881Seric #if NETINET
72258778Seric case AF_INET:
72358755Seric bcopy(hp->h_addr,
72458778Seric &addr.sin.sin_addr,
72568693Seric INADDRSZ);
72658778Seric break;
72758778Seric #endif
72858778Seric
72958778Seric default:
73058755Seric bcopy(hp->h_addr,
73158778Seric addr.sa.sa_data,
73258755Seric hp->h_length);
73358778Seric break;
73458778Seric }
73529430Sbloom i = 1;
7369610Seric }
7379610Seric
7389610Seric /*
7399610Seric ** Determine the port number.
7409610Seric */
7419610Seric
74269840Seric if (port == 0)
7439610Seric {
7449610Seric register struct servent *sp = getservbyname("smtp", "tcp");
7459610Seric
7469610Seric if (sp == NULL)
7479610Seric {
74868745Seric #ifdef LOG
74968745Seric if (LogLevel > 2)
75068745Seric syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown");
75168745Seric #endif
75265169Seric port = htons(25);
7539610Seric }
75465169Seric else
75565169Seric port = sp->s_port;
7569610Seric }
7576039Seric
75858778Seric switch (addr.sa.sa_family)
75958755Seric {
76069881Seric #if NETINET
76158755Seric case AF_INET:
76258778Seric addr.sin.sin_port = port;
76358755Seric addrlen = sizeof (struct sockaddr_in);
76458755Seric break;
76559884Seric #endif
76658755Seric
76769881Seric #if NETISO
76858755Seric case AF_ISO:
76958755Seric /* assume two byte transport selector */
77058755Seric bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
77158755Seric addrlen = sizeof (struct sockaddr_iso);
77258755Seric break;
77358755Seric #endif
77458755Seric
77558755Seric default:
77658778Seric syserr("Can't connect to address family %d", addr.sa.sa_family);
77758755Seric return (EX_NOHOST);
77858755Seric }
77958755Seric
7806039Seric /*
7816039Seric ** Try to actually open the connection.
7826039Seric */
7836039Seric
78459156Seric #ifdef XLA
78559156Seric /* if too many connections, don't bother trying */
78659156Seric if (!xla_noqueue_ok(host))
78759156Seric return EX_TEMPFAIL;
78859156Seric #endif
78959156Seric
79068693Seric firstconnect = TRUE;
79157736Seric for (;;)
79252106Seric {
79357736Seric if (tTd(16, 1))
79458755Seric printf("makeconnection (%s [%s])\n",
79558755Seric host, anynet_ntoa(&addr));
79652106Seric
79758588Seric /* save for logging */
79858588Seric CurHostAddr = addr;
79958588Seric
80057736Seric if (usesecureport)
80157736Seric {
80257736Seric int rport = IPPORT_RESERVED - 1;
8036039Seric
80457736Seric s = rresvport(&rport);
80557736Seric }
80657736Seric else
80757736Seric {
80857736Seric s = socket(AF_INET, SOCK_STREAM, 0);
80957736Seric }
81057736Seric if (s < 0)
81157736Seric {
81257736Seric sav_errno = errno;
81369949Seric syserr("makeconnection: cannot create socket");
81457736Seric goto failure;
81557736Seric }
81610347Seric
81764381Seric #ifdef SO_SNDBUF
81864381Seric if (TcpSndBufferSize > 0)
81964381Seric {
82064381Seric if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
82164561Seric (char *) &TcpSndBufferSize,
82264381Seric sizeof(TcpSndBufferSize)) < 0)
82364381Seric syserr("makeconnection: setsockopt(SO_SNDBUF)");
82464381Seric }
82564381Seric #endif
82664381Seric
82757736Seric if (tTd(16, 1))
82857736Seric printf("makeconnection: fd=%d\n", s);
82957736Seric
83057736Seric /* turn on network debugging? */
83157736Seric if (tTd(16, 101))
83257736Seric {
83357736Seric int on = 1;
83466861Seric (void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
83557736Seric (char *)&on, sizeof on);
83657736Seric }
83757736Seric if (CurEnv->e_xfp != NULL)
83857736Seric (void) fflush(CurEnv->e_xfp); /* for debugging */
83957736Seric errno = 0; /* for debugging */
84058755Seric if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
84157736Seric break;
84257736Seric
84368693Seric /* if running demand-dialed connection, try again */
84468693Seric if (DialDelay > 0 && firstconnect)
84568693Seric {
84668693Seric if (tTd(16, 1))
84768693Seric printf("Connect failed (%s); trying again...\n",
84868693Seric errstring(sav_errno));
84968693Seric firstconnect = FALSE;
85068693Seric sleep(DialDelay);
85168693Seric continue;
85268693Seric }
85368693Seric
85457736Seric /* couldn't connect.... figure out why */
85527744Sbloom sav_errno = errno;
85627744Sbloom (void) close(s);
85768693Seric if (hp != NULL && hp->h_addr_list[i])
85829430Sbloom {
85957736Seric if (tTd(16, 1))
86058755Seric printf("Connect failed (%s); trying new address....\n",
86158755Seric errstring(sav_errno));
86258778Seric switch (addr.sa.sa_family)
86358778Seric {
86469881Seric #if NETINET
86558778Seric case AF_INET:
86658755Seric bcopy(hp->h_addr_list[i++],
86758778Seric &addr.sin.sin_addr,
86868693Seric INADDRSZ);
86958778Seric break;
87058778Seric #endif
87158778Seric
87258778Seric default:
87358755Seric bcopy(hp->h_addr_list[i++],
87458778Seric addr.sa.sa_data,
87552106Seric hp->h_length);
87658778Seric break;
87758778Seric }
87857736Seric continue;
87929430Sbloom }
88029430Sbloom
8816039Seric /* failure, decide if temporary or not */
8826039Seric failure:
88359254Seric #ifdef XLA
88459254Seric xla_host_end(host);
88559254Seric #endif
88658542Seric if (transienterror(sav_errno))
88758542Seric return EX_TEMPFAIL;
88858542Seric else
88958542Seric {
89058542Seric message("%s", errstring(sav_errno));
89158542Seric return (EX_UNAVAILABLE);
8926039Seric }
8936039Seric }
8946039Seric
8956039Seric /* connection ok, put it into canonical form */
89664724Seric if ((mci->mci_out = fdopen(s, "w")) == NULL ||
89764724Seric (s = dup(s)) < 0 ||
89864725Seric (mci->mci_in = fdopen(s, "r")) == NULL)
89964724Seric {
90064724Seric syserr("cannot open SMTP client channel, fd=%d", s);
90164724Seric return EX_TEMPFAIL;
90264724Seric }
9036039Seric
90410098Seric return (EX_OK);
9056039Seric }
90610758Seric /*
90710758Seric ** MYHOSTNAME -- return the name of this host.
90810758Seric **
90910758Seric ** Parameters:
91010758Seric ** hostbuf -- a place to return the name of this host.
91112313Seric ** size -- the size of hostbuf.
91210758Seric **
91310758Seric ** Returns:
91410758Seric ** A list of aliases for this host.
91510758Seric **
91610758Seric ** Side Effects:
91764338Seric ** Adds numeric codes to $=w.
91810758Seric */
9196039Seric
92068693Seric struct hostent *
myhostname(hostbuf,size)92112313Seric myhostname(hostbuf, size)
92210758Seric char hostbuf[];
92312313Seric int size;
92410758Seric {
92558110Seric register struct hostent *hp;
92668693Seric extern bool getcanonname();
92710758Seric
92823120Seric if (gethostname(hostbuf, size) < 0)
92923120Seric {
93023120Seric (void) strcpy(hostbuf, "localhost");
93123120Seric }
93268693Seric hp = sm_gethostbyname(hostbuf);
93366853Seric if (hp == NULL)
93468693Seric return NULL;
93568693Seric if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
93667448Seric {
93768693Seric (void) strncpy(hostbuf, hp->h_name, size - 1);
93868693Seric hostbuf[size - 1] = '\0';
93967448Seric }
94066853Seric
94168693Seric /*
94269877Seric ** If there is still no dot in the name, try looking for a
94369877Seric ** dotted alias.
94468693Seric */
94568693Seric
94669818Seric if (strchr(hostbuf, '.') == NULL)
94768612Seric {
94869877Seric char **ha;
94969818Seric
95069877Seric for (ha = hp->h_aliases; *ha != NULL; ha++)
95169877Seric {
95269877Seric if (strchr(*ha, '.') != NULL)
95369877Seric {
95469877Seric (void) strncpy(hostbuf, *ha, size - 1);
95569877Seric hostbuf[size - 1] = '\0';
95669818Seric break;
95769877Seric }
95869818Seric }
95966853Seric }
96069877Seric
96169877Seric /*
96269877Seric ** If _still_ no dot, wait for a while and try again -- it is
96369877Seric ** possible that some service is starting up. This can result
96469877Seric ** in excessive delays if the system is badly configured, but
96569877Seric ** there really isn't a way around that, particularly given that
96669877Seric ** the config file hasn't been read at this point.
96769877Seric ** All in all, a bit of a mess.
96869877Seric */
96969877Seric
97069877Seric if (strchr(hostbuf, '.') == NULL &&
97169877Seric !getcanonname(hostbuf, size, TRUE))
97269877Seric {
97369877Seric message("My unqualifed host name (%s) unknown; sleeping for retry",
97469877Seric hostbuf);
97569877Seric sleep(60);
97669877Seric (void) getcanonname(hostbuf, size, TRUE);
97769877Seric }
97868693Seric return (hp);
97910758Seric }
98051315Seric /*
98158951Seric ** GETAUTHINFO -- get the real host name asociated with a file descriptor
98258308Seric **
98358951Seric ** Uses RFC1413 protocol to try to get info from the other end.
98458951Seric **
98558308Seric ** Parameters:
98658308Seric ** fd -- the descriptor
98758308Seric **
98858308Seric ** Returns:
98958951Seric ** The user@host information associated with this descriptor.
99058308Seric */
99158308Seric
99258951Seric static jmp_buf CtxAuthTimeout;
99358951Seric
99468693Seric static void
authtimeout()99558951Seric authtimeout()
99658951Seric {
99758951Seric longjmp(CtxAuthTimeout, 1);
99858951Seric }
99958951Seric
100058308Seric char *
getauthinfo(fd)100158951Seric getauthinfo(fd)
100258308Seric int fd;
100358308Seric {
100458951Seric int falen;
100559104Seric register char *p;
100658951Seric SOCKADDR la;
100758951Seric int lalen;
100858951Seric register struct servent *sp;
100958951Seric int s;
101058951Seric int i;
101158951Seric EVENT *ev;
101268444Seric int nleft;
101368462Seric char ibuf[MAXNAME + 1];
101458951Seric static char hbuf[MAXNAME * 2 + 2];
101558951Seric extern char *hostnamebyanyaddr();
101658308Seric
101766761Seric falen = sizeof RealHostAddr;
101868693Seric if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 ||
101968693Seric falen <= 0 || RealHostAddr.sa.sa_family == 0)
102058951Seric {
102158951Seric (void) sprintf(hbuf, "%s@localhost", RealUserName);
102258957Seric if (tTd(9, 1))
102358951Seric printf("getauthinfo: %s\n", hbuf);
102458951Seric return hbuf;
102558951Seric }
102658951Seric
102766761Seric if (RealHostName == NULL)
102866761Seric {
102966761Seric /* translate that to a host name */
103066761Seric RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
103166761Seric }
103266761Seric
103365831Seric if (TimeOuts.to_ident == 0)
103465831Seric goto noident;
103565831Seric
103658951Seric lalen = sizeof la;
103766761Seric if (RealHostAddr.sa.sa_family != AF_INET ||
103858951Seric getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
103958951Seric la.sa.sa_family != AF_INET)
104058951Seric {
104158951Seric /* no ident info */
104258951Seric goto noident;
104358951Seric }
104458951Seric
104558951Seric /* create ident query */
104668457Seric (void) sprintf(ibuf, "%d,%d\r\n",
104766761Seric ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
104858951Seric
104958951Seric /* create local address */
105064747Seric la.sin.sin_port = 0;
105158951Seric
105258951Seric /* create foreign address */
105358951Seric sp = getservbyname("auth", "tcp");
105458951Seric if (sp != NULL)
105566761Seric RealHostAddr.sin.sin_port = sp->s_port;
105658308Seric else
105766761Seric RealHostAddr.sin.sin_port = htons(113);
105858951Seric
105958951Seric s = -1;
106058951Seric if (setjmp(CtxAuthTimeout) != 0)
106158951Seric {
106258951Seric if (s >= 0)
106358951Seric (void) close(s);
106458951Seric goto noident;
106558951Seric }
106658951Seric
106758951Seric /* put a timeout around the whole thing */
106864255Seric ev = setevent(TimeOuts.to_ident, authtimeout, 0);
106958951Seric
107064747Seric /* connect to foreign IDENT server using same address as SMTP socket */
107158951Seric s = socket(AF_INET, SOCK_STREAM, 0);
107258951Seric if (s < 0)
107358951Seric {
107458951Seric clrevent(ev);
107558951Seric goto noident;
107658951Seric }
107764747Seric if (bind(s, &la.sa, sizeof la.sin) < 0 ||
107866761Seric connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
107958951Seric {
108066011Seric goto closeident;
108158951Seric }
108258951Seric
108358957Seric if (tTd(9, 10))
108468457Seric printf("getauthinfo: sent %s", ibuf);
108558951Seric
108658951Seric /* send query */
108768457Seric if (write(s, ibuf, strlen(ibuf)) < 0)
108858951Seric goto closeident;
108958951Seric
109058951Seric /* get result */
109168457Seric p = &ibuf[0];
109268525Seric nleft = sizeof ibuf - 1;
109368444Seric while ((i = read(s, p, nleft)) > 0)
109468444Seric {
109568444Seric p += i;
109668444Seric nleft -= i;
109768444Seric }
109858951Seric (void) close(s);
109958951Seric clrevent(ev);
110068457Seric if (i < 0 || p == &ibuf[0])
110158951Seric goto noident;
110258951Seric
110368444Seric if (*--p == '\n' && *--p == '\r')
110468444Seric p--;
110568444Seric *++p = '\0';
110668444Seric
110758957Seric if (tTd(9, 3))
110868457Seric printf("getauthinfo: got %s\n", ibuf);
110958951Seric
111058951Seric /* parse result */
111168457Seric p = strchr(ibuf, ':');
111258951Seric if (p == NULL)
111358951Seric {
111458951Seric /* malformed response */
111558951Seric goto noident;
111658951Seric }
111758951Seric while (isascii(*++p) && isspace(*p))
111858951Seric continue;
111958951Seric if (strncasecmp(p, "userid", 6) != 0)
112058951Seric {
112158951Seric /* presumably an error string */
112258951Seric goto noident;
112358951Seric }
112458951Seric p += 6;
112558951Seric while (isascii(*p) && isspace(*p))
112658951Seric p++;
112758951Seric if (*p++ != ':')
112858951Seric {
112958951Seric /* either useridxx or malformed response */
113058951Seric goto noident;
113158951Seric }
113258951Seric
113358951Seric /* p now points to the OSTYPE field */
113468693Seric while (isascii(*p) && isspace(*p))
113568693Seric p++;
113668693Seric if (strncasecmp(p, "other", 5) == 0 &&
113768693Seric (p[5] == ':' || p[5] == ' ' || p[5] == ',' || p[5] == '\0'))
113868693Seric {
113968693Seric /* not useful information */
114068693Seric goto noident;
114168693Seric }
114258951Seric p = strchr(p, ':');
114358951Seric if (p == NULL)
114458951Seric {
114558951Seric /* malformed response */
114658951Seric goto noident;
114758951Seric }
114858951Seric
114958957Seric /* 1413 says don't do this -- but it's broken otherwise */
115058957Seric while (isascii(*++p) && isspace(*p))
115158957Seric continue;
115258957Seric
115367935Seric /* p now points to the authenticated name -- copy carefully */
115468457Seric cleanstrcpy(hbuf, p, MAXNAME);
115568875Seric i = strlen(hbuf);
115667935Seric hbuf[i++] = '@';
115767935Seric strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName);
115869601Seric goto postident;
115958957Seric
116066011Seric closeident:
116166011Seric (void) close(s);
116266011Seric clrevent(ev);
116366011Seric
116458957Seric noident:
116566003Seric if (RealHostName == NULL)
116666003Seric {
116766003Seric if (tTd(9, 1))
116866003Seric printf("getauthinfo: NULL\n");
116966003Seric return NULL;
117066003Seric }
117158957Seric (void) strcpy(hbuf, RealHostName);
117258957Seric
117369601Seric postident:
117469601Seric #if IP_SRCROUTE
117569601Seric /*
117669601Seric ** Extract IP source routing information.
117769601Seric **
117869601Seric ** Format of output for a connection from site a through b
117969601Seric ** through c to d:
118069601Seric ** loose: @site-c@site-b:site-a
118169601Seric ** strict: !@site-c@site-b:site-a
118269601Seric **
118369601Seric ** o - pointer within ipopt_list structure.
118469601Seric ** q - pointer within ls/ss rr route data
118569601Seric ** p - pointer to hbuf
118669601Seric */
118769601Seric
118869601Seric if (RealHostAddr.sa.sa_family == AF_INET)
118969601Seric {
119069601Seric int ipoptlen, j;
119169637Seric u_char *q;
119269601Seric u_char *o;
119369601Seric struct in_addr addr;
119469601Seric struct ipoption ipopt;
119569601Seric
119669601Seric ipoptlen = sizeof ipopt;
119769601Seric if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS,
119869601Seric (char *) &ipopt, &ipoptlen) < 0)
119969601Seric goto noipsr;
120069601Seric if (ipoptlen == 0)
120169601Seric goto noipsr;
120269637Seric o = (u_char *) ipopt.ipopt_list;
120369731Seric while (o != NULL && o < (u_char *) &ipopt + ipoptlen)
120469601Seric {
120569601Seric switch (*o)
120669601Seric {
120769601Seric case IPOPT_EOL:
120869601Seric o = NULL;
120969601Seric break;
121069601Seric
121169601Seric case IPOPT_NOP:
121269601Seric o++;
121369601Seric break;
121469601Seric
121569601Seric case IPOPT_SSRR:
121669601Seric case IPOPT_LSRR:
121769601Seric p = &hbuf[strlen(hbuf)];
121869601Seric sprintf(p, " [%s@%s",
121969601Seric *o == IPOPT_SSRR ? "!" : "",
122069601Seric inet_ntoa(ipopt.ipopt_dst));
122169601Seric p += strlen(p);
122269601Seric
122369601Seric /* o[1] is option length */
122469601Seric j = *++o / sizeof(struct in_addr) - 1;
122569601Seric
122669601Seric /* q skips length and router pointer to data */
122769601Seric q = o + 2;
122869601Seric for ( ; j >= 0; j--)
122969601Seric {
123069601Seric memcpy(&addr, q, sizeof(addr));
123169637Seric sprintf(p, "%c%s",
123269601Seric j ? '@' : ':',
123369601Seric inet_ntoa(addr));
123469637Seric p += strlen(p);
123569637Seric q += sizeof(struct in_addr);
123669601Seric }
123769601Seric o += *o;
123869601Seric break;
123969601Seric
124069601Seric default:
124169601Seric /* Skip over option */
124269601Seric o += o[1];
124369601Seric break;
124469601Seric }
124569601Seric }
124669601Seric strcat(hbuf,"]");
124769601Seric goto postipsr;
124869601Seric }
124969601Seric #endif
125069601Seric
125169601Seric noipsr:
125266003Seric if (RealHostName != NULL && RealHostName[0] != '[')
125358951Seric {
125458951Seric p = &hbuf[strlen(hbuf)];
125558951Seric (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
125658951Seric }
125769601Seric
125869601Seric postipsr:
125958957Seric if (tTd(9, 1))
126058951Seric printf("getauthinfo: %s\n", hbuf);
126158308Seric return hbuf;
126258308Seric }
126358308Seric /*
126460089Seric ** HOST_MAP_LOOKUP -- turn a hostname into canonical form
126553751Seric **
126653751Seric ** Parameters:
126756823Seric ** map -- a pointer to this map (unused).
126860089Seric ** name -- the (presumably unqualified) hostname.
126960257Seric ** av -- unused -- for compatibility with other mapping
127055019Seric ** functions.
127159084Seric ** statp -- an exit status (out parameter) -- set to
127259084Seric ** EX_TEMPFAIL if the name server is unavailable.
127353751Seric **
127453751Seric ** Returns:
127553751Seric ** The mapping, if found.
127653751Seric ** NULL if no mapping found.
127753751Seric **
127853751Seric ** Side Effects:
127953751Seric ** Looks up the host specified in hbuf. If it is not
128053751Seric ** the canonical name for that host, return the canonical
128153751Seric ** name.
128253751Seric */
128351315Seric
128453751Seric char *
host_map_lookup(map,name,av,statp)128560257Seric host_map_lookup(map, name, av, statp)
128656823Seric MAP *map;
128760089Seric char *name;
128860257Seric char **av;
128959084Seric int *statp;
129016911Seric {
129116911Seric register struct hostent *hp;
129268693Seric struct in_addr in_addr;
129356823Seric char *cp;
129459671Seric register STAB *s;
129568693Seric char hbuf[MAXNAME + 1];
129616911Seric
129725574Smiriam /*
129859671Seric ** See if we have already looked up this name. If so, just
129959671Seric ** return it.
130059671Seric */
130153751Seric
130260089Seric s = stab(name, ST_NAMECANON, ST_ENTER);
130359671Seric if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
130459671Seric {
130559986Seric if (tTd(9, 1))
130660089Seric printf("host_map_lookup(%s) => CACHE %s\n",
130769511Seric name,
130869511Seric s->s_namecanon.nc_cname == NULL
130969511Seric ? "NULL"
131069512Seric : s->s_namecanon.nc_cname);
131159671Seric errno = s->s_namecanon.nc_errno;
131266334Seric #if NAMED_BIND
131359671Seric h_errno = s->s_namecanon.nc_herrno;
131466029Seric #endif
131559671Seric *statp = s->s_namecanon.nc_stat;
131668817Seric if (*statp == EX_TEMPFAIL)
131765199Seric {
131868857Seric CurEnv->e_status = "4.4.3";
131968817Seric usrerr("451 %s: Name server timeout",
132065199Seric shortenstring(name, 33));
132165199Seric }
132259671Seric return s->s_namecanon.nc_cname;
132359671Seric }
132459671Seric
132559671Seric /*
132659671Seric ** If first character is a bracket, then it is an address
132759671Seric ** lookup. Address is copied into a temporary buffer to
132860089Seric ** strip the brackets and to preserve name if address is
132959671Seric ** unknown.
133059671Seric */
133159671Seric
133260089Seric if (*name != '[')
133353751Seric {
133455019Seric extern bool getcanonname();
133555019Seric
133658798Seric if (tTd(9, 1))
133760089Seric printf("host_map_lookup(%s) => ", name);
133859671Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */
133968693Seric if (strlen(name) < sizeof hbuf)
134068693Seric (void) strcpy(hbuf, name);
134168693Seric else
134268693Seric {
134368693Seric bcopy(name, hbuf, sizeof hbuf - 1);
134468693Seric hbuf[sizeof hbuf - 1] = '\0';
134568693Seric }
134669896Seric if (getcanonname(hbuf, sizeof hbuf - 1, !HasWildcardMX))
134758796Seric {
134858796Seric if (tTd(9, 1))
134958796Seric printf("%s\n", hbuf);
135060257Seric cp = map_rewrite(map, hbuf, strlen(hbuf), av);
135160257Seric s->s_namecanon.nc_cname = newstr(cp);
135260257Seric return cp;
135358796Seric }
135453751Seric else
135558796Seric {
135659084Seric register struct hostent *hp;
135759084Seric
135866029Seric s->s_namecanon.nc_errno = errno;
135966334Seric #if NAMED_BIND
136066029Seric s->s_namecanon.nc_herrno = h_errno;
136158796Seric if (tTd(9, 1))
136259084Seric printf("FAIL (%d)\n", h_errno);
136359084Seric switch (h_errno)
136459084Seric {
136559084Seric case TRY_AGAIN:
136659596Seric if (UseNameServer)
136759734Seric {
136868857Seric CurEnv->e_status = "4.4.3";
136968817Seric usrerr("451 %s: Name server timeout",
137065199Seric shortenstring(name, 33));
137159734Seric }
137259084Seric *statp = EX_TEMPFAIL;
137359084Seric break;
137459084Seric
137559084Seric case HOST_NOT_FOUND:
137668881Seric case NO_DATA:
137759084Seric *statp = EX_NOHOST;
137859084Seric break;
137959084Seric
138059084Seric case NO_RECOVERY:
138159084Seric *statp = EX_SOFTWARE;
138259084Seric break;
138359084Seric
138459084Seric default:
138559084Seric *statp = EX_UNAVAILABLE;
138659084Seric break;
138759084Seric }
138866029Seric #else
138966029Seric if (tTd(9, 1))
139066029Seric printf("FAIL\n");
139166029Seric *statp = EX_NOHOST;
139266029Seric #endif
139359671Seric s->s_namecanon.nc_stat = *statp;
139468693Seric if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) ||
139568693Seric UseNameServer)
139659084Seric return NULL;
139759084Seric
139859084Seric /*
139959084Seric ** Try to look it up in /etc/hosts
140059084Seric */
140159084Seric
140268693Seric hp = sm_gethostbyname(name);
140359084Seric if (hp == NULL)
140459084Seric {
140559084Seric /* no dice there either */
140659671Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST;
140759084Seric return NULL;
140859084Seric }
140959084Seric
141059671Seric s->s_namecanon.nc_stat = *statp = EX_OK;
141160257Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
141260257Seric s->s_namecanon.nc_cname = newstr(cp);
141360257Seric return cp;
141458796Seric }
141553751Seric }
141660089Seric if ((cp = strchr(name, ']')) == NULL)
141753751Seric return (NULL);
141840994Sbostic *cp = '\0';
141968693Seric in_addr.s_addr = inet_addr(&name[1]);
142058110Seric
142158110Seric /* nope -- ask the name server */
142268693Seric hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET);
142359671Seric s->s_namecanon.nc_errno = errno;
142466334Seric #if NAMED_BIND
142559671Seric s->s_namecanon.nc_herrno = h_errno;
142666029Seric #endif
142759671Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */
142833932Sbostic if (hp == NULL)
142959671Seric {
143059671Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST;
143153751Seric return (NULL);
143259671Seric }
143353751Seric
143458110Seric /* found a match -- copy out */
143560257Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
143659671Seric s->s_namecanon.nc_stat = *statp = EX_OK;
143760257Seric s->s_namecanon.nc_cname = newstr(cp);
143860257Seric return cp;
143933932Sbostic }
144058755Seric /*
144158755Seric ** ANYNET_NTOA -- convert a network address to printable form.
144258755Seric **
144358755Seric ** Parameters:
144458755Seric ** sap -- a pointer to a sockaddr structure.
144558755Seric **
144658755Seric ** Returns:
144758755Seric ** A printable version of that sockaddr.
144858755Seric */
144916911Seric
145069881Seric #if NETLINK
145169516Seric # include <net/if_dl.h>
145269516Seric #endif
145369516Seric
145458755Seric char *
anynet_ntoa(sap)145558755Seric anynet_ntoa(sap)
145658755Seric register SOCKADDR *sap;
145758755Seric {
145858755Seric register char *bp;
145958755Seric register char *ap;
146058755Seric int l;
146164734Seric static char buf[100];
146258755Seric
146358798Seric /* check for null/zero family */
146458798Seric if (sap == NULL)
146558798Seric return "NULLADDR";
146658798Seric if (sap->sa.sa_family == 0)
146758798Seric return "0";
146858798Seric
146964734Seric switch (sap->sa.sa_family)
147064734Seric {
147169881Seric #if NETUNIX
147264734Seric case AF_UNIX:
147364758Seric if (sap->sunix.sun_path[0] != '\0')
147464758Seric sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
147564734Seric else
147664734Seric sprintf(buf, "[UNIX: localhost]");
147764734Seric return buf;
147864734Seric #endif
147964734Seric
148069881Seric #if NETINET
148164734Seric case AF_INET:
148268776Seric return inet_ntoa(sap->sin.sin_addr);
148358778Seric #endif
148458755Seric
148569881Seric #if NETLINK
148669516Seric case AF_LINK:
148769516Seric sprintf(buf, "[LINK: %s]",
148869516Seric link_ntoa((struct sockaddr_dl *) &sap->sa));
148969516Seric return buf;
149069516Seric #endif
149164734Seric default:
149269516Seric /* this case is needed when nothing is #defined */
149369516Seric /* in order to keep the switch syntactically correct */
149469516Seric break;
149564734Seric }
149664734Seric
149758755Seric /* unknown family -- just dump bytes */
149858778Seric (void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
149958755Seric bp = &buf[strlen(buf)];
150058778Seric ap = sap->sa.sa_data;
150158778Seric for (l = sizeof sap->sa.sa_data; --l >= 0; )
150258755Seric {
150358755Seric (void) sprintf(bp, "%02x:", *ap++ & 0377);
150458755Seric bp += 3;
150558755Seric }
150658755Seric *--bp = '\0';
150758755Seric return buf;
150858755Seric }
150958951Seric /*
151058951Seric ** HOSTNAMEBYANYADDR -- return name of host based on address
151158951Seric **
151258951Seric ** Parameters:
151358951Seric ** sap -- SOCKADDR pointer
151458951Seric **
151558951Seric ** Returns:
151658951Seric ** text representation of host name.
151758951Seric **
151858951Seric ** Side Effects:
151958951Seric ** none.
152058951Seric */
152158755Seric
152258951Seric char *
hostnamebyanyaddr(sap)152358951Seric hostnamebyanyaddr(sap)
152458951Seric register SOCKADDR *sap;
152558951Seric {
152658951Seric register struct hostent *hp;
152764734Seric int saveretry;
152858951Seric
152966334Seric #if NAMED_BIND
153059042Seric /* shorten name server timeout to avoid higher level timeouts */
153159042Seric saveretry = _res.retry;
153259042Seric _res.retry = 3;
153359042Seric #endif /* NAMED_BIND */
153459042Seric
153558951Seric switch (sap->sa.sa_family)
153658951Seric {
153769881Seric #if NETINET
153858951Seric case AF_INET:
153968693Seric hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
154068693Seric INADDRSZ,
154158951Seric AF_INET);
154258951Seric break;
154358951Seric #endif
154458951Seric
154569881Seric #if NETISO
154658951Seric case AF_ISO:
154768693Seric hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
154858951Seric sizeof sap->siso.siso_addr,
154958951Seric AF_ISO);
155058951Seric break;
155158951Seric #endif
155258951Seric
155364734Seric case AF_UNIX:
155464734Seric hp = NULL;
155564734Seric break;
155664734Seric
155758951Seric default:
155868693Seric hp = sm_gethostbyaddr(sap->sa.sa_data,
155958951Seric sizeof sap->sa.sa_data,
156058951Seric sap->sa.sa_family);
156158951Seric break;
156258951Seric }
156358951Seric
156466334Seric #if NAMED_BIND
156559042Seric _res.retry = saveretry;
156659042Seric #endif /* NAMED_BIND */
156759042Seric
156858951Seric if (hp != NULL)
156958951Seric return hp->h_name;
157058951Seric else
157158951Seric {
157258951Seric /* produce a dotted quad */
157358951Seric static char buf[512];
157458951Seric
157558951Seric (void) sprintf(buf, "[%s]", anynet_ntoa(sap));
157658951Seric return buf;
157758951Seric }
157858951Seric }
157958951Seric
158056795Seric # else /* DAEMON */
158116911Seric /* code for systems without sophisticated networking */
158210758Seric
158310758Seric /*
158410758Seric ** MYHOSTNAME -- stub version for case of no daemon code.
158511297Seric **
158611297Seric ** Can't convert to upper case here because might be a UUCP name.
158712313Seric **
158812313Seric ** Mark, you can change this to be anything you want......
158910758Seric */
159010758Seric
159110758Seric char **
myhostname(hostbuf,size)159212313Seric myhostname(hostbuf, size)
159310758Seric char hostbuf[];
159412313Seric int size;
159510758Seric {
159610758Seric register FILE *f;
159710758Seric
159810758Seric hostbuf[0] = '\0';
159910758Seric f = fopen("/usr/include/whoami", "r");
160010758Seric if (f != NULL)
160110758Seric {
160212313Seric (void) fgets(hostbuf, size, f);
160310758Seric fixcrlf(hostbuf, TRUE);
160410758Seric (void) fclose(f);
160510758Seric }
160610758Seric return (NULL);
160710758Seric }
160816911Seric /*
160958951Seric ** GETAUTHINFO -- get the real host name asociated with a file descriptor
161058308Seric **
161158308Seric ** Parameters:
161258308Seric ** fd -- the descriptor
161358308Seric **
161458308Seric ** Returns:
161558308Seric ** The host name associated with this descriptor, if it can
161658308Seric ** be determined.
161758308Seric ** NULL otherwise.
161858308Seric **
161958308Seric ** Side Effects:
162058308Seric ** none
162158308Seric */
162258308Seric
162358308Seric char *
getauthinfo(fd)162458951Seric getauthinfo(fd)
162558308Seric int fd;
162658308Seric {
162758308Seric return NULL;
162858308Seric }
162958308Seric /*
163016911Seric ** MAPHOSTNAME -- turn a hostname into canonical form
163116911Seric **
163216911Seric ** Parameters:
163356823Seric ** map -- a pointer to the database map.
163460089Seric ** name -- a buffer containing a hostname.
163553751Seric ** avp -- a pointer to a (cf file defined) argument vector.
163659084Seric ** statp -- an exit status (out parameter).
163716911Seric **
163816911Seric ** Returns:
163953751Seric ** mapped host name
164051315Seric ** FALSE otherwise.
164116911Seric **
164216911Seric ** Side Effects:
164360089Seric ** Looks up the host specified in name. If it is not
164416911Seric ** the canonical name for that host, replace it with
164516911Seric ** the canonical name. If the name is unknown, or it
164616911Seric ** is already the canonical name, leave it unchanged.
164716911Seric */
164810758Seric
164916911Seric /*ARGSUSED*/
165053751Seric char *
host_map_lookup(map,name,avp,statp)165160089Seric host_map_lookup(map, name, avp, statp)
165256823Seric MAP *map;
165360089Seric char *name;
165453751Seric char **avp;
165559084Seric char *statp;
165616911Seric {
165759084Seric register struct hostent *hp;
165859084Seric
165968693Seric hp = sm_gethostbyname(name);
166059084Seric if (hp != NULL)
166159084Seric return hp->h_name;
166259084Seric *statp = EX_NOHOST;
166353751Seric return NULL;
166416911Seric }
166516911Seric
166656795Seric #endif /* DAEMON */
1667