1939f5b94Sdist /* 20942ea6aSbostic * Copyright (c) 1983 Eric P. Allman 3c9d2fa25Sbostic * Copyright (c) 1988, 1993 4c9d2fa25Sbostic * The Regents of the University of California. All rights reserved. 5da1c6175Sbostic * 63bc94712Sbostic * %sccs.include.redist.c% 7939f5b94Sdist */ 8939f5b94Sdist 97aa493c5Seric #include <errno.h> 106c05f684Sbostic #include "sendmail.h" 117fa39d90Seric 12af5e902cSeric #ifndef lint 13da1c6175Sbostic #ifdef DAEMON 14*2b9178d3Seric static char sccsid[] = "@(#)daemon.c 8.5 (Berkeley) 07/26/93 (with daemon mode)"; 15d0a9e852Seric #else 16*2b9178d3Seric static char sccsid[] = "@(#)daemon.c 8.5 (Berkeley) 07/26/93 (without daemon mode)"; 17da1c6175Sbostic #endif 18da1c6175Sbostic #endif /* not lint */ 19da1c6175Sbostic 20da1c6175Sbostic #ifdef DAEMON 21d0a9e852Seric 221c71e510Seric # include <netdb.h> 231e1663f7Swnj # include <sys/wait.h> 24af5e902cSeric # include <sys/time.h> 25d0a9e852Seric 263490b9dfSeric #ifdef NAMED_BIND 273490b9dfSeric # include <arpa/nameser.h> 283490b9dfSeric # include <resolv.h> 293490b9dfSeric #endif 303490b9dfSeric 317fa39d90Seric /* 327fa39d90Seric ** DAEMON.C -- routines to use when running as a daemon. 3347b12ae1Seric ** 3447b12ae1Seric ** This entire file is highly dependent on the 4.2 BSD 3547b12ae1Seric ** interprocess communication primitives. No attempt has 3647b12ae1Seric ** been made to make this file portable to Version 7, 3747b12ae1Seric ** Version 6, MPX files, etc. If you should try such a 3847b12ae1Seric ** thing yourself, I recommend chucking the entire file 3947b12ae1Seric ** and starting from scratch. Basic semantics are: 4047b12ae1Seric ** 4147b12ae1Seric ** getrequests() 4247b12ae1Seric ** Opens a port and initiates a connection. 4347b12ae1Seric ** Returns in a child. Must set InChannel and 4447b12ae1Seric ** OutChannel appropriately. 45b7d7afcbSeric ** clrdaemon() 46b7d7afcbSeric ** Close any open files associated with getting 47b7d7afcbSeric ** the connection; this is used when running the queue, 48b7d7afcbSeric ** etc., to avoid having extra file descriptors during 49b7d7afcbSeric ** the queue run and to avoid confusing the network 50b7d7afcbSeric ** code (if it cares). 51914346b1Seric ** makeconnection(host, port, outfile, infile, usesecureport) 5247b12ae1Seric ** Make a connection to the named host on the given 5347b12ae1Seric ** port. Set *outfile and *infile to the files 5447b12ae1Seric ** appropriate for communication. Returns zero on 5547b12ae1Seric ** success, else an exit status describing the 5647b12ae1Seric ** error. 5708de856eSeric ** host_map_lookup(map, hbuf, avp, pstat) 5805b57da8Seric ** Convert the entry in hbuf into a canonical form. 597fa39d90Seric */ 607fa39d90Seric /* 617fa39d90Seric ** GETREQUESTS -- open mail IPC port and get requests. 627fa39d90Seric ** 637fa39d90Seric ** Parameters: 647fa39d90Seric ** none. 657fa39d90Seric ** 667fa39d90Seric ** Returns: 677fa39d90Seric ** none. 687fa39d90Seric ** 697fa39d90Seric ** Side Effects: 707fa39d90Seric ** Waits until some interesting activity occurs. When 717fa39d90Seric ** it does, a child is created to process it, and the 727fa39d90Seric ** parent waits for completion. Return from this 73147303b1Seric ** routine is always in the child. The file pointers 74147303b1Seric ** "InChannel" and "OutChannel" should be set to point 75147303b1Seric ** to the communication channel. 767fa39d90Seric */ 777fa39d90Seric 78b7d7afcbSeric int DaemonSocket = -1; /* fd describing socket */ 79bfb80540Seric SOCKADDR DaemonAddr; /* socket for incoming */ 80bfc1eaf8Seric int ListenQueueSize = 10; /* size of listen queue */ 811c71e510Seric 827fa39d90Seric getrequests() 837fa39d90Seric { 841c71e510Seric int t; 851c71e510Seric register struct servent *sp; 867868dfc2Seric int on = 1; 8715d084d5Seric bool refusingconnections = TRUE; 880aae1086Seric FILE *pidf; 899b100374Sbostic extern void reapchild(); 90eb889047Seric 91a8268164Seric /* 921c71e510Seric ** Set up the address for the mailer. 93eb889047Seric */ 94eb889047Seric 95bfb80540Seric if (DaemonAddr.sin.sin_family == 0) 96bfb80540Seric DaemonAddr.sin.sin_family = AF_INET; 97bfb80540Seric if (DaemonAddr.sin.sin_addr.s_addr == 0) 98bfb80540Seric DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY; 99bfb80540Seric if (DaemonAddr.sin.sin_port == 0) 100bfb80540Seric { 1011c71e510Seric sp = getservbyname("smtp", "tcp"); 1021c71e510Seric if (sp == NULL) 103d0a9e852Seric { 104ad977999Seric syserr("554 service \"smtp\" unknown"); 105a1961f2aSeric goto severe; 1061c71e510Seric } 107bfb80540Seric DaemonAddr.sin.sin_port = sp->s_port; 108bfb80540Seric } 1091c71e510Seric 1101c71e510Seric /* 1111c71e510Seric ** Try to actually open the connection. 1121c71e510Seric */ 1131c71e510Seric 1141c71e510Seric if (tTd(15, 1)) 115bfb80540Seric printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port); 1161c71e510Seric 1171c71e510Seric /* get a socket for the SMTP connection */ 11898e28903Seric DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0); 119b7d7afcbSeric if (DaemonSocket < 0) 1201c71e510Seric { 1211c71e510Seric /* probably another daemon already */ 1221c71e510Seric syserr("getrequests: can't create socket"); 1231c71e510Seric severe: 124b0ba8827Seric # ifdef LOG 125b0ba8827Seric if (LogLevel > 0) 1260c034190Seric syslog(LOG_ALERT, "problem creating SMTP socket"); 1276c2c3107Seric # endif /* LOG */ 12847b12ae1Seric finis(); 129d0a9e852Seric } 1301b6e4a15Seric 1311b6e4a15Seric /* turn on network debugging? */ 132a2ef5fa4Seric if (tTd(15, 101)) 13352308a50Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 1341b6e4a15Seric 1357868dfc2Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 1367868dfc2Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 1377868dfc2Seric 13898e28903Seric switch (DaemonAddr.sa.sa_family) 13998e28903Seric { 14098e28903Seric # ifdef NETINET 14198e28903Seric case AF_INET: 14298e28903Seric t = sizeof DaemonAddr.sin; 14398e28903Seric break; 14498e28903Seric # endif 14598e28903Seric 14698e28903Seric # ifdef NETISO 14798e28903Seric case AF_ISO: 14898e28903Seric t = sizeof DaemonAddr.siso; 14998e28903Seric break; 15098e28903Seric # endif 15198e28903Seric 15298e28903Seric default: 15398e28903Seric t = sizeof DaemonAddr; 15498e28903Seric break; 15598e28903Seric } 15698e28903Seric 15798e28903Seric if (bind(DaemonSocket, &DaemonAddr.sa, t) < 0) 1581c71e510Seric { 1591c71e510Seric syserr("getrequests: cannot bind"); 160b7d7afcbSeric (void) close(DaemonSocket); 1611c71e510Seric goto severe; 1621c71e510Seric } 1631c71e510Seric 164*2b9178d3Seric (void) setsignal(SIGCHLD, reapchild); 16552308a50Seric 1660aae1086Seric /* write the pid to the log file for posterity */ 1670aae1086Seric pidf = fopen(PidFile, "w"); 1680aae1086Seric if (pidf != NULL) 1690aae1086Seric { 17037950f67Seric extern char *CommandLineArgs; 17137950f67Seric 17237950f67Seric /* write the process id on line 1 */ 1730aae1086Seric fprintf(pidf, "%d\n", getpid()); 17437950f67Seric 17537950f67Seric /* line 2 contains all command line flags */ 17637950f67Seric fprintf(pidf, "%s\n", CommandLineArgs); 17737950f67Seric 17837950f67Seric /* flush and close */ 1790aae1086Seric fclose(pidf); 1800aae1086Seric } 1810aae1086Seric 1820aae1086Seric 1831c71e510Seric if (tTd(15, 1)) 184b7d7afcbSeric printf("getrequests: %d\n", DaemonSocket); 1851c71e510Seric 1861c71e510Seric for (;;) 1871c71e510Seric { 1883a099713Seric register int pid; 189a44d5a5eSeric auto int lotherend; 19015d084d5Seric extern bool refuseconnections(); 1913a099713Seric 1923a099713Seric /* see if we are rejecting connections */ 19315d084d5Seric CurrentLA = getla(); 19415d084d5Seric if (refuseconnections()) 1956775ec03Sbostic { 19615d084d5Seric if (!refusingconnections) 19715d084d5Seric { 19815d084d5Seric /* don't queue so peer will fail quickly */ 19915d084d5Seric (void) listen(DaemonSocket, 0); 20015d084d5Seric refusingconnections = TRUE; 20115d084d5Seric } 20271e5e267Seric setproctitle("rejecting connections: load average: %d", 20371e5e267Seric CurrentLA); 2043a099713Seric sleep(5); 20515d084d5Seric continue; 20615d084d5Seric } 20715d084d5Seric 20815d084d5Seric if (refusingconnections) 20915d084d5Seric { 21015d084d5Seric /* start listening again */ 211bfc1eaf8Seric if (listen(DaemonSocket, ListenQueueSize) < 0) 21215d084d5Seric { 21315d084d5Seric syserr("getrequests: cannot listen"); 21415d084d5Seric (void) close(DaemonSocket); 21515d084d5Seric goto severe; 21615d084d5Seric } 21715d084d5Seric setproctitle("accepting connections"); 21815d084d5Seric refusingconnections = FALSE; 2196775ec03Sbostic } 220a44d5a5eSeric 2211c71e510Seric /* wait for a connection */ 2221c71e510Seric do 2231c71e510Seric { 2241c71e510Seric errno = 0; 2259f9a15b6Skarels lotherend = sizeof RealHostAddr; 2269b100374Sbostic t = accept(DaemonSocket, 2279b100374Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 2281c71e510Seric } while (t < 0 && errno == EINTR); 2291c71e510Seric if (t < 0) 2301c71e510Seric { 2311c71e510Seric syserr("getrequests: accept"); 2321c71e510Seric sleep(5); 2331c71e510Seric continue; 2341c71e510Seric } 235d0a9e852Seric 236d0a9e852Seric /* 237d0a9e852Seric ** Create a subprocess to process the mail. 238d0a9e852Seric */ 239d0a9e852Seric 24061e4310fSeric if (tTd(15, 2)) 2411c71e510Seric printf("getrequests: forking (fd = %d)\n", t); 242eb889047Seric 243a8268164Seric pid = fork(); 244a8268164Seric if (pid < 0) 245a8268164Seric { 246a8268164Seric syserr("daemon: cannot fork"); 247a8268164Seric sleep(10); 2481c71e510Seric (void) close(t); 249a8268164Seric continue; 250a8268164Seric } 251a8268164Seric 252a8268164Seric if (pid == 0) 253a8268164Seric { 2549f8b0eadSeric extern char *hostnamebyanyaddr(); 255a44d5a5eSeric 256a8268164Seric /* 257a8268164Seric ** CHILD -- return to caller. 258a44d5a5eSeric ** Collect verified idea of sending host. 259a8268164Seric ** Verify calling user id if possible here. 260a8268164Seric */ 261a8268164Seric 262*2b9178d3Seric (void) setsignal(SIGCHLD, SIG_DFL); 263e17a3a5aSeric OpMode = MD_SMTP; 264779ac194Seric 265a44d5a5eSeric /* determine host name */ 2669f8b0eadSeric RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr)); 26729dcf4baSeric 2682a6bc25bSeric #ifdef LOG 2691f2ff1a4Seric if (LogLevel > 11) 2702a6bc25bSeric { 2712a6bc25bSeric /* log connection information */ 2722a6bc25bSeric syslog(LOG_INFO, "connect from %s (%s)", 2739f8b0eadSeric RealHostName, anynet_ntoa(&RealHostAddr)); 2742a6bc25bSeric } 2752a6bc25bSeric #endif 2762a6bc25bSeric 277244b09d1Seric (void) close(DaemonSocket); 278244b09d1Seric InChannel = fdopen(t, "r"); 279244b09d1Seric OutChannel = fdopen(dup(t), "w"); 280244b09d1Seric 28129dcf4baSeric /* should we check for illegal connection here? XXX */ 282e17a3a5aSeric #ifdef XLA 283e17a3a5aSeric if (!xla_host_ok(RealHostName)) 284e17a3a5aSeric { 285244b09d1Seric message("421 Too many SMTP sessions for this host"); 286e17a3a5aSeric exit(0); 287e17a3a5aSeric } 288e17a3a5aSeric #endif 289a44d5a5eSeric 29061e4310fSeric if (tTd(15, 2)) 291d0a9e852Seric printf("getreq: returning\n"); 292a8268164Seric return; 293a8268164Seric } 294a8268164Seric 2953c154354Seric /* close the port so that others will hang (for a while) */ 2963c154354Seric (void) close(t); 2978e3e4b17Seric } 2983c154354Seric /*NOTREACHED*/ 2993c154354Seric } 3008e3e4b17Seric /* 301b7d7afcbSeric ** CLRDAEMON -- reset the daemon connection 302b7d7afcbSeric ** 303b7d7afcbSeric ** Parameters: 304b7d7afcbSeric ** none. 305b7d7afcbSeric ** 306b7d7afcbSeric ** Returns: 307b7d7afcbSeric ** none. 308b7d7afcbSeric ** 309b7d7afcbSeric ** Side Effects: 310b7d7afcbSeric ** releases any resources used by the passive daemon. 311b7d7afcbSeric */ 312b7d7afcbSeric 313b7d7afcbSeric clrdaemon() 314b7d7afcbSeric { 315b7d7afcbSeric if (DaemonSocket >= 0) 316b7d7afcbSeric (void) close(DaemonSocket); 317b7d7afcbSeric DaemonSocket = -1; 318b7d7afcbSeric } 319b7d7afcbSeric /* 320bfb80540Seric ** SETDAEMONOPTIONS -- set options for running the daemon 321bfb80540Seric ** 322bfb80540Seric ** Parameters: 323bfb80540Seric ** p -- the options line. 324bfb80540Seric ** 325bfb80540Seric ** Returns: 326bfb80540Seric ** none. 327bfb80540Seric */ 328bfb80540Seric 329bfb80540Seric setdaemonoptions(p) 330bfb80540Seric register char *p; 331bfb80540Seric { 332850144caSeric if (DaemonAddr.sa.sa_family == AF_UNSPEC) 333850144caSeric DaemonAddr.sa.sa_family = AF_INET; 334850144caSeric 335bfb80540Seric while (p != NULL) 336bfb80540Seric { 337bfb80540Seric register char *f; 338bfb80540Seric register char *v; 339bfb80540Seric 340bfb80540Seric while (isascii(*p) && isspace(*p)) 341bfb80540Seric p++; 342bfb80540Seric if (*p == '\0') 343bfb80540Seric break; 344bfb80540Seric f = p; 345bfb80540Seric p = strchr(p, ','); 346bfb80540Seric if (p != NULL) 347bfb80540Seric *p++ = '\0'; 348bfb80540Seric v = strchr(f, '='); 349bfb80540Seric if (v == NULL) 350bfb80540Seric continue; 351bfb80540Seric while (isascii(*++v) && isspace(*v)) 352bfb80540Seric continue; 353bfb80540Seric 354bfb80540Seric switch (*f) 355bfb80540Seric { 356850144caSeric case 'F': /* address family */ 357850144caSeric if (isascii(*v) && isdigit(*v)) 358850144caSeric DaemonAddr.sa.sa_family = atoi(v); 359850144caSeric #ifdef NETINET 360850144caSeric else if (strcasecmp(v, "inet") == 0) 361850144caSeric DaemonAddr.sa.sa_family = AF_INET; 362850144caSeric #endif 363850144caSeric #ifdef NETISO 364850144caSeric else if (strcasecmp(v, "iso") == 0) 365850144caSeric DaemonAddr.sa.sa_family = AF_ISO; 366850144caSeric #endif 367850144caSeric #ifdef NETNS 368850144caSeric else if (strcasecmp(v, "ns") == 0) 369850144caSeric DaemonAddr.sa.sa_family = AF_NS; 370850144caSeric #endif 371850144caSeric #ifdef NETX25 372850144caSeric else if (strcasecmp(v, "x.25") == 0) 373850144caSeric DaemonAddr.sa.sa_family = AF_CCITT; 374850144caSeric #endif 375850144caSeric else 376850144caSeric syserr("554 Unknown address family %s in Family=option", v); 377850144caSeric break; 378850144caSeric 379850144caSeric case 'A': /* address */ 380850144caSeric switch (DaemonAddr.sa.sa_family) 381850144caSeric { 382850144caSeric #ifdef NETINET 383850144caSeric case AF_INET: 384850144caSeric if (isascii(*v) && isdigit(*v)) 385850144caSeric DaemonAddr.sin.sin_addr.s_addr = inet_network(v); 386850144caSeric else 387850144caSeric { 388850144caSeric register struct netent *np; 389850144caSeric 390850144caSeric np = getnetbyname(v); 391850144caSeric if (np == NULL) 392850144caSeric syserr("554 network \"%s\" unknown", v); 393850144caSeric else 394850144caSeric DaemonAddr.sin.sin_addr.s_addr = np->n_net; 395850144caSeric } 396850144caSeric break; 397850144caSeric #endif 398850144caSeric 399850144caSeric default: 400850144caSeric syserr("554 Address= option unsupported for family %d", 401850144caSeric DaemonAddr.sa.sa_family); 402850144caSeric break; 403850144caSeric } 404850144caSeric break; 405850144caSeric 406bfb80540Seric case 'P': /* port */ 407850144caSeric switch (DaemonAddr.sa.sa_family) 408850144caSeric { 409850144caSeric short port; 410850144caSeric 411850144caSeric #ifdef NETINET 412850144caSeric case AF_INET: 413bfb80540Seric if (isascii(*v) && isdigit(*v)) 414bfb80540Seric DaemonAddr.sin.sin_port = atoi(v); 415bfb80540Seric else 416bfb80540Seric { 417bfb80540Seric register struct servent *sp; 418bfb80540Seric 419bfb80540Seric sp = getservbyname(v, "tcp"); 420bfb80540Seric if (sp == NULL) 421ad977999Seric syserr("554 service \"%s\" unknown", v); 422bfb80540Seric else 423bfb80540Seric DaemonAddr.sin.sin_port = sp->s_port; 424bfb80540Seric } 425bfb80540Seric break; 426850144caSeric #endif 427bfb80540Seric 428850144caSeric #ifdef NETISO 429850144caSeric case AF_ISO: 430850144caSeric /* assume two byte transport selector */ 431bfb80540Seric if (isascii(*v) && isdigit(*v)) 432850144caSeric port = atoi(v); 433bfb80540Seric else 434bfb80540Seric { 435850144caSeric register struct servent *sp; 436bfb80540Seric 437850144caSeric sp = getservbyname(v, "tcp"); 438850144caSeric if (sp == NULL) 439ad977999Seric syserr("554 service \"%s\" unknown", v); 440bfb80540Seric else 441850144caSeric port = sp->s_port; 442850144caSeric } 443850144caSeric bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2); 444850144caSeric break; 445850144caSeric #endif 446850144caSeric 447850144caSeric default: 448850144caSeric syserr("554 Port= option unsupported for family %d", 449850144caSeric DaemonAddr.sa.sa_family); 450850144caSeric break; 451bfb80540Seric } 452bfb80540Seric break; 453bfc1eaf8Seric 454bfc1eaf8Seric case 'L': /* listen queue size */ 455bfc1eaf8Seric ListenQueueSize = atoi(v); 456bfc1eaf8Seric break; 457bfb80540Seric } 458bfb80540Seric } 459bfb80540Seric } 460bfb80540Seric /* 4617aa493c5Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 4627aa493c5Seric ** 4637aa493c5Seric ** Parameters: 4647aa493c5Seric ** host -- the name of the host. 46548ff0a9dSeric ** port -- the port number to connect to. 466655feedbSeric ** mci -- a pointer to the mail connection information 467655feedbSeric ** structure to be filled in. 468914346b1Seric ** usesecureport -- if set, use a low numbered (reserved) 469914346b1Seric ** port to provide some rudimentary authentication. 4707aa493c5Seric ** 4717aa493c5Seric ** Returns: 4727aa493c5Seric ** An exit code telling whether the connection could be 4737aa493c5Seric ** made and if not why not. 4747aa493c5Seric ** 4757aa493c5Seric ** Side Effects: 4767aa493c5Seric ** none. 4777aa493c5Seric */ 4787aa493c5Seric 479e2f2f828Seric SOCKADDR CurHostAddr; /* address of current host */ 48071ff6caaSeric 481b31e7f2bSeric int 482655feedbSeric makeconnection(host, port, mci, usesecureport) 4837aa493c5Seric char *host; 484210215eaSeric u_short port; 485b31e7f2bSeric register MCI *mci; 486914346b1Seric bool usesecureport; 4877aa493c5Seric { 48804344589Sbloom register int i, s; 48904344589Sbloom register struct hostent *hp = (struct hostent *)NULL; 490e2f2f828Seric SOCKADDR addr; 4916286bb75Sbloom int sav_errno; 492e2f2f828Seric int addrlen; 493134746fbSeric #ifdef NAMED_BIND 494134746fbSeric extern int h_errno; 495134746fbSeric #endif 4967aa493c5Seric 4977aa493c5Seric /* 4987aa493c5Seric ** Set up the address for the mailer. 49971096d12Seric ** Accept "[a.b.c.d]" syntax for host name. 5007aa493c5Seric */ 5017aa493c5Seric 502134746fbSeric #ifdef NAMED_BIND 503794bdbb9Smiriam h_errno = 0; 504134746fbSeric #endif 505794bdbb9Smiriam errno = 0; 506967778e2Seric bzero(&CurHostAddr, sizeof CurHostAddr); 507d945ebe8Seric CurHostName = host; 508794bdbb9Smiriam 50971096d12Seric if (host[0] == '[') 51071096d12Seric { 511a44d5a5eSeric long hid; 5126c2c3107Seric register char *p = strchr(host, ']'); 51371096d12Seric 514a44d5a5eSeric if (p != NULL) 51571096d12Seric { 516a44d5a5eSeric *p = '\0'; 5174d9c42c2Seric #ifdef NETINET 518a44d5a5eSeric hid = inet_addr(&host[1]); 519a7e21fe6Seric if (hid == -1) 5204d9c42c2Seric #endif 521a7e21fe6Seric { 522a7e21fe6Seric /* try it as a host name (avoid MX lookup) */ 523a7e21fe6Seric hp = gethostbyname(&host[1]); 524a7e21fe6Seric *p = ']'; 525a7e21fe6Seric goto gothostent; 526a7e21fe6Seric } 527a44d5a5eSeric *p = ']'; 52871096d12Seric } 529a7e21fe6Seric if (p == NULL) 53071096d12Seric { 53108b25121Seric usrerr("553 Invalid numeric domain spec \"%s\"", host); 53271096d12Seric return (EX_NOHOST); 53371096d12Seric } 5344d9c42c2Seric #ifdef NETINET 5354d9c42c2Seric addr.sin.sin_family = AF_INET; /*XXX*/ 53683c1f4bcSeric addr.sin.sin_addr.s_addr = hid; 5374d9c42c2Seric #endif 53871096d12Seric } 5391c71e510Seric else 5401c71e510Seric { 54104344589Sbloom hp = gethostbyname(host); 542a7e21fe6Seric gothostent: 543794bdbb9Smiriam if (hp == NULL) 544794bdbb9Smiriam { 545134746fbSeric #ifdef NAMED_BIND 546794bdbb9Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 54752308a50Seric return (EX_TEMPFAIL); 54882e5d8ddSeric 549134746fbSeric /* if name server is specified, assume temp fail */ 550134746fbSeric if (errno == ECONNREFUSED && UseNameServer) 551134746fbSeric return (EX_TEMPFAIL); 552134746fbSeric #endif 5537aa493c5Seric return (EX_NOHOST); 554794bdbb9Smiriam } 55583c1f4bcSeric addr.sa.sa_family = hp->h_addrtype; 55683c1f4bcSeric switch (hp->h_addrtype) 55783c1f4bcSeric { 55883c1f4bcSeric #ifdef NETINET 55983c1f4bcSeric case AF_INET: 560e2f2f828Seric bcopy(hp->h_addr, 56183c1f4bcSeric &addr.sin.sin_addr, 562e2f2f828Seric hp->h_length); 56383c1f4bcSeric break; 56483c1f4bcSeric #endif 56583c1f4bcSeric 56683c1f4bcSeric default: 567e2f2f828Seric bcopy(hp->h_addr, 56883c1f4bcSeric addr.sa.sa_data, 569e2f2f828Seric hp->h_length); 57083c1f4bcSeric break; 57183c1f4bcSeric } 57204344589Sbloom i = 1; 5731c71e510Seric } 5741c71e510Seric 5751c71e510Seric /* 5761c71e510Seric ** Determine the port number. 5771c71e510Seric */ 5781c71e510Seric 579fd7c0790Seric if (port != 0) 580e2f2f828Seric port = htons(port); 581fd7c0790Seric else 5821c71e510Seric { 5831c71e510Seric register struct servent *sp = getservbyname("smtp", "tcp"); 5841c71e510Seric 5851c71e510Seric if (sp == NULL) 5861c71e510Seric { 587ad977999Seric syserr("554 makeconnection: service \"smtp\" unknown"); 588845e533cSeric return (EX_OSERR); 5891c71e510Seric } 590e2f2f828Seric port = sp->s_port; 591e2f2f828Seric } 592e2f2f828Seric 59383c1f4bcSeric switch (addr.sa.sa_family) 594e2f2f828Seric { 5954d9c42c2Seric #ifdef NETINET 596e2f2f828Seric case AF_INET: 59783c1f4bcSeric addr.sin.sin_port = port; 598e2f2f828Seric addrlen = sizeof (struct sockaddr_in); 599e2f2f828Seric break; 6004d9c42c2Seric #endif 601e2f2f828Seric 602e2f2f828Seric #ifdef NETISO 603e2f2f828Seric case AF_ISO: 604e2f2f828Seric /* assume two byte transport selector */ 605e2f2f828Seric bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2); 606e2f2f828Seric addrlen = sizeof (struct sockaddr_iso); 607e2f2f828Seric break; 608e2f2f828Seric #endif 609e2f2f828Seric 610e2f2f828Seric default: 61183c1f4bcSeric syserr("Can't connect to address family %d", addr.sa.sa_family); 612e2f2f828Seric return (EX_NOHOST); 6131c71e510Seric } 6147aa493c5Seric 6157aa493c5Seric /* 6167aa493c5Seric ** Try to actually open the connection. 6177aa493c5Seric */ 6187aa493c5Seric 619e17a3a5aSeric #ifdef XLA 620e17a3a5aSeric /* if too many connections, don't bother trying */ 621e17a3a5aSeric if (!xla_noqueue_ok(host)) 622e17a3a5aSeric return EX_TEMPFAIL; 623e17a3a5aSeric #endif 624e17a3a5aSeric 625aea02ca1Seric for (;;) 626aea02ca1Seric { 62761e4310fSeric if (tTd(16, 1)) 628e2f2f828Seric printf("makeconnection (%s [%s])\n", 629e2f2f828Seric host, anynet_ntoa(&addr)); 6307aa493c5Seric 631226e3022Seric /* save for logging */ 632226e3022Seric CurHostAddr = addr; 633226e3022Seric 634914346b1Seric if (usesecureport) 635914346b1Seric { 636914346b1Seric int rport = IPPORT_RESERVED - 1; 637914346b1Seric 638914346b1Seric s = rresvport(&rport); 639914346b1Seric } 640914346b1Seric else 641914346b1Seric { 642af5e902cSeric s = socket(AF_INET, SOCK_STREAM, 0); 643914346b1Seric } 6447aa493c5Seric if (s < 0) 6457aa493c5Seric { 6466286bb75Sbloom sav_errno = errno; 647914346b1Seric syserr("makeconnection: no socket"); 6487aa493c5Seric goto failure; 6497aa493c5Seric } 6507aa493c5Seric 65161e4310fSeric if (tTd(16, 1)) 652b31e7f2bSeric printf("makeconnection: fd=%d\n", s); 6531b6e4a15Seric 6541b6e4a15Seric /* turn on network debugging? */ 655a2ef5fa4Seric if (tTd(16, 101)) 65652308a50Seric { 65752308a50Seric int on = 1; 658aea02ca1Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 659aea02ca1Seric (char *)&on, sizeof on); 66052308a50Seric } 66187d6e633Srick if (CurEnv->e_xfp != NULL) 662877a6142Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 6634bd6a662Seric errno = 0; /* for debugging */ 664e2f2f828Seric if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0) 665aea02ca1Seric break; 666aea02ca1Seric 667aea02ca1Seric /* couldn't connect.... figure out why */ 6686286bb75Sbloom sav_errno = errno; 6696286bb75Sbloom (void) close(s); 67004344589Sbloom if (hp && hp->h_addr_list[i]) 67104344589Sbloom { 672aea02ca1Seric if (tTd(16, 1)) 673e2f2f828Seric printf("Connect failed (%s); trying new address....\n", 674e2f2f828Seric errstring(sav_errno)); 67583c1f4bcSeric switch (addr.sa.sa_family) 67683c1f4bcSeric { 67783c1f4bcSeric #ifdef NETINET 67883c1f4bcSeric case AF_INET: 679e2f2f828Seric bcopy(hp->h_addr_list[i++], 68083c1f4bcSeric &addr.sin.sin_addr, 681e2f2f828Seric hp->h_length); 68283c1f4bcSeric break; 68383c1f4bcSeric #endif 68483c1f4bcSeric 68583c1f4bcSeric default: 686e2f2f828Seric bcopy(hp->h_addr_list[i++], 68783c1f4bcSeric addr.sa.sa_data, 688914346b1Seric hp->h_length); 68983c1f4bcSeric break; 69083c1f4bcSeric } 691aea02ca1Seric continue; 69204344589Sbloom } 69304344589Sbloom 6947aa493c5Seric /* failure, decide if temporary or not */ 6957aa493c5Seric failure: 696244b09d1Seric #ifdef XLA 697244b09d1Seric xla_host_end(host); 698244b09d1Seric #endif 699e2de2524Seric if (transienterror(sav_errno)) 700e2de2524Seric return EX_TEMPFAIL; 701e2de2524Seric else 70287d6e633Srick { 70308b25121Seric message("%s", errstring(sav_errno)); 7047aa493c5Seric return (EX_UNAVAILABLE); 7057aa493c5Seric } 7067aa493c5Seric } 7077aa493c5Seric 7087aa493c5Seric /* connection ok, put it into canonical form */ 709655feedbSeric mci->mci_out = fdopen(s, "w"); 710655feedbSeric mci->mci_in = fdopen(dup(s), "r"); 7117aa493c5Seric 712dca8e1f7Seric return (EX_OK); 7137aa493c5Seric } 714444eaf03Seric /* 715444eaf03Seric ** MYHOSTNAME -- return the name of this host. 716444eaf03Seric ** 717444eaf03Seric ** Parameters: 718444eaf03Seric ** hostbuf -- a place to return the name of this host. 719897f1869Seric ** size -- the size of hostbuf. 720444eaf03Seric ** 721444eaf03Seric ** Returns: 722444eaf03Seric ** A list of aliases for this host. 723444eaf03Seric ** 724444eaf03Seric ** Side Effects: 72538ad259dSeric ** Sets the MyIpAddrs buffer to a list of my IP addresses. 726444eaf03Seric */ 727444eaf03Seric 72838ad259dSeric struct in_addr MyIpAddrs[MAXIPADDR + 1]; 72938ad259dSeric 730444eaf03Seric char ** 731897f1869Seric myhostname(hostbuf, size) 732444eaf03Seric char hostbuf[]; 733897f1869Seric int size; 734444eaf03Seric { 73538ad259dSeric register struct hostent *hp; 736444eaf03Seric extern struct hostent *gethostbyname(); 737444eaf03Seric 738af5e902cSeric if (gethostname(hostbuf, size) < 0) 739af5e902cSeric { 740af5e902cSeric (void) strcpy(hostbuf, "localhost"); 741af5e902cSeric } 742a44d5a5eSeric hp = gethostbyname(hostbuf); 743a44d5a5eSeric if (hp != NULL) 7447364df9fSeric { 74538ad259dSeric (void) strncpy(hostbuf, hp->h_name, size - 1); 74638ad259dSeric hostbuf[size - 1] = '\0'; 74738ad259dSeric 74838ad259dSeric if (hp->h_addrtype == AF_INET && hp->h_length == 4) 74938ad259dSeric { 75038ad259dSeric register int i; 75138ad259dSeric 75238ad259dSeric for (i = 0; i < MAXIPADDR; i++) 75338ad259dSeric { 75438ad259dSeric if (hp->h_addr_list[i] == NULL) 75538ad259dSeric break; 75638ad259dSeric MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i]; 75738ad259dSeric } 75838ad259dSeric MyIpAddrs[i].s_addr = 0; 75938ad259dSeric } 76038ad259dSeric 761a44d5a5eSeric return (hp->h_aliases); 7627364df9fSeric } 763444eaf03Seric else 764444eaf03Seric return (NULL); 765444eaf03Seric } 766cb452edcSeric /* 7679f8b0eadSeric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 7689f8b0eadSeric ** 7699f8b0eadSeric ** Uses RFC1413 protocol to try to get info from the other end. 770320e0d1cSeric ** 771320e0d1cSeric ** Parameters: 772320e0d1cSeric ** fd -- the descriptor 773320e0d1cSeric ** 774320e0d1cSeric ** Returns: 7759f8b0eadSeric ** The user@host information associated with this descriptor. 776320e0d1cSeric ** 777320e0d1cSeric ** Side Effects: 7789f8b0eadSeric ** Sets RealHostName to the name of the host at the other end. 779320e0d1cSeric */ 780320e0d1cSeric 7819f8b0eadSeric #ifdef IDENTPROTO 7829f8b0eadSeric 7839f8b0eadSeric static jmp_buf CtxAuthTimeout; 7849f8b0eadSeric 7859f8b0eadSeric static 7869f8b0eadSeric authtimeout() 7879f8b0eadSeric { 7889f8b0eadSeric longjmp(CtxAuthTimeout, 1); 7899f8b0eadSeric } 7909f8b0eadSeric 7919f8b0eadSeric #endif 7929f8b0eadSeric 793320e0d1cSeric char * 7949f8b0eadSeric getauthinfo(fd) 795320e0d1cSeric int fd; 796320e0d1cSeric { 7979f8b0eadSeric SOCKADDR fa; 7989f8b0eadSeric int falen; 799a5546e24Seric register char *p; 8009f8b0eadSeric #ifdef IDENTPROTO 8019f8b0eadSeric SOCKADDR la; 8029f8b0eadSeric int lalen; 8039f8b0eadSeric register struct servent *sp; 8049f8b0eadSeric int s; 8059f8b0eadSeric int i; 8069f8b0eadSeric EVENT *ev; 8079f8b0eadSeric #endif 8089f8b0eadSeric static char hbuf[MAXNAME * 2 + 2]; 8099f8b0eadSeric extern char *hostnamebyanyaddr(); 8109f8b0eadSeric extern char RealUserName[]; /* main.c */ 811320e0d1cSeric 8129f8b0eadSeric falen = sizeof fa; 8139f8b0eadSeric if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0) 8149f8b0eadSeric { 8159f8b0eadSeric RealHostName = "localhost"; 8169f8b0eadSeric (void) sprintf(hbuf, "%s@localhost", RealUserName); 81753853673Seric if (tTd(9, 1)) 8189f8b0eadSeric printf("getauthinfo: %s\n", hbuf); 819320e0d1cSeric return hbuf; 820320e0d1cSeric } 8219f8b0eadSeric 8229f8b0eadSeric RealHostName = newstr(hostnamebyanyaddr(&fa)); 8239f8b0eadSeric RealHostAddr = fa; 8249f8b0eadSeric 8259f8b0eadSeric #ifdef IDENTPROTO 8269f8b0eadSeric lalen = sizeof la; 8279f8b0eadSeric if (fa.sa.sa_family != AF_INET || 8289f8b0eadSeric getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 || 8299f8b0eadSeric la.sa.sa_family != AF_INET) 8309f8b0eadSeric { 8319f8b0eadSeric /* no ident info */ 8329f8b0eadSeric goto noident; 8339f8b0eadSeric } 8349f8b0eadSeric 8359f8b0eadSeric /* create ident query */ 836f2d880b6Seric (void) sprintf(hbuf, "%d,%d\r\n", 837f2d880b6Seric ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port)); 8389f8b0eadSeric 8399f8b0eadSeric /* create local address */ 8409f8b0eadSeric bzero(&la, sizeof la); 8419f8b0eadSeric 8429f8b0eadSeric /* create foreign address */ 8439f8b0eadSeric sp = getservbyname("auth", "tcp"); 8449f8b0eadSeric if (sp != NULL) 8459f8b0eadSeric fa.sin.sin_port = sp->s_port; 8469f8b0eadSeric else 8471038598cSeric fa.sin.sin_port = htons(113); 8489f8b0eadSeric 8499f8b0eadSeric s = -1; 8509f8b0eadSeric if (setjmp(CtxAuthTimeout) != 0) 8519f8b0eadSeric { 8529f8b0eadSeric if (s >= 0) 8539f8b0eadSeric (void) close(s); 8549f8b0eadSeric goto noident; 8559f8b0eadSeric } 8569f8b0eadSeric 8579f8b0eadSeric /* put a timeout around the whole thing */ 8589f8b0eadSeric ev = setevent((time_t) 30, authtimeout, 0); 8599f8b0eadSeric 8609f8b0eadSeric /* connect to foreign IDENT server */ 8619f8b0eadSeric s = socket(AF_INET, SOCK_STREAM, 0); 8629f8b0eadSeric if (s < 0) 8639f8b0eadSeric { 8649f8b0eadSeric clrevent(ev); 8659f8b0eadSeric goto noident; 8669f8b0eadSeric } 8679f8b0eadSeric if (connect(s, &fa.sa, sizeof fa.sin) < 0) 8689f8b0eadSeric { 8699f8b0eadSeric closeident: 8709f8b0eadSeric (void) close(s); 8719f8b0eadSeric clrevent(ev); 8729f8b0eadSeric goto noident; 8739f8b0eadSeric } 8749f8b0eadSeric 87553853673Seric if (tTd(9, 10)) 8769f8b0eadSeric printf("getauthinfo: sent %s", hbuf); 8779f8b0eadSeric 8789f8b0eadSeric /* send query */ 8799f8b0eadSeric if (write(s, hbuf, strlen(hbuf)) < 0) 8809f8b0eadSeric goto closeident; 8819f8b0eadSeric 8829f8b0eadSeric /* get result */ 8839f8b0eadSeric i = read(s, hbuf, sizeof hbuf); 8849f8b0eadSeric (void) close(s); 8859f8b0eadSeric clrevent(ev); 8869f8b0eadSeric if (i <= 0) 8879f8b0eadSeric goto noident; 8889f8b0eadSeric if (hbuf[--i] == '\n' && hbuf[--i] == '\r') 8899f8b0eadSeric i--; 8909f8b0eadSeric hbuf[++i] = '\0'; 8919f8b0eadSeric 89253853673Seric if (tTd(9, 3)) 8939f8b0eadSeric printf("getauthinfo: got %s\n", hbuf); 8949f8b0eadSeric 8959f8b0eadSeric /* parse result */ 8969f8b0eadSeric p = strchr(hbuf, ':'); 8979f8b0eadSeric if (p == NULL) 8989f8b0eadSeric { 8999f8b0eadSeric /* malformed response */ 9009f8b0eadSeric goto noident; 9019f8b0eadSeric } 9029f8b0eadSeric while (isascii(*++p) && isspace(*p)) 9039f8b0eadSeric continue; 9049f8b0eadSeric if (strncasecmp(p, "userid", 6) != 0) 9059f8b0eadSeric { 9069f8b0eadSeric /* presumably an error string */ 9079f8b0eadSeric goto noident; 9089f8b0eadSeric } 9099f8b0eadSeric p += 6; 9109f8b0eadSeric while (isascii(*p) && isspace(*p)) 9119f8b0eadSeric p++; 9129f8b0eadSeric if (*p++ != ':') 9139f8b0eadSeric { 9149f8b0eadSeric /* either useridxx or malformed response */ 9159f8b0eadSeric goto noident; 9169f8b0eadSeric } 9179f8b0eadSeric 9189f8b0eadSeric /* p now points to the OSTYPE field */ 9199f8b0eadSeric p = strchr(p, ':'); 9209f8b0eadSeric if (p == NULL) 9219f8b0eadSeric { 9229f8b0eadSeric /* malformed response */ 9239f8b0eadSeric goto noident; 9249f8b0eadSeric } 92553853673Seric 92653853673Seric /* 1413 says don't do this -- but it's broken otherwise */ 92753853673Seric while (isascii(*++p) && isspace(*p)) 92853853673Seric continue; 9299f8b0eadSeric 9309f8b0eadSeric /* p now points to the authenticated name */ 9319f8b0eadSeric (void) sprintf(hbuf, "%s@%s", p, RealHostName); 93253853673Seric goto finish; 93353853673Seric 93453853673Seric #endif /* IDENTPROTO */ 93553853673Seric 93653853673Seric noident: 93753853673Seric (void) strcpy(hbuf, RealHostName); 93853853673Seric 93953853673Seric finish: 9409f8b0eadSeric if (RealHostName[0] != '[') 9419f8b0eadSeric { 9429f8b0eadSeric p = &hbuf[strlen(hbuf)]; 9439f8b0eadSeric (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr)); 9449f8b0eadSeric } 94553853673Seric if (tTd(9, 1)) 9469f8b0eadSeric printf("getauthinfo: %s\n", hbuf); 9479f8b0eadSeric return hbuf; 9489f8b0eadSeric } 949320e0d1cSeric /* 95008de856eSeric ** HOST_MAP_LOOKUP -- turn a hostname into canonical form 95115d084d5Seric ** 95215d084d5Seric ** Parameters: 95305b57da8Seric ** map -- a pointer to this map (unused). 95408de856eSeric ** name -- the (presumably unqualified) hostname. 95500b385a9Seric ** av -- unused -- for compatibility with other mapping 956d798a1deSeric ** functions. 9572d29d43aSeric ** statp -- an exit status (out parameter) -- set to 9582d29d43aSeric ** EX_TEMPFAIL if the name server is unavailable. 95915d084d5Seric ** 96015d084d5Seric ** Returns: 96115d084d5Seric ** The mapping, if found. 96215d084d5Seric ** NULL if no mapping found. 96315d084d5Seric ** 96415d084d5Seric ** Side Effects: 96515d084d5Seric ** Looks up the host specified in hbuf. If it is not 96615d084d5Seric ** the canonical name for that host, return the canonical 96715d084d5Seric ** name. 968f36ede03Sbostic */ 969cb452edcSeric 97015d084d5Seric char * 97100b385a9Seric host_map_lookup(map, name, av, statp) 97205b57da8Seric MAP *map; 97308de856eSeric char *name; 97400b385a9Seric char **av; 9752d29d43aSeric int *statp; 97699f7cf32Seric { 97799f7cf32Seric register struct hostent *hp; 9785f78836eSmiriam u_long in_addr; 97905b57da8Seric char *cp; 98038ad259dSeric int i; 981eea91d78Seric register STAB *s; 98200b385a9Seric char hbuf[MAXNAME]; 983eea91d78Seric extern struct hostent *gethostbyaddr(); 984eea91d78Seric extern int h_errno; 9855f78836eSmiriam 986f36ede03Sbostic /* 987eea91d78Seric ** See if we have already looked up this name. If so, just 988eea91d78Seric ** return it. 989eea91d78Seric */ 990eea91d78Seric 99108de856eSeric s = stab(name, ST_NAMECANON, ST_ENTER); 992eea91d78Seric if (bitset(NCF_VALID, s->s_namecanon.nc_flags)) 993eea91d78Seric { 994f92c3297Seric if (tTd(9, 1)) 99508de856eSeric printf("host_map_lookup(%s) => CACHE %s\n", 99608de856eSeric name, s->s_namecanon.nc_cname); 997eea91d78Seric errno = s->s_namecanon.nc_errno; 998eea91d78Seric h_errno = s->s_namecanon.nc_herrno; 999eea91d78Seric *statp = s->s_namecanon.nc_stat; 1000eea91d78Seric return s->s_namecanon.nc_cname; 1001eea91d78Seric } 1002eea91d78Seric 1003eea91d78Seric /* 1004eea91d78Seric ** If first character is a bracket, then it is an address 1005eea91d78Seric ** lookup. Address is copied into a temporary buffer to 100608de856eSeric ** strip the brackets and to preserve name if address is 1007eea91d78Seric ** unknown. 1008f36ede03Sbostic */ 100915d084d5Seric 101008de856eSeric if (*name != '[') 101115d084d5Seric { 1012d798a1deSeric extern bool getcanonname(); 1013d798a1deSeric 10148cb4653dSeric if (tTd(9, 1)) 101508de856eSeric printf("host_map_lookup(%s) => ", name); 1016eea91d78Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 101708de856eSeric (void) strcpy(hbuf, name); 10181f2ff1a4Seric if (getcanonname(hbuf, sizeof hbuf - 1, TRUE)) 10199040ec4fSeric { 10209040ec4fSeric if (tTd(9, 1)) 10219040ec4fSeric printf("%s\n", hbuf); 102200b385a9Seric cp = map_rewrite(map, hbuf, strlen(hbuf), av); 102300b385a9Seric s->s_namecanon.nc_cname = newstr(cp); 102400b385a9Seric return cp; 10259040ec4fSeric } 102615d084d5Seric else 10279040ec4fSeric { 10282d29d43aSeric register struct hostent *hp; 10292d29d43aSeric 10309040ec4fSeric if (tTd(9, 1)) 10312d29d43aSeric printf("FAIL (%d)\n", h_errno); 1032eea91d78Seric s->s_namecanon.nc_errno = errno; 1033eea91d78Seric s->s_namecanon.nc_herrno = h_errno; 10342d29d43aSeric switch (h_errno) 10352d29d43aSeric { 10362d29d43aSeric case TRY_AGAIN: 103789cb2793Seric if (UseNameServer) 10388820d51bSeric { 10398820d51bSeric char *msg = "Recipient domain nameserver timed out"; 10408820d51bSeric 10418820d51bSeric message(msg); 10428820d51bSeric if (CurEnv->e_message == NULL) 104398da658aSeric CurEnv->e_message = newstr(msg); 10448820d51bSeric } 10452d29d43aSeric *statp = EX_TEMPFAIL; 10462d29d43aSeric break; 10472d29d43aSeric 10482d29d43aSeric case HOST_NOT_FOUND: 10492d29d43aSeric *statp = EX_NOHOST; 10502d29d43aSeric break; 10512d29d43aSeric 10522d29d43aSeric case NO_RECOVERY: 10532d29d43aSeric *statp = EX_SOFTWARE; 10542d29d43aSeric break; 10552d29d43aSeric 10562d29d43aSeric default: 10572d29d43aSeric *statp = EX_UNAVAILABLE; 10582d29d43aSeric break; 10592d29d43aSeric } 1060eea91d78Seric s->s_namecanon.nc_stat = *statp; 10612d29d43aSeric if (*statp != EX_TEMPFAIL || UseNameServer) 106215d084d5Seric return NULL; 10632d29d43aSeric 10642d29d43aSeric /* 10652d29d43aSeric ** Try to look it up in /etc/hosts 10662d29d43aSeric */ 10672d29d43aSeric 106808de856eSeric hp = gethostbyname(name); 10692d29d43aSeric if (hp == NULL) 10702d29d43aSeric { 10712d29d43aSeric /* no dice there either */ 1072eea91d78Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST; 10732d29d43aSeric return NULL; 10742d29d43aSeric } 10752d29d43aSeric 1076eea91d78Seric s->s_namecanon.nc_stat = *statp = EX_OK; 107700b385a9Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 107800b385a9Seric s->s_namecanon.nc_cname = newstr(cp); 107900b385a9Seric return cp; 108015d084d5Seric } 10819040ec4fSeric } 108208de856eSeric if ((cp = strchr(name, ']')) == NULL) 108315d084d5Seric return (NULL); 108434e39927Sbostic *cp = '\0'; 108508de856eSeric in_addr = inet_addr(&name[1]); 108638ad259dSeric 108738ad259dSeric /* check to see if this is one of our addresses */ 108838ad259dSeric for (i = 0; MyIpAddrs[i].s_addr != 0; i++) 108938ad259dSeric { 109038ad259dSeric if (MyIpAddrs[i].s_addr == in_addr) 109138ad259dSeric { 109200b385a9Seric return map_rewrite(map, MyHostName, strlen(MyHostName), av); 109338ad259dSeric } 109438ad259dSeric } 109538ad259dSeric 109638ad259dSeric /* nope -- ask the name server */ 109731601fa7Seric hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 1098eea91d78Seric s->s_namecanon.nc_errno = errno; 1099eea91d78Seric s->s_namecanon.nc_herrno = h_errno; 1100eea91d78Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 11015f78836eSmiriam if (hp == NULL) 1102eea91d78Seric { 1103eea91d78Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST; 110415d084d5Seric return (NULL); 1105eea91d78Seric } 110615d084d5Seric 110738ad259dSeric /* found a match -- copy out */ 110800b385a9Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 1109eea91d78Seric s->s_namecanon.nc_stat = *statp = EX_OK; 111000b385a9Seric s->s_namecanon.nc_cname = newstr(cp); 111100b385a9Seric return cp; 111299f7cf32Seric } 1113e2f2f828Seric /* 1114e2f2f828Seric ** ANYNET_NTOA -- convert a network address to printable form. 1115e2f2f828Seric ** 1116e2f2f828Seric ** Parameters: 1117e2f2f828Seric ** sap -- a pointer to a sockaddr structure. 1118e2f2f828Seric ** 1119e2f2f828Seric ** Returns: 1120e2f2f828Seric ** A printable version of that sockaddr. 1121e2f2f828Seric */ 1122e2f2f828Seric 1123e2f2f828Seric char * 1124e2f2f828Seric anynet_ntoa(sap) 1125e2f2f828Seric register SOCKADDR *sap; 1126e2f2f828Seric { 1127e2f2f828Seric register char *bp; 1128e2f2f828Seric register char *ap; 1129e2f2f828Seric int l; 1130e2f2f828Seric static char buf[80]; 1131e2f2f828Seric 11328cb4653dSeric /* check for null/zero family */ 11338cb4653dSeric if (sap == NULL) 11348cb4653dSeric return "NULLADDR"; 11358cb4653dSeric if (sap->sa.sa_family == 0) 11368cb4653dSeric return "0"; 11378cb4653dSeric 113883c1f4bcSeric #ifdef NETINET 113983c1f4bcSeric if (sap->sa.sa_family == AF_INET) 1140e2f2f828Seric { 1141e2f2f828Seric extern char *inet_ntoa(); 1142e2f2f828Seric 1143e2f2f828Seric return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr); 1144e2f2f828Seric } 114583c1f4bcSeric #endif 1146e2f2f828Seric 1147e2f2f828Seric /* unknown family -- just dump bytes */ 114883c1f4bcSeric (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); 1149e2f2f828Seric bp = &buf[strlen(buf)]; 115083c1f4bcSeric ap = sap->sa.sa_data; 115183c1f4bcSeric for (l = sizeof sap->sa.sa_data; --l >= 0; ) 1152e2f2f828Seric { 1153e2f2f828Seric (void) sprintf(bp, "%02x:", *ap++ & 0377); 1154e2f2f828Seric bp += 3; 1155e2f2f828Seric } 1156e2f2f828Seric *--bp = '\0'; 1157e2f2f828Seric return buf; 1158e2f2f828Seric } 11599f8b0eadSeric /* 11609f8b0eadSeric ** HOSTNAMEBYANYADDR -- return name of host based on address 11619f8b0eadSeric ** 11629f8b0eadSeric ** Parameters: 11639f8b0eadSeric ** sap -- SOCKADDR pointer 11649f8b0eadSeric ** 11659f8b0eadSeric ** Returns: 11669f8b0eadSeric ** text representation of host name. 11679f8b0eadSeric ** 11689f8b0eadSeric ** Side Effects: 11699f8b0eadSeric ** none. 11709f8b0eadSeric */ 11719f8b0eadSeric 11729f8b0eadSeric char * 11739f8b0eadSeric hostnamebyanyaddr(sap) 11749f8b0eadSeric register SOCKADDR *sap; 11759f8b0eadSeric { 11769f8b0eadSeric register struct hostent *hp; 11779f8b0eadSeric 11783490b9dfSeric #ifdef NAMED_BIND 11793490b9dfSeric int saveretry; 11803490b9dfSeric 11813490b9dfSeric /* shorten name server timeout to avoid higher level timeouts */ 11823490b9dfSeric saveretry = _res.retry; 11833490b9dfSeric _res.retry = 3; 11843490b9dfSeric #endif /* NAMED_BIND */ 11853490b9dfSeric 11869f8b0eadSeric switch (sap->sa.sa_family) 11879f8b0eadSeric { 11889f8b0eadSeric #ifdef NETINET 11899f8b0eadSeric case AF_INET: 11909f8b0eadSeric hp = gethostbyaddr((char *) &sap->sin.sin_addr, 11919f8b0eadSeric sizeof sap->sin.sin_addr, 11929f8b0eadSeric AF_INET); 11939f8b0eadSeric break; 11949f8b0eadSeric #endif 11959f8b0eadSeric 11969f8b0eadSeric #ifdef NETISO 11979f8b0eadSeric case AF_ISO: 11989f8b0eadSeric hp = gethostbyaddr((char *) &sap->siso.siso_addr, 11999f8b0eadSeric sizeof sap->siso.siso_addr, 12009f8b0eadSeric AF_ISO); 12019f8b0eadSeric break; 12029f8b0eadSeric #endif 12039f8b0eadSeric 12049f8b0eadSeric default: 12059f8b0eadSeric hp = gethostbyaddr(sap->sa.sa_data, 12069f8b0eadSeric sizeof sap->sa.sa_data, 12079f8b0eadSeric sap->sa.sa_family); 12089f8b0eadSeric break; 12099f8b0eadSeric } 12109f8b0eadSeric 12113490b9dfSeric #ifdef NAMED_BIND 12123490b9dfSeric _res.retry = saveretry; 12133490b9dfSeric #endif /* NAMED_BIND */ 12143490b9dfSeric 12159f8b0eadSeric if (hp != NULL) 12169f8b0eadSeric return hp->h_name; 12179f8b0eadSeric else 12189f8b0eadSeric { 12199f8b0eadSeric /* produce a dotted quad */ 12209f8b0eadSeric static char buf[512]; 12219f8b0eadSeric 12229f8b0eadSeric (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); 12239f8b0eadSeric return buf; 12249f8b0eadSeric } 12259f8b0eadSeric } 1226f36ede03Sbostic 12276c2c3107Seric # else /* DAEMON */ 122899f7cf32Seric /* code for systems without sophisticated networking */ 1229444eaf03Seric 1230444eaf03Seric /* 1231444eaf03Seric ** MYHOSTNAME -- stub version for case of no daemon code. 123221e9914dSeric ** 123321e9914dSeric ** Can't convert to upper case here because might be a UUCP name. 1234897f1869Seric ** 1235897f1869Seric ** Mark, you can change this to be anything you want...... 1236444eaf03Seric */ 1237444eaf03Seric 1238444eaf03Seric char ** 1239897f1869Seric myhostname(hostbuf, size) 1240444eaf03Seric char hostbuf[]; 1241897f1869Seric int size; 1242444eaf03Seric { 1243444eaf03Seric register FILE *f; 1244444eaf03Seric 1245444eaf03Seric hostbuf[0] = '\0'; 1246444eaf03Seric f = fopen("/usr/include/whoami", "r"); 1247444eaf03Seric if (f != NULL) 1248444eaf03Seric { 1249897f1869Seric (void) fgets(hostbuf, size, f); 1250444eaf03Seric fixcrlf(hostbuf, TRUE); 1251444eaf03Seric (void) fclose(f); 1252444eaf03Seric } 1253444eaf03Seric return (NULL); 1254444eaf03Seric } 125599f7cf32Seric /* 12569f8b0eadSeric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 1257320e0d1cSeric ** 1258320e0d1cSeric ** Parameters: 1259320e0d1cSeric ** fd -- the descriptor 1260320e0d1cSeric ** 1261320e0d1cSeric ** Returns: 1262320e0d1cSeric ** The host name associated with this descriptor, if it can 1263320e0d1cSeric ** be determined. 1264320e0d1cSeric ** NULL otherwise. 1265320e0d1cSeric ** 1266320e0d1cSeric ** Side Effects: 1267320e0d1cSeric ** none 1268320e0d1cSeric */ 1269320e0d1cSeric 1270320e0d1cSeric char * 12719f8b0eadSeric getauthinfo(fd) 1272320e0d1cSeric int fd; 1273320e0d1cSeric { 1274320e0d1cSeric return NULL; 1275320e0d1cSeric } 1276320e0d1cSeric /* 127799f7cf32Seric ** MAPHOSTNAME -- turn a hostname into canonical form 127899f7cf32Seric ** 127999f7cf32Seric ** Parameters: 128005b57da8Seric ** map -- a pointer to the database map. 128108de856eSeric ** name -- a buffer containing a hostname. 128215d084d5Seric ** avp -- a pointer to a (cf file defined) argument vector. 12832d29d43aSeric ** statp -- an exit status (out parameter). 128499f7cf32Seric ** 128599f7cf32Seric ** Returns: 128615d084d5Seric ** mapped host name 1287cb452edcSeric ** FALSE otherwise. 128899f7cf32Seric ** 128999f7cf32Seric ** Side Effects: 129008de856eSeric ** Looks up the host specified in name. If it is not 129199f7cf32Seric ** the canonical name for that host, replace it with 129299f7cf32Seric ** the canonical name. If the name is unknown, or it 129399f7cf32Seric ** is already the canonical name, leave it unchanged. 129499f7cf32Seric */ 129599f7cf32Seric 129699f7cf32Seric /*ARGSUSED*/ 129715d084d5Seric char * 129808de856eSeric host_map_lookup(map, name, avp, statp) 129905b57da8Seric MAP *map; 130008de856eSeric char *name; 130115d084d5Seric char **avp; 13022d29d43aSeric char *statp; 130399f7cf32Seric { 13042d29d43aSeric register struct hostent *hp; 13052d29d43aSeric 130608de856eSeric hp = gethostbyname(name); 13072d29d43aSeric if (hp != NULL) 13082d29d43aSeric return hp->h_name; 13092d29d43aSeric *statp = EX_NOHOST; 131015d084d5Seric return NULL; 131199f7cf32Seric } 131299f7cf32Seric 13136c2c3107Seric #endif /* DAEMON */ 1314