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*51920Seric static char sccsid[] = "@(#)daemon.c 5.40 (Berkeley) 12/14/91 (with daemon mode)"; 1533780Sbostic #else 16*51920Seric static char sccsid[] = "@(#)daemon.c 5.40 (Berkeley) 12/14/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 */ 155*51920Seric while ((CurrentLA = getla()) > RefuseLA) 15636584Sbostic { 157*51920Seric 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: 4226039Seric /* there are others, I'm sure..... */ 4236039Seric return (EX_TEMPFAIL); 4246039Seric 42511147Seric case EPERM: 42611147Seric /* why is this happening? */ 42711147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 42811147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 42914383Seric return (EX_TEMPFAIL); 43011147Seric 4316039Seric default: 43240932Srick { 43340932Srick extern char *errstring(); 43440932Srick 43540932Srick message(Arpa_Info, "%s", errstring(sav_errno)); 43640932Srick return (EX_UNAVAILABLE); 43740932Srick } 4386039Seric } 4396039Seric } 4406039Seric 4416039Seric /* connection ok, put it into canonical form */ 4426039Seric *outfile = fdopen(s, "w"); 44340992Sbostic *infile = fdopen(dup(s), "r"); 4446039Seric 44510098Seric return (EX_OK); 4466039Seric } 44710758Seric /* 44810758Seric ** MYHOSTNAME -- return the name of this host. 44910758Seric ** 45010758Seric ** Parameters: 45110758Seric ** hostbuf -- a place to return the name of this host. 45212313Seric ** size -- the size of hostbuf. 45310758Seric ** 45410758Seric ** Returns: 45510758Seric ** A list of aliases for this host. 45610758Seric ** 45710758Seric ** Side Effects: 45810758Seric ** none. 45910758Seric */ 4606039Seric 46110758Seric char ** 46212313Seric myhostname(hostbuf, size) 46310758Seric char hostbuf[]; 46412313Seric int size; 46510758Seric { 46610758Seric extern struct hostent *gethostbyname(); 46711147Seric struct hostent *hp; 46810758Seric 46923120Seric if (gethostname(hostbuf, size) < 0) 47023120Seric { 47123120Seric (void) strcpy(hostbuf, "localhost"); 47223120Seric } 47311147Seric hp = gethostbyname(hostbuf); 47411147Seric if (hp != NULL) 47516877Seric { 47623104Seric (void) strcpy(hostbuf, hp->h_name); 47711147Seric return (hp->h_aliases); 47816877Seric } 47910758Seric else 48010758Seric return (NULL); 48110758Seric } 48251315Seric /* 48333932Sbostic * MAPHOSTNAME -- turn a hostname into canonical form 48433932Sbostic * 48533932Sbostic * Parameters: 48633932Sbostic * hbuf -- a buffer containing a hostname. 48733932Sbostic * hbsize -- the size of hbuf. 48833932Sbostic * 48933932Sbostic * Returns: 49051315Seric * TRUE if the host name was mapped. 49151315Seric * FALSE otherwise. 49233932Sbostic * 49333932Sbostic * Side Effects: 49433932Sbostic * Looks up the host specified in hbuf. If it is not 49533932Sbostic * the canonical name for that host, replace it with 49633932Sbostic * the canonical name. If the name is unknown, or it 49733932Sbostic * is already the canonical name, leave it unchanged. 49833932Sbostic */ 49951315Seric 50051315Seric bool 50116911Seric maphostname(hbuf, hbsize) 50216911Seric char *hbuf; 50316911Seric int hbsize; 50416911Seric { 50516911Seric register struct hostent *hp; 50633932Sbostic u_long in_addr; 50740994Sbostic char ptr[256], *cp; 50833932Sbostic struct hostent *gethostbyaddr(); 50916911Seric 51025574Smiriam /* 51133932Sbostic * If first character is a bracket, then it is an address 51233932Sbostic * lookup. Address is copied into a temporary buffer to 51333932Sbostic * strip the brackets and to preserve hbuf if address is 51433932Sbostic * unknown. 51533932Sbostic */ 51651315Seric if (*hbuf != '[') 51751315Seric return (getcanonname(hbuf, hbsize)); 51840994Sbostic if ((cp = index(strcpy(ptr, hbuf), ']')) == NULL) 51951315Seric return (FALSE); 52040994Sbostic *cp = '\0'; 52133932Sbostic in_addr = inet_addr(&ptr[1]); 52233932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 52333932Sbostic if (hp == NULL) 52451315Seric return (FALSE); 52533932Sbostic if (strlen(hp->h_name) >= hbsize) 52633932Sbostic hp->h_name[hbsize - 1] = '\0'; 52733932Sbostic (void)strcpy(hbuf, hp->h_name); 52851315Seric return (TRUE); 52933932Sbostic } 53016911Seric 53110758Seric # else DAEMON 53216911Seric /* code for systems without sophisticated networking */ 53310758Seric 53410758Seric /* 53510758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 53611297Seric ** 53711297Seric ** Can't convert to upper case here because might be a UUCP name. 53812313Seric ** 53912313Seric ** Mark, you can change this to be anything you want...... 54010758Seric */ 54110758Seric 54210758Seric char ** 54312313Seric myhostname(hostbuf, size) 54410758Seric char hostbuf[]; 54512313Seric int size; 54610758Seric { 54710758Seric register FILE *f; 54810758Seric 54910758Seric hostbuf[0] = '\0'; 55010758Seric f = fopen("/usr/include/whoami", "r"); 55110758Seric if (f != NULL) 55210758Seric { 55312313Seric (void) fgets(hostbuf, size, f); 55410758Seric fixcrlf(hostbuf, TRUE); 55510758Seric (void) fclose(f); 55610758Seric } 55710758Seric return (NULL); 55810758Seric } 55916911Seric /* 56016911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 56116911Seric ** 56216911Seric ** Parameters: 56316911Seric ** hbuf -- a buffer containing a hostname. 56416911Seric ** hbsize -- the size of hbuf. 56516911Seric ** 56616911Seric ** Returns: 56751315Seric ** TRUE if the hostname was mapped. 56851315Seric ** FALSE otherwise. 56916911Seric ** 57016911Seric ** Side Effects: 57116911Seric ** Looks up the host specified in hbuf. If it is not 57216911Seric ** the canonical name for that host, replace it with 57316911Seric ** the canonical name. If the name is unknown, or it 57416911Seric ** is already the canonical name, leave it unchanged. 57516911Seric */ 57610758Seric 57716911Seric /*ARGSUSED*/ 57851315Seric bool 57916911Seric maphostname(hbuf, hbsize) 58016911Seric char *hbuf; 58116911Seric int hbsize; 58216911Seric { 58351315Seric return (FALSE); 58416911Seric } 58516911Seric 5865978Seric #endif DAEMON 587