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*40932Srick static char sccsid[] = "@(#)daemon.c 5.32 (Berkeley) 04/16/90 (with daemon mode)"; 2533780Sbostic #else 26*40932Srick static char sccsid[] = "@(#)daemon.c 5.32 (Berkeley) 04/16/90 (without daemon mode)"; 2733780Sbostic #endif 2833780Sbostic #endif /* not lint */ 294535Seric 30*40932Srick int la; /* load average */ 31*40932Srick 3233780Sbostic #ifdef DAEMON 3333780Sbostic 3423120Seric # include <netdb.h> 3524945Seric # include <sys/signal.h> 3623120Seric # include <sys/wait.h> 3723120Seric # include <sys/time.h> 3823120Seric # include <sys/resource.h> 395978Seric 404535Seric /* 414535Seric ** DAEMON.C -- routines to use when running as a daemon. 427556Seric ** 437556Seric ** This entire file is highly dependent on the 4.2 BSD 447556Seric ** interprocess communication primitives. No attempt has 457556Seric ** been made to make this file portable to Version 7, 467556Seric ** Version 6, MPX files, etc. If you should try such a 477556Seric ** thing yourself, I recommend chucking the entire file 487556Seric ** and starting from scratch. Basic semantics are: 497556Seric ** 507556Seric ** getrequests() 517556Seric ** Opens a port and initiates a connection. 527556Seric ** Returns in a child. Must set InChannel and 537556Seric ** OutChannel appropriately. 5410206Seric ** clrdaemon() 5510206Seric ** Close any open files associated with getting 5610206Seric ** the connection; this is used when running the queue, 5710206Seric ** etc., to avoid having extra file descriptors during 5810206Seric ** the queue run and to avoid confusing the network 5910206Seric ** code (if it cares). 607556Seric ** makeconnection(host, port, outfile, infile) 617556Seric ** Make a connection to the named host on the given 627556Seric ** port. Set *outfile and *infile to the files 637556Seric ** appropriate for communication. Returns zero on 647556Seric ** success, else an exit status describing the 657556Seric ** error. 6625699Seric ** maphostname(hbuf, hbufsize) 6725699Seric ** Convert the entry in hbuf into a canonical form. It 6825699Seric ** may not be larger than hbufsize. 694535Seric */ 704535Seric /* 714535Seric ** GETREQUESTS -- open mail IPC port and get requests. 724535Seric ** 734535Seric ** Parameters: 744535Seric ** none. 754535Seric ** 764535Seric ** Returns: 774535Seric ** none. 784535Seric ** 794535Seric ** Side Effects: 804535Seric ** Waits until some interesting activity occurs. When 814535Seric ** it does, a child is created to process it, and the 824535Seric ** parent waits for completion. Return from this 839886Seric ** routine is always in the child. The file pointers 849886Seric ** "InChannel" and "OutChannel" should be set to point 859886Seric ** to the communication channel. 864535Seric */ 874535Seric 8810206Seric struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 899610Seric 9016144Seric int DaemonSocket = -1; /* fd describing socket */ 9116890Seric char *NetName; /* name of home (local?) network */ 9216144Seric 934535Seric getrequests() 944535Seric { 959610Seric int t; 969610Seric register struct servent *sp; 9725027Seric int on = 1; 9824945Seric extern reapchild(); 997117Seric 1009610Seric /* 1019610Seric ** Set up the address for the mailer. 1029610Seric */ 1039610Seric 1049610Seric sp = getservbyname("smtp", "tcp"); 1059610Seric if (sp == NULL) 1069610Seric { 1079610Seric syserr("server \"smtp\" unknown"); 10810167Seric goto severe; 1099610Seric } 1109610Seric SendmailAddress.sin_family = AF_INET; 1119610Seric SendmailAddress.sin_addr.s_addr = INADDR_ANY; 1129740Ssam SendmailAddress.sin_port = sp->s_port; 1139610Seric 1149610Seric /* 1159610Seric ** Try to actually open the connection. 1169610Seric */ 1179610Seric 1189610Seric if (tTd(15, 1)) 1199610Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 1209610Seric 1219610Seric /* get a socket for the SMTP connection */ 12223120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 12310206Seric if (DaemonSocket < 0) 1249610Seric { 1259610Seric /* probably another daemon already */ 1269610Seric syserr("getrequests: can't create socket"); 1279610Seric severe: 1289610Seric # ifdef LOG 1299610Seric if (LogLevel > 0) 13024858Seric syslog(LOG_ALERT, "cannot get connection"); 1319610Seric # endif LOG 1329610Seric finis(); 1339610Seric } 13410347Seric 13510347Seric /* turn on network debugging? */ 13610347Seric if (tTd(15, 15)) 13724945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 13810347Seric 13925027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 14025027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 14125027Seric 14223120Seric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0) 1439610Seric { 1449610Seric syserr("getrequests: cannot bind"); 14510206Seric (void) close(DaemonSocket); 1469610Seric goto severe; 1479610Seric } 14823120Seric if (listen(DaemonSocket, 10) < 0) 14923120Seric { 15023120Seric syserr("getrequests: cannot listen"); 15123120Seric (void) close(DaemonSocket); 15223120Seric goto severe; 15323120Seric } 1549610Seric 15524955Seric (void) signal(SIGCHLD, reapchild); 15624945Seric 1579610Seric if (tTd(15, 1)) 15810206Seric printf("getrequests: %d\n", DaemonSocket); 1599610Seric 1604631Seric for (;;) 1614631Seric { 16214875Seric register int pid; 16311147Seric auto int lotherend; 16414875Seric extern int RefuseLA; 16511147Seric 16614875Seric /* see if we are rejecting connections */ 16736584Sbostic while ((la = getla()) > RefuseLA) 16836584Sbostic { 16939953Smckusick setproctitle("rejecting connections: load average: %.2f", (double)la); 17014875Seric sleep(5); 17136584Sbostic } 17214875Seric 1739610Seric /* wait for a connection */ 17436584Sbostic setproctitle("accepting connections"); 1759610Seric do 1769610Seric { 1779610Seric errno = 0; 17836230Skarels lotherend = sizeof RealHostAddr; 17936230Skarels t = accept(DaemonSocket, &RealHostAddr, &lotherend); 1809610Seric } while (t < 0 && errno == EINTR); 1819610Seric if (t < 0) 1825978Seric { 1839610Seric syserr("getrequests: accept"); 1849610Seric sleep(5); 1859610Seric continue; 1865978Seric } 1874631Seric 1885978Seric /* 1895978Seric ** Create a subprocess to process the mail. 1905978Seric */ 1915978Seric 1927677Seric if (tTd(15, 2)) 1939610Seric printf("getrequests: forking (fd = %d)\n", t); 1945978Seric 1954636Seric pid = fork(); 1964636Seric if (pid < 0) 1974631Seric { 1984636Seric syserr("daemon: cannot fork"); 1994636Seric sleep(10); 2009610Seric (void) close(t); 2014636Seric continue; 2024631Seric } 2034631Seric 2044636Seric if (pid == 0) 2054631Seric { 20611147Seric extern struct hostent *gethostbyaddr(); 20711147Seric register struct hostent *hp; 20811147Seric char buf[MAXNAME]; 20911147Seric 2104636Seric /* 2114636Seric ** CHILD -- return to caller. 21211147Seric ** Collect verified idea of sending host. 2134636Seric ** Verify calling user id if possible here. 2144636Seric */ 2154631Seric 21624955Seric (void) signal(SIGCHLD, SIG_DFL); 21724950Seric 21811147Seric /* determine host name */ 21936230Skarels hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 22011147Seric if (hp != NULL) 22123104Seric (void) strcpy(buf, hp->h_name); 22211147Seric else 22316884Seric { 22416884Seric extern char *inet_ntoa(); 22516884Seric 22616884Seric /* produce a dotted quad */ 22716884Seric (void) sprintf(buf, "[%s]", 22836230Skarels inet_ntoa(RealHostAddr.sin_addr)); 22916884Seric } 23016884Seric 23116884Seric /* should we check for illegal connection here? XXX */ 23216884Seric 23311147Seric RealHostName = newstr(buf); 23411147Seric 23510206Seric (void) close(DaemonSocket); 2369610Seric InChannel = fdopen(t, "r"); 23721062Seric OutChannel = fdopen(dup(t), "w"); 2387677Seric if (tTd(15, 2)) 2395978Seric printf("getreq: returning\n"); 2407876Seric # ifdef LOG 2417876Seric if (LogLevel > 11) 2427876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2437876Seric # endif LOG 2444636Seric return; 2454631Seric } 2464631Seric 2477117Seric /* close the port so that others will hang (for a while) */ 2489610Seric (void) close(t); 2494631Seric } 2509886Seric /*NOTREACHED*/ 2514631Seric } 2525978Seric /* 25310206Seric ** CLRDAEMON -- reset the daemon connection 25410206Seric ** 25510206Seric ** Parameters: 25610206Seric ** none. 25710206Seric ** 25810206Seric ** Returns: 25910206Seric ** none. 26010206Seric ** 26110206Seric ** Side Effects: 26210206Seric ** releases any resources used by the passive daemon. 26310206Seric */ 26410206Seric 26510206Seric clrdaemon() 26610206Seric { 26710206Seric if (DaemonSocket >= 0) 26810206Seric (void) close(DaemonSocket); 26910206Seric DaemonSocket = -1; 27010206Seric } 27110206Seric /* 2726039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2736039Seric ** 2746039Seric ** Parameters: 2756039Seric ** host -- the name of the host. 2766633Seric ** port -- the port number to connect to. 2776039Seric ** outfile -- a pointer to a place to put the outfile 2786039Seric ** descriptor. 2796039Seric ** infile -- ditto for infile. 2806039Seric ** 2816039Seric ** Returns: 2826039Seric ** An exit code telling whether the connection could be 2836039Seric ** made and if not why not. 2846039Seric ** 2856039Seric ** Side Effects: 2866039Seric ** none. 2876039Seric */ 2885978Seric 2896633Seric makeconnection(host, port, outfile, infile) 2906039Seric char *host; 2917286Seric u_short port; 2926039Seric FILE **outfile; 2936039Seric FILE **infile; 2946039Seric { 29529430Sbloom register int i, s; 29629430Sbloom register struct hostent *hp = (struct hostent *)NULL; 29729430Sbloom extern char *inet_ntoa(); 29827744Sbloom int sav_errno; 29935651Seric #ifdef NAMED_BIND 30035651Seric extern int h_errno; 30135651Seric #endif 3026039Seric 3036039Seric /* 3046039Seric ** Set up the address for the mailer. 3059308Seric ** Accept "[a.b.c.d]" syntax for host name. 3066039Seric */ 3076039Seric 30835651Seric #ifdef NAMED_BIND 30925475Smiriam h_errno = 0; 31035651Seric #endif 31125475Smiriam errno = 0; 31225475Smiriam 3139308Seric if (host[0] == '[') 3149308Seric { 31511147Seric long hid; 31611147Seric register char *p = index(host, ']'); 3179308Seric 31811147Seric if (p != NULL) 3199308Seric { 32011147Seric *p = '\0'; 32111147Seric hid = inet_addr(&host[1]); 32211147Seric *p = ']'; 3239308Seric } 32411147Seric if (p == NULL || hid == -1) 3259308Seric { 3269308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3279308Seric return (EX_NOHOST); 3289308Seric } 3299308Seric SendmailAddress.sin_addr.s_addr = hid; 3309308Seric } 3319610Seric else 3329610Seric { 33329430Sbloom hp = gethostbyname(host); 33425475Smiriam if (hp == NULL) 33524945Seric { 33635651Seric #ifdef NAMED_BIND 33725475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 33825475Smiriam return (EX_TEMPFAIL); 33925657Seric 34035651Seric /* if name server is specified, assume temp fail */ 34135651Seric if (errno == ECONNREFUSED && UseNameServer) 34235651Seric return (EX_TEMPFAIL); 34335651Seric #endif 34435651Seric 34525657Seric /* 34625657Seric ** XXX Should look for mail forwarder record here 34725657Seric ** XXX if (h_errno == NO_ADDRESS). 34825657Seric */ 34925657Seric 35025475Smiriam return (EX_NOHOST); 35124945Seric } 35216884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 35329430Sbloom i = 1; 3549610Seric } 3559610Seric 3569610Seric /* 3579610Seric ** Determine the port number. 3589610Seric */ 3599610Seric 36010011Seric if (port != 0) 36110011Seric SendmailAddress.sin_port = htons(port); 36210011Seric else 3639610Seric { 3649610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3659610Seric 3669610Seric if (sp == NULL) 3679610Seric { 3689610Seric syserr("makeconnection: server \"smtp\" unknown"); 3699610Seric return (EX_OSFILE); 3709610Seric } 37110011Seric SendmailAddress.sin_port = sp->s_port; 3729610Seric } 3736039Seric 3746039Seric /* 3756039Seric ** Try to actually open the connection. 3766039Seric */ 3776039Seric 37829430Sbloom again: 3797677Seric if (tTd(16, 1)) 38029430Sbloom printf("makeconnection (%s [%s])\n", host, 38129430Sbloom inet_ntoa(SendmailAddress.sin_addr.s_addr)); 3826039Seric 38323120Seric s = socket(AF_INET, SOCK_STREAM, 0); 3846039Seric if (s < 0) 3856039Seric { 3866039Seric syserr("makeconnection: no socket"); 38727744Sbloom sav_errno = errno; 3886039Seric goto failure; 3896039Seric } 3906039Seric 3917677Seric if (tTd(16, 1)) 3926039Seric printf("makeconnection: %d\n", s); 39310347Seric 39410347Seric /* turn on network debugging? */ 39510347Seric if (tTd(16, 14)) 39624945Seric { 39724945Seric int on = 1; 39824945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 39924945Seric } 400*40932Srick if (CurEnv->e_xfp != NULL) 401*40932Srick (void) fflush(CurEnv->e_xfp); /* for debugging */ 40214383Seric errno = 0; /* for debugging */ 4039610Seric SendmailAddress.sin_family = AF_INET; 40423120Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0) 4056039Seric { 40627744Sbloom sav_errno = errno; 40727744Sbloom (void) close(s); 40829430Sbloom if (hp && hp->h_addr_list[i]) 40929430Sbloom { 41029430Sbloom bcopy(hp->h_addr_list[i++], 41129430Sbloom (char *)&SendmailAddress.sin_addr, hp->h_length); 41229430Sbloom goto again; 41329430Sbloom } 41429430Sbloom 4156039Seric /* failure, decide if temporary or not */ 4166039Seric failure: 41727744Sbloom switch (sav_errno) 4186039Seric { 4196039Seric case EISCONN: 4206039Seric case ETIMEDOUT: 4216897Seric case EINPROGRESS: 4226897Seric case EALREADY: 4236897Seric case EADDRINUSE: 42410123Seric case EHOSTDOWN: 4256897Seric case ENETDOWN: 4266897Seric case ENETRESET: 4276897Seric case ENOBUFS: 4287204Seric case ECONNREFUSED: 42911546Seric case ECONNRESET: 43010081Seric case EHOSTUNREACH: 43110098Seric case ENETUNREACH: 4326039Seric /* there are others, I'm sure..... */ 4336039Seric return (EX_TEMPFAIL); 4346039Seric 43511147Seric case EPERM: 43611147Seric /* why is this happening? */ 43711147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 43811147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 43914383Seric return (EX_TEMPFAIL); 44011147Seric 4416039Seric default: 442*40932Srick { 443*40932Srick extern char *errstring(); 444*40932Srick 445*40932Srick message(Arpa_Info, "%s", errstring(sav_errno)); 446*40932Srick return (EX_UNAVAILABLE); 447*40932Srick } 4486039Seric } 4496039Seric } 4506039Seric 4516039Seric /* connection ok, put it into canonical form */ 4526039Seric *outfile = fdopen(s, "w"); 4536039Seric *infile = fdopen(s, "r"); 4546039Seric 45510098Seric return (EX_OK); 4566039Seric } 45710758Seric /* 45810758Seric ** MYHOSTNAME -- return the name of this host. 45910758Seric ** 46010758Seric ** Parameters: 46110758Seric ** hostbuf -- a place to return the name of this host. 46212313Seric ** size -- the size of hostbuf. 46310758Seric ** 46410758Seric ** Returns: 46510758Seric ** A list of aliases for this host. 46610758Seric ** 46710758Seric ** Side Effects: 46810758Seric ** none. 46910758Seric */ 4706039Seric 47110758Seric char ** 47212313Seric myhostname(hostbuf, size) 47310758Seric char hostbuf[]; 47412313Seric int size; 47510758Seric { 47610758Seric extern struct hostent *gethostbyname(); 47711147Seric struct hostent *hp; 47810758Seric 47923120Seric if (gethostname(hostbuf, size) < 0) 48023120Seric { 48123120Seric (void) strcpy(hostbuf, "localhost"); 48223120Seric } 48311147Seric hp = gethostbyname(hostbuf); 48411147Seric if (hp != NULL) 48516877Seric { 48623104Seric (void) strcpy(hostbuf, hp->h_name); 48711147Seric return (hp->h_aliases); 48816877Seric } 48910758Seric else 49010758Seric return (NULL); 49110758Seric } 49210758Seric 49333932Sbostic /* 49433932Sbostic * MAPHOSTNAME -- turn a hostname into canonical form 49533932Sbostic * 49633932Sbostic * Parameters: 49733932Sbostic * hbuf -- a buffer containing a hostname. 49833932Sbostic * hbsize -- the size of hbuf. 49933932Sbostic * 50033932Sbostic * Returns: 50133932Sbostic * none. 50233932Sbostic * 50333932Sbostic * Side Effects: 50433932Sbostic * Looks up the host specified in hbuf. If it is not 50533932Sbostic * the canonical name for that host, replace it with 50633932Sbostic * the canonical name. If the name is unknown, or it 50733932Sbostic * is already the canonical name, leave it unchanged. 50833932Sbostic */ 50916911Seric maphostname(hbuf, hbsize) 51016911Seric char *hbuf; 51116911Seric int hbsize; 51216911Seric { 51316911Seric register struct hostent *hp; 51433932Sbostic u_long in_addr; 51533932Sbostic char ptr[256]; 51633932Sbostic struct hostent *gethostbyaddr(); 51716911Seric 51825574Smiriam /* 51933932Sbostic * If first character is a bracket, then it is an address 52033932Sbostic * lookup. Address is copied into a temporary buffer to 52133932Sbostic * strip the brackets and to preserve hbuf if address is 52233932Sbostic * unknown. 52333932Sbostic */ 52433932Sbostic if (*hbuf != '[') { 52529654Sbloom getcanonname(hbuf, hbsize); 52629654Sbloom return; 52725574Smiriam } 52833932Sbostic *index(strcpy(ptr, hbuf), ']') = '\0'; 52933932Sbostic in_addr = inet_addr(&ptr[1]); 53033932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 53133932Sbostic if (hp == NULL) 53233932Sbostic return; 53333932Sbostic if (strlen(hp->h_name) >= hbsize) 53433932Sbostic hp->h_name[hbsize - 1] = '\0'; 53533932Sbostic (void)strcpy(hbuf, hp->h_name); 53633932Sbostic } 53716911Seric 53810758Seric # else DAEMON 53916911Seric /* code for systems without sophisticated networking */ 54010758Seric 54110758Seric /* 54210758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 54311297Seric ** 54411297Seric ** Can't convert to upper case here because might be a UUCP name. 54512313Seric ** 54612313Seric ** Mark, you can change this to be anything you want...... 54710758Seric */ 54810758Seric 54910758Seric char ** 55012313Seric myhostname(hostbuf, size) 55110758Seric char hostbuf[]; 55212313Seric int size; 55310758Seric { 55410758Seric register FILE *f; 55510758Seric 55610758Seric hostbuf[0] = '\0'; 55710758Seric f = fopen("/usr/include/whoami", "r"); 55810758Seric if (f != NULL) 55910758Seric { 56012313Seric (void) fgets(hostbuf, size, f); 56110758Seric fixcrlf(hostbuf, TRUE); 56210758Seric (void) fclose(f); 56310758Seric } 56410758Seric return (NULL); 56510758Seric } 56616911Seric /* 56716911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 56816911Seric ** 56916911Seric ** Parameters: 57016911Seric ** hbuf -- a buffer containing a hostname. 57116911Seric ** hbsize -- the size of hbuf. 57216911Seric ** 57316911Seric ** Returns: 57416911Seric ** none. 57516911Seric ** 57616911Seric ** Side Effects: 57716911Seric ** Looks up the host specified in hbuf. If it is not 57816911Seric ** the canonical name for that host, replace it with 57916911Seric ** the canonical name. If the name is unknown, or it 58016911Seric ** is already the canonical name, leave it unchanged. 58116911Seric */ 58210758Seric 58316911Seric /*ARGSUSED*/ 58416911Seric maphostname(hbuf, hbsize) 58516911Seric char *hbuf; 58616911Seric int hbsize; 58716911Seric { 58816911Seric return; 58916911Seric } 59016911Seric 5915978Seric #endif DAEMON 592