122700Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 333780Sbostic * Copyright (c) 1988 Regents of the University of California. 433780Sbostic * All rights reserved. 533780Sbostic * 633780Sbostic * Redistribution and use in source and binary forms are permitted 734920Sbostic * provided that the above copyright notice and this paragraph are 834920Sbostic * duplicated in all such forms and that any documentation, 934920Sbostic * advertising materials, and other materials related to such 1034920Sbostic * distribution and use acknowledge that the software was developed 1134920Sbostic * by the University of California, Berkeley. The name of the 1234920Sbostic * University may not be used to endorse or promote products derived 1334920Sbostic * from this software without specific prior written permission. 1434920Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1534920Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1634920Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1733780Sbostic */ 1822700Sdist 1933932Sbostic #include <errno.h> 2033932Sbostic #include <sendmail.h> 214535Seric 2233780Sbostic #ifndef lint 2333780Sbostic #ifdef DAEMON 24*39953Smckusick static char sccsid[] = "@(#)daemon.c 5.31 (Berkeley) 01/30/90 (with daemon mode)"; 2533780Sbostic #else 26*39953Smckusick static char sccsid[] = "@(#)daemon.c 5.31 (Berkeley) 01/30/90 (without daemon mode)"; 2733780Sbostic #endif 2833780Sbostic #endif /* not lint */ 294535Seric 3033780Sbostic #ifdef DAEMON 3133780Sbostic 3223120Seric # include <netdb.h> 3324945Seric # include <sys/signal.h> 3423120Seric # include <sys/wait.h> 3523120Seric # include <sys/time.h> 3623120Seric # include <sys/resource.h> 375978Seric 384535Seric /* 394535Seric ** DAEMON.C -- routines to use when running as a daemon. 407556Seric ** 417556Seric ** This entire file is highly dependent on the 4.2 BSD 427556Seric ** interprocess communication primitives. No attempt has 437556Seric ** been made to make this file portable to Version 7, 447556Seric ** Version 6, MPX files, etc. If you should try such a 457556Seric ** thing yourself, I recommend chucking the entire file 467556Seric ** and starting from scratch. Basic semantics are: 477556Seric ** 487556Seric ** getrequests() 497556Seric ** Opens a port and initiates a connection. 507556Seric ** Returns in a child. Must set InChannel and 517556Seric ** OutChannel appropriately. 5210206Seric ** clrdaemon() 5310206Seric ** Close any open files associated with getting 5410206Seric ** the connection; this is used when running the queue, 5510206Seric ** etc., to avoid having extra file descriptors during 5610206Seric ** the queue run and to avoid confusing the network 5710206Seric ** code (if it cares). 587556Seric ** makeconnection(host, port, outfile, infile) 597556Seric ** Make a connection to the named host on the given 607556Seric ** port. Set *outfile and *infile to the files 617556Seric ** appropriate for communication. Returns zero on 627556Seric ** success, else an exit status describing the 637556Seric ** error. 6425699Seric ** maphostname(hbuf, hbufsize) 6525699Seric ** Convert the entry in hbuf into a canonical form. It 6625699Seric ** may not be larger than hbufsize. 674535Seric */ 684535Seric /* 694535Seric ** GETREQUESTS -- open mail IPC port and get requests. 704535Seric ** 714535Seric ** Parameters: 724535Seric ** none. 734535Seric ** 744535Seric ** Returns: 754535Seric ** none. 764535Seric ** 774535Seric ** Side Effects: 784535Seric ** Waits until some interesting activity occurs. When 794535Seric ** it does, a child is created to process it, and the 804535Seric ** parent waits for completion. Return from this 819886Seric ** routine is always in the child. The file pointers 829886Seric ** "InChannel" and "OutChannel" should be set to point 839886Seric ** to the communication channel. 844535Seric */ 854535Seric 8610206Seric struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 879610Seric 8816144Seric int DaemonSocket = -1; /* fd describing socket */ 8916890Seric char *NetName; /* name of home (local?) network */ 9016144Seric 914535Seric getrequests() 924535Seric { 939610Seric int t; 949610Seric register struct servent *sp; 9525027Seric int on = 1; 9624945Seric extern reapchild(); 977117Seric 989610Seric /* 999610Seric ** Set up the address for the mailer. 1009610Seric */ 1019610Seric 1029610Seric sp = getservbyname("smtp", "tcp"); 1039610Seric if (sp == NULL) 1049610Seric { 1059610Seric syserr("server \"smtp\" unknown"); 10610167Seric goto severe; 1079610Seric } 1089610Seric SendmailAddress.sin_family = AF_INET; 1099610Seric SendmailAddress.sin_addr.s_addr = INADDR_ANY; 1109740Ssam SendmailAddress.sin_port = sp->s_port; 1119610Seric 1129610Seric /* 1139610Seric ** Try to actually open the connection. 1149610Seric */ 1159610Seric 1169610Seric if (tTd(15, 1)) 1179610Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 1189610Seric 1199610Seric /* get a socket for the SMTP connection */ 12023120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 12110206Seric if (DaemonSocket < 0) 1229610Seric { 1239610Seric /* probably another daemon already */ 1249610Seric syserr("getrequests: can't create socket"); 1259610Seric severe: 1269610Seric # ifdef LOG 1279610Seric if (LogLevel > 0) 12824858Seric syslog(LOG_ALERT, "cannot get connection"); 1299610Seric # endif LOG 1309610Seric finis(); 1319610Seric } 13210347Seric 13310347Seric /* turn on network debugging? */ 13410347Seric if (tTd(15, 15)) 13524945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 13610347Seric 13725027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 13825027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 13925027Seric 14023120Seric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0) 1419610Seric { 1429610Seric syserr("getrequests: cannot bind"); 14310206Seric (void) close(DaemonSocket); 1449610Seric goto severe; 1459610Seric } 14623120Seric if (listen(DaemonSocket, 10) < 0) 14723120Seric { 14823120Seric syserr("getrequests: cannot listen"); 14923120Seric (void) close(DaemonSocket); 15023120Seric goto severe; 15123120Seric } 1529610Seric 15324955Seric (void) signal(SIGCHLD, reapchild); 15424945Seric 1559610Seric if (tTd(15, 1)) 15610206Seric printf("getrequests: %d\n", DaemonSocket); 1579610Seric 1584631Seric for (;;) 1594631Seric { 16014875Seric register int pid; 16111147Seric auto int lotherend; 16214875Seric extern int RefuseLA; 16336584Sbostic register int la; 16411147Seric 16514875Seric /* see if we are rejecting connections */ 16636584Sbostic while ((la = getla()) > RefuseLA) 16736584Sbostic { 168*39953Smckusick setproctitle("rejecting connections: load average: %.2f", (double)la); 16914875Seric sleep(5); 17036584Sbostic } 17114875Seric 1729610Seric /* wait for a connection */ 17336584Sbostic setproctitle("accepting connections"); 1749610Seric do 1759610Seric { 1769610Seric errno = 0; 17736230Skarels lotherend = sizeof RealHostAddr; 17836230Skarels t = accept(DaemonSocket, &RealHostAddr, &lotherend); 1799610Seric } while (t < 0 && errno == EINTR); 1809610Seric if (t < 0) 1815978Seric { 1829610Seric syserr("getrequests: accept"); 1839610Seric sleep(5); 1849610Seric continue; 1855978Seric } 1864631Seric 1875978Seric /* 1885978Seric ** Create a subprocess to process the mail. 1895978Seric */ 1905978Seric 1917677Seric if (tTd(15, 2)) 1929610Seric printf("getrequests: forking (fd = %d)\n", t); 1935978Seric 1944636Seric pid = fork(); 1954636Seric if (pid < 0) 1964631Seric { 1974636Seric syserr("daemon: cannot fork"); 1984636Seric sleep(10); 1999610Seric (void) close(t); 2004636Seric continue; 2014631Seric } 2024631Seric 2034636Seric if (pid == 0) 2044631Seric { 20511147Seric extern struct hostent *gethostbyaddr(); 20611147Seric register struct hostent *hp; 20711147Seric char buf[MAXNAME]; 20811147Seric 2094636Seric /* 2104636Seric ** CHILD -- return to caller. 21111147Seric ** Collect verified idea of sending host. 2124636Seric ** Verify calling user id if possible here. 2134636Seric */ 2144631Seric 21524955Seric (void) signal(SIGCHLD, SIG_DFL); 21624950Seric 21711147Seric /* determine host name */ 21836230Skarels hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 21911147Seric if (hp != NULL) 22023104Seric (void) strcpy(buf, hp->h_name); 22111147Seric else 22216884Seric { 22316884Seric extern char *inet_ntoa(); 22416884Seric 22516884Seric /* produce a dotted quad */ 22616884Seric (void) sprintf(buf, "[%s]", 22736230Skarels inet_ntoa(RealHostAddr.sin_addr)); 22816884Seric } 22916884Seric 23016884Seric /* should we check for illegal connection here? XXX */ 23116884Seric 23211147Seric RealHostName = newstr(buf); 23311147Seric 23410206Seric (void) close(DaemonSocket); 2359610Seric InChannel = fdopen(t, "r"); 23621062Seric OutChannel = fdopen(dup(t), "w"); 2377677Seric if (tTd(15, 2)) 2385978Seric printf("getreq: returning\n"); 2397876Seric # ifdef LOG 2407876Seric if (LogLevel > 11) 2417876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2427876Seric # endif LOG 2434636Seric return; 2444631Seric } 2454631Seric 2467117Seric /* close the port so that others will hang (for a while) */ 2479610Seric (void) close(t); 2484631Seric } 2499886Seric /*NOTREACHED*/ 2504631Seric } 2515978Seric /* 25210206Seric ** CLRDAEMON -- reset the daemon connection 25310206Seric ** 25410206Seric ** Parameters: 25510206Seric ** none. 25610206Seric ** 25710206Seric ** Returns: 25810206Seric ** none. 25910206Seric ** 26010206Seric ** Side Effects: 26110206Seric ** releases any resources used by the passive daemon. 26210206Seric */ 26310206Seric 26410206Seric clrdaemon() 26510206Seric { 26610206Seric if (DaemonSocket >= 0) 26710206Seric (void) close(DaemonSocket); 26810206Seric DaemonSocket = -1; 26910206Seric } 27010206Seric /* 2716039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2726039Seric ** 2736039Seric ** Parameters: 2746039Seric ** host -- the name of the host. 2756633Seric ** port -- the port number to connect to. 2766039Seric ** outfile -- a pointer to a place to put the outfile 2776039Seric ** descriptor. 2786039Seric ** infile -- ditto for infile. 2796039Seric ** 2806039Seric ** Returns: 2816039Seric ** An exit code telling whether the connection could be 2826039Seric ** made and if not why not. 2836039Seric ** 2846039Seric ** Side Effects: 2856039Seric ** none. 2866039Seric */ 2875978Seric 2886633Seric makeconnection(host, port, outfile, infile) 2896039Seric char *host; 2907286Seric u_short port; 2916039Seric FILE **outfile; 2926039Seric FILE **infile; 2936039Seric { 29429430Sbloom register int i, s; 29529430Sbloom register struct hostent *hp = (struct hostent *)NULL; 29629430Sbloom extern char *inet_ntoa(); 29727744Sbloom int sav_errno; 29835651Seric #ifdef NAMED_BIND 29935651Seric extern int h_errno; 30035651Seric #endif 3016039Seric 3026039Seric /* 3036039Seric ** Set up the address for the mailer. 3049308Seric ** Accept "[a.b.c.d]" syntax for host name. 3056039Seric */ 3066039Seric 30735651Seric #ifdef NAMED_BIND 30825475Smiriam h_errno = 0; 30935651Seric #endif 31025475Smiriam errno = 0; 31125475Smiriam 3129308Seric if (host[0] == '[') 3139308Seric { 31411147Seric long hid; 31511147Seric register char *p = index(host, ']'); 3169308Seric 31711147Seric if (p != NULL) 3189308Seric { 31911147Seric *p = '\0'; 32011147Seric hid = inet_addr(&host[1]); 32111147Seric *p = ']'; 3229308Seric } 32311147Seric if (p == NULL || hid == -1) 3249308Seric { 3259308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3269308Seric return (EX_NOHOST); 3279308Seric } 3289308Seric SendmailAddress.sin_addr.s_addr = hid; 3299308Seric } 3309610Seric else 3319610Seric { 33229430Sbloom hp = gethostbyname(host); 33325475Smiriam if (hp == NULL) 33424945Seric { 33535651Seric #ifdef NAMED_BIND 33625475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 33725475Smiriam return (EX_TEMPFAIL); 33825657Seric 33935651Seric /* if name server is specified, assume temp fail */ 34035651Seric if (errno == ECONNREFUSED && UseNameServer) 34135651Seric return (EX_TEMPFAIL); 34235651Seric #endif 34335651Seric 34425657Seric /* 34525657Seric ** XXX Should look for mail forwarder record here 34625657Seric ** XXX if (h_errno == NO_ADDRESS). 34725657Seric */ 34825657Seric 34925475Smiriam return (EX_NOHOST); 35024945Seric } 35116884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 35229430Sbloom i = 1; 3539610Seric } 3549610Seric 3559610Seric /* 3569610Seric ** Determine the port number. 3579610Seric */ 3589610Seric 35910011Seric if (port != 0) 36010011Seric SendmailAddress.sin_port = htons(port); 36110011Seric else 3629610Seric { 3639610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3649610Seric 3659610Seric if (sp == NULL) 3669610Seric { 3679610Seric syserr("makeconnection: server \"smtp\" unknown"); 3689610Seric return (EX_OSFILE); 3699610Seric } 37010011Seric SendmailAddress.sin_port = sp->s_port; 3719610Seric } 3726039Seric 3736039Seric /* 3746039Seric ** Try to actually open the connection. 3756039Seric */ 3766039Seric 37729430Sbloom again: 3787677Seric if (tTd(16, 1)) 37929430Sbloom printf("makeconnection (%s [%s])\n", host, 38029430Sbloom inet_ntoa(SendmailAddress.sin_addr.s_addr)); 3816039Seric 38223120Seric s = socket(AF_INET, SOCK_STREAM, 0); 3836039Seric if (s < 0) 3846039Seric { 3856039Seric syserr("makeconnection: no socket"); 38627744Sbloom sav_errno = errno; 3876039Seric goto failure; 3886039Seric } 3896039Seric 3907677Seric if (tTd(16, 1)) 3916039Seric printf("makeconnection: %d\n", s); 39210347Seric 39310347Seric /* turn on network debugging? */ 39410347Seric if (tTd(16, 14)) 39524945Seric { 39624945Seric int on = 1; 39724945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 39824945Seric } 3999546Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 40014383Seric errno = 0; /* for debugging */ 4019610Seric SendmailAddress.sin_family = AF_INET; 40223120Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0) 4036039Seric { 40427744Sbloom sav_errno = errno; 40527744Sbloom (void) close(s); 40629430Sbloom if (hp && hp->h_addr_list[i]) 40729430Sbloom { 40829430Sbloom bcopy(hp->h_addr_list[i++], 40929430Sbloom (char *)&SendmailAddress.sin_addr, hp->h_length); 41029430Sbloom goto again; 41129430Sbloom } 41229430Sbloom 4136039Seric /* failure, decide if temporary or not */ 4146039Seric failure: 41527744Sbloom switch (sav_errno) 4166039Seric { 4176039Seric case EISCONN: 4186039Seric case ETIMEDOUT: 4196897Seric case EINPROGRESS: 4206897Seric case EALREADY: 4216897Seric case EADDRINUSE: 42210123Seric case EHOSTDOWN: 4236897Seric case ENETDOWN: 4246897Seric case ENETRESET: 4256897Seric case ENOBUFS: 4267204Seric case ECONNREFUSED: 42711546Seric case ECONNRESET: 42810081Seric case EHOSTUNREACH: 42910098Seric case ENETUNREACH: 4306039Seric /* there are others, I'm sure..... */ 4316039Seric return (EX_TEMPFAIL); 4326039Seric 43311147Seric case EPERM: 43411147Seric /* why is this happening? */ 43511147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 43611147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 43714383Seric return (EX_TEMPFAIL); 43811147Seric 4396039Seric default: 44031953Sbostic message(Arpa_Info, "%s", errstring(sav_errno)); 4416039Seric return (EX_UNAVAILABLE); 4426039Seric } 4436039Seric } 4446039Seric 4456039Seric /* connection ok, put it into canonical form */ 4466039Seric *outfile = fdopen(s, "w"); 4476039Seric *infile = fdopen(s, "r"); 4486039Seric 44910098Seric return (EX_OK); 4506039Seric } 45110758Seric /* 45210758Seric ** MYHOSTNAME -- return the name of this host. 45310758Seric ** 45410758Seric ** Parameters: 45510758Seric ** hostbuf -- a place to return the name of this host. 45612313Seric ** size -- the size of hostbuf. 45710758Seric ** 45810758Seric ** Returns: 45910758Seric ** A list of aliases for this host. 46010758Seric ** 46110758Seric ** Side Effects: 46210758Seric ** none. 46310758Seric */ 4646039Seric 46510758Seric char ** 46612313Seric myhostname(hostbuf, size) 46710758Seric char hostbuf[]; 46812313Seric int size; 46910758Seric { 47010758Seric extern struct hostent *gethostbyname(); 47111147Seric struct hostent *hp; 47210758Seric 47323120Seric if (gethostname(hostbuf, size) < 0) 47423120Seric { 47523120Seric (void) strcpy(hostbuf, "localhost"); 47623120Seric } 47711147Seric hp = gethostbyname(hostbuf); 47811147Seric if (hp != NULL) 47916877Seric { 48023104Seric (void) strcpy(hostbuf, hp->h_name); 48111147Seric return (hp->h_aliases); 48216877Seric } 48310758Seric else 48410758Seric return (NULL); 48510758Seric } 48610758Seric 48733932Sbostic /* 48833932Sbostic * MAPHOSTNAME -- turn a hostname into canonical form 48933932Sbostic * 49033932Sbostic * Parameters: 49133932Sbostic * hbuf -- a buffer containing a hostname. 49233932Sbostic * hbsize -- the size of hbuf. 49333932Sbostic * 49433932Sbostic * Returns: 49533932Sbostic * none. 49633932Sbostic * 49733932Sbostic * Side Effects: 49833932Sbostic * Looks up the host specified in hbuf. If it is not 49933932Sbostic * the canonical name for that host, replace it with 50033932Sbostic * the canonical name. If the name is unknown, or it 50133932Sbostic * is already the canonical name, leave it unchanged. 50233932Sbostic */ 50316911Seric maphostname(hbuf, hbsize) 50416911Seric char *hbuf; 50516911Seric int hbsize; 50616911Seric { 50716911Seric register struct hostent *hp; 50833932Sbostic u_long in_addr; 50933932Sbostic char ptr[256]; 51033932Sbostic struct hostent *gethostbyaddr(); 51116911Seric 51225574Smiriam /* 51333932Sbostic * If first character is a bracket, then it is an address 51433932Sbostic * lookup. Address is copied into a temporary buffer to 51533932Sbostic * strip the brackets and to preserve hbuf if address is 51633932Sbostic * unknown. 51733932Sbostic */ 51833932Sbostic if (*hbuf != '[') { 51929654Sbloom getcanonname(hbuf, hbsize); 52029654Sbloom return; 52125574Smiriam } 52233932Sbostic *index(strcpy(ptr, hbuf), ']') = '\0'; 52333932Sbostic in_addr = inet_addr(&ptr[1]); 52433932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 52533932Sbostic if (hp == NULL) 52633932Sbostic return; 52733932Sbostic if (strlen(hp->h_name) >= hbsize) 52833932Sbostic hp->h_name[hbsize - 1] = '\0'; 52933932Sbostic (void)strcpy(hbuf, hp->h_name); 53033932Sbostic } 53116911Seric 53210758Seric # else DAEMON 53316911Seric /* code for systems without sophisticated networking */ 53410758Seric 53510758Seric /* 53610758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 53711297Seric ** 53811297Seric ** Can't convert to upper case here because might be a UUCP name. 53912313Seric ** 54012313Seric ** Mark, you can change this to be anything you want...... 54110758Seric */ 54210758Seric 54310758Seric char ** 54412313Seric myhostname(hostbuf, size) 54510758Seric char hostbuf[]; 54612313Seric int size; 54710758Seric { 54810758Seric register FILE *f; 54910758Seric 55010758Seric hostbuf[0] = '\0'; 55110758Seric f = fopen("/usr/include/whoami", "r"); 55210758Seric if (f != NULL) 55310758Seric { 55412313Seric (void) fgets(hostbuf, size, f); 55510758Seric fixcrlf(hostbuf, TRUE); 55610758Seric (void) fclose(f); 55710758Seric } 55810758Seric return (NULL); 55910758Seric } 56016911Seric /* 56116911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 56216911Seric ** 56316911Seric ** Parameters: 56416911Seric ** hbuf -- a buffer containing a hostname. 56516911Seric ** hbsize -- the size of hbuf. 56616911Seric ** 56716911Seric ** Returns: 56816911Seric ** none. 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*/ 57816911Seric maphostname(hbuf, hbsize) 57916911Seric char *hbuf; 58016911Seric int hbsize; 58116911Seric { 58216911Seric return; 58316911Seric } 58416911Seric 5855978Seric #endif DAEMON 586