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*51315Seric static char sccsid[] = "@(#)daemon.c 5.39 (Berkeley) 10/05/91 (with daemon mode)"; 1533780Sbostic #else 16*51315Seric static char sccsid[] = "@(#)daemon.c 5.39 (Berkeley) 10/05/91 (without daemon mode)"; 1733780Sbostic #endif 1833780Sbostic #endif /* not lint */ 194535Seric 2040932Srick int la; /* load average */ 2140932Srick 2233780Sbostic #ifdef DAEMON 2333780Sbostic 2423120Seric # include <netdb.h> 2524945Seric # include <sys/signal.h> 2623120Seric # include <sys/wait.h> 2723120Seric # include <sys/time.h> 2823120Seric # include <sys/resource.h> 295978Seric 304535Seric /* 314535Seric ** DAEMON.C -- routines to use when running as a daemon. 327556Seric ** 337556Seric ** This entire file is highly dependent on the 4.2 BSD 347556Seric ** interprocess communication primitives. No attempt has 357556Seric ** been made to make this file portable to Version 7, 367556Seric ** Version 6, MPX files, etc. If you should try such a 377556Seric ** thing yourself, I recommend chucking the entire file 387556Seric ** and starting from scratch. Basic semantics are: 397556Seric ** 407556Seric ** getrequests() 417556Seric ** Opens a port and initiates a connection. 427556Seric ** Returns in a child. Must set InChannel and 437556Seric ** OutChannel appropriately. 4410206Seric ** clrdaemon() 4510206Seric ** Close any open files associated with getting 4610206Seric ** the connection; this is used when running the queue, 4710206Seric ** etc., to avoid having extra file descriptors during 4810206Seric ** the queue run and to avoid confusing the network 4910206Seric ** code (if it cares). 507556Seric ** makeconnection(host, port, outfile, infile) 517556Seric ** Make a connection to the named host on the given 527556Seric ** port. Set *outfile and *infile to the files 537556Seric ** appropriate for communication. Returns zero on 547556Seric ** success, else an exit status describing the 557556Seric ** error. 5625699Seric ** maphostname(hbuf, hbufsize) 5725699Seric ** Convert the entry in hbuf into a canonical form. It 5825699Seric ** may not be larger than hbufsize. 594535Seric */ 604535Seric /* 614535Seric ** GETREQUESTS -- open mail IPC port and get requests. 624535Seric ** 634535Seric ** Parameters: 644535Seric ** none. 654535Seric ** 664535Seric ** Returns: 674535Seric ** none. 684535Seric ** 694535Seric ** Side Effects: 704535Seric ** Waits until some interesting activity occurs. When 714535Seric ** it does, a child is created to process it, and the 724535Seric ** parent waits for completion. Return from this 739886Seric ** routine is always in the child. The file pointers 749886Seric ** "InChannel" and "OutChannel" should be set to point 759886Seric ** to the communication channel. 764535Seric */ 774535Seric 7810206Seric struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 799610Seric 8016144Seric int DaemonSocket = -1; /* fd describing socket */ 8116890Seric char *NetName; /* name of home (local?) network */ 8216144Seric 834535Seric getrequests() 844535Seric { 859610Seric int t; 869610Seric register struct servent *sp; 8725027Seric int on = 1; 8846928Sbostic extern void reapchild(); 897117Seric 909610Seric /* 919610Seric ** Set up the address for the mailer. 929610Seric */ 939610Seric 949610Seric sp = getservbyname("smtp", "tcp"); 959610Seric if (sp == NULL) 969610Seric { 979610Seric syserr("server \"smtp\" unknown"); 9810167Seric goto severe; 999610Seric } 1009610Seric SendmailAddress.sin_family = AF_INET; 1019610Seric SendmailAddress.sin_addr.s_addr = INADDR_ANY; 1029740Ssam SendmailAddress.sin_port = sp->s_port; 1039610Seric 1049610Seric /* 1059610Seric ** Try to actually open the connection. 1069610Seric */ 1079610Seric 1089610Seric if (tTd(15, 1)) 1099610Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 1109610Seric 1119610Seric /* get a socket for the SMTP connection */ 11223120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 11310206Seric if (DaemonSocket < 0) 1149610Seric { 1159610Seric /* probably another daemon already */ 1169610Seric syserr("getrequests: can't create socket"); 1179610Seric severe: 1189610Seric # ifdef LOG 1199610Seric if (LogLevel > 0) 12024858Seric syslog(LOG_ALERT, "cannot get connection"); 1219610Seric # endif LOG 1229610Seric finis(); 1239610Seric } 12410347Seric 12510347Seric /* turn on network debugging? */ 12610347Seric if (tTd(15, 15)) 12724945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 12810347Seric 12925027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 13025027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 13125027Seric 13246928Sbostic if (bind(DaemonSocket, 13346928Sbostic (struct sockaddr *)&SendmailAddress, sizeof SendmailAddress) < 0) 1349610Seric { 1359610Seric syserr("getrequests: cannot bind"); 13610206Seric (void) close(DaemonSocket); 1379610Seric goto severe; 1389610Seric } 13923120Seric if (listen(DaemonSocket, 10) < 0) 14023120Seric { 14123120Seric syserr("getrequests: cannot listen"); 14223120Seric (void) close(DaemonSocket); 14323120Seric goto severe; 14423120Seric } 1459610Seric 14624955Seric (void) signal(SIGCHLD, reapchild); 14724945Seric 1489610Seric if (tTd(15, 1)) 14910206Seric printf("getrequests: %d\n", DaemonSocket); 1509610Seric 1514631Seric for (;;) 1524631Seric { 15314875Seric register int pid; 15411147Seric auto int lotherend; 15514875Seric extern int RefuseLA; 15611147Seric 15714875Seric /* see if we are rejecting connections */ 15836584Sbostic while ((la = getla()) > RefuseLA) 15936584Sbostic { 16039953Smckusick setproctitle("rejecting connections: load average: %.2f", (double)la); 16114875Seric sleep(5); 16236584Sbostic } 16314875Seric 1649610Seric /* wait for a connection */ 16536584Sbostic setproctitle("accepting connections"); 1669610Seric do 1679610Seric { 1689610Seric errno = 0; 16936230Skarels lotherend = sizeof RealHostAddr; 17046928Sbostic t = accept(DaemonSocket, 17146928Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 1729610Seric } while (t < 0 && errno == EINTR); 1739610Seric if (t < 0) 1745978Seric { 1759610Seric syserr("getrequests: accept"); 1769610Seric sleep(5); 1779610Seric continue; 1785978Seric } 1794631Seric 1805978Seric /* 1815978Seric ** Create a subprocess to process the mail. 1825978Seric */ 1835978Seric 1847677Seric if (tTd(15, 2)) 1859610Seric printf("getrequests: forking (fd = %d)\n", t); 1865978Seric 1874636Seric pid = fork(); 1884636Seric if (pid < 0) 1894631Seric { 1904636Seric syserr("daemon: cannot fork"); 1914636Seric sleep(10); 1929610Seric (void) close(t); 1934636Seric continue; 1944631Seric } 1954631Seric 1964636Seric if (pid == 0) 1974631Seric { 19811147Seric extern struct hostent *gethostbyaddr(); 19911147Seric register struct hostent *hp; 20011147Seric char buf[MAXNAME]; 20111147Seric 2024636Seric /* 2034636Seric ** CHILD -- return to caller. 20411147Seric ** Collect verified idea of sending host. 2054636Seric ** Verify calling user id if possible here. 2064636Seric */ 2074631Seric 20824955Seric (void) signal(SIGCHLD, SIG_DFL); 20924950Seric 21011147Seric /* determine host name */ 21136230Skarels hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 21211147Seric if (hp != NULL) 21323104Seric (void) strcpy(buf, hp->h_name); 21411147Seric else 21516884Seric { 21616884Seric extern char *inet_ntoa(); 21716884Seric 21816884Seric /* produce a dotted quad */ 21916884Seric (void) sprintf(buf, "[%s]", 22036230Skarels inet_ntoa(RealHostAddr.sin_addr)); 22116884Seric } 22216884Seric 22316884Seric /* should we check for illegal connection here? XXX */ 22416884Seric 22511147Seric RealHostName = newstr(buf); 22611147Seric 22710206Seric (void) close(DaemonSocket); 2289610Seric InChannel = fdopen(t, "r"); 22921062Seric OutChannel = fdopen(dup(t), "w"); 2307677Seric if (tTd(15, 2)) 2315978Seric printf("getreq: returning\n"); 2327876Seric # ifdef LOG 2337876Seric if (LogLevel > 11) 2347876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2357876Seric # endif LOG 2364636Seric return; 2374631Seric } 2384631Seric 2397117Seric /* close the port so that others will hang (for a while) */ 2409610Seric (void) close(t); 2414631Seric } 2429886Seric /*NOTREACHED*/ 2434631Seric } 2445978Seric /* 24510206Seric ** CLRDAEMON -- reset the daemon connection 24610206Seric ** 24710206Seric ** Parameters: 24810206Seric ** none. 24910206Seric ** 25010206Seric ** Returns: 25110206Seric ** none. 25210206Seric ** 25310206Seric ** Side Effects: 25410206Seric ** releases any resources used by the passive daemon. 25510206Seric */ 25610206Seric 25710206Seric clrdaemon() 25810206Seric { 25910206Seric if (DaemonSocket >= 0) 26010206Seric (void) close(DaemonSocket); 26110206Seric DaemonSocket = -1; 26210206Seric } 26310206Seric /* 2646039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2656039Seric ** 2666039Seric ** Parameters: 2676039Seric ** host -- the name of the host. 2686633Seric ** port -- the port number to connect to. 2696039Seric ** outfile -- a pointer to a place to put the outfile 2706039Seric ** descriptor. 2716039Seric ** infile -- ditto for infile. 2726039Seric ** 2736039Seric ** Returns: 2746039Seric ** An exit code telling whether the connection could be 2756039Seric ** made and if not why not. 2766039Seric ** 2776039Seric ** Side Effects: 2786039Seric ** none. 2796039Seric */ 2805978Seric 2816633Seric makeconnection(host, port, outfile, infile) 2826039Seric char *host; 2837286Seric u_short port; 2846039Seric FILE **outfile; 2856039Seric FILE **infile; 2866039Seric { 28729430Sbloom register int i, s; 28829430Sbloom register struct hostent *hp = (struct hostent *)NULL; 28929430Sbloom extern char *inet_ntoa(); 29027744Sbloom int sav_errno; 29135651Seric #ifdef NAMED_BIND 29235651Seric extern int h_errno; 29335651Seric #endif 2946039Seric 2956039Seric /* 2966039Seric ** Set up the address for the mailer. 2979308Seric ** Accept "[a.b.c.d]" syntax for host name. 2986039Seric */ 2996039Seric 30035651Seric #ifdef NAMED_BIND 30125475Smiriam h_errno = 0; 30235651Seric #endif 30325475Smiriam errno = 0; 30425475Smiriam 3059308Seric if (host[0] == '[') 3069308Seric { 30711147Seric long hid; 30811147Seric register char *p = index(host, ']'); 3099308Seric 31011147Seric if (p != NULL) 3119308Seric { 31211147Seric *p = '\0'; 31311147Seric hid = inet_addr(&host[1]); 31411147Seric *p = ']'; 3159308Seric } 31611147Seric if (p == NULL || hid == -1) 3179308Seric { 3189308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3199308Seric return (EX_NOHOST); 3209308Seric } 3219308Seric SendmailAddress.sin_addr.s_addr = hid; 3229308Seric } 3239610Seric else 3249610Seric { 32529430Sbloom hp = gethostbyname(host); 32625475Smiriam if (hp == NULL) 32724945Seric { 32835651Seric #ifdef NAMED_BIND 32925475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 33025475Smiriam return (EX_TEMPFAIL); 33125657Seric 33235651Seric /* if name server is specified, assume temp fail */ 33335651Seric if (errno == ECONNREFUSED && UseNameServer) 33435651Seric return (EX_TEMPFAIL); 33535651Seric #endif 33635651Seric 33725657Seric /* 33825657Seric ** XXX Should look for mail forwarder record here 33925657Seric ** XXX if (h_errno == NO_ADDRESS). 34025657Seric */ 34125657Seric 34225475Smiriam return (EX_NOHOST); 34324945Seric } 34416884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 34529430Sbloom i = 1; 3469610Seric } 3479610Seric 3489610Seric /* 3499610Seric ** Determine the port number. 3509610Seric */ 3519610Seric 35210011Seric if (port != 0) 35310011Seric SendmailAddress.sin_port = htons(port); 35410011Seric else 3559610Seric { 3569610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3579610Seric 3589610Seric if (sp == NULL) 3599610Seric { 3609610Seric syserr("makeconnection: server \"smtp\" unknown"); 3619610Seric return (EX_OSFILE); 3629610Seric } 36310011Seric SendmailAddress.sin_port = sp->s_port; 3649610Seric } 3656039Seric 3666039Seric /* 3676039Seric ** Try to actually open the connection. 3686039Seric */ 3696039Seric 37029430Sbloom again: 3717677Seric if (tTd(16, 1)) 37229430Sbloom printf("makeconnection (%s [%s])\n", host, 37350969Sleres inet_ntoa(SendmailAddress.sin_addr)); 3746039Seric 37523120Seric s = socket(AF_INET, SOCK_STREAM, 0); 3766039Seric if (s < 0) 3776039Seric { 3786039Seric syserr("makeconnection: no socket"); 37927744Sbloom sav_errno = errno; 3806039Seric goto failure; 3816039Seric } 3826039Seric 3837677Seric if (tTd(16, 1)) 3846039Seric printf("makeconnection: %d\n", s); 38510347Seric 38610347Seric /* turn on network debugging? */ 38710347Seric if (tTd(16, 14)) 38824945Seric { 38924945Seric int on = 1; 39024945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 39124945Seric } 39240932Srick if (CurEnv->e_xfp != NULL) 39340932Srick (void) fflush(CurEnv->e_xfp); /* for debugging */ 39414383Seric errno = 0; /* for debugging */ 3959610Seric SendmailAddress.sin_family = AF_INET; 39646928Sbostic if (connect(s, 39746928Sbostic (struct sockaddr *)&SendmailAddress, sizeof SendmailAddress) < 0) 3986039Seric { 39927744Sbloom sav_errno = errno; 40027744Sbloom (void) close(s); 40129430Sbloom if (hp && hp->h_addr_list[i]) 40229430Sbloom { 40329430Sbloom bcopy(hp->h_addr_list[i++], 40429430Sbloom (char *)&SendmailAddress.sin_addr, hp->h_length); 40529430Sbloom goto again; 40629430Sbloom } 40729430Sbloom 4086039Seric /* failure, decide if temporary or not */ 4096039Seric failure: 41027744Sbloom switch (sav_errno) 4116039Seric { 4126039Seric case EISCONN: 4136039Seric case ETIMEDOUT: 4146897Seric case EINPROGRESS: 4156897Seric case EALREADY: 4166897Seric case EADDRINUSE: 41710123Seric case EHOSTDOWN: 4186897Seric case ENETDOWN: 4196897Seric case ENETRESET: 4206897Seric case ENOBUFS: 4217204Seric case ECONNREFUSED: 42211546Seric case ECONNRESET: 42310081Seric case EHOSTUNREACH: 42410098Seric case ENETUNREACH: 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 } 485*51315Seric /* 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: 493*51315Seric * TRUE if the host name was mapped. 494*51315Seric * 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 */ 502*51315Seric 503*51315Seric 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 */ 519*51315Seric if (*hbuf != '[') 520*51315Seric return (getcanonname(hbuf, hbsize)); 52140994Sbostic if ((cp = index(strcpy(ptr, hbuf), ']')) == NULL) 522*51315Seric 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) 527*51315Seric return (FALSE); 52833932Sbostic if (strlen(hp->h_name) >= hbsize) 52933932Sbostic hp->h_name[hbsize - 1] = '\0'; 53033932Sbostic (void)strcpy(hbuf, hp->h_name); 531*51315Seric 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: 570*51315Seric ** TRUE if the hostname was mapped. 571*51315Seric ** 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*/ 581*51315Seric bool 58216911Seric maphostname(hbuf, hbsize) 58316911Seric char *hbuf; 58416911Seric int hbsize; 58516911Seric { 586*51315Seric return (FALSE); 58716911Seric } 58816911Seric 5895978Seric #endif DAEMON 590