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*51995Seric static char sccsid[] = "@(#)daemon.c 5.41 (Berkeley) 12/17/91 (with daemon mode)"; 1533780Sbostic #else 16*51995Seric static char sccsid[] = "@(#)daemon.c 5.41 (Berkeley) 12/17/91 (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). 487556Seric ** makeconnection(host, port, outfile, infile) 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 7610206Seric struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 779610Seric 7816144Seric int DaemonSocket = -1; /* fd describing socket */ 7916890Seric char *NetName; /* name of home (local?) network */ 8016144Seric 814535Seric getrequests() 824535Seric { 839610Seric int t; 849610Seric register struct servent *sp; 8525027Seric int on = 1; 8646928Sbostic extern void reapchild(); 877117Seric 889610Seric /* 899610Seric ** Set up the address for the mailer. 909610Seric */ 919610Seric 929610Seric sp = getservbyname("smtp", "tcp"); 939610Seric if (sp == NULL) 949610Seric { 959610Seric syserr("server \"smtp\" unknown"); 9610167Seric goto severe; 979610Seric } 989610Seric SendmailAddress.sin_family = AF_INET; 999610Seric SendmailAddress.sin_addr.s_addr = INADDR_ANY; 1009740Ssam SendmailAddress.sin_port = sp->s_port; 1019610Seric 1029610Seric /* 1039610Seric ** Try to actually open the connection. 1049610Seric */ 1059610Seric 1069610Seric if (tTd(15, 1)) 1079610Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 1089610Seric 1099610Seric /* get a socket for the SMTP connection */ 11023120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 11110206Seric if (DaemonSocket < 0) 1129610Seric { 1139610Seric /* probably another daemon already */ 1149610Seric syserr("getrequests: can't create socket"); 1159610Seric severe: 1169610Seric # ifdef LOG 1179610Seric if (LogLevel > 0) 11824858Seric syslog(LOG_ALERT, "cannot get connection"); 1199610Seric # endif LOG 1209610Seric finis(); 1219610Seric } 12210347Seric 12310347Seric /* turn on network debugging? */ 12410347Seric if (tTd(15, 15)) 12524945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 12610347Seric 12725027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 12825027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 12925027Seric 13046928Sbostic if (bind(DaemonSocket, 13146928Sbostic (struct sockaddr *)&SendmailAddress, sizeof SendmailAddress) < 0) 1329610Seric { 1339610Seric syserr("getrequests: cannot bind"); 13410206Seric (void) close(DaemonSocket); 1359610Seric goto severe; 1369610Seric } 13723120Seric if (listen(DaemonSocket, 10) < 0) 13823120Seric { 13923120Seric syserr("getrequests: cannot listen"); 14023120Seric (void) close(DaemonSocket); 14123120Seric goto severe; 14223120Seric } 1439610Seric 14424955Seric (void) signal(SIGCHLD, reapchild); 14524945Seric 1469610Seric if (tTd(15, 1)) 14710206Seric printf("getrequests: %d\n", DaemonSocket); 1489610Seric 1494631Seric for (;;) 1504631Seric { 15114875Seric register int pid; 15211147Seric auto int lotherend; 15311147Seric 15414875Seric /* see if we are rejecting connections */ 15551920Seric while ((CurrentLA = getla()) > RefuseLA) 15636584Sbostic { 15751920Seric setproctitle("rejecting connections: load average: %.2f", (double)CurrentLA); 15814875Seric sleep(5); 15936584Sbostic } 16014875Seric 1619610Seric /* wait for a connection */ 16236584Sbostic setproctitle("accepting connections"); 1639610Seric do 1649610Seric { 1659610Seric errno = 0; 16636230Skarels lotherend = sizeof RealHostAddr; 16746928Sbostic t = accept(DaemonSocket, 16846928Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 1699610Seric } while (t < 0 && errno == EINTR); 1709610Seric if (t < 0) 1715978Seric { 1729610Seric syserr("getrequests: accept"); 1739610Seric sleep(5); 1749610Seric continue; 1755978Seric } 1764631Seric 1775978Seric /* 1785978Seric ** Create a subprocess to process the mail. 1795978Seric */ 1805978Seric 1817677Seric if (tTd(15, 2)) 1829610Seric printf("getrequests: forking (fd = %d)\n", t); 1835978Seric 1844636Seric pid = fork(); 1854636Seric if (pid < 0) 1864631Seric { 1874636Seric syserr("daemon: cannot fork"); 1884636Seric sleep(10); 1899610Seric (void) close(t); 1904636Seric continue; 1914631Seric } 1924631Seric 1934636Seric if (pid == 0) 1944631Seric { 19511147Seric extern struct hostent *gethostbyaddr(); 19611147Seric register struct hostent *hp; 19711147Seric char buf[MAXNAME]; 19811147Seric 1994636Seric /* 2004636Seric ** CHILD -- return to caller. 20111147Seric ** Collect verified idea of sending host. 2024636Seric ** Verify calling user id if possible here. 2034636Seric */ 2044631Seric 20524955Seric (void) signal(SIGCHLD, SIG_DFL); 20624950Seric 20711147Seric /* determine host name */ 20836230Skarels hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 20911147Seric if (hp != NULL) 21023104Seric (void) strcpy(buf, hp->h_name); 21111147Seric else 21216884Seric { 21316884Seric extern char *inet_ntoa(); 21416884Seric 21516884Seric /* produce a dotted quad */ 21616884Seric (void) sprintf(buf, "[%s]", 21736230Skarels inet_ntoa(RealHostAddr.sin_addr)); 21816884Seric } 21916884Seric 22016884Seric /* should we check for illegal connection here? XXX */ 22116884Seric 22211147Seric RealHostName = newstr(buf); 22311147Seric 22410206Seric (void) close(DaemonSocket); 2259610Seric InChannel = fdopen(t, "r"); 22621062Seric OutChannel = fdopen(dup(t), "w"); 2277677Seric if (tTd(15, 2)) 2285978Seric printf("getreq: returning\n"); 2297876Seric # ifdef LOG 2307876Seric if (LogLevel > 11) 2317876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2327876Seric # endif LOG 2334636Seric return; 2344631Seric } 2354631Seric 2367117Seric /* close the port so that others will hang (for a while) */ 2379610Seric (void) close(t); 2384631Seric } 2399886Seric /*NOTREACHED*/ 2404631Seric } 2415978Seric /* 24210206Seric ** CLRDAEMON -- reset the daemon connection 24310206Seric ** 24410206Seric ** Parameters: 24510206Seric ** none. 24610206Seric ** 24710206Seric ** Returns: 24810206Seric ** none. 24910206Seric ** 25010206Seric ** Side Effects: 25110206Seric ** releases any resources used by the passive daemon. 25210206Seric */ 25310206Seric 25410206Seric clrdaemon() 25510206Seric { 25610206Seric if (DaemonSocket >= 0) 25710206Seric (void) close(DaemonSocket); 25810206Seric DaemonSocket = -1; 25910206Seric } 26010206Seric /* 2616039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2626039Seric ** 2636039Seric ** Parameters: 2646039Seric ** host -- the name of the host. 2656633Seric ** port -- the port number to connect to. 2666039Seric ** outfile -- a pointer to a place to put the outfile 2676039Seric ** descriptor. 2686039Seric ** infile -- ditto for infile. 2696039Seric ** 2706039Seric ** Returns: 2716039Seric ** An exit code telling whether the connection could be 2726039Seric ** made and if not why not. 2736039Seric ** 2746039Seric ** Side Effects: 2756039Seric ** none. 2766039Seric */ 2775978Seric 2786633Seric makeconnection(host, port, outfile, infile) 2796039Seric char *host; 2807286Seric u_short port; 2816039Seric FILE **outfile; 2826039Seric FILE **infile; 2836039Seric { 28429430Sbloom register int i, s; 28529430Sbloom register struct hostent *hp = (struct hostent *)NULL; 28629430Sbloom extern char *inet_ntoa(); 28727744Sbloom int sav_errno; 28835651Seric #ifdef NAMED_BIND 28935651Seric extern int h_errno; 29035651Seric #endif 2916039Seric 2926039Seric /* 2936039Seric ** Set up the address for the mailer. 2949308Seric ** Accept "[a.b.c.d]" syntax for host name. 2956039Seric */ 2966039Seric 29735651Seric #ifdef NAMED_BIND 29825475Smiriam h_errno = 0; 29935651Seric #endif 30025475Smiriam errno = 0; 30125475Smiriam 3029308Seric if (host[0] == '[') 3039308Seric { 30411147Seric long hid; 30511147Seric register char *p = index(host, ']'); 3069308Seric 30711147Seric if (p != NULL) 3089308Seric { 30911147Seric *p = '\0'; 31011147Seric hid = inet_addr(&host[1]); 31111147Seric *p = ']'; 3129308Seric } 31311147Seric if (p == NULL || hid == -1) 3149308Seric { 3159308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3169308Seric return (EX_NOHOST); 3179308Seric } 3189308Seric SendmailAddress.sin_addr.s_addr = hid; 3199308Seric } 3209610Seric else 3219610Seric { 32229430Sbloom hp = gethostbyname(host); 32325475Smiriam if (hp == NULL) 32424945Seric { 32535651Seric #ifdef NAMED_BIND 32625475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 32725475Smiriam return (EX_TEMPFAIL); 32825657Seric 32935651Seric /* if name server is specified, assume temp fail */ 33035651Seric if (errno == ECONNREFUSED && UseNameServer) 33135651Seric return (EX_TEMPFAIL); 33235651Seric #endif 33335651Seric 33425657Seric /* 33525657Seric ** XXX Should look for mail forwarder record here 33625657Seric ** XXX if (h_errno == NO_ADDRESS). 33725657Seric */ 33825657Seric 33925475Smiriam return (EX_NOHOST); 34024945Seric } 34116884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 34229430Sbloom i = 1; 3439610Seric } 3449610Seric 3459610Seric /* 3469610Seric ** Determine the port number. 3479610Seric */ 3489610Seric 34910011Seric if (port != 0) 35010011Seric SendmailAddress.sin_port = htons(port); 35110011Seric else 3529610Seric { 3539610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3549610Seric 3559610Seric if (sp == NULL) 3569610Seric { 3579610Seric syserr("makeconnection: server \"smtp\" unknown"); 3589610Seric return (EX_OSFILE); 3599610Seric } 36010011Seric SendmailAddress.sin_port = sp->s_port; 3619610Seric } 3626039Seric 3636039Seric /* 3646039Seric ** Try to actually open the connection. 3656039Seric */ 3666039Seric 36729430Sbloom again: 3687677Seric if (tTd(16, 1)) 36929430Sbloom printf("makeconnection (%s [%s])\n", host, 37050969Sleres inet_ntoa(SendmailAddress.sin_addr)); 3716039Seric 37223120Seric s = socket(AF_INET, SOCK_STREAM, 0); 3736039Seric if (s < 0) 3746039Seric { 3756039Seric syserr("makeconnection: no socket"); 37627744Sbloom sav_errno = errno; 3776039Seric goto failure; 3786039Seric } 3796039Seric 3807677Seric if (tTd(16, 1)) 3816039Seric printf("makeconnection: %d\n", s); 38210347Seric 38310347Seric /* turn on network debugging? */ 38410347Seric if (tTd(16, 14)) 38524945Seric { 38624945Seric int on = 1; 38724945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 38824945Seric } 38940932Srick if (CurEnv->e_xfp != NULL) 39040932Srick (void) fflush(CurEnv->e_xfp); /* for debugging */ 39114383Seric errno = 0; /* for debugging */ 3929610Seric SendmailAddress.sin_family = AF_INET; 39346928Sbostic if (connect(s, 39446928Sbostic (struct sockaddr *)&SendmailAddress, sizeof SendmailAddress) < 0) 3956039Seric { 39627744Sbloom sav_errno = errno; 39727744Sbloom (void) close(s); 39829430Sbloom if (hp && hp->h_addr_list[i]) 39929430Sbloom { 40029430Sbloom bcopy(hp->h_addr_list[i++], 40129430Sbloom (char *)&SendmailAddress.sin_addr, hp->h_length); 40229430Sbloom goto again; 40329430Sbloom } 40429430Sbloom 4056039Seric /* failure, decide if temporary or not */ 4066039Seric failure: 40727744Sbloom switch (sav_errno) 4086039Seric { 4096039Seric case EISCONN: 4106039Seric case ETIMEDOUT: 4116897Seric case EINPROGRESS: 4126897Seric case EALREADY: 4136897Seric case EADDRINUSE: 41410123Seric case EHOSTDOWN: 4156897Seric case ENETDOWN: 4166897Seric case ENETRESET: 4176897Seric case ENOBUFS: 4187204Seric case ECONNREFUSED: 41911546Seric case ECONNRESET: 42010081Seric case EHOSTUNREACH: 42110098Seric case ENETUNREACH: 422*51995Seric #ifdef ENOSR 423*51995Seric case ENOSR: 424*51995Seric #endif 4256039Seric /* there are others, I'm sure..... */ 4266039Seric return (EX_TEMPFAIL); 4276039Seric 42811147Seric case EPERM: 42911147Seric /* why is this happening? */ 43011147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 43111147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 43214383Seric return (EX_TEMPFAIL); 43311147Seric 4346039Seric default: 43540932Srick { 43640932Srick extern char *errstring(); 43740932Srick 43840932Srick message(Arpa_Info, "%s", errstring(sav_errno)); 43940932Srick return (EX_UNAVAILABLE); 44040932Srick } 4416039Seric } 4426039Seric } 4436039Seric 4446039Seric /* connection ok, put it into canonical form */ 4456039Seric *outfile = fdopen(s, "w"); 44640992Sbostic *infile = fdopen(dup(s), "r"); 4476039Seric 44810098Seric return (EX_OK); 4496039Seric } 45010758Seric /* 45110758Seric ** MYHOSTNAME -- return the name of this host. 45210758Seric ** 45310758Seric ** Parameters: 45410758Seric ** hostbuf -- a place to return the name of this host. 45512313Seric ** size -- the size of hostbuf. 45610758Seric ** 45710758Seric ** Returns: 45810758Seric ** A list of aliases for this host. 45910758Seric ** 46010758Seric ** Side Effects: 46110758Seric ** none. 46210758Seric */ 4636039Seric 46410758Seric char ** 46512313Seric myhostname(hostbuf, size) 46610758Seric char hostbuf[]; 46712313Seric int size; 46810758Seric { 46910758Seric extern struct hostent *gethostbyname(); 47011147Seric struct hostent *hp; 47110758Seric 47223120Seric if (gethostname(hostbuf, size) < 0) 47323120Seric { 47423120Seric (void) strcpy(hostbuf, "localhost"); 47523120Seric } 47611147Seric hp = gethostbyname(hostbuf); 47711147Seric if (hp != NULL) 47816877Seric { 47923104Seric (void) strcpy(hostbuf, hp->h_name); 48011147Seric return (hp->h_aliases); 48116877Seric } 48210758Seric else 48310758Seric return (NULL); 48410758Seric } 48551315Seric /* 48633932Sbostic * MAPHOSTNAME -- turn a hostname into canonical form 48733932Sbostic * 48833932Sbostic * Parameters: 48933932Sbostic * hbuf -- a buffer containing a hostname. 49033932Sbostic * hbsize -- the size of hbuf. 49133932Sbostic * 49233932Sbostic * Returns: 49351315Seric * TRUE if the host name was mapped. 49451315Seric * FALSE otherwise. 49533932Sbostic * 49633932Sbostic * Side Effects: 49733932Sbostic * Looks up the host specified in hbuf. If it is not 49833932Sbostic * the canonical name for that host, replace it with 49933932Sbostic * the canonical name. If the name is unknown, or it 50033932Sbostic * is already the canonical name, leave it unchanged. 50133932Sbostic */ 50251315Seric 50351315Seric bool 50416911Seric maphostname(hbuf, hbsize) 50516911Seric char *hbuf; 50616911Seric int hbsize; 50716911Seric { 50816911Seric register struct hostent *hp; 50933932Sbostic u_long in_addr; 51040994Sbostic char ptr[256], *cp; 51133932Sbostic struct hostent *gethostbyaddr(); 51216911Seric 51325574Smiriam /* 51433932Sbostic * If first character is a bracket, then it is an address 51533932Sbostic * lookup. Address is copied into a temporary buffer to 51633932Sbostic * strip the brackets and to preserve hbuf if address is 51733932Sbostic * unknown. 51833932Sbostic */ 51951315Seric if (*hbuf != '[') 52051315Seric return (getcanonname(hbuf, hbsize)); 52140994Sbostic if ((cp = index(strcpy(ptr, hbuf), ']')) == NULL) 52251315Seric return (FALSE); 52340994Sbostic *cp = '\0'; 52433932Sbostic in_addr = inet_addr(&ptr[1]); 52533932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 52633932Sbostic if (hp == NULL) 52751315Seric return (FALSE); 52833932Sbostic if (strlen(hp->h_name) >= hbsize) 52933932Sbostic hp->h_name[hbsize - 1] = '\0'; 53033932Sbostic (void)strcpy(hbuf, hp->h_name); 53151315Seric return (TRUE); 53233932Sbostic } 53316911Seric 53410758Seric # else DAEMON 53516911Seric /* code for systems without sophisticated networking */ 53610758Seric 53710758Seric /* 53810758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 53911297Seric ** 54011297Seric ** Can't convert to upper case here because might be a UUCP name. 54112313Seric ** 54212313Seric ** Mark, you can change this to be anything you want...... 54310758Seric */ 54410758Seric 54510758Seric char ** 54612313Seric myhostname(hostbuf, size) 54710758Seric char hostbuf[]; 54812313Seric int size; 54910758Seric { 55010758Seric register FILE *f; 55110758Seric 55210758Seric hostbuf[0] = '\0'; 55310758Seric f = fopen("/usr/include/whoami", "r"); 55410758Seric if (f != NULL) 55510758Seric { 55612313Seric (void) fgets(hostbuf, size, f); 55710758Seric fixcrlf(hostbuf, TRUE); 55810758Seric (void) fclose(f); 55910758Seric } 56010758Seric return (NULL); 56110758Seric } 56216911Seric /* 56316911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 56416911Seric ** 56516911Seric ** Parameters: 56616911Seric ** hbuf -- a buffer containing a hostname. 56716911Seric ** hbsize -- the size of hbuf. 56816911Seric ** 56916911Seric ** Returns: 57051315Seric ** TRUE if the hostname was mapped. 57151315Seric ** FALSE otherwise. 57216911Seric ** 57316911Seric ** Side Effects: 57416911Seric ** Looks up the host specified in hbuf. If it is not 57516911Seric ** the canonical name for that host, replace it with 57616911Seric ** the canonical name. If the name is unknown, or it 57716911Seric ** is already the canonical name, leave it unchanged. 57816911Seric */ 57910758Seric 58016911Seric /*ARGSUSED*/ 58151315Seric bool 58216911Seric maphostname(hbuf, hbsize) 58316911Seric char *hbuf; 58416911Seric int hbsize; 58516911Seric { 58651315Seric return (FALSE); 58716911Seric } 58816911Seric 5895978Seric #endif DAEMON 590