122700Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 333780Sbostic * Copyright (c) 1988 Regents of the University of California. 433780Sbostic * All rights reserved. 533780Sbostic * 6*42825Sbostic * %sccs.include.redist.c% 733780Sbostic */ 822700Sdist 933932Sbostic #include <errno.h> 1040962Sbostic #include "sendmail.h" 114535Seric 1233780Sbostic #ifndef lint 1333780Sbostic #ifdef DAEMON 14*42825Sbostic static char sccsid[] = "@(#)daemon.c 5.36 (Berkeley) 06/01/90 (with daemon mode)"; 1533780Sbostic #else 16*42825Sbostic static char sccsid[] = "@(#)daemon.c 5.36 (Berkeley) 06/01/90 (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; 8824945Seric extern 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 13223120Seric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0) 1339610Seric { 1349610Seric syserr("getrequests: cannot bind"); 13510206Seric (void) close(DaemonSocket); 1369610Seric goto severe; 1379610Seric } 13823120Seric if (listen(DaemonSocket, 10) < 0) 13923120Seric { 14023120Seric syserr("getrequests: cannot listen"); 14123120Seric (void) close(DaemonSocket); 14223120Seric goto severe; 14323120Seric } 1449610Seric 14524955Seric (void) signal(SIGCHLD, reapchild); 14624945Seric 1479610Seric if (tTd(15, 1)) 14810206Seric printf("getrequests: %d\n", DaemonSocket); 1499610Seric 1504631Seric for (;;) 1514631Seric { 15214875Seric register int pid; 15311147Seric auto int lotherend; 15414875Seric extern int RefuseLA; 15511147Seric 15614875Seric /* see if we are rejecting connections */ 15736584Sbostic while ((la = getla()) > RefuseLA) 15836584Sbostic { 15939953Smckusick setproctitle("rejecting connections: load average: %.2f", (double)la); 16014875Seric sleep(5); 16136584Sbostic } 16214875Seric 1639610Seric /* wait for a connection */ 16436584Sbostic setproctitle("accepting connections"); 1659610Seric do 1669610Seric { 1679610Seric errno = 0; 16836230Skarels lotherend = sizeof RealHostAddr; 16936230Skarels t = accept(DaemonSocket, &RealHostAddr, &lotherend); 1709610Seric } while (t < 0 && errno == EINTR); 1719610Seric if (t < 0) 1725978Seric { 1739610Seric syserr("getrequests: accept"); 1749610Seric sleep(5); 1759610Seric continue; 1765978Seric } 1774631Seric 1785978Seric /* 1795978Seric ** Create a subprocess to process the mail. 1805978Seric */ 1815978Seric 1827677Seric if (tTd(15, 2)) 1839610Seric printf("getrequests: forking (fd = %d)\n", t); 1845978Seric 1854636Seric pid = fork(); 1864636Seric if (pid < 0) 1874631Seric { 1884636Seric syserr("daemon: cannot fork"); 1894636Seric sleep(10); 1909610Seric (void) close(t); 1914636Seric continue; 1924631Seric } 1934631Seric 1944636Seric if (pid == 0) 1954631Seric { 19611147Seric extern struct hostent *gethostbyaddr(); 19711147Seric register struct hostent *hp; 19811147Seric char buf[MAXNAME]; 19911147Seric 2004636Seric /* 2014636Seric ** CHILD -- return to caller. 20211147Seric ** Collect verified idea of sending host. 2034636Seric ** Verify calling user id if possible here. 2044636Seric */ 2054631Seric 20624955Seric (void) signal(SIGCHLD, SIG_DFL); 20724950Seric 20811147Seric /* determine host name */ 20936230Skarels hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 21011147Seric if (hp != NULL) 21123104Seric (void) strcpy(buf, hp->h_name); 21211147Seric else 21316884Seric { 21416884Seric extern char *inet_ntoa(); 21516884Seric 21616884Seric /* produce a dotted quad */ 21716884Seric (void) sprintf(buf, "[%s]", 21836230Skarels inet_ntoa(RealHostAddr.sin_addr)); 21916884Seric } 22016884Seric 22116884Seric /* should we check for illegal connection here? XXX */ 22216884Seric 22311147Seric RealHostName = newstr(buf); 22411147Seric 22510206Seric (void) close(DaemonSocket); 2269610Seric InChannel = fdopen(t, "r"); 22721062Seric OutChannel = fdopen(dup(t), "w"); 2287677Seric if (tTd(15, 2)) 2295978Seric printf("getreq: returning\n"); 2307876Seric # ifdef LOG 2317876Seric if (LogLevel > 11) 2327876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2337876Seric # endif LOG 2344636Seric return; 2354631Seric } 2364631Seric 2377117Seric /* close the port so that others will hang (for a while) */ 2389610Seric (void) close(t); 2394631Seric } 2409886Seric /*NOTREACHED*/ 2414631Seric } 2425978Seric /* 24310206Seric ** CLRDAEMON -- reset the daemon connection 24410206Seric ** 24510206Seric ** Parameters: 24610206Seric ** none. 24710206Seric ** 24810206Seric ** Returns: 24910206Seric ** none. 25010206Seric ** 25110206Seric ** Side Effects: 25210206Seric ** releases any resources used by the passive daemon. 25310206Seric */ 25410206Seric 25510206Seric clrdaemon() 25610206Seric { 25710206Seric if (DaemonSocket >= 0) 25810206Seric (void) close(DaemonSocket); 25910206Seric DaemonSocket = -1; 26010206Seric } 26110206Seric /* 2626039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2636039Seric ** 2646039Seric ** Parameters: 2656039Seric ** host -- the name of the host. 2666633Seric ** port -- the port number to connect to. 2676039Seric ** outfile -- a pointer to a place to put the outfile 2686039Seric ** descriptor. 2696039Seric ** infile -- ditto for infile. 2706039Seric ** 2716039Seric ** Returns: 2726039Seric ** An exit code telling whether the connection could be 2736039Seric ** made and if not why not. 2746039Seric ** 2756039Seric ** Side Effects: 2766039Seric ** none. 2776039Seric */ 2785978Seric 2796633Seric makeconnection(host, port, outfile, infile) 2806039Seric char *host; 2817286Seric u_short port; 2826039Seric FILE **outfile; 2836039Seric FILE **infile; 2846039Seric { 28529430Sbloom register int i, s; 28629430Sbloom register struct hostent *hp = (struct hostent *)NULL; 28729430Sbloom extern char *inet_ntoa(); 28827744Sbloom int sav_errno; 28935651Seric #ifdef NAMED_BIND 29035651Seric extern int h_errno; 29135651Seric #endif 2926039Seric 2936039Seric /* 2946039Seric ** Set up the address for the mailer. 2959308Seric ** Accept "[a.b.c.d]" syntax for host name. 2966039Seric */ 2976039Seric 29835651Seric #ifdef NAMED_BIND 29925475Smiriam h_errno = 0; 30035651Seric #endif 30125475Smiriam errno = 0; 30225475Smiriam 3039308Seric if (host[0] == '[') 3049308Seric { 30511147Seric long hid; 30611147Seric register char *p = index(host, ']'); 3079308Seric 30811147Seric if (p != NULL) 3099308Seric { 31011147Seric *p = '\0'; 31111147Seric hid = inet_addr(&host[1]); 31211147Seric *p = ']'; 3139308Seric } 31411147Seric if (p == NULL || hid == -1) 3159308Seric { 3169308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3179308Seric return (EX_NOHOST); 3189308Seric } 3199308Seric SendmailAddress.sin_addr.s_addr = hid; 3209308Seric } 3219610Seric else 3229610Seric { 32329430Sbloom hp = gethostbyname(host); 32425475Smiriam if (hp == NULL) 32524945Seric { 32635651Seric #ifdef NAMED_BIND 32725475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 32825475Smiriam return (EX_TEMPFAIL); 32925657Seric 33035651Seric /* if name server is specified, assume temp fail */ 33135651Seric if (errno == ECONNREFUSED && UseNameServer) 33235651Seric return (EX_TEMPFAIL); 33335651Seric #endif 33435651Seric 33525657Seric /* 33625657Seric ** XXX Should look for mail forwarder record here 33725657Seric ** XXX if (h_errno == NO_ADDRESS). 33825657Seric */ 33925657Seric 34025475Smiriam return (EX_NOHOST); 34124945Seric } 34216884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 34329430Sbloom i = 1; 3449610Seric } 3459610Seric 3469610Seric /* 3479610Seric ** Determine the port number. 3489610Seric */ 3499610Seric 35010011Seric if (port != 0) 35110011Seric SendmailAddress.sin_port = htons(port); 35210011Seric else 3539610Seric { 3549610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3559610Seric 3569610Seric if (sp == NULL) 3579610Seric { 3589610Seric syserr("makeconnection: server \"smtp\" unknown"); 3599610Seric return (EX_OSFILE); 3609610Seric } 36110011Seric SendmailAddress.sin_port = sp->s_port; 3629610Seric } 3636039Seric 3646039Seric /* 3656039Seric ** Try to actually open the connection. 3666039Seric */ 3676039Seric 36829430Sbloom again: 3697677Seric if (tTd(16, 1)) 37029430Sbloom printf("makeconnection (%s [%s])\n", host, 37129430Sbloom inet_ntoa(SendmailAddress.sin_addr.s_addr)); 3726039Seric 37323120Seric s = socket(AF_INET, SOCK_STREAM, 0); 3746039Seric if (s < 0) 3756039Seric { 3766039Seric syserr("makeconnection: no socket"); 37727744Sbloom sav_errno = errno; 3786039Seric goto failure; 3796039Seric } 3806039Seric 3817677Seric if (tTd(16, 1)) 3826039Seric printf("makeconnection: %d\n", s); 38310347Seric 38410347Seric /* turn on network debugging? */ 38510347Seric if (tTd(16, 14)) 38624945Seric { 38724945Seric int on = 1; 38824945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 38924945Seric } 39040932Srick if (CurEnv->e_xfp != NULL) 39140932Srick (void) fflush(CurEnv->e_xfp); /* for debugging */ 39214383Seric errno = 0; /* for debugging */ 3939610Seric SendmailAddress.sin_family = AF_INET; 39423120Seric if (connect(s, &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 } 48210758Seric 48333932Sbostic /* 48433932Sbostic * MAPHOSTNAME -- turn a hostname into canonical form 48533932Sbostic * 48633932Sbostic * Parameters: 48733932Sbostic * hbuf -- a buffer containing a hostname. 48833932Sbostic * hbsize -- the size of hbuf. 48933932Sbostic * 49033932Sbostic * Returns: 49133932Sbostic * none. 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 */ 49916911Seric maphostname(hbuf, hbsize) 50016911Seric char *hbuf; 50116911Seric int hbsize; 50216911Seric { 50316911Seric register struct hostent *hp; 50433932Sbostic u_long in_addr; 50540994Sbostic char ptr[256], *cp; 50633932Sbostic struct hostent *gethostbyaddr(); 50716911Seric 50825574Smiriam /* 50933932Sbostic * If first character is a bracket, then it is an address 51033932Sbostic * lookup. Address is copied into a temporary buffer to 51133932Sbostic * strip the brackets and to preserve hbuf if address is 51233932Sbostic * unknown. 51333932Sbostic */ 51433932Sbostic if (*hbuf != '[') { 51529654Sbloom getcanonname(hbuf, hbsize); 51629654Sbloom return; 51725574Smiriam } 51840994Sbostic if ((cp = index(strcpy(ptr, hbuf), ']')) == NULL) 51940994Sbostic return; 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) 52433932Sbostic return; 52533932Sbostic if (strlen(hp->h_name) >= hbsize) 52633932Sbostic hp->h_name[hbsize - 1] = '\0'; 52733932Sbostic (void)strcpy(hbuf, hp->h_name); 52833932Sbostic } 52916911Seric 53010758Seric # else DAEMON 53116911Seric /* code for systems without sophisticated networking */ 53210758Seric 53310758Seric /* 53410758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 53511297Seric ** 53611297Seric ** Can't convert to upper case here because might be a UUCP name. 53712313Seric ** 53812313Seric ** Mark, you can change this to be anything you want...... 53910758Seric */ 54010758Seric 54110758Seric char ** 54212313Seric myhostname(hostbuf, size) 54310758Seric char hostbuf[]; 54412313Seric int size; 54510758Seric { 54610758Seric register FILE *f; 54710758Seric 54810758Seric hostbuf[0] = '\0'; 54910758Seric f = fopen("/usr/include/whoami", "r"); 55010758Seric if (f != NULL) 55110758Seric { 55212313Seric (void) fgets(hostbuf, size, f); 55310758Seric fixcrlf(hostbuf, TRUE); 55410758Seric (void) fclose(f); 55510758Seric } 55610758Seric return (NULL); 55710758Seric } 55816911Seric /* 55916911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 56016911Seric ** 56116911Seric ** Parameters: 56216911Seric ** hbuf -- a buffer containing a hostname. 56316911Seric ** hbsize -- the size of hbuf. 56416911Seric ** 56516911Seric ** Returns: 56616911Seric ** none. 56716911Seric ** 56816911Seric ** Side Effects: 56916911Seric ** Looks up the host specified in hbuf. If it is not 57016911Seric ** the canonical name for that host, replace it with 57116911Seric ** the canonical name. If the name is unknown, or it 57216911Seric ** is already the canonical name, leave it unchanged. 57316911Seric */ 57410758Seric 57516911Seric /*ARGSUSED*/ 57616911Seric maphostname(hbuf, hbsize) 57716911Seric char *hbuf; 57816911Seric int hbsize; 57916911Seric { 58016911Seric return; 58116911Seric } 58216911Seric 5835978Seric #endif DAEMON 584