122700Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 333780Sbostic * Copyright (c) 1988 Regents of the University of California. 433780Sbostic * All rights reserved. 533780Sbostic * 642825Sbostic * %sccs.include.redist.c% 733780Sbostic */ 822700Sdist 933932Sbostic #include <errno.h> 1040962Sbostic #include "sendmail.h" 114535Seric 1233780Sbostic #ifndef lint 1333780Sbostic #ifdef DAEMON 14*53739Seric static char sccsid[] = "@(#)daemon.c 5.42.1.2 (Berkeley) 05/29/92 (with daemon mode)"; 1533780Sbostic #else 16*53739Seric static char sccsid[] = "@(#)daemon.c 5.42.1.2 (Berkeley) 05/29/92 (without daemon mode)"; 1733780Sbostic #endif 1833780Sbostic #endif /* not lint */ 194535Seric 2033780Sbostic #ifdef DAEMON 2133780Sbostic 2223120Seric # include <netdb.h> 2324945Seric # include <sys/signal.h> 2423120Seric # include <sys/wait.h> 2523120Seric # include <sys/time.h> 2623120Seric # include <sys/resource.h> 275978Seric 284535Seric /* 294535Seric ** DAEMON.C -- routines to use when running as a daemon. 307556Seric ** 317556Seric ** This entire file is highly dependent on the 4.2 BSD 327556Seric ** interprocess communication primitives. No attempt has 337556Seric ** been made to make this file portable to Version 7, 347556Seric ** Version 6, MPX files, etc. If you should try such a 357556Seric ** thing yourself, I recommend chucking the entire file 367556Seric ** and starting from scratch. Basic semantics are: 377556Seric ** 387556Seric ** getrequests() 397556Seric ** Opens a port and initiates a connection. 407556Seric ** Returns in a child. Must set InChannel and 417556Seric ** OutChannel appropriately. 4210206Seric ** clrdaemon() 4310206Seric ** Close any open files associated with getting 4410206Seric ** the connection; this is used when running the queue, 4510206Seric ** etc., to avoid having extra file descriptors during 4610206Seric ** the queue run and to avoid confusing the network 4710206Seric ** code (if it cares). 4852106Seric ** makeconnection(host, port, outfile, infile, usesecureport) 497556Seric ** Make a connection to the named host on the given 507556Seric ** port. Set *outfile and *infile to the files 517556Seric ** appropriate for communication. Returns zero on 527556Seric ** success, else an exit status describing the 537556Seric ** error. 5425699Seric ** maphostname(hbuf, hbufsize) 5525699Seric ** Convert the entry in hbuf into a canonical form. It 5625699Seric ** may not be larger than hbufsize. 574535Seric */ 584535Seric /* 594535Seric ** GETREQUESTS -- open mail IPC port and get requests. 604535Seric ** 614535Seric ** Parameters: 624535Seric ** none. 634535Seric ** 644535Seric ** Returns: 654535Seric ** none. 664535Seric ** 674535Seric ** Side Effects: 684535Seric ** Waits until some interesting activity occurs. When 694535Seric ** it does, a child is created to process it, and the 704535Seric ** parent waits for completion. Return from this 719886Seric ** routine is always in the child. The file pointers 729886Seric ** "InChannel" and "OutChannel" should be set to point 739886Seric ** to the communication channel. 744535Seric */ 754535Seric 7616144Seric int DaemonSocket = -1; /* fd describing socket */ 7716144Seric 784535Seric getrequests() 794535Seric { 809610Seric int t; 819610Seric register struct servent *sp; 8225027Seric int on = 1; 8352106Seric struct sockaddr_in srvraddr; 8446928Sbostic extern void reapchild(); 857117Seric 869610Seric /* 879610Seric ** Set up the address for the mailer. 889610Seric */ 899610Seric 909610Seric sp = getservbyname("smtp", "tcp"); 919610Seric if (sp == NULL) 929610Seric { 939610Seric syserr("server \"smtp\" unknown"); 9410167Seric goto severe; 959610Seric } 9652106Seric srvraddr.sin_family = AF_INET; 9752106Seric srvraddr.sin_addr.s_addr = INADDR_ANY; 9852106Seric srvraddr.sin_port = sp->s_port; 999610Seric 1009610Seric /* 1019610Seric ** Try to actually open the connection. 1029610Seric */ 1039610Seric 1049610Seric if (tTd(15, 1)) 10552106Seric printf("getrequests: port 0x%x\n", srvraddr.sin_port); 1069610Seric 1079610Seric /* get a socket for the SMTP connection */ 10823120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 10910206Seric if (DaemonSocket < 0) 1109610Seric { 1119610Seric /* probably another daemon already */ 1129610Seric syserr("getrequests: can't create socket"); 1139610Seric severe: 1149610Seric # ifdef LOG 1159610Seric if (LogLevel > 0) 11624858Seric syslog(LOG_ALERT, "cannot get connection"); 1179610Seric # endif LOG 1189610Seric finis(); 1199610Seric } 12010347Seric 12110347Seric /* turn on network debugging? */ 12210347Seric if (tTd(15, 15)) 12324945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 12410347Seric 12525027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 12625027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 12725027Seric 12852106Seric if (bind(DaemonSocket, (struct sockaddr *)&srvraddr, sizeof srvraddr) < 0) 1299610Seric { 1309610Seric syserr("getrequests: cannot bind"); 13110206Seric (void) close(DaemonSocket); 1329610Seric goto severe; 1339610Seric } 134*53739Seric if (listen(DaemonSocket, 10) < 0) 135*53739Seric { 136*53739Seric syserr("getrequests: cannot listen"); 137*53739Seric (void) close(DaemonSocket); 138*53739Seric goto severe; 139*53739Seric } 1409610Seric 14124955Seric (void) signal(SIGCHLD, reapchild); 14224945Seric 1439610Seric if (tTd(15, 1)) 14410206Seric printf("getrequests: %d\n", DaemonSocket); 1459610Seric 1464631Seric for (;;) 1474631Seric { 14814875Seric register int pid; 14911147Seric auto int lotherend; 15011147Seric 15114875Seric /* see if we are rejecting connections */ 152*53739Seric while ((CurrentLA = getla()) > RefuseLA) 15336584Sbostic { 154*53739Seric setproctitle("rejecting connections: load average: %.2f", (double)CurrentLA); 15514875Seric sleep(5); 15636584Sbostic } 15714875Seric 1589610Seric /* wait for a connection */ 159*53739Seric setproctitle("accepting connections"); 1609610Seric do 1619610Seric { 1629610Seric errno = 0; 16336230Skarels lotherend = sizeof RealHostAddr; 16446928Sbostic t = accept(DaemonSocket, 16546928Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 1669610Seric } while (t < 0 && errno == EINTR); 1679610Seric if (t < 0) 1685978Seric { 1699610Seric syserr("getrequests: accept"); 1709610Seric sleep(5); 1719610Seric continue; 1725978Seric } 1734631Seric 1745978Seric /* 1755978Seric ** Create a subprocess to process the mail. 1765978Seric */ 1775978Seric 1787677Seric if (tTd(15, 2)) 1799610Seric printf("getrequests: forking (fd = %d)\n", t); 1805978Seric 1814636Seric pid = fork(); 1824636Seric if (pid < 0) 1834631Seric { 1844636Seric syserr("daemon: cannot fork"); 1854636Seric sleep(10); 1869610Seric (void) close(t); 1874636Seric continue; 1884631Seric } 1894631Seric 1904636Seric if (pid == 0) 1914631Seric { 19211147Seric extern struct hostent *gethostbyaddr(); 19311147Seric register struct hostent *hp; 19411147Seric char buf[MAXNAME]; 19511147Seric 1964636Seric /* 1974636Seric ** CHILD -- return to caller. 19811147Seric ** Collect verified idea of sending host. 1994636Seric ** Verify calling user id if possible here. 2004636Seric */ 2014631Seric 20224955Seric (void) signal(SIGCHLD, SIG_DFL); 20324950Seric 20411147Seric /* determine host name */ 20536230Skarels hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 20611147Seric if (hp != NULL) 20723104Seric (void) strcpy(buf, hp->h_name); 20811147Seric else 20916884Seric { 21016884Seric extern char *inet_ntoa(); 21116884Seric 21216884Seric /* produce a dotted quad */ 21316884Seric (void) sprintf(buf, "[%s]", 21436230Skarels inet_ntoa(RealHostAddr.sin_addr)); 21516884Seric } 21616884Seric 21716884Seric /* should we check for illegal connection here? XXX */ 21816884Seric 21911147Seric RealHostName = newstr(buf); 22011147Seric 22110206Seric (void) close(DaemonSocket); 2229610Seric InChannel = fdopen(t, "r"); 22321062Seric OutChannel = fdopen(dup(t), "w"); 2247677Seric if (tTd(15, 2)) 2255978Seric printf("getreq: returning\n"); 2267876Seric # ifdef LOG 2277876Seric if (LogLevel > 11) 2287876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2297876Seric # endif LOG 2304636Seric return; 2314631Seric } 2324631Seric 2337117Seric /* close the port so that others will hang (for a while) */ 2349610Seric (void) close(t); 2354631Seric } 2369886Seric /*NOTREACHED*/ 2374631Seric } 2385978Seric /* 23910206Seric ** CLRDAEMON -- reset the daemon connection 24010206Seric ** 24110206Seric ** Parameters: 24210206Seric ** none. 24310206Seric ** 24410206Seric ** Returns: 24510206Seric ** none. 24610206Seric ** 24710206Seric ** Side Effects: 24810206Seric ** releases any resources used by the passive daemon. 24910206Seric */ 25010206Seric 25110206Seric clrdaemon() 25210206Seric { 25310206Seric if (DaemonSocket >= 0) 25410206Seric (void) close(DaemonSocket); 25510206Seric DaemonSocket = -1; 25610206Seric } 25710206Seric /* 2586039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2596039Seric ** 2606039Seric ** Parameters: 2616039Seric ** host -- the name of the host. 2626633Seric ** port -- the port number to connect to. 263*53739Seric ** mci -- a pointer to the mail connection information 264*53739Seric ** structure to be filled in. 26552106Seric ** usesecureport -- if set, use a low numbered (reserved) 26652106Seric ** port to provide some rudimentary authentication. 2676039Seric ** 2686039Seric ** Returns: 2696039Seric ** An exit code telling whether the connection could be 2706039Seric ** made and if not why not. 2716039Seric ** 2726039Seric ** Side Effects: 2736039Seric ** none. 2746039Seric */ 2755978Seric 276*53739Seric makeconnection(host, port, mci, usesecureport) 2776039Seric char *host; 2787286Seric u_short port; 279*53739Seric register MCONINFO *mci; 28052106Seric bool usesecureport; 2816039Seric { 28229430Sbloom register int i, s; 28329430Sbloom register struct hostent *hp = (struct hostent *)NULL; 28452106Seric struct sockaddr_in addr; 28552106Seric int sav_errno; 28629430Sbloom extern char *inet_ntoa(); 28735651Seric #ifdef NAMED_BIND 28835651Seric extern int h_errno; 28935651Seric #endif 2906039Seric 2916039Seric /* 2926039Seric ** Set up the address for the mailer. 2939308Seric ** Accept "[a.b.c.d]" syntax for host name. 2946039Seric */ 2956039Seric 29635651Seric #ifdef NAMED_BIND 29725475Smiriam h_errno = 0; 29835651Seric #endif 29925475Smiriam errno = 0; 30025475Smiriam 3019308Seric if (host[0] == '[') 3029308Seric { 30311147Seric long hid; 30411147Seric register char *p = index(host, ']'); 3059308Seric 30611147Seric if (p != NULL) 3079308Seric { 30811147Seric *p = '\0'; 30911147Seric hid = inet_addr(&host[1]); 31011147Seric *p = ']'; 3119308Seric } 31211147Seric if (p == NULL || hid == -1) 3139308Seric { 3149308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3159308Seric return (EX_NOHOST); 3169308Seric } 31752106Seric addr.sin_addr.s_addr = hid; 3189308Seric } 3199610Seric else 3209610Seric { 32129430Sbloom hp = gethostbyname(host); 32225475Smiriam if (hp == NULL) 32324945Seric { 32435651Seric #ifdef NAMED_BIND 32525475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 32625475Smiriam return (EX_TEMPFAIL); 32725657Seric 32835651Seric /* if name server is specified, assume temp fail */ 32935651Seric if (errno == ECONNREFUSED && UseNameServer) 33035651Seric return (EX_TEMPFAIL); 33135651Seric #endif 33235651Seric 33325657Seric /* 33425657Seric ** XXX Should look for mail forwarder record here 33525657Seric ** XXX if (h_errno == NO_ADDRESS). 33625657Seric */ 33725657Seric 33825475Smiriam return (EX_NOHOST); 33924945Seric } 34052106Seric bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length); 34129430Sbloom i = 1; 3429610Seric } 3439610Seric 3449610Seric /* 3459610Seric ** Determine the port number. 3469610Seric */ 3479610Seric 34810011Seric if (port != 0) 34952106Seric addr.sin_port = htons(port); 35010011Seric else 3519610Seric { 3529610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3539610Seric 3549610Seric if (sp == NULL) 3559610Seric { 3569610Seric syserr("makeconnection: server \"smtp\" unknown"); 3579610Seric return (EX_OSFILE); 3589610Seric } 35952106Seric addr.sin_port = sp->s_port; 3609610Seric } 3616039Seric 3626039Seric /* 3636039Seric ** Try to actually open the connection. 3646039Seric */ 3656039Seric 36629430Sbloom again: 3677677Seric if (tTd(16, 1)) 36829430Sbloom printf("makeconnection (%s [%s])\n", host, 36952106Seric inet_ntoa(addr.sin_addr)); 3706039Seric 37152106Seric if (usesecureport) 37252106Seric { 37352106Seric int rport = IPPORT_RESERVED - 1; 37452106Seric 37552106Seric s = rresvport(&rport); 37652106Seric } 37752106Seric else 37852106Seric { 37952106Seric s = socket(AF_INET, SOCK_STREAM, 0); 38052106Seric } 3816039Seric if (s < 0) 3826039Seric { 38352106Seric sav_errno = errno; 3846039Seric syserr("makeconnection: no socket"); 3856039Seric goto failure; 3866039Seric } 3876039Seric 3887677Seric if (tTd(16, 1)) 3896039Seric printf("makeconnection: %d\n", s); 39010347Seric 39110347Seric /* turn on network debugging? */ 39210347Seric if (tTd(16, 14)) 39324945Seric { 39424945Seric int on = 1; 39524945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 39624945Seric } 39740932Srick if (CurEnv->e_xfp != NULL) 39840932Srick (void) fflush(CurEnv->e_xfp); /* for debugging */ 39914383Seric errno = 0; /* for debugging */ 40052106Seric addr.sin_family = AF_INET; 40152106Seric if (connect(s, (struct sockaddr *) &addr, sizeof addr) < 0) 4026039Seric { 40327744Sbloom sav_errno = errno; 40427744Sbloom (void) close(s); 40529430Sbloom if (hp && hp->h_addr_list[i]) 40629430Sbloom { 40752106Seric bcopy(hp->h_addr_list[i++], (char *) &addr.sin_addr, 40852106Seric hp->h_length); 40929430Sbloom goto again; 41029430Sbloom } 41129430Sbloom 4126039Seric /* failure, decide if temporary or not */ 4136039Seric failure: 41427744Sbloom switch (sav_errno) 4156039Seric { 4166039Seric case EISCONN: 4176039Seric case ETIMEDOUT: 4186897Seric case EINPROGRESS: 4196897Seric case EALREADY: 4206897Seric case EADDRINUSE: 42110123Seric case EHOSTDOWN: 4226897Seric case ENETDOWN: 4236897Seric case ENETRESET: 4246897Seric case ENOBUFS: 4257204Seric case ECONNREFUSED: 42611546Seric case ECONNRESET: 42710081Seric case EHOSTUNREACH: 42810098Seric case ENETUNREACH: 42951995Seric #ifdef ENOSR 43051995Seric case ENOSR: 43151995Seric #endif 4326039Seric /* there are others, I'm sure..... */ 4336039Seric return (EX_TEMPFAIL); 4346039Seric 43511147Seric case EPERM: 43611147Seric /* why is this happening? */ 43711147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 43852106Seric addr.sin_addr.s_addr, addr.sin_port); 43914383Seric return (EX_TEMPFAIL); 44011147Seric 4416039Seric default: 44240932Srick { 44340932Srick extern char *errstring(); 44440932Srick 44540932Srick message(Arpa_Info, "%s", errstring(sav_errno)); 44640932Srick return (EX_UNAVAILABLE); 44740932Srick } 4486039Seric } 4496039Seric } 4506039Seric 4516039Seric /* connection ok, put it into canonical form */ 452*53739Seric mci->mci_out = fdopen(s, "w"); 453*53739Seric mci->mci_in = fdopen(dup(s), "r"); 4546039Seric 45510098Seric return (EX_OK); 4566039Seric } 45710758Seric /* 45810758Seric ** MYHOSTNAME -- return the name of this host. 45910758Seric ** 46010758Seric ** Parameters: 46110758Seric ** hostbuf -- a place to return the name of this host. 46212313Seric ** size -- the size of hostbuf. 46310758Seric ** 46410758Seric ** Returns: 46510758Seric ** A list of aliases for this host. 46610758Seric ** 46710758Seric ** Side Effects: 46810758Seric ** none. 46910758Seric */ 4706039Seric 47110758Seric char ** 47212313Seric myhostname(hostbuf, size) 47310758Seric char hostbuf[]; 47412313Seric int size; 47510758Seric { 47610758Seric extern struct hostent *gethostbyname(); 47711147Seric struct hostent *hp; 47810758Seric 47923120Seric if (gethostname(hostbuf, size) < 0) 48023120Seric { 48123120Seric (void) strcpy(hostbuf, "localhost"); 48223120Seric } 48311147Seric hp = gethostbyname(hostbuf); 48411147Seric if (hp != NULL) 48516877Seric { 48623104Seric (void) strcpy(hostbuf, hp->h_name); 48711147Seric return (hp->h_aliases); 48816877Seric } 48910758Seric else 49010758Seric return (NULL); 49110758Seric } 49251315Seric /* 493*53739Seric * MAPHOSTNAME -- turn a hostname into canonical form 494*53739Seric * 495*53739Seric * Parameters: 496*53739Seric * hbuf -- a buffer containing a hostname. 497*53739Seric * hbsize -- the size of hbuf. 498*53739Seric * 499*53739Seric * Returns: 500*53739Seric * TRUE if the host name was mapped. 501*53739Seric * FALSE otherwise. 502*53739Seric * 503*53739Seric * Side Effects: 504*53739Seric * Looks up the host specified in hbuf. If it is not 505*53739Seric * the canonical name for that host, replace it with 506*53739Seric * the canonical name. If the name is unknown, or it 507*53739Seric * is already the canonical name, leave it unchanged. 508*53739Seric */ 50951315Seric 510*53739Seric bool 511*53739Seric maphostname(hbuf, hbsize) 51216911Seric char *hbuf; 51316911Seric int hbsize; 51416911Seric { 51516911Seric register struct hostent *hp; 51633932Sbostic u_long in_addr; 51740994Sbostic char ptr[256], *cp; 51833932Sbostic struct hostent *gethostbyaddr(); 51916911Seric 52025574Smiriam /* 52133932Sbostic * If first character is a bracket, then it is an address 52233932Sbostic * lookup. Address is copied into a temporary buffer to 52333932Sbostic * strip the brackets and to preserve hbuf if address is 52433932Sbostic * unknown. 52533932Sbostic */ 52651315Seric if (*hbuf != '[') 527*53739Seric return (getcanonname(hbuf, hbsize)); 52840994Sbostic if ((cp = index(strcpy(ptr, hbuf), ']')) == NULL) 529*53739Seric return (FALSE); 53040994Sbostic *cp = '\0'; 53133932Sbostic in_addr = inet_addr(&ptr[1]); 53233932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 53333932Sbostic if (hp == NULL) 534*53739Seric return (FALSE); 53533932Sbostic if (strlen(hp->h_name) >= hbsize) 53633932Sbostic hp->h_name[hbsize - 1] = '\0'; 537*53739Seric (void)strcpy(hbuf, hp->h_name); 538*53739Seric return (TRUE); 53933932Sbostic } 54016911Seric 54110758Seric # else DAEMON 54216911Seric /* code for systems without sophisticated networking */ 54310758Seric 54410758Seric /* 54510758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 54611297Seric ** 54711297Seric ** Can't convert to upper case here because might be a UUCP name. 54812313Seric ** 54912313Seric ** Mark, you can change this to be anything you want...... 55010758Seric */ 55110758Seric 55210758Seric char ** 55312313Seric myhostname(hostbuf, size) 55410758Seric char hostbuf[]; 55512313Seric int size; 55610758Seric { 55710758Seric register FILE *f; 55810758Seric 55910758Seric hostbuf[0] = '\0'; 56010758Seric f = fopen("/usr/include/whoami", "r"); 56110758Seric if (f != NULL) 56210758Seric { 56312313Seric (void) fgets(hostbuf, size, f); 56410758Seric fixcrlf(hostbuf, TRUE); 56510758Seric (void) fclose(f); 56610758Seric } 56710758Seric return (NULL); 56810758Seric } 56916911Seric /* 57016911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 57116911Seric ** 57216911Seric ** Parameters: 57316911Seric ** hbuf -- a buffer containing a hostname. 57416911Seric ** hbsize -- the size of hbuf. 57516911Seric ** 57616911Seric ** Returns: 577*53739Seric ** TRUE if the hostname was mapped. 57851315Seric ** FALSE otherwise. 57916911Seric ** 58016911Seric ** Side Effects: 58116911Seric ** Looks up the host specified in hbuf. If it is not 58216911Seric ** the canonical name for that host, replace it with 58316911Seric ** the canonical name. If the name is unknown, or it 58416911Seric ** is already the canonical name, leave it unchanged. 58516911Seric */ 58610758Seric 58716911Seric /*ARGSUSED*/ 588*53739Seric bool 589*53739Seric maphostname(hbuf, hbsize) 59016911Seric char *hbuf; 59116911Seric int hbsize; 59216911Seric { 593*53739Seric return (FALSE); 59416911Seric } 59516911Seric 5965978Seric #endif DAEMON 597