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*423161fbSeric static char sccsid[] = "@(#)daemon.c 8.56 (Berkeley) 06/25/94 (with daemon mode)"; 15d0a9e852Seric #else 16*423161fbSeric static char sccsid[] = "@(#)daemon.c 8.56 (Berkeley) 06/25/94 (without daemon mode)"; 17da1c6175Sbostic #endif 18da1c6175Sbostic #endif /* not lint */ 19da1c6175Sbostic 20da1c6175Sbostic #ifdef DAEMON 21d0a9e852Seric 221c71e510Seric # include <netdb.h> 23d8d0a4aeSeric # include <arpa/inet.h> 24d0a9e852Seric 259d4a8008Seric #if NAMED_BIND 263490b9dfSeric # include <resolv.h> 273490b9dfSeric #endif 283490b9dfSeric 297fa39d90Seric /* 307fa39d90Seric ** DAEMON.C -- routines to use when running as a daemon. 3147b12ae1Seric ** 3247b12ae1Seric ** This entire file is highly dependent on the 4.2 BSD 3347b12ae1Seric ** interprocess communication primitives. No attempt has 3447b12ae1Seric ** been made to make this file portable to Version 7, 3547b12ae1Seric ** Version 6, MPX files, etc. If you should try such a 3647b12ae1Seric ** thing yourself, I recommend chucking the entire file 3747b12ae1Seric ** and starting from scratch. Basic semantics are: 3847b12ae1Seric ** 3947b12ae1Seric ** getrequests() 4047b12ae1Seric ** Opens a port and initiates a connection. 4147b12ae1Seric ** Returns in a child. Must set InChannel and 4247b12ae1Seric ** OutChannel appropriately. 43b7d7afcbSeric ** clrdaemon() 44b7d7afcbSeric ** Close any open files associated with getting 45b7d7afcbSeric ** the connection; this is used when running the queue, 46b7d7afcbSeric ** etc., to avoid having extra file descriptors during 47b7d7afcbSeric ** the queue run and to avoid confusing the network 48b7d7afcbSeric ** code (if it cares). 49914346b1Seric ** makeconnection(host, port, outfile, infile, usesecureport) 5047b12ae1Seric ** Make a connection to the named host on the given 5147b12ae1Seric ** port. Set *outfile and *infile to the files 5247b12ae1Seric ** appropriate for communication. Returns zero on 5347b12ae1Seric ** success, else an exit status describing the 5447b12ae1Seric ** error. 5508de856eSeric ** host_map_lookup(map, hbuf, avp, pstat) 5605b57da8Seric ** Convert the entry in hbuf into a canonical form. 577fa39d90Seric */ 587fa39d90Seric /* 597fa39d90Seric ** GETREQUESTS -- open mail IPC port and get requests. 607fa39d90Seric ** 617fa39d90Seric ** Parameters: 627fa39d90Seric ** none. 637fa39d90Seric ** 647fa39d90Seric ** Returns: 657fa39d90Seric ** none. 667fa39d90Seric ** 677fa39d90Seric ** Side Effects: 687fa39d90Seric ** Waits until some interesting activity occurs. When 697fa39d90Seric ** it does, a child is created to process it, and the 707fa39d90Seric ** parent waits for completion. Return from this 71147303b1Seric ** routine is always in the child. The file pointers 72147303b1Seric ** "InChannel" and "OutChannel" should be set to point 73147303b1Seric ** to the communication channel. 747fa39d90Seric */ 757fa39d90Seric 76b7d7afcbSeric int DaemonSocket = -1; /* fd describing socket */ 77bfb80540Seric SOCKADDR DaemonAddr; /* socket for incoming */ 78bfc1eaf8Seric int ListenQueueSize = 10; /* size of listen queue */ 79b35447dbSeric int TcpRcvBufferSize = 0; /* size of TCP receive buffer */ 80b35447dbSeric int TcpSndBufferSize = 0; /* size of TCP send buffer */ 811c71e510Seric 827fa39d90Seric getrequests() 837fa39d90Seric { 841c71e510Seric int t; 8515d084d5Seric bool refusingconnections = TRUE; 860aae1086Seric FILE *pidf; 87dadb8687Seric int socksize; 88dfe840b2Seric #ifdef XDEBUG 89dfe840b2Seric bool j_has_dot; 90dfe840b2Seric #endif 919b100374Sbostic extern void reapchild(); 92eb889047Seric 93a8268164Seric /* 941c71e510Seric ** Set up the address for the mailer. 95eb889047Seric */ 96eb889047Seric 97bfb80540Seric if (DaemonAddr.sin.sin_family == 0) 98bfb80540Seric DaemonAddr.sin.sin_family = AF_INET; 99bfb80540Seric if (DaemonAddr.sin.sin_addr.s_addr == 0) 100bfb80540Seric DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY; 101bfb80540Seric if (DaemonAddr.sin.sin_port == 0) 102bfb80540Seric { 103e5311662Seric register struct servent *sp; 104e5311662Seric 1051c71e510Seric sp = getservbyname("smtp", "tcp"); 1061c71e510Seric if (sp == NULL) 107d0a9e852Seric { 108ad977999Seric syserr("554 service \"smtp\" unknown"); 109e5311662Seric DaemonAddr.sin.sin_port = htons(25); 1101c71e510Seric } 111e5311662Seric else 112bfb80540Seric DaemonAddr.sin.sin_port = sp->s_port; 113bfb80540Seric } 1141c71e510Seric 1151c71e510Seric /* 1161c71e510Seric ** Try to actually open the connection. 1171c71e510Seric */ 1181c71e510Seric 1191c71e510Seric if (tTd(15, 1)) 120bfb80540Seric printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port); 1211c71e510Seric 1221c71e510Seric /* get a socket for the SMTP connection */ 12388ea5609Seric socksize = opendaemonsocket(TRUE); 1241c71e510Seric 1252b9178d3Seric (void) setsignal(SIGCHLD, reapchild); 12652308a50Seric 1270aae1086Seric /* write the pid to the log file for posterity */ 1280aae1086Seric pidf = fopen(PidFile, "w"); 1290aae1086Seric if (pidf != NULL) 1300aae1086Seric { 13137950f67Seric extern char *CommandLineArgs; 13237950f67Seric 13337950f67Seric /* write the process id on line 1 */ 1340aae1086Seric fprintf(pidf, "%d\n", getpid()); 13537950f67Seric 13637950f67Seric /* line 2 contains all command line flags */ 13737950f67Seric fprintf(pidf, "%s\n", CommandLineArgs); 13837950f67Seric 13937950f67Seric /* flush and close */ 1400aae1086Seric fclose(pidf); 1410aae1086Seric } 1420aae1086Seric 143dfe840b2Seric #ifdef XDEBUG 144dfe840b2Seric { 14535852b23Seric char jbuf[MAXHOSTNAMELEN]; 146dfe840b2Seric 14735852b23Seric expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv); 14835852b23Seric j_has_dot = strchr(jbuf, '.') != NULL; 149dfe840b2Seric } 150dfe840b2Seric #endif 1510aae1086Seric 1521c71e510Seric if (tTd(15, 1)) 153b7d7afcbSeric printf("getrequests: %d\n", DaemonSocket); 1541c71e510Seric 1551c71e510Seric for (;;) 1561c71e510Seric { 1573a099713Seric register int pid; 158a44d5a5eSeric auto int lotherend; 15915d084d5Seric extern bool refuseconnections(); 1603a099713Seric 1613a099713Seric /* see if we are rejecting connections */ 16215d084d5Seric CurrentLA = getla(); 16315d084d5Seric if (refuseconnections()) 1646775ec03Sbostic { 1650084e6f6Seric if (DaemonSocket >= 0) 16615d084d5Seric { 1670084e6f6Seric /* close socket so peer will fail quickly */ 1680084e6f6Seric (void) close(DaemonSocket); 1690084e6f6Seric DaemonSocket = -1; 17015d084d5Seric } 1710084e6f6Seric refusingconnections = TRUE; 17271e5e267Seric setproctitle("rejecting connections: load average: %d", 17371e5e267Seric CurrentLA); 1740084e6f6Seric sleep(15); 17515d084d5Seric continue; 17615d084d5Seric } 17715d084d5Seric 17815d084d5Seric if (refusingconnections) 17915d084d5Seric { 18015d084d5Seric /* start listening again */ 18188ea5609Seric (void) opendaemonsocket(FALSE); 18215d084d5Seric setproctitle("accepting connections"); 18315d084d5Seric refusingconnections = FALSE; 1846775ec03Sbostic } 185a44d5a5eSeric 186dfe840b2Seric #ifdef XDEBUG 187dfe840b2Seric /* check for disaster */ 188dfe840b2Seric { 189dfe840b2Seric register STAB *s; 19035852b23Seric char jbuf[MAXHOSTNAMELEN]; 191dfe840b2Seric 19235852b23Seric expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv); 19335852b23Seric if ((s = stab(jbuf, ST_CLASS, ST_FIND)) == NULL || 194dfe840b2Seric !bitnset('w', s->s_class)) 195dfe840b2Seric { 196dfe840b2Seric dumpstate("daemon lost $j"); 197dfe840b2Seric syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog"); 198dfe840b2Seric abort(); 199dfe840b2Seric } 20035852b23Seric else if (j_has_dot && strchr(jbuf, '.') == NULL) 201dfe840b2Seric { 202dfe840b2Seric dumpstate("daemon $j lost dot"); 203dfe840b2Seric syslog(LOG_ALERT, "daemon process $j lost dot; see syslog"); 204dfe840b2Seric abort(); 205dfe840b2Seric } 206dfe840b2Seric } 207dfe840b2Seric #endif 208dfe840b2Seric 2091c71e510Seric /* wait for a connection */ 2101c71e510Seric do 2111c71e510Seric { 2121c71e510Seric errno = 0; 213dadb8687Seric lotherend = socksize; 2149b100374Sbostic t = accept(DaemonSocket, 2159b100374Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 2161c71e510Seric } while (t < 0 && errno == EINTR); 2171c71e510Seric if (t < 0) 2181c71e510Seric { 2191c71e510Seric syserr("getrequests: accept"); 2201c71e510Seric sleep(5); 2211c71e510Seric continue; 2221c71e510Seric } 223d0a9e852Seric 224d0a9e852Seric /* 225d0a9e852Seric ** Create a subprocess to process the mail. 226d0a9e852Seric */ 227d0a9e852Seric 22861e4310fSeric if (tTd(15, 2)) 2291c71e510Seric printf("getrequests: forking (fd = %d)\n", t); 230eb889047Seric 231a8268164Seric pid = fork(); 232a8268164Seric if (pid < 0) 233a8268164Seric { 234a8268164Seric syserr("daemon: cannot fork"); 235a8268164Seric sleep(10); 2361c71e510Seric (void) close(t); 237a8268164Seric continue; 238a8268164Seric } 239a8268164Seric 240a8268164Seric if (pid == 0) 241a8268164Seric { 242da662164Seric char *p; 2439f8b0eadSeric extern char *hostnamebyanyaddr(); 244a44d5a5eSeric 245a8268164Seric /* 246a8268164Seric ** CHILD -- return to caller. 247a44d5a5eSeric ** Collect verified idea of sending host. 248a8268164Seric ** Verify calling user id if possible here. 249a8268164Seric */ 250a8268164Seric 2512b9178d3Seric (void) setsignal(SIGCHLD, SIG_DFL); 2526c72170fSeric (void) close(DaemonSocket); 2539f9b003eSeric DisConnected = FALSE; 254779ac194Seric 2554dd09a90Seric setproctitle("startup with %s", 2564dd09a90Seric anynet_ntoa(&RealHostAddr)); 2574dd09a90Seric 258a44d5a5eSeric /* determine host name */ 259da662164Seric p = hostnamebyanyaddr(&RealHostAddr); 260da662164Seric RealHostName = newstr(p); 2614dd09a90Seric setproctitle("startup with %s", p); 26229dcf4baSeric 2632a6bc25bSeric #ifdef LOG 2641f2ff1a4Seric if (LogLevel > 11) 2652a6bc25bSeric { 2662a6bc25bSeric /* log connection information */ 2672a6bc25bSeric syslog(LOG_INFO, "connect from %s (%s)", 2689f8b0eadSeric RealHostName, anynet_ntoa(&RealHostAddr)); 2692a6bc25bSeric } 2702a6bc25bSeric #endif 2712a6bc25bSeric 272335eae58Seric if ((InChannel = fdopen(t, "r")) == NULL || 273335eae58Seric (t = dup(t)) < 0 || 274335eae58Seric (OutChannel = fdopen(t, "w")) == NULL) 275335eae58Seric { 276335eae58Seric syserr("cannot open SMTP server channel, fd=%d", t); 277335eae58Seric exit(0); 278335eae58Seric } 279244b09d1Seric 28029dcf4baSeric /* should we check for illegal connection here? XXX */ 281e17a3a5aSeric #ifdef XLA 282e17a3a5aSeric if (!xla_host_ok(RealHostName)) 283e17a3a5aSeric { 284244b09d1Seric message("421 Too many SMTP sessions for this host"); 285e17a3a5aSeric exit(0); 286e17a3a5aSeric } 287e17a3a5aSeric #endif 288a44d5a5eSeric 28961e4310fSeric if (tTd(15, 2)) 290d0a9e852Seric printf("getreq: returning\n"); 291a8268164Seric return; 292a8268164Seric } 293a8268164Seric 2943c154354Seric /* close the port so that others will hang (for a while) */ 2953c154354Seric (void) close(t); 2968e3e4b17Seric } 2973c154354Seric /*NOTREACHED*/ 2983c154354Seric } 2998e3e4b17Seric /* 3000084e6f6Seric ** OPENDAEMONSOCKET -- open the SMTP socket 3010084e6f6Seric ** 3020084e6f6Seric ** Deals with setting all appropriate options. DaemonAddr must 3030084e6f6Seric ** be set up in advance. 3040084e6f6Seric ** 3050084e6f6Seric ** Parameters: 30688ea5609Seric ** firsttime -- set if this is the initial open. 3070084e6f6Seric ** 3080084e6f6Seric ** Returns: 3090084e6f6Seric ** Size in bytes of the daemon socket addr. 3100084e6f6Seric ** 3110084e6f6Seric ** Side Effects: 3120084e6f6Seric ** Leaves DaemonSocket set to the open socket. 3130084e6f6Seric ** Exits if the socket cannot be created. 3140084e6f6Seric */ 3150084e6f6Seric 3166173568dSeric #define MAXOPENTRIES 10 /* maximum number of tries to open connection */ 3176173568dSeric 3180084e6f6Seric int 31988ea5609Seric opendaemonsocket(firsttime) 32088ea5609Seric bool firsttime; 3210084e6f6Seric { 3220084e6f6Seric int on = 1; 3230084e6f6Seric int socksize; 3246173568dSeric int ntries = 0; 3256173568dSeric int saveerrno; 3260084e6f6Seric 3270084e6f6Seric if (tTd(15, 2)) 3280084e6f6Seric printf("opendaemonsocket()\n"); 3290084e6f6Seric 3306173568dSeric do 3316173568dSeric { 332254b93c3Seric if (ntries > 0) 333254b93c3Seric sleep(5); 33488ea5609Seric if (firsttime || DaemonSocket < 0) 33588ea5609Seric { 3360084e6f6Seric DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0); 3370084e6f6Seric if (DaemonSocket < 0) 3380084e6f6Seric { 3390084e6f6Seric /* probably another daemon already */ 3406173568dSeric saveerrno = errno; 3410084e6f6Seric syserr("opendaemonsocket: can't create server SMTP socket"); 3420084e6f6Seric severe: 3430084e6f6Seric # ifdef LOG 3440084e6f6Seric if (LogLevel > 0) 3450084e6f6Seric syslog(LOG_ALERT, "problem creating SMTP socket"); 3460084e6f6Seric # endif /* LOG */ 3476173568dSeric DaemonSocket = -1; 3486173568dSeric continue; 3490084e6f6Seric } 3500084e6f6Seric 3510084e6f6Seric /* turn on network debugging? */ 3520084e6f6Seric if (tTd(15, 101)) 3536173568dSeric (void) setsockopt(DaemonSocket, SOL_SOCKET, 3546173568dSeric SO_DEBUG, (char *)&on, 3556173568dSeric sizeof on); 3560084e6f6Seric 3576173568dSeric (void) setsockopt(DaemonSocket, SOL_SOCKET, 3586173568dSeric SO_REUSEADDR, (char *)&on, sizeof on); 3596173568dSeric (void) setsockopt(DaemonSocket, SOL_SOCKET, 3606173568dSeric SO_KEEPALIVE, (char *)&on, sizeof on); 3610084e6f6Seric 3620084e6f6Seric #ifdef SO_RCVBUF 3630084e6f6Seric if (TcpRcvBufferSize > 0) 3640084e6f6Seric { 3656173568dSeric if (setsockopt(DaemonSocket, SOL_SOCKET, 3666173568dSeric SO_RCVBUF, 3670084e6f6Seric (char *) &TcpRcvBufferSize, 3680084e6f6Seric sizeof(TcpRcvBufferSize)) < 0) 3690084e6f6Seric syserr("getrequests: setsockopt(SO_RCVBUF)"); 3700084e6f6Seric } 3710084e6f6Seric #endif 3720084e6f6Seric 3730084e6f6Seric switch (DaemonAddr.sa.sa_family) 3740084e6f6Seric { 3750084e6f6Seric # ifdef NETINET 3760084e6f6Seric case AF_INET: 3770084e6f6Seric socksize = sizeof DaemonAddr.sin; 3780084e6f6Seric break; 3790084e6f6Seric # endif 3800084e6f6Seric 3810084e6f6Seric # ifdef NETISO 3820084e6f6Seric case AF_ISO: 3830084e6f6Seric socksize = sizeof DaemonAddr.siso; 3840084e6f6Seric break; 3850084e6f6Seric # endif 3860084e6f6Seric 3870084e6f6Seric default: 3880084e6f6Seric socksize = sizeof DaemonAddr; 3890084e6f6Seric break; 3900084e6f6Seric } 3910084e6f6Seric 3920084e6f6Seric if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0) 3930084e6f6Seric { 3946173568dSeric saveerrno = errno; 3950084e6f6Seric syserr("getrequests: cannot bind"); 3960084e6f6Seric (void) close(DaemonSocket); 3970084e6f6Seric goto severe; 3980084e6f6Seric } 39988ea5609Seric } 40088ea5609Seric if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0) 4010084e6f6Seric { 4026173568dSeric saveerrno = errno; 4030084e6f6Seric syserr("getrequests: cannot listen"); 4040084e6f6Seric (void) close(DaemonSocket); 4050084e6f6Seric goto severe; 4060084e6f6Seric } 4070084e6f6Seric return socksize; 4086173568dSeric } while (ntries++ < MAXOPENTRIES && transienterror(saveerrno)); 409*423161fbSeric syserr("!opendaemonsocket: server SMTP socket wedged: exiting"); 4106173568dSeric finis(); 4110084e6f6Seric } 4120084e6f6Seric /* 413b7d7afcbSeric ** CLRDAEMON -- reset the daemon connection 414b7d7afcbSeric ** 415b7d7afcbSeric ** Parameters: 416b7d7afcbSeric ** none. 417b7d7afcbSeric ** 418b7d7afcbSeric ** Returns: 419b7d7afcbSeric ** none. 420b7d7afcbSeric ** 421b7d7afcbSeric ** Side Effects: 422b7d7afcbSeric ** releases any resources used by the passive daemon. 423b7d7afcbSeric */ 424b7d7afcbSeric 425b7d7afcbSeric clrdaemon() 426b7d7afcbSeric { 427b7d7afcbSeric if (DaemonSocket >= 0) 428b7d7afcbSeric (void) close(DaemonSocket); 429b7d7afcbSeric DaemonSocket = -1; 430b7d7afcbSeric } 431b7d7afcbSeric /* 432bfb80540Seric ** SETDAEMONOPTIONS -- set options for running the daemon 433bfb80540Seric ** 434bfb80540Seric ** Parameters: 435bfb80540Seric ** p -- the options line. 436bfb80540Seric ** 437bfb80540Seric ** Returns: 438bfb80540Seric ** none. 439bfb80540Seric */ 440bfb80540Seric 441bfb80540Seric setdaemonoptions(p) 442bfb80540Seric register char *p; 443bfb80540Seric { 444850144caSeric if (DaemonAddr.sa.sa_family == AF_UNSPEC) 445850144caSeric DaemonAddr.sa.sa_family = AF_INET; 446850144caSeric 447bfb80540Seric while (p != NULL) 448bfb80540Seric { 449bfb80540Seric register char *f; 450bfb80540Seric register char *v; 451bfb80540Seric 452bfb80540Seric while (isascii(*p) && isspace(*p)) 453bfb80540Seric p++; 454bfb80540Seric if (*p == '\0') 455bfb80540Seric break; 456bfb80540Seric f = p; 457bfb80540Seric p = strchr(p, ','); 458bfb80540Seric if (p != NULL) 459bfb80540Seric *p++ = '\0'; 460bfb80540Seric v = strchr(f, '='); 461bfb80540Seric if (v == NULL) 462bfb80540Seric continue; 463bfb80540Seric while (isascii(*++v) && isspace(*v)) 464bfb80540Seric continue; 465bfb80540Seric 466bfb80540Seric switch (*f) 467bfb80540Seric { 468850144caSeric case 'F': /* address family */ 469850144caSeric if (isascii(*v) && isdigit(*v)) 470850144caSeric DaemonAddr.sa.sa_family = atoi(v); 471850144caSeric #ifdef NETINET 472850144caSeric else if (strcasecmp(v, "inet") == 0) 473850144caSeric DaemonAddr.sa.sa_family = AF_INET; 474850144caSeric #endif 475850144caSeric #ifdef NETISO 476850144caSeric else if (strcasecmp(v, "iso") == 0) 477850144caSeric DaemonAddr.sa.sa_family = AF_ISO; 478850144caSeric #endif 479850144caSeric #ifdef NETNS 480850144caSeric else if (strcasecmp(v, "ns") == 0) 481850144caSeric DaemonAddr.sa.sa_family = AF_NS; 482850144caSeric #endif 483850144caSeric #ifdef NETX25 484850144caSeric else if (strcasecmp(v, "x.25") == 0) 485850144caSeric DaemonAddr.sa.sa_family = AF_CCITT; 486850144caSeric #endif 487850144caSeric else 488850144caSeric syserr("554 Unknown address family %s in Family=option", v); 489850144caSeric break; 490850144caSeric 491850144caSeric case 'A': /* address */ 492850144caSeric switch (DaemonAddr.sa.sa_family) 493850144caSeric { 494850144caSeric #ifdef NETINET 495850144caSeric case AF_INET: 496850144caSeric if (isascii(*v) && isdigit(*v)) 497850144caSeric DaemonAddr.sin.sin_addr.s_addr = inet_network(v); 498850144caSeric else 499850144caSeric { 500850144caSeric register struct netent *np; 501850144caSeric 502850144caSeric np = getnetbyname(v); 503850144caSeric if (np == NULL) 504850144caSeric syserr("554 network \"%s\" unknown", v); 505850144caSeric else 506850144caSeric DaemonAddr.sin.sin_addr.s_addr = np->n_net; 507850144caSeric } 508850144caSeric break; 509850144caSeric #endif 510850144caSeric 511850144caSeric default: 512850144caSeric syserr("554 Address= option unsupported for family %d", 513850144caSeric DaemonAddr.sa.sa_family); 514850144caSeric break; 515850144caSeric } 516850144caSeric break; 517850144caSeric 518bfb80540Seric case 'P': /* port */ 519850144caSeric switch (DaemonAddr.sa.sa_family) 520850144caSeric { 521850144caSeric short port; 522850144caSeric 523850144caSeric #ifdef NETINET 524850144caSeric case AF_INET: 525bfb80540Seric if (isascii(*v) && isdigit(*v)) 52676b70c58Seric DaemonAddr.sin.sin_port = htons(atoi(v)); 527bfb80540Seric else 528bfb80540Seric { 529bfb80540Seric register struct servent *sp; 530bfb80540Seric 531bfb80540Seric sp = getservbyname(v, "tcp"); 532bfb80540Seric if (sp == NULL) 533ad977999Seric syserr("554 service \"%s\" unknown", v); 534bfb80540Seric else 535bfb80540Seric DaemonAddr.sin.sin_port = sp->s_port; 536bfb80540Seric } 537bfb80540Seric break; 538850144caSeric #endif 539bfb80540Seric 540850144caSeric #ifdef NETISO 541850144caSeric case AF_ISO: 542850144caSeric /* assume two byte transport selector */ 543bfb80540Seric if (isascii(*v) && isdigit(*v)) 54476b70c58Seric port = htons(atoi(v)); 545bfb80540Seric else 546bfb80540Seric { 547850144caSeric register struct servent *sp; 548bfb80540Seric 549850144caSeric sp = getservbyname(v, "tcp"); 550850144caSeric if (sp == NULL) 551ad977999Seric syserr("554 service \"%s\" unknown", v); 552bfb80540Seric else 553850144caSeric port = sp->s_port; 554850144caSeric } 555850144caSeric bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2); 556850144caSeric break; 557850144caSeric #endif 558850144caSeric 559850144caSeric default: 560850144caSeric syserr("554 Port= option unsupported for family %d", 561850144caSeric DaemonAddr.sa.sa_family); 562850144caSeric break; 563bfb80540Seric } 564bfb80540Seric break; 565bfc1eaf8Seric 566bfc1eaf8Seric case 'L': /* listen queue size */ 567bfc1eaf8Seric ListenQueueSize = atoi(v); 568bfc1eaf8Seric break; 569b35447dbSeric 570b35447dbSeric case 'S': /* send buffer size */ 571b35447dbSeric TcpSndBufferSize = atoi(v); 572b35447dbSeric break; 573b35447dbSeric 574b35447dbSeric case 'R': /* receive buffer size */ 575b35447dbSeric TcpRcvBufferSize = atoi(v); 576b35447dbSeric break; 577bfb80540Seric } 578bfb80540Seric } 579bfb80540Seric } 580bfb80540Seric /* 5817aa493c5Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 5827aa493c5Seric ** 5837aa493c5Seric ** Parameters: 5847aa493c5Seric ** host -- the name of the host. 58548ff0a9dSeric ** port -- the port number to connect to. 586655feedbSeric ** mci -- a pointer to the mail connection information 587655feedbSeric ** structure to be filled in. 588914346b1Seric ** usesecureport -- if set, use a low numbered (reserved) 589914346b1Seric ** port to provide some rudimentary authentication. 5907aa493c5Seric ** 5917aa493c5Seric ** Returns: 5927aa493c5Seric ** An exit code telling whether the connection could be 5937aa493c5Seric ** made and if not why not. 5947aa493c5Seric ** 5957aa493c5Seric ** Side Effects: 5967aa493c5Seric ** none. 5977aa493c5Seric */ 5987aa493c5Seric 599e2f2f828Seric SOCKADDR CurHostAddr; /* address of current host */ 60071ff6caaSeric 601b31e7f2bSeric int 602655feedbSeric makeconnection(host, port, mci, usesecureport) 6037aa493c5Seric char *host; 604210215eaSeric u_short port; 605b31e7f2bSeric register MCI *mci; 606914346b1Seric bool usesecureport; 6077aa493c5Seric { 60804344589Sbloom register int i, s; 60904344589Sbloom register struct hostent *hp = (struct hostent *)NULL; 610e2f2f828Seric SOCKADDR addr; 6116286bb75Sbloom int sav_errno; 612e2f2f828Seric int addrlen; 6139d4a8008Seric #if NAMED_BIND 614134746fbSeric extern int h_errno; 615134746fbSeric #endif 6167aa493c5Seric 6177aa493c5Seric /* 6187aa493c5Seric ** Set up the address for the mailer. 61971096d12Seric ** Accept "[a.b.c.d]" syntax for host name. 6207aa493c5Seric */ 6217aa493c5Seric 6229d4a8008Seric #if NAMED_BIND 623794bdbb9Smiriam h_errno = 0; 624134746fbSeric #endif 625794bdbb9Smiriam errno = 0; 626967778e2Seric bzero(&CurHostAddr, sizeof CurHostAddr); 627c931b82bSeric SmtpPhase = mci->mci_phase = "initial connection"; 628d945ebe8Seric CurHostName = host; 629794bdbb9Smiriam 63071096d12Seric if (host[0] == '[') 63171096d12Seric { 632a44d5a5eSeric long hid; 6336c2c3107Seric register char *p = strchr(host, ']'); 63471096d12Seric 635a44d5a5eSeric if (p != NULL) 63671096d12Seric { 637a44d5a5eSeric *p = '\0'; 6384d9c42c2Seric #ifdef NETINET 639a44d5a5eSeric hid = inet_addr(&host[1]); 640a7e21fe6Seric if (hid == -1) 6414d9c42c2Seric #endif 642a7e21fe6Seric { 643a7e21fe6Seric /* try it as a host name (avoid MX lookup) */ 644a7e21fe6Seric hp = gethostbyname(&host[1]); 645d8984352Seric if (hp == NULL && p[-1] == '.') 646d8984352Seric { 647910ff93bSeric #ifdef NAMED_BIND 648910ff93bSeric int oldopts = _res.options; 649910ff93bSeric 650910ff93bSeric _res.options &= ~(RES_DEFNAMES|RES_DNSRCH); 651910ff93bSeric #endif 652d8984352Seric p[-1] = '\0'; 653d8984352Seric hp = gethostbyname(&host[1]); 654d8984352Seric p[-1] = '.'; 655910ff93bSeric #ifdef NAMED_BIND 656910ff93bSeric _res.options = oldopts; 657910ff93bSeric #endif 658d8984352Seric } 659a7e21fe6Seric *p = ']'; 660a7e21fe6Seric goto gothostent; 661a7e21fe6Seric } 662a44d5a5eSeric *p = ']'; 66371096d12Seric } 664a7e21fe6Seric if (p == NULL) 66571096d12Seric { 66608b25121Seric usrerr("553 Invalid numeric domain spec \"%s\"", host); 66771096d12Seric return (EX_NOHOST); 66871096d12Seric } 6694d9c42c2Seric #ifdef NETINET 6704d9c42c2Seric addr.sin.sin_family = AF_INET; /*XXX*/ 67183c1f4bcSeric addr.sin.sin_addr.s_addr = hid; 6724d9c42c2Seric #endif 67371096d12Seric } 6741c71e510Seric else 6751c71e510Seric { 676d8984352Seric register char *p = &host[strlen(host) - 1]; 677d8984352Seric 67804344589Sbloom hp = gethostbyname(host); 679d8984352Seric if (hp == NULL && *p == '.') 680d8984352Seric { 681910ff93bSeric #ifdef NAMED_BIND 682910ff93bSeric int oldopts = _res.options; 683910ff93bSeric 684910ff93bSeric _res.options &= ~(RES_DEFNAMES|RES_DNSRCH); 685910ff93bSeric #endif 686d8984352Seric *p = '\0'; 687d8984352Seric hp = gethostbyname(host); 688d8984352Seric *p = '.'; 689910ff93bSeric #ifdef NAMED_BIND 690910ff93bSeric _res.options = oldopts; 691910ff93bSeric #endif 692d8984352Seric } 693a7e21fe6Seric gothostent: 694794bdbb9Smiriam if (hp == NULL) 695794bdbb9Smiriam { 6969d4a8008Seric #if NAMED_BIND 697794bdbb9Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 69852308a50Seric return (EX_TEMPFAIL); 69982e5d8ddSeric 700134746fbSeric /* if name server is specified, assume temp fail */ 701134746fbSeric if (errno == ECONNREFUSED && UseNameServer) 702134746fbSeric return (EX_TEMPFAIL); 703134746fbSeric #endif 7047aa493c5Seric return (EX_NOHOST); 705794bdbb9Smiriam } 70683c1f4bcSeric addr.sa.sa_family = hp->h_addrtype; 70783c1f4bcSeric switch (hp->h_addrtype) 70883c1f4bcSeric { 70983c1f4bcSeric #ifdef NETINET 71083c1f4bcSeric case AF_INET: 711e2f2f828Seric bcopy(hp->h_addr, 71283c1f4bcSeric &addr.sin.sin_addr, 713b4df10cdSeric INADDRSZ); 71483c1f4bcSeric break; 71583c1f4bcSeric #endif 71683c1f4bcSeric 71783c1f4bcSeric default: 718e2f2f828Seric bcopy(hp->h_addr, 71983c1f4bcSeric addr.sa.sa_data, 720e2f2f828Seric hp->h_length); 72183c1f4bcSeric break; 72283c1f4bcSeric } 72304344589Sbloom i = 1; 7241c71e510Seric } 7251c71e510Seric 7261c71e510Seric /* 7271c71e510Seric ** Determine the port number. 7281c71e510Seric */ 7291c71e510Seric 730fd7c0790Seric if (port != 0) 731e2f2f828Seric port = htons(port); 732fd7c0790Seric else 7331c71e510Seric { 7341c71e510Seric register struct servent *sp = getservbyname("smtp", "tcp"); 7351c71e510Seric 7361c71e510Seric if (sp == NULL) 7371c71e510Seric { 738ad977999Seric syserr("554 makeconnection: service \"smtp\" unknown"); 739e5311662Seric port = htons(25); 7401c71e510Seric } 741e5311662Seric else 742e2f2f828Seric port = sp->s_port; 743e2f2f828Seric } 744e2f2f828Seric 74583c1f4bcSeric switch (addr.sa.sa_family) 746e2f2f828Seric { 7474d9c42c2Seric #ifdef NETINET 748e2f2f828Seric case AF_INET: 74983c1f4bcSeric addr.sin.sin_port = port; 750e2f2f828Seric addrlen = sizeof (struct sockaddr_in); 751e2f2f828Seric break; 7524d9c42c2Seric #endif 753e2f2f828Seric 754e2f2f828Seric #ifdef NETISO 755e2f2f828Seric case AF_ISO: 756e2f2f828Seric /* assume two byte transport selector */ 757e2f2f828Seric bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2); 758e2f2f828Seric addrlen = sizeof (struct sockaddr_iso); 759e2f2f828Seric break; 760e2f2f828Seric #endif 761e2f2f828Seric 762e2f2f828Seric default: 76383c1f4bcSeric syserr("Can't connect to address family %d", addr.sa.sa_family); 764e2f2f828Seric return (EX_NOHOST); 7651c71e510Seric } 7667aa493c5Seric 7677aa493c5Seric /* 7687aa493c5Seric ** Try to actually open the connection. 7697aa493c5Seric */ 7707aa493c5Seric 771e17a3a5aSeric #ifdef XLA 772e17a3a5aSeric /* if too many connections, don't bother trying */ 773e17a3a5aSeric if (!xla_noqueue_ok(host)) 774e17a3a5aSeric return EX_TEMPFAIL; 775e17a3a5aSeric #endif 776e17a3a5aSeric 777aea02ca1Seric for (;;) 778aea02ca1Seric { 77961e4310fSeric if (tTd(16, 1)) 780e2f2f828Seric printf("makeconnection (%s [%s])\n", 781e2f2f828Seric host, anynet_ntoa(&addr)); 7827aa493c5Seric 783226e3022Seric /* save for logging */ 784226e3022Seric CurHostAddr = addr; 785226e3022Seric 786914346b1Seric if (usesecureport) 787914346b1Seric { 788914346b1Seric int rport = IPPORT_RESERVED - 1; 789914346b1Seric 790914346b1Seric s = rresvport(&rport); 791914346b1Seric } 792914346b1Seric else 793914346b1Seric { 794af5e902cSeric s = socket(AF_INET, SOCK_STREAM, 0); 795914346b1Seric } 7967aa493c5Seric if (s < 0) 7977aa493c5Seric { 7986286bb75Sbloom sav_errno = errno; 799914346b1Seric syserr("makeconnection: no socket"); 8007aa493c5Seric goto failure; 8017aa493c5Seric } 8027aa493c5Seric 803b35447dbSeric #ifdef SO_SNDBUF 804b35447dbSeric if (TcpSndBufferSize > 0) 805b35447dbSeric { 806b35447dbSeric if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, 807bf217a95Seric (char *) &TcpSndBufferSize, 808b35447dbSeric sizeof(TcpSndBufferSize)) < 0) 809b35447dbSeric syserr("makeconnection: setsockopt(SO_SNDBUF)"); 810b35447dbSeric } 811b35447dbSeric #endif 812b35447dbSeric 81361e4310fSeric if (tTd(16, 1)) 814b31e7f2bSeric printf("makeconnection: fd=%d\n", s); 8151b6e4a15Seric 8161b6e4a15Seric /* turn on network debugging? */ 817a2ef5fa4Seric if (tTd(16, 101)) 81852308a50Seric { 81952308a50Seric int on = 1; 8206173568dSeric (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 821aea02ca1Seric (char *)&on, sizeof on); 82252308a50Seric } 82387d6e633Srick if (CurEnv->e_xfp != NULL) 824877a6142Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 8254bd6a662Seric errno = 0; /* for debugging */ 826e2f2f828Seric if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0) 827aea02ca1Seric break; 828aea02ca1Seric 829aea02ca1Seric /* couldn't connect.... figure out why */ 8306286bb75Sbloom sav_errno = errno; 8316286bb75Sbloom (void) close(s); 83204344589Sbloom if (hp && hp->h_addr_list[i]) 83304344589Sbloom { 834aea02ca1Seric if (tTd(16, 1)) 835e2f2f828Seric printf("Connect failed (%s); trying new address....\n", 836e2f2f828Seric errstring(sav_errno)); 83783c1f4bcSeric switch (addr.sa.sa_family) 83883c1f4bcSeric { 83983c1f4bcSeric #ifdef NETINET 84083c1f4bcSeric case AF_INET: 841e2f2f828Seric bcopy(hp->h_addr_list[i++], 84283c1f4bcSeric &addr.sin.sin_addr, 843b4df10cdSeric INADDRSZ); 84483c1f4bcSeric break; 84583c1f4bcSeric #endif 84683c1f4bcSeric 84783c1f4bcSeric default: 848e2f2f828Seric bcopy(hp->h_addr_list[i++], 84983c1f4bcSeric addr.sa.sa_data, 850914346b1Seric hp->h_length); 85183c1f4bcSeric break; 85283c1f4bcSeric } 853aea02ca1Seric continue; 85404344589Sbloom } 85504344589Sbloom 8567aa493c5Seric /* failure, decide if temporary or not */ 8577aa493c5Seric failure: 858244b09d1Seric #ifdef XLA 859244b09d1Seric xla_host_end(host); 860244b09d1Seric #endif 861e2de2524Seric if (transienterror(sav_errno)) 862e2de2524Seric return EX_TEMPFAIL; 863e2de2524Seric else 86487d6e633Srick { 86508b25121Seric message("%s", errstring(sav_errno)); 8667aa493c5Seric return (EX_UNAVAILABLE); 8677aa493c5Seric } 8687aa493c5Seric } 8697aa493c5Seric 8707aa493c5Seric /* connection ok, put it into canonical form */ 871335eae58Seric if ((mci->mci_out = fdopen(s, "w")) == NULL || 872335eae58Seric (s = dup(s)) < 0 || 873ab81ee53Seric (mci->mci_in = fdopen(s, "r")) == NULL) 874335eae58Seric { 875335eae58Seric syserr("cannot open SMTP client channel, fd=%d", s); 876335eae58Seric return EX_TEMPFAIL; 877335eae58Seric } 8787aa493c5Seric 879dca8e1f7Seric return (EX_OK); 8807aa493c5Seric } 881444eaf03Seric /* 882444eaf03Seric ** MYHOSTNAME -- return the name of this host. 883444eaf03Seric ** 884444eaf03Seric ** Parameters: 885444eaf03Seric ** hostbuf -- a place to return the name of this host. 886897f1869Seric ** size -- the size of hostbuf. 887444eaf03Seric ** 888444eaf03Seric ** Returns: 889444eaf03Seric ** A list of aliases for this host. 890444eaf03Seric ** 891444eaf03Seric ** Side Effects: 892d8d0a4aeSeric ** Adds numeric codes to $=w. 893444eaf03Seric */ 894444eaf03Seric 895fff14d4eSeric struct hostent * 896897f1869Seric myhostname(hostbuf, size) 897444eaf03Seric char hostbuf[]; 898897f1869Seric int size; 899444eaf03Seric { 90038ad259dSeric register struct hostent *hp; 901444eaf03Seric extern struct hostent *gethostbyname(); 902*423161fbSeric extern bool getcanonname(); 903*423161fbSeric extern int h_errno; 904444eaf03Seric 905af5e902cSeric if (gethostname(hostbuf, size) < 0) 906af5e902cSeric { 907af5e902cSeric (void) strcpy(hostbuf, "localhost"); 908af5e902cSeric } 909a44d5a5eSeric hp = gethostbyname(hostbuf); 9104a5c6430Seric if (hp == NULL) 9117364df9fSeric { 9124a5c6430Seric syserr("!My host name (%s) does not seem to exist!", hostbuf); 9134a5c6430Seric } 914*423161fbSeric if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL) 915*423161fbSeric { 91635852b23Seric (void) strncpy(hostbuf, hp->h_name, size - 1); 91735852b23Seric hostbuf[size - 1] = '\0'; 918*423161fbSeric } 9194a5c6430Seric 9204a5c6430Seric #if NAMED_BIND 921*423161fbSeric /* 922*423161fbSeric ** If still no dot, try DNS directly (i.e., avoid NIS problems). 923*423161fbSeric ** This ought to be driven from the configuration file, but 924*423161fbSeric ** we are called before the configuration is read. We could 925*423161fbSeric ** check for an /etc/resolv.conf file, but that isn't required. 926*423161fbSeric ** All in all, a bit of a mess. 927*423161fbSeric */ 928*423161fbSeric 929*423161fbSeric if (strchr(hostbuf, '.') == NULL && 930*423161fbSeric !getcanonname(hostbuf, size, TRUE) && 931*423161fbSeric h_errno == TRY_AGAIN) 932747df804Seric { 933*423161fbSeric struct stat stbuf; 934747df804Seric 9354a5c6430Seric /* try twice in case name server not yet started up */ 936*423161fbSeric message("My unqualifed host name (%s) unknown to DNS; sleeping for retry", 9374a5c6430Seric hostbuf); 938*423161fbSeric sleep(60); 939*423161fbSeric if (!getcanonname(hostbuf, size, TRUE)) 940*423161fbSeric errno = h_errno + E_DNSBASE; 941747df804Seric } 942747df804Seric #endif 943fff14d4eSeric return (hp); 9447364df9fSeric } 945cb452edcSeric /* 9469f8b0eadSeric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 9479f8b0eadSeric ** 9489f8b0eadSeric ** Uses RFC1413 protocol to try to get info from the other end. 949320e0d1cSeric ** 950320e0d1cSeric ** Parameters: 951320e0d1cSeric ** fd -- the descriptor 952320e0d1cSeric ** 953320e0d1cSeric ** Returns: 9549f8b0eadSeric ** The user@host information associated with this descriptor. 955320e0d1cSeric */ 956320e0d1cSeric 957c73f2aa4Seric #if IDENTPROTO 9589f8b0eadSeric 9599f8b0eadSeric static jmp_buf CtxAuthTimeout; 9609f8b0eadSeric 9619f8b0eadSeric static 9629f8b0eadSeric authtimeout() 9639f8b0eadSeric { 9649f8b0eadSeric longjmp(CtxAuthTimeout, 1); 9659f8b0eadSeric } 9669f8b0eadSeric 9679f8b0eadSeric #endif 9689f8b0eadSeric 969320e0d1cSeric char * 9709f8b0eadSeric getauthinfo(fd) 971320e0d1cSeric int fd; 972320e0d1cSeric { 9739f8b0eadSeric int falen; 974a5546e24Seric register char *p; 975c73f2aa4Seric #if IDENTPROTO 9769f8b0eadSeric SOCKADDR la; 9779f8b0eadSeric int lalen; 9789f8b0eadSeric register struct servent *sp; 9799f8b0eadSeric int s; 9809f8b0eadSeric int i; 9819f8b0eadSeric EVENT *ev; 9829f8b0eadSeric #endif 9839f8b0eadSeric static char hbuf[MAXNAME * 2 + 2]; 9849f8b0eadSeric extern char *hostnamebyanyaddr(); 9859f8b0eadSeric extern char RealUserName[]; /* main.c */ 986320e0d1cSeric 987e29a76d1Seric falen = sizeof RealHostAddr; 988e29a76d1Seric if (getpeername(fd, &RealHostAddr.sa, &falen) < 0 || falen <= 0 || 989e29a76d1Seric RealHostAddr.sa.sa_family == 0) 9909f8b0eadSeric { 9919f8b0eadSeric (void) sprintf(hbuf, "%s@localhost", RealUserName); 99253853673Seric if (tTd(9, 1)) 9939f8b0eadSeric printf("getauthinfo: %s\n", hbuf); 994320e0d1cSeric return hbuf; 995320e0d1cSeric } 9969f8b0eadSeric 997e29a76d1Seric if (RealHostName == NULL) 998e29a76d1Seric { 999e29a76d1Seric /* translate that to a host name */ 1000e29a76d1Seric RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr)); 1001e29a76d1Seric } 1002e29a76d1Seric 1003c73f2aa4Seric #if IDENTPROTO 100493b3215bSeric if (TimeOuts.to_ident == 0) 100593b3215bSeric goto noident; 100693b3215bSeric 10079f8b0eadSeric lalen = sizeof la; 1008e29a76d1Seric if (RealHostAddr.sa.sa_family != AF_INET || 10099f8b0eadSeric getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 || 10109f8b0eadSeric la.sa.sa_family != AF_INET) 10119f8b0eadSeric { 10129f8b0eadSeric /* no ident info */ 10139f8b0eadSeric goto noident; 10149f8b0eadSeric } 10159f8b0eadSeric 10169f8b0eadSeric /* create ident query */ 1017f2d880b6Seric (void) sprintf(hbuf, "%d,%d\r\n", 1018e29a76d1Seric ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port)); 10199f8b0eadSeric 10209f8b0eadSeric /* create local address */ 1021d6af7dadSeric la.sin.sin_port = 0; 10229f8b0eadSeric 10239f8b0eadSeric /* create foreign address */ 10249f8b0eadSeric sp = getservbyname("auth", "tcp"); 10259f8b0eadSeric if (sp != NULL) 1026e29a76d1Seric RealHostAddr.sin.sin_port = sp->s_port; 10279f8b0eadSeric else 1028e29a76d1Seric RealHostAddr.sin.sin_port = htons(113); 10299f8b0eadSeric 10309f8b0eadSeric s = -1; 10319f8b0eadSeric if (setjmp(CtxAuthTimeout) != 0) 10329f8b0eadSeric { 10339f8b0eadSeric if (s >= 0) 10349f8b0eadSeric (void) close(s); 10359f8b0eadSeric goto noident; 10369f8b0eadSeric } 10379f8b0eadSeric 10389f8b0eadSeric /* put a timeout around the whole thing */ 1039a0f780efSeric ev = setevent(TimeOuts.to_ident, authtimeout, 0); 10409f8b0eadSeric 1041d6af7dadSeric /* connect to foreign IDENT server using same address as SMTP socket */ 10429f8b0eadSeric s = socket(AF_INET, SOCK_STREAM, 0); 10439f8b0eadSeric if (s < 0) 10449f8b0eadSeric { 10459f8b0eadSeric clrevent(ev); 10469f8b0eadSeric goto noident; 10479f8b0eadSeric } 1048d6af7dadSeric if (bind(s, &la.sa, sizeof la.sin) < 0 || 1049e29a76d1Seric connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0) 10509f8b0eadSeric { 10517c201575Seric goto closeident; 10529f8b0eadSeric } 10539f8b0eadSeric 105453853673Seric if (tTd(9, 10)) 10559f8b0eadSeric printf("getauthinfo: sent %s", hbuf); 10569f8b0eadSeric 10579f8b0eadSeric /* send query */ 10589f8b0eadSeric if (write(s, hbuf, strlen(hbuf)) < 0) 10599f8b0eadSeric goto closeident; 10609f8b0eadSeric 10619f8b0eadSeric /* get result */ 10629f8b0eadSeric i = read(s, hbuf, sizeof hbuf); 10639f8b0eadSeric (void) close(s); 10649f8b0eadSeric clrevent(ev); 10659f8b0eadSeric if (i <= 0) 10669f8b0eadSeric goto noident; 10679f8b0eadSeric if (hbuf[--i] == '\n' && hbuf[--i] == '\r') 10689f8b0eadSeric i--; 10699f8b0eadSeric hbuf[++i] = '\0'; 10709f8b0eadSeric 107153853673Seric if (tTd(9, 3)) 10729f8b0eadSeric printf("getauthinfo: got %s\n", hbuf); 10739f8b0eadSeric 10749f8b0eadSeric /* parse result */ 10759f8b0eadSeric p = strchr(hbuf, ':'); 10769f8b0eadSeric if (p == NULL) 10779f8b0eadSeric { 10789f8b0eadSeric /* malformed response */ 10799f8b0eadSeric goto noident; 10809f8b0eadSeric } 10819f8b0eadSeric while (isascii(*++p) && isspace(*p)) 10829f8b0eadSeric continue; 10839f8b0eadSeric if (strncasecmp(p, "userid", 6) != 0) 10849f8b0eadSeric { 10859f8b0eadSeric /* presumably an error string */ 10869f8b0eadSeric goto noident; 10879f8b0eadSeric } 10889f8b0eadSeric p += 6; 10899f8b0eadSeric while (isascii(*p) && isspace(*p)) 10909f8b0eadSeric p++; 10919f8b0eadSeric if (*p++ != ':') 10929f8b0eadSeric { 10939f8b0eadSeric /* either useridxx or malformed response */ 10949f8b0eadSeric goto noident; 10959f8b0eadSeric } 10969f8b0eadSeric 10979f8b0eadSeric /* p now points to the OSTYPE field */ 10989f8b0eadSeric p = strchr(p, ':'); 10999f8b0eadSeric if (p == NULL) 11009f8b0eadSeric { 11019f8b0eadSeric /* malformed response */ 11029f8b0eadSeric goto noident; 11039f8b0eadSeric } 110453853673Seric 110553853673Seric /* 1413 says don't do this -- but it's broken otherwise */ 110653853673Seric while (isascii(*++p) && isspace(*p)) 110753853673Seric continue; 11089f8b0eadSeric 11099f8b0eadSeric /* p now points to the authenticated name */ 1110f7869e68Seric (void) sprintf(hbuf, "%s@%s", 1111f7869e68Seric p, RealHostName == NULL ? "localhost" : RealHostName); 111253853673Seric goto finish; 111353853673Seric 11147c201575Seric closeident: 11157c201575Seric (void) close(s); 11167c201575Seric clrevent(ev); 11177c201575Seric 111853853673Seric #endif /* IDENTPROTO */ 111953853673Seric 112053853673Seric noident: 1121f7869e68Seric if (RealHostName == NULL) 1122f7869e68Seric { 1123f7869e68Seric if (tTd(9, 1)) 1124f7869e68Seric printf("getauthinfo: NULL\n"); 1125f7869e68Seric return NULL; 1126f7869e68Seric } 112753853673Seric (void) strcpy(hbuf, RealHostName); 112853853673Seric 112953853673Seric finish: 1130f7869e68Seric if (RealHostName != NULL && RealHostName[0] != '[') 11319f8b0eadSeric { 11329f8b0eadSeric p = &hbuf[strlen(hbuf)]; 11339f8b0eadSeric (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr)); 11349f8b0eadSeric } 113553853673Seric if (tTd(9, 1)) 11369f8b0eadSeric printf("getauthinfo: %s\n", hbuf); 11379f8b0eadSeric return hbuf; 11389f8b0eadSeric } 1139320e0d1cSeric /* 114008de856eSeric ** HOST_MAP_LOOKUP -- turn a hostname into canonical form 114115d084d5Seric ** 114215d084d5Seric ** Parameters: 114305b57da8Seric ** map -- a pointer to this map (unused). 114408de856eSeric ** name -- the (presumably unqualified) hostname. 114500b385a9Seric ** av -- unused -- for compatibility with other mapping 1146d798a1deSeric ** functions. 11472d29d43aSeric ** statp -- an exit status (out parameter) -- set to 11482d29d43aSeric ** EX_TEMPFAIL if the name server is unavailable. 114915d084d5Seric ** 115015d084d5Seric ** Returns: 115115d084d5Seric ** The mapping, if found. 115215d084d5Seric ** NULL if no mapping found. 115315d084d5Seric ** 115415d084d5Seric ** Side Effects: 115515d084d5Seric ** Looks up the host specified in hbuf. If it is not 115615d084d5Seric ** the canonical name for that host, return the canonical 115715d084d5Seric ** name. 1158f36ede03Sbostic */ 1159cb452edcSeric 116015d084d5Seric char * 116100b385a9Seric host_map_lookup(map, name, av, statp) 116205b57da8Seric MAP *map; 116308de856eSeric char *name; 116400b385a9Seric char **av; 11652d29d43aSeric int *statp; 116699f7cf32Seric { 116799f7cf32Seric register struct hostent *hp; 11688f95558bSeric struct in_addr in_addr; 116905b57da8Seric char *cp; 117038ad259dSeric int i; 1171eea91d78Seric register STAB *s; 117200b385a9Seric char hbuf[MAXNAME]; 1173eea91d78Seric extern struct hostent *gethostbyaddr(); 11749d4a8008Seric #if NAMED_BIND 1175eea91d78Seric extern int h_errno; 1176c304a798Seric #endif 11775f78836eSmiriam 1178f36ede03Sbostic /* 1179eea91d78Seric ** See if we have already looked up this name. If so, just 1180eea91d78Seric ** return it. 1181eea91d78Seric */ 1182eea91d78Seric 118308de856eSeric s = stab(name, ST_NAMECANON, ST_ENTER); 1184eea91d78Seric if (bitset(NCF_VALID, s->s_namecanon.nc_flags)) 1185eea91d78Seric { 1186f92c3297Seric if (tTd(9, 1)) 118708de856eSeric printf("host_map_lookup(%s) => CACHE %s\n", 118808de856eSeric name, s->s_namecanon.nc_cname); 1189eea91d78Seric errno = s->s_namecanon.nc_errno; 11909d4a8008Seric #if NAMED_BIND 1191eea91d78Seric h_errno = s->s_namecanon.nc_herrno; 1192c304a798Seric #endif 1193eea91d78Seric *statp = s->s_namecanon.nc_stat; 119492270fb3Seric if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL) 1195ed63aae0Seric { 1196ed63aae0Seric sprintf(hbuf, "%s: Name server timeout", 1197ed63aae0Seric shortenstring(name, 33)); 1198ed63aae0Seric CurEnv->e_message = newstr(hbuf); 1199ed63aae0Seric } 1200eea91d78Seric return s->s_namecanon.nc_cname; 1201eea91d78Seric } 1202eea91d78Seric 1203eea91d78Seric /* 1204eea91d78Seric ** If first character is a bracket, then it is an address 1205eea91d78Seric ** lookup. Address is copied into a temporary buffer to 120608de856eSeric ** strip the brackets and to preserve name if address is 1207eea91d78Seric ** unknown. 1208f36ede03Sbostic */ 120915d084d5Seric 121008de856eSeric if (*name != '[') 121115d084d5Seric { 1212d798a1deSeric extern bool getcanonname(); 1213d798a1deSeric 12148cb4653dSeric if (tTd(9, 1)) 121508de856eSeric printf("host_map_lookup(%s) => ", name); 1216eea91d78Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 121708de856eSeric (void) strcpy(hbuf, name); 12181f2ff1a4Seric if (getcanonname(hbuf, sizeof hbuf - 1, TRUE)) 12199040ec4fSeric { 12209040ec4fSeric if (tTd(9, 1)) 12219040ec4fSeric printf("%s\n", hbuf); 122200b385a9Seric cp = map_rewrite(map, hbuf, strlen(hbuf), av); 122300b385a9Seric s->s_namecanon.nc_cname = newstr(cp); 122400b385a9Seric return cp; 12259040ec4fSeric } 122615d084d5Seric else 12279040ec4fSeric { 12282d29d43aSeric register struct hostent *hp; 12292d29d43aSeric 1230c304a798Seric s->s_namecanon.nc_errno = errno; 12319d4a8008Seric #if NAMED_BIND 1232c304a798Seric s->s_namecanon.nc_herrno = h_errno; 12339040ec4fSeric if (tTd(9, 1)) 12342d29d43aSeric printf("FAIL (%d)\n", h_errno); 12352d29d43aSeric switch (h_errno) 12362d29d43aSeric { 12372d29d43aSeric case TRY_AGAIN: 123889cb2793Seric if (UseNameServer) 12398820d51bSeric { 1240e0326f4fSeric sprintf(hbuf, "%s: Name server timeout", 1241ed63aae0Seric shortenstring(name, 33)); 1242e0326f4fSeric message("%s", hbuf); 12438820d51bSeric if (CurEnv->e_message == NULL) 1244e0326f4fSeric CurEnv->e_message = newstr(hbuf); 12458820d51bSeric } 12462d29d43aSeric *statp = EX_TEMPFAIL; 12472d29d43aSeric break; 12482d29d43aSeric 12492d29d43aSeric case HOST_NOT_FOUND: 12502d29d43aSeric *statp = EX_NOHOST; 12512d29d43aSeric break; 12522d29d43aSeric 12532d29d43aSeric case NO_RECOVERY: 12542d29d43aSeric *statp = EX_SOFTWARE; 12552d29d43aSeric break; 12562d29d43aSeric 12572d29d43aSeric default: 12582d29d43aSeric *statp = EX_UNAVAILABLE; 12592d29d43aSeric break; 12602d29d43aSeric } 1261c304a798Seric #else 1262c304a798Seric if (tTd(9, 1)) 1263c304a798Seric printf("FAIL\n"); 1264c304a798Seric *statp = EX_NOHOST; 1265c304a798Seric #endif 1266eea91d78Seric s->s_namecanon.nc_stat = *statp; 12677152aeb4Seric if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) || 12687152aeb4Seric UseNameServer) 126915d084d5Seric return NULL; 12702d29d43aSeric 12712d29d43aSeric /* 12722d29d43aSeric ** Try to look it up in /etc/hosts 12732d29d43aSeric */ 12742d29d43aSeric 127508de856eSeric hp = gethostbyname(name); 12762d29d43aSeric if (hp == NULL) 12772d29d43aSeric { 12782d29d43aSeric /* no dice there either */ 1279eea91d78Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST; 12802d29d43aSeric return NULL; 12812d29d43aSeric } 12822d29d43aSeric 1283eea91d78Seric s->s_namecanon.nc_stat = *statp = EX_OK; 128400b385a9Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 128500b385a9Seric s->s_namecanon.nc_cname = newstr(cp); 128600b385a9Seric return cp; 128715d084d5Seric } 12889040ec4fSeric } 128908de856eSeric if ((cp = strchr(name, ']')) == NULL) 129015d084d5Seric return (NULL); 129134e39927Sbostic *cp = '\0'; 12928f95558bSeric in_addr.s_addr = inet_addr(&name[1]); 129338ad259dSeric 129438ad259dSeric /* nope -- ask the name server */ 1295b4df10cdSeric hp = gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET); 1296eea91d78Seric s->s_namecanon.nc_errno = errno; 12979d4a8008Seric #if NAMED_BIND 1298eea91d78Seric s->s_namecanon.nc_herrno = h_errno; 1299c304a798Seric #endif 1300eea91d78Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 13015f78836eSmiriam if (hp == NULL) 1302eea91d78Seric { 1303eea91d78Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST; 130415d084d5Seric return (NULL); 1305eea91d78Seric } 130615d084d5Seric 130738ad259dSeric /* found a match -- copy out */ 130800b385a9Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 1309eea91d78Seric s->s_namecanon.nc_stat = *statp = EX_OK; 131000b385a9Seric s->s_namecanon.nc_cname = newstr(cp); 131100b385a9Seric return cp; 131299f7cf32Seric } 1313e2f2f828Seric /* 1314e2f2f828Seric ** ANYNET_NTOA -- convert a network address to printable form. 1315e2f2f828Seric ** 1316e2f2f828Seric ** Parameters: 1317e2f2f828Seric ** sap -- a pointer to a sockaddr structure. 1318e2f2f828Seric ** 1319e2f2f828Seric ** Returns: 1320e2f2f828Seric ** A printable version of that sockaddr. 1321e2f2f828Seric */ 1322e2f2f828Seric 1323e2f2f828Seric char * 1324e2f2f828Seric anynet_ntoa(sap) 1325e2f2f828Seric register SOCKADDR *sap; 1326e2f2f828Seric { 1327e2f2f828Seric register char *bp; 1328e2f2f828Seric register char *ap; 1329e2f2f828Seric int l; 1330e387851eSeric static char buf[100]; 1331e2f2f828Seric 13328cb4653dSeric /* check for null/zero family */ 13338cb4653dSeric if (sap == NULL) 13348cb4653dSeric return "NULLADDR"; 13358cb4653dSeric if (sap->sa.sa_family == 0) 13368cb4653dSeric return "0"; 13378cb4653dSeric 1338e387851eSeric switch (sap->sa.sa_family) 1339e387851eSeric { 1340139b52c8Seric #ifdef NETUNIX 1341e387851eSeric case AF_UNIX: 1342c24cf5a4Seric if (sap->sunix.sun_path[0] != '\0') 1343c24cf5a4Seric sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path); 1344e387851eSeric else 1345e387851eSeric sprintf(buf, "[UNIX: localhost]"); 1346e387851eSeric return buf; 1347e387851eSeric #endif 1348e387851eSeric 134983c1f4bcSeric #ifdef NETINET 1350e387851eSeric case AF_INET: 1351e2f2f828Seric return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr); 135283c1f4bcSeric #endif 1353e2f2f828Seric 1354e387851eSeric default: 1355e387851eSeric /* this case is only to ensure syntactic correctness */ 1356e387851eSeric break; 1357e387851eSeric } 1358e387851eSeric 1359e2f2f828Seric /* unknown family -- just dump bytes */ 136083c1f4bcSeric (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); 1361e2f2f828Seric bp = &buf[strlen(buf)]; 136283c1f4bcSeric ap = sap->sa.sa_data; 136383c1f4bcSeric for (l = sizeof sap->sa.sa_data; --l >= 0; ) 1364e2f2f828Seric { 1365e2f2f828Seric (void) sprintf(bp, "%02x:", *ap++ & 0377); 1366e2f2f828Seric bp += 3; 1367e2f2f828Seric } 1368e2f2f828Seric *--bp = '\0'; 1369e2f2f828Seric return buf; 1370e2f2f828Seric } 13719f8b0eadSeric /* 13729f8b0eadSeric ** HOSTNAMEBYANYADDR -- return name of host based on address 13739f8b0eadSeric ** 13749f8b0eadSeric ** Parameters: 13759f8b0eadSeric ** sap -- SOCKADDR pointer 13769f8b0eadSeric ** 13779f8b0eadSeric ** Returns: 13789f8b0eadSeric ** text representation of host name. 13799f8b0eadSeric ** 13809f8b0eadSeric ** Side Effects: 13819f8b0eadSeric ** none. 13829f8b0eadSeric */ 13839f8b0eadSeric 13849f8b0eadSeric char * 13859f8b0eadSeric hostnamebyanyaddr(sap) 13869f8b0eadSeric register SOCKADDR *sap; 13879f8b0eadSeric { 13889f8b0eadSeric register struct hostent *hp; 13893490b9dfSeric int saveretry; 13903490b9dfSeric 13919d4a8008Seric #if NAMED_BIND 13923490b9dfSeric /* shorten name server timeout to avoid higher level timeouts */ 13933490b9dfSeric saveretry = _res.retry; 13943490b9dfSeric _res.retry = 3; 13953490b9dfSeric #endif /* NAMED_BIND */ 13963490b9dfSeric 13979f8b0eadSeric switch (sap->sa.sa_family) 13989f8b0eadSeric { 13999f8b0eadSeric #ifdef NETINET 14009f8b0eadSeric case AF_INET: 14019f8b0eadSeric hp = gethostbyaddr((char *) &sap->sin.sin_addr, 1402b4df10cdSeric INADDRSZ, 14039f8b0eadSeric AF_INET); 14049f8b0eadSeric break; 14059f8b0eadSeric #endif 14069f8b0eadSeric 14079f8b0eadSeric #ifdef NETISO 14089f8b0eadSeric case AF_ISO: 14099f8b0eadSeric hp = gethostbyaddr((char *) &sap->siso.siso_addr, 14109f8b0eadSeric sizeof sap->siso.siso_addr, 14119f8b0eadSeric AF_ISO); 14129f8b0eadSeric break; 14139f8b0eadSeric #endif 14149f8b0eadSeric 1415e387851eSeric case AF_UNIX: 1416e387851eSeric hp = NULL; 1417e387851eSeric break; 1418e387851eSeric 14199f8b0eadSeric default: 14209f8b0eadSeric hp = gethostbyaddr(sap->sa.sa_data, 14219f8b0eadSeric sizeof sap->sa.sa_data, 14229f8b0eadSeric sap->sa.sa_family); 14239f8b0eadSeric break; 14249f8b0eadSeric } 14259f8b0eadSeric 14269d4a8008Seric #if NAMED_BIND 14273490b9dfSeric _res.retry = saveretry; 14283490b9dfSeric #endif /* NAMED_BIND */ 14293490b9dfSeric 14309f8b0eadSeric if (hp != NULL) 14319f8b0eadSeric return hp->h_name; 14329f8b0eadSeric else 14339f8b0eadSeric { 14349f8b0eadSeric /* produce a dotted quad */ 14359f8b0eadSeric static char buf[512]; 14369f8b0eadSeric 14379f8b0eadSeric (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); 14389f8b0eadSeric return buf; 14399f8b0eadSeric } 14409f8b0eadSeric } 1441f36ede03Sbostic 14426c2c3107Seric # else /* DAEMON */ 144399f7cf32Seric /* code for systems without sophisticated networking */ 1444444eaf03Seric 1445444eaf03Seric /* 1446444eaf03Seric ** MYHOSTNAME -- stub version for case of no daemon code. 144721e9914dSeric ** 144821e9914dSeric ** Can't convert to upper case here because might be a UUCP name. 1449897f1869Seric ** 1450897f1869Seric ** Mark, you can change this to be anything you want...... 1451444eaf03Seric */ 1452444eaf03Seric 1453444eaf03Seric char ** 1454897f1869Seric myhostname(hostbuf, size) 1455444eaf03Seric char hostbuf[]; 1456897f1869Seric int size; 1457444eaf03Seric { 1458444eaf03Seric register FILE *f; 1459444eaf03Seric 1460444eaf03Seric hostbuf[0] = '\0'; 1461444eaf03Seric f = fopen("/usr/include/whoami", "r"); 1462444eaf03Seric if (f != NULL) 1463444eaf03Seric { 1464897f1869Seric (void) fgets(hostbuf, size, f); 1465444eaf03Seric fixcrlf(hostbuf, TRUE); 1466444eaf03Seric (void) fclose(f); 1467444eaf03Seric } 1468444eaf03Seric return (NULL); 1469444eaf03Seric } 147099f7cf32Seric /* 14719f8b0eadSeric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 1472320e0d1cSeric ** 1473320e0d1cSeric ** Parameters: 1474320e0d1cSeric ** fd -- the descriptor 1475320e0d1cSeric ** 1476320e0d1cSeric ** Returns: 1477320e0d1cSeric ** The host name associated with this descriptor, if it can 1478320e0d1cSeric ** be determined. 1479320e0d1cSeric ** NULL otherwise. 1480320e0d1cSeric ** 1481320e0d1cSeric ** Side Effects: 1482320e0d1cSeric ** none 1483320e0d1cSeric */ 1484320e0d1cSeric 1485320e0d1cSeric char * 14869f8b0eadSeric getauthinfo(fd) 1487320e0d1cSeric int fd; 1488320e0d1cSeric { 1489320e0d1cSeric return NULL; 1490320e0d1cSeric } 1491320e0d1cSeric /* 149299f7cf32Seric ** MAPHOSTNAME -- turn a hostname into canonical form 149399f7cf32Seric ** 149499f7cf32Seric ** Parameters: 149505b57da8Seric ** map -- a pointer to the database map. 149608de856eSeric ** name -- a buffer containing a hostname. 149715d084d5Seric ** avp -- a pointer to a (cf file defined) argument vector. 14982d29d43aSeric ** statp -- an exit status (out parameter). 149999f7cf32Seric ** 150099f7cf32Seric ** Returns: 150115d084d5Seric ** mapped host name 1502cb452edcSeric ** FALSE otherwise. 150399f7cf32Seric ** 150499f7cf32Seric ** Side Effects: 150508de856eSeric ** Looks up the host specified in name. If it is not 150699f7cf32Seric ** the canonical name for that host, replace it with 150799f7cf32Seric ** the canonical name. If the name is unknown, or it 150899f7cf32Seric ** is already the canonical name, leave it unchanged. 150999f7cf32Seric */ 151099f7cf32Seric 151199f7cf32Seric /*ARGSUSED*/ 151215d084d5Seric char * 151308de856eSeric host_map_lookup(map, name, avp, statp) 151405b57da8Seric MAP *map; 151508de856eSeric char *name; 151615d084d5Seric char **avp; 15172d29d43aSeric char *statp; 151899f7cf32Seric { 15192d29d43aSeric register struct hostent *hp; 15202d29d43aSeric 152108de856eSeric hp = gethostbyname(name); 15222d29d43aSeric if (hp != NULL) 15232d29d43aSeric return hp->h_name; 15242d29d43aSeric *statp = EX_NOHOST; 152515d084d5Seric return NULL; 152699f7cf32Seric } 152799f7cf32Seric 15286c2c3107Seric #endif /* DAEMON */ 1529