122700Sdist /* 222700Sdist ** Sendmail 322700Sdist ** Copyright (c) 1983 Eric P. Allman 422700Sdist ** Berkeley, California 522700Sdist ** 622700Sdist ** Copyright (c) 1983 Regents of the University of California. 722700Sdist ** All rights reserved. The Berkeley software License Agreement 822700Sdist ** specifies the terms and conditions for redistribution. 922700Sdist */ 1022700Sdist 1122700Sdist 126039Seric # include <errno.h> 134535Seric # include "sendmail.h" 144535Seric 1523120Seric # ifndef DAEMON 1623120Seric # ifndef lint 17*29430Sbloom static char SccsId[] = "@(#)daemon.c 5.20 (Berkeley) 06/30/86 (w/o daemon mode)"; 1823120Seric # endif not lint 1923120Seric # else 204535Seric 2123120Seric # include <netdb.h> 2224945Seric # include <sys/signal.h> 2323120Seric # include <sys/wait.h> 2423120Seric # include <sys/time.h> 2523120Seric # include <sys/resource.h> 265978Seric 2723120Seric # ifndef lint 28*29430Sbloom static char SccsId[] = "@(#)daemon.c 5.20 (Berkeley) 06/30/86 (with daemon mode)"; 2923120Seric # endif not lint 305978Seric 314535Seric /* 324535Seric ** DAEMON.C -- routines to use when running as a daemon. 337556Seric ** 347556Seric ** This entire file is highly dependent on the 4.2 BSD 357556Seric ** interprocess communication primitives. No attempt has 367556Seric ** been made to make this file portable to Version 7, 377556Seric ** Version 6, MPX files, etc. If you should try such a 387556Seric ** thing yourself, I recommend chucking the entire file 397556Seric ** and starting from scratch. Basic semantics are: 407556Seric ** 417556Seric ** getrequests() 427556Seric ** Opens a port and initiates a connection. 437556Seric ** Returns in a child. Must set InChannel and 447556Seric ** OutChannel appropriately. 4510206Seric ** clrdaemon() 4610206Seric ** Close any open files associated with getting 4710206Seric ** the connection; this is used when running the queue, 4810206Seric ** etc., to avoid having extra file descriptors during 4910206Seric ** the queue run and to avoid confusing the network 5010206Seric ** code (if it cares). 517556Seric ** makeconnection(host, port, outfile, infile) 527556Seric ** Make a connection to the named host on the given 537556Seric ** port. Set *outfile and *infile to the files 547556Seric ** appropriate for communication. Returns zero on 557556Seric ** success, else an exit status describing the 567556Seric ** error. 5725699Seric ** maphostname(hbuf, hbufsize) 5825699Seric ** Convert the entry in hbuf into a canonical form. It 5925699Seric ** may not be larger than hbufsize. 604535Seric */ 614535Seric /* 624535Seric ** GETREQUESTS -- open mail IPC port and get requests. 634535Seric ** 644535Seric ** Parameters: 654535Seric ** none. 664535Seric ** 674535Seric ** Returns: 684535Seric ** none. 694535Seric ** 704535Seric ** Side Effects: 714535Seric ** Waits until some interesting activity occurs. When 724535Seric ** it does, a child is created to process it, and the 734535Seric ** parent waits for completion. Return from this 749886Seric ** routine is always in the child. The file pointers 759886Seric ** "InChannel" and "OutChannel" should be set to point 769886Seric ** to the communication channel. 774535Seric */ 784535Seric 7910206Seric struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 809610Seric 8116144Seric int DaemonSocket = -1; /* fd describing socket */ 8216890Seric char *NetName; /* name of home (local?) network */ 8316144Seric 844535Seric getrequests() 854535Seric { 869610Seric int t; 879610Seric register struct servent *sp; 8825027Seric int on = 1; 8924945Seric extern reapchild(); 907117Seric 919610Seric /* 929610Seric ** Set up the address for the mailer. 939610Seric */ 949610Seric 959610Seric sp = getservbyname("smtp", "tcp"); 969610Seric if (sp == NULL) 979610Seric { 989610Seric syserr("server \"smtp\" unknown"); 9910167Seric goto severe; 1009610Seric } 1019610Seric SendmailAddress.sin_family = AF_INET; 1029610Seric SendmailAddress.sin_addr.s_addr = INADDR_ANY; 1039740Ssam SendmailAddress.sin_port = sp->s_port; 1049610Seric 1059610Seric /* 1069610Seric ** Try to actually open the connection. 1079610Seric */ 1089610Seric 1099610Seric # ifdef DEBUG 1109610Seric if (tTd(15, 1)) 1119610Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 1129610Seric # endif DEBUG 1139610Seric 1149610Seric /* get a socket for the SMTP connection */ 11523120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 11610206Seric if (DaemonSocket < 0) 1179610Seric { 1189610Seric /* probably another daemon already */ 1199610Seric syserr("getrequests: can't create socket"); 1209610Seric severe: 1219610Seric # ifdef LOG 1229610Seric if (LogLevel > 0) 12324858Seric syslog(LOG_ALERT, "cannot get connection"); 1249610Seric # endif LOG 1259610Seric finis(); 1269610Seric } 12710347Seric 12810347Seric #ifdef DEBUG 12910347Seric /* turn on network debugging? */ 13010347Seric if (tTd(15, 15)) 13124945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 13210347Seric #endif DEBUG 13310347Seric 13425027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 13525027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 13625027Seric 13723120Seric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0) 1389610Seric { 1399610Seric syserr("getrequests: cannot bind"); 14010206Seric (void) close(DaemonSocket); 1419610Seric goto severe; 1429610Seric } 14323120Seric if (listen(DaemonSocket, 10) < 0) 14423120Seric { 14523120Seric syserr("getrequests: cannot listen"); 14623120Seric (void) close(DaemonSocket); 14723120Seric goto severe; 14823120Seric } 1499610Seric 15024955Seric (void) signal(SIGCHLD, reapchild); 15124945Seric 1529610Seric # ifdef DEBUG 1539610Seric if (tTd(15, 1)) 15410206Seric printf("getrequests: %d\n", DaemonSocket); 1559610Seric # endif DEBUG 1569610Seric 1574631Seric for (;;) 1584631Seric { 15914875Seric register int pid; 16011147Seric auto int lotherend; 16111147Seric struct sockaddr_in otherend; 16214875Seric extern int RefuseLA; 16311147Seric 16414875Seric /* see if we are rejecting connections */ 16514875Seric while (getla() > RefuseLA) 16614875Seric sleep(5); 16714875Seric 1689610Seric /* wait for a connection */ 1699610Seric do 1709610Seric { 1719610Seric errno = 0; 1729610Seric lotherend = sizeof otherend; 17323120Seric t = accept(DaemonSocket, &otherend, &lotherend); 1749610Seric } while (t < 0 && errno == EINTR); 1759610Seric if (t < 0) 1765978Seric { 1779610Seric syserr("getrequests: accept"); 1789610Seric sleep(5); 1799610Seric continue; 1805978Seric } 1814631Seric 1825978Seric /* 1835978Seric ** Create a subprocess to process the mail. 1845978Seric */ 1855978Seric 1865978Seric # ifdef DEBUG 1877677Seric if (tTd(15, 2)) 1889610Seric printf("getrequests: forking (fd = %d)\n", t); 1895978Seric # endif DEBUG 1905978Seric 1914636Seric pid = fork(); 1924636Seric if (pid < 0) 1934631Seric { 1944636Seric syserr("daemon: cannot fork"); 1954636Seric sleep(10); 1969610Seric (void) close(t); 1974636Seric continue; 1984631Seric } 1994631Seric 2004636Seric if (pid == 0) 2014631Seric { 20211147Seric extern struct hostent *gethostbyaddr(); 20311147Seric register struct hostent *hp; 20411147Seric char buf[MAXNAME]; 20511147Seric 2064636Seric /* 2074636Seric ** CHILD -- return to caller. 20811147Seric ** Collect verified idea of sending host. 2094636Seric ** Verify calling user id if possible here. 2104636Seric */ 2114631Seric 21224955Seric (void) signal(SIGCHLD, SIG_DFL); 21324950Seric 21411147Seric /* determine host name */ 21525616Seric hp = gethostbyaddr((char *) &otherend.sin_addr, sizeof otherend.sin_addr, AF_INET); 21611147Seric if (hp != NULL) 21716890Seric { 21823104Seric (void) strcpy(buf, hp->h_name); 21916890Seric if (NetName != NULL && NetName[0] != '\0' && 22016894Seric index(hp->h_name, '.') == NULL) 22116890Seric { 22223104Seric (void) strcat(buf, "."); 22323104Seric (void) strcat(buf, NetName); 22416890Seric } 22516890Seric } 22611147Seric else 22716884Seric { 22816884Seric extern char *inet_ntoa(); 22916884Seric 23016884Seric /* produce a dotted quad */ 23116884Seric (void) sprintf(buf, "[%s]", 23216884Seric inet_ntoa(otherend.sin_addr)); 23316884Seric } 23416884Seric 23516884Seric /* should we check for illegal connection here? XXX */ 23616884Seric 23711147Seric RealHostName = newstr(buf); 23811147Seric 23910206Seric (void) close(DaemonSocket); 2409610Seric InChannel = fdopen(t, "r"); 24121062Seric OutChannel = fdopen(dup(t), "w"); 2425978Seric # ifdef DEBUG 2437677Seric if (tTd(15, 2)) 2445978Seric printf("getreq: returning\n"); 2455978Seric # endif DEBUG 2467876Seric # ifdef LOG 2477876Seric if (LogLevel > 11) 2487876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2497876Seric # endif LOG 2504636Seric return; 2514631Seric } 2524631Seric 2537117Seric /* close the port so that others will hang (for a while) */ 2549610Seric (void) close(t); 2554631Seric } 2569886Seric /*NOTREACHED*/ 2574631Seric } 2585978Seric /* 25910206Seric ** CLRDAEMON -- reset the daemon connection 26010206Seric ** 26110206Seric ** Parameters: 26210206Seric ** none. 26310206Seric ** 26410206Seric ** Returns: 26510206Seric ** none. 26610206Seric ** 26710206Seric ** Side Effects: 26810206Seric ** releases any resources used by the passive daemon. 26910206Seric */ 27010206Seric 27110206Seric clrdaemon() 27210206Seric { 27310206Seric if (DaemonSocket >= 0) 27410206Seric (void) close(DaemonSocket); 27510206Seric DaemonSocket = -1; 27610206Seric } 27710206Seric /* 2786039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2796039Seric ** 2806039Seric ** Parameters: 2816039Seric ** host -- the name of the host. 2826633Seric ** port -- the port number to connect to. 2836039Seric ** outfile -- a pointer to a place to put the outfile 2846039Seric ** descriptor. 2856039Seric ** infile -- ditto for infile. 2866039Seric ** 2876039Seric ** Returns: 2886039Seric ** An exit code telling whether the connection could be 2896039Seric ** made and if not why not. 2906039Seric ** 2916039Seric ** Side Effects: 2926039Seric ** none. 2936039Seric */ 2945978Seric 29525475Smiriam int h_errno; /*this will go away when code implemented*/ 29625475Smiriam 2976633Seric makeconnection(host, port, outfile, infile) 2986039Seric char *host; 2997286Seric u_short port; 3006039Seric FILE **outfile; 3016039Seric FILE **infile; 3026039Seric { 303*29430Sbloom register int i, s; 304*29430Sbloom register struct hostent *hp = (struct hostent *)NULL; 305*29430Sbloom extern char *inet_ntoa(); 30627744Sbloom int sav_errno; 3076039Seric 3086039Seric /* 3096039Seric ** Set up the address for the mailer. 3109308Seric ** Accept "[a.b.c.d]" syntax for host name. 3116039Seric */ 3126039Seric 31325475Smiriam h_errno = 0; 31425475Smiriam errno = 0; 31525475Smiriam 3169308Seric if (host[0] == '[') 3179308Seric { 31811147Seric long hid; 31911147Seric register char *p = index(host, ']'); 3209308Seric 32111147Seric if (p != NULL) 3229308Seric { 32311147Seric *p = '\0'; 32411147Seric hid = inet_addr(&host[1]); 32511147Seric *p = ']'; 3269308Seric } 32711147Seric if (p == NULL || hid == -1) 3289308Seric { 3299308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3309308Seric return (EX_NOHOST); 3319308Seric } 3329308Seric SendmailAddress.sin_addr.s_addr = hid; 3339308Seric } 3349610Seric else 3359610Seric { 336*29430Sbloom hp = gethostbyname(host); 33725475Smiriam if (hp == NULL) 33824945Seric { 33925475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 34025475Smiriam return (EX_TEMPFAIL); 34125657Seric 34225657Seric /* 34325657Seric ** XXX Should look for mail forwarder record here 34425657Seric ** XXX if (h_errno == NO_ADDRESS). 34525657Seric */ 34625657Seric 34725475Smiriam return (EX_NOHOST); 34824945Seric } 34916884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 350*29430Sbloom i = 1; 3519610Seric } 3529610Seric 3539610Seric /* 3549610Seric ** Determine the port number. 3559610Seric */ 3569610Seric 35710011Seric if (port != 0) 35810011Seric SendmailAddress.sin_port = htons(port); 35910011Seric else 3609610Seric { 3619610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3629610Seric 3639610Seric if (sp == NULL) 3649610Seric { 3659610Seric syserr("makeconnection: server \"smtp\" unknown"); 3669610Seric return (EX_OSFILE); 3679610Seric } 36810011Seric SendmailAddress.sin_port = sp->s_port; 3699610Seric } 3706039Seric 3716039Seric /* 3726039Seric ** Try to actually open the connection. 3736039Seric */ 3746039Seric 375*29430Sbloom again: 3766039Seric # ifdef DEBUG 3777677Seric if (tTd(16, 1)) 378*29430Sbloom printf("makeconnection (%s [%s])\n", host, 379*29430Sbloom inet_ntoa(SendmailAddress.sin_addr.s_addr)); 3806039Seric # endif DEBUG 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 3906039Seric # ifdef DEBUG 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 } 4006039Seric # endif DEBUG 4019546Seric (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); 408*29430Sbloom if (hp && hp->h_addr_list[i]) 409*29430Sbloom { 410*29430Sbloom bcopy(hp->h_addr_list[i++], 411*29430Sbloom (char *)&SendmailAddress.sin_addr, hp->h_length); 412*29430Sbloom goto again; 413*29430Sbloom } 414*29430Sbloom 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: 4426039Seric return (EX_UNAVAILABLE); 4436039Seric } 4446039Seric } 4456039Seric 4466039Seric /* connection ok, put it into canonical form */ 4476039Seric *outfile = fdopen(s, "w"); 4486039Seric *infile = fdopen(s, "r"); 4496039Seric 45010098Seric return (EX_OK); 4516039Seric } 45210758Seric /* 45310758Seric ** MYHOSTNAME -- return the name of this host. 45410758Seric ** 45510758Seric ** Parameters: 45610758Seric ** hostbuf -- a place to return the name of this host. 45712313Seric ** size -- the size of hostbuf. 45810758Seric ** 45910758Seric ** Returns: 46010758Seric ** A list of aliases for this host. 46110758Seric ** 46210758Seric ** Side Effects: 46310758Seric ** none. 46410758Seric */ 4656039Seric 46610758Seric char ** 46712313Seric myhostname(hostbuf, size) 46810758Seric char hostbuf[]; 46912313Seric int size; 47010758Seric { 47110758Seric extern struct hostent *gethostbyname(); 47211147Seric struct hostent *hp; 47310758Seric 47423120Seric if (gethostname(hostbuf, size) < 0) 47523120Seric { 47623120Seric (void) strcpy(hostbuf, "localhost"); 47723120Seric } 47811147Seric hp = gethostbyname(hostbuf); 47911147Seric if (hp != NULL) 48016877Seric { 48123104Seric (void) strcpy(hostbuf, hp->h_name); 48211147Seric return (hp->h_aliases); 48316877Seric } 48410758Seric else 48510758Seric return (NULL); 48610758Seric } 48716911Seric /* 48816911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 48916911Seric ** 49016911Seric ** Parameters: 49116911Seric ** hbuf -- a buffer containing a hostname. 49216911Seric ** hbsize -- the size of hbuf. 49316911Seric ** 49416911Seric ** Returns: 49516911Seric ** none. 49616911Seric ** 49716911Seric ** Side Effects: 49816911Seric ** Looks up the host specified in hbuf. If it is not 49916911Seric ** the canonical name for that host, replace it with 50016911Seric ** the canonical name. If the name is unknown, or it 50116911Seric ** is already the canonical name, leave it unchanged. 50216911Seric */ 50310758Seric 50416911Seric maphostname(hbuf, hbsize) 50516911Seric char *hbuf; 50616911Seric int hbsize; 50716911Seric { 50816911Seric register struct hostent *hp; 50916911Seric extern struct hostent *gethostbyname(); 51016911Seric 51125574Smiriam /* 51225616Seric ** If first character is a bracket, then it is an address 51325616Seric ** lookup. Address is copied into a temporary buffer to 51425616Seric ** strip the brackets and to preserve hbuf if address is 51525616Seric ** unknown. 51625616Seric */ 51725574Smiriam 51825574Smiriam if (*hbuf == '[') 51925574Smiriam { 52025574Smiriam extern struct hostent *gethostbyaddr(); 52125574Smiriam u_long in_addr; 52225574Smiriam char ptr[256]; 52325574Smiriam char *bptr; 52425574Smiriam 52525616Seric (void) strcpy(ptr, hbuf); 52625574Smiriam bptr = index(ptr,']'); 52725574Smiriam *bptr = '\0'; 52825574Smiriam in_addr = inet_addr(&ptr[1]); 52925616Seric hp = gethostbyaddr((char *) &in_addr, sizeof(struct in_addr), AF_INET); 53025616Seric if (hp == NULL) 53125616Seric return; 53225574Smiriam } 53325574Smiriam else 53425574Smiriam { 53525574Smiriam makelower(hbuf); 53625574Smiriam hp = gethostbyname(hbuf); 53725574Smiriam } 53816911Seric if (hp != NULL) 53916911Seric { 54016911Seric int i = strlen(hp->h_name); 54116911Seric 54216911Seric if (i >= hbsize) 54316911Seric hp->h_name[--i] = '\0'; 54423104Seric (void) strcpy(hbuf, hp->h_name); 54516911Seric } 54616911Seric } 54716911Seric 54810758Seric # else DAEMON 54916911Seric /* code for systems without sophisticated networking */ 55010758Seric 55110758Seric /* 55210758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 55311297Seric ** 55411297Seric ** Can't convert to upper case here because might be a UUCP name. 55512313Seric ** 55612313Seric ** Mark, you can change this to be anything you want...... 55710758Seric */ 55810758Seric 55910758Seric char ** 56012313Seric myhostname(hostbuf, size) 56110758Seric char hostbuf[]; 56212313Seric int size; 56310758Seric { 56410758Seric register FILE *f; 56510758Seric 56610758Seric hostbuf[0] = '\0'; 56710758Seric f = fopen("/usr/include/whoami", "r"); 56810758Seric if (f != NULL) 56910758Seric { 57012313Seric (void) fgets(hostbuf, size, f); 57110758Seric fixcrlf(hostbuf, TRUE); 57210758Seric (void) fclose(f); 57310758Seric } 57410758Seric return (NULL); 57510758Seric } 57616911Seric /* 57716911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 57816911Seric ** 57916911Seric ** Parameters: 58016911Seric ** hbuf -- a buffer containing a hostname. 58116911Seric ** hbsize -- the size of hbuf. 58216911Seric ** 58316911Seric ** Returns: 58416911Seric ** none. 58516911Seric ** 58616911Seric ** Side Effects: 58716911Seric ** Looks up the host specified in hbuf. If it is not 58816911Seric ** the canonical name for that host, replace it with 58916911Seric ** the canonical name. If the name is unknown, or it 59016911Seric ** is already the canonical name, leave it unchanged. 59116911Seric */ 59210758Seric 59316911Seric /*ARGSUSED*/ 59416911Seric maphostname(hbuf, hbsize) 59516911Seric char *hbuf; 59616911Seric int hbsize; 59716911Seric { 59816911Seric return; 59916911Seric } 60016911Seric 6015978Seric #endif DAEMON 602