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*35651Seric static char sccsid[] = "@(#)daemon.c 5.27 (Berkeley) 09/20/88 (with daemon mode)"; 2533780Sbostic #else 26*35651Seric static char sccsid[] = "@(#)daemon.c 5.27 (Berkeley) 09/20/88 (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 # ifdef DEBUG 1179610Seric if (tTd(15, 1)) 1189610Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 1199610Seric # endif DEBUG 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 #ifdef DEBUG 13610347Seric /* turn on network debugging? */ 13710347Seric if (tTd(15, 15)) 13824945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 13910347Seric #endif DEBUG 14010347Seric 14125027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 14225027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 14325027Seric 14423120Seric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0) 1459610Seric { 1469610Seric syserr("getrequests: cannot bind"); 14710206Seric (void) close(DaemonSocket); 1489610Seric goto severe; 1499610Seric } 15023120Seric if (listen(DaemonSocket, 10) < 0) 15123120Seric { 15223120Seric syserr("getrequests: cannot listen"); 15323120Seric (void) close(DaemonSocket); 15423120Seric goto severe; 15523120Seric } 1569610Seric 15724955Seric (void) signal(SIGCHLD, reapchild); 15824945Seric 1599610Seric # ifdef DEBUG 1609610Seric if (tTd(15, 1)) 16110206Seric printf("getrequests: %d\n", DaemonSocket); 1629610Seric # endif DEBUG 1639610Seric 1644631Seric for (;;) 1654631Seric { 16614875Seric register int pid; 16711147Seric auto int lotherend; 16811147Seric struct sockaddr_in otherend; 16914875Seric extern int RefuseLA; 17011147Seric 17114875Seric /* see if we are rejecting connections */ 17214875Seric while (getla() > RefuseLA) 17314875Seric sleep(5); 17414875Seric 1759610Seric /* wait for a connection */ 1769610Seric do 1779610Seric { 1789610Seric errno = 0; 1799610Seric lotherend = sizeof otherend; 18023120Seric t = accept(DaemonSocket, &otherend, &lotherend); 1819610Seric } while (t < 0 && errno == EINTR); 1829610Seric if (t < 0) 1835978Seric { 1849610Seric syserr("getrequests: accept"); 1859610Seric sleep(5); 1869610Seric continue; 1875978Seric } 1884631Seric 1895978Seric /* 1905978Seric ** Create a subprocess to process the mail. 1915978Seric */ 1925978Seric 1935978Seric # ifdef DEBUG 1947677Seric if (tTd(15, 2)) 1959610Seric printf("getrequests: forking (fd = %d)\n", t); 1965978Seric # endif DEBUG 1975978Seric 1984636Seric pid = fork(); 1994636Seric if (pid < 0) 2004631Seric { 2014636Seric syserr("daemon: cannot fork"); 2024636Seric sleep(10); 2039610Seric (void) close(t); 2044636Seric continue; 2054631Seric } 2064631Seric 2074636Seric if (pid == 0) 2084631Seric { 20911147Seric extern struct hostent *gethostbyaddr(); 21011147Seric register struct hostent *hp; 21111147Seric char buf[MAXNAME]; 21211147Seric 2134636Seric /* 2144636Seric ** CHILD -- return to caller. 21511147Seric ** Collect verified idea of sending host. 2164636Seric ** Verify calling user id if possible here. 2174636Seric */ 2184631Seric 21924955Seric (void) signal(SIGCHLD, SIG_DFL); 22024950Seric 22111147Seric /* determine host name */ 22225616Seric hp = gethostbyaddr((char *) &otherend.sin_addr, sizeof otherend.sin_addr, AF_INET); 22311147Seric if (hp != NULL) 22416890Seric { 22523104Seric (void) strcpy(buf, hp->h_name); 22616890Seric if (NetName != NULL && NetName[0] != '\0' && 22716894Seric index(hp->h_name, '.') == NULL) 22816890Seric { 22923104Seric (void) strcat(buf, "."); 23023104Seric (void) strcat(buf, NetName); 23116890Seric } 23216890Seric } 23311147Seric else 23416884Seric { 23516884Seric extern char *inet_ntoa(); 23616884Seric 23716884Seric /* produce a dotted quad */ 23816884Seric (void) sprintf(buf, "[%s]", 23916884Seric inet_ntoa(otherend.sin_addr)); 24016884Seric } 24116884Seric 24216884Seric /* should we check for illegal connection here? XXX */ 24316884Seric 24411147Seric RealHostName = newstr(buf); 24511147Seric 24610206Seric (void) close(DaemonSocket); 2479610Seric InChannel = fdopen(t, "r"); 24821062Seric OutChannel = fdopen(dup(t), "w"); 2495978Seric # ifdef DEBUG 2507677Seric if (tTd(15, 2)) 2515978Seric printf("getreq: returning\n"); 2525978Seric # endif DEBUG 2537876Seric # ifdef LOG 2547876Seric if (LogLevel > 11) 2557876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2567876Seric # endif LOG 2574636Seric return; 2584631Seric } 2594631Seric 2607117Seric /* close the port so that others will hang (for a while) */ 2619610Seric (void) close(t); 2624631Seric } 2639886Seric /*NOTREACHED*/ 2644631Seric } 2655978Seric /* 26610206Seric ** CLRDAEMON -- reset the daemon connection 26710206Seric ** 26810206Seric ** Parameters: 26910206Seric ** none. 27010206Seric ** 27110206Seric ** Returns: 27210206Seric ** none. 27310206Seric ** 27410206Seric ** Side Effects: 27510206Seric ** releases any resources used by the passive daemon. 27610206Seric */ 27710206Seric 27810206Seric clrdaemon() 27910206Seric { 28010206Seric if (DaemonSocket >= 0) 28110206Seric (void) close(DaemonSocket); 28210206Seric DaemonSocket = -1; 28310206Seric } 28410206Seric /* 2856039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2866039Seric ** 2876039Seric ** Parameters: 2886039Seric ** host -- the name of the host. 2896633Seric ** port -- the port number to connect to. 2906039Seric ** outfile -- a pointer to a place to put the outfile 2916039Seric ** descriptor. 2926039Seric ** infile -- ditto for infile. 2936039Seric ** 2946039Seric ** Returns: 2956039Seric ** An exit code telling whether the connection could be 2966039Seric ** made and if not why not. 2976039Seric ** 2986039Seric ** Side Effects: 2996039Seric ** none. 3006039Seric */ 3015978Seric 3026633Seric makeconnection(host, port, outfile, infile) 3036039Seric char *host; 3047286Seric u_short port; 3056039Seric FILE **outfile; 3066039Seric FILE **infile; 3076039Seric { 30829430Sbloom register int i, s; 30929430Sbloom register struct hostent *hp = (struct hostent *)NULL; 31029430Sbloom extern char *inet_ntoa(); 31127744Sbloom int sav_errno; 312*35651Seric #ifdef NAMED_BIND 313*35651Seric extern int h_errno; 314*35651Seric #endif 3156039Seric 3166039Seric /* 3176039Seric ** Set up the address for the mailer. 3189308Seric ** Accept "[a.b.c.d]" syntax for host name. 3196039Seric */ 3206039Seric 321*35651Seric #ifdef NAMED_BIND 32225475Smiriam h_errno = 0; 323*35651Seric #endif 32425475Smiriam errno = 0; 32525475Smiriam 3269308Seric if (host[0] == '[') 3279308Seric { 32811147Seric long hid; 32911147Seric register char *p = index(host, ']'); 3309308Seric 33111147Seric if (p != NULL) 3329308Seric { 33311147Seric *p = '\0'; 33411147Seric hid = inet_addr(&host[1]); 33511147Seric *p = ']'; 3369308Seric } 33711147Seric if (p == NULL || hid == -1) 3389308Seric { 3399308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3409308Seric return (EX_NOHOST); 3419308Seric } 3429308Seric SendmailAddress.sin_addr.s_addr = hid; 3439308Seric } 3449610Seric else 3459610Seric { 34629430Sbloom hp = gethostbyname(host); 34725475Smiriam if (hp == NULL) 34824945Seric { 349*35651Seric #ifdef NAMED_BIND 35025475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 35125475Smiriam return (EX_TEMPFAIL); 35225657Seric 353*35651Seric /* if name server is specified, assume temp fail */ 354*35651Seric if (errno == ECONNREFUSED && UseNameServer) 355*35651Seric return (EX_TEMPFAIL); 356*35651Seric #endif 357*35651Seric 35825657Seric /* 35925657Seric ** XXX Should look for mail forwarder record here 36025657Seric ** XXX if (h_errno == NO_ADDRESS). 36125657Seric */ 36225657Seric 36325475Smiriam return (EX_NOHOST); 36424945Seric } 36516884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 36629430Sbloom i = 1; 3679610Seric } 3689610Seric 3699610Seric /* 3709610Seric ** Determine the port number. 3719610Seric */ 3729610Seric 37310011Seric if (port != 0) 37410011Seric SendmailAddress.sin_port = htons(port); 37510011Seric else 3769610Seric { 3779610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3789610Seric 3799610Seric if (sp == NULL) 3809610Seric { 3819610Seric syserr("makeconnection: server \"smtp\" unknown"); 3829610Seric return (EX_OSFILE); 3839610Seric } 38410011Seric SendmailAddress.sin_port = sp->s_port; 3859610Seric } 3866039Seric 3876039Seric /* 3886039Seric ** Try to actually open the connection. 3896039Seric */ 3906039Seric 39129430Sbloom again: 3926039Seric # ifdef DEBUG 3937677Seric if (tTd(16, 1)) 39429430Sbloom printf("makeconnection (%s [%s])\n", host, 39529430Sbloom inet_ntoa(SendmailAddress.sin_addr.s_addr)); 3966039Seric # endif DEBUG 3976039Seric 39823120Seric s = socket(AF_INET, SOCK_STREAM, 0); 3996039Seric if (s < 0) 4006039Seric { 4016039Seric syserr("makeconnection: no socket"); 40227744Sbloom sav_errno = errno; 4036039Seric goto failure; 4046039Seric } 4056039Seric 4066039Seric # ifdef DEBUG 4077677Seric if (tTd(16, 1)) 4086039Seric printf("makeconnection: %d\n", s); 40910347Seric 41010347Seric /* turn on network debugging? */ 41110347Seric if (tTd(16, 14)) 41224945Seric { 41324945Seric int on = 1; 41424945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 41524945Seric } 4166039Seric # endif DEBUG 4179546Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 41814383Seric errno = 0; /* for debugging */ 4199610Seric SendmailAddress.sin_family = AF_INET; 42023120Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0) 4216039Seric { 42227744Sbloom sav_errno = errno; 42327744Sbloom (void) close(s); 42429430Sbloom if (hp && hp->h_addr_list[i]) 42529430Sbloom { 42629430Sbloom bcopy(hp->h_addr_list[i++], 42729430Sbloom (char *)&SendmailAddress.sin_addr, hp->h_length); 42829430Sbloom goto again; 42929430Sbloom } 43029430Sbloom 4316039Seric /* failure, decide if temporary or not */ 4326039Seric failure: 43327744Sbloom switch (sav_errno) 4346039Seric { 4356039Seric case EISCONN: 4366039Seric case ETIMEDOUT: 4376897Seric case EINPROGRESS: 4386897Seric case EALREADY: 4396897Seric case EADDRINUSE: 44010123Seric case EHOSTDOWN: 4416897Seric case ENETDOWN: 4426897Seric case ENETRESET: 4436897Seric case ENOBUFS: 4447204Seric case ECONNREFUSED: 44511546Seric case ECONNRESET: 44610081Seric case EHOSTUNREACH: 44710098Seric case ENETUNREACH: 4486039Seric /* there are others, I'm sure..... */ 4496039Seric return (EX_TEMPFAIL); 4506039Seric 45111147Seric case EPERM: 45211147Seric /* why is this happening? */ 45311147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 45411147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 45514383Seric return (EX_TEMPFAIL); 45611147Seric 4576039Seric default: 45831953Sbostic message(Arpa_Info, "%s", errstring(sav_errno)); 4596039Seric return (EX_UNAVAILABLE); 4606039Seric } 4616039Seric } 4626039Seric 4636039Seric /* connection ok, put it into canonical form */ 4646039Seric *outfile = fdopen(s, "w"); 4656039Seric *infile = fdopen(s, "r"); 4666039Seric 46710098Seric return (EX_OK); 4686039Seric } 46910758Seric /* 47010758Seric ** MYHOSTNAME -- return the name of this host. 47110758Seric ** 47210758Seric ** Parameters: 47310758Seric ** hostbuf -- a place to return the name of this host. 47412313Seric ** size -- the size of hostbuf. 47510758Seric ** 47610758Seric ** Returns: 47710758Seric ** A list of aliases for this host. 47810758Seric ** 47910758Seric ** Side Effects: 48010758Seric ** none. 48110758Seric */ 4826039Seric 48310758Seric char ** 48412313Seric myhostname(hostbuf, size) 48510758Seric char hostbuf[]; 48612313Seric int size; 48710758Seric { 48810758Seric extern struct hostent *gethostbyname(); 48911147Seric struct hostent *hp; 49010758Seric 49123120Seric if (gethostname(hostbuf, size) < 0) 49223120Seric { 49323120Seric (void) strcpy(hostbuf, "localhost"); 49423120Seric } 49511147Seric hp = gethostbyname(hostbuf); 49611147Seric if (hp != NULL) 49716877Seric { 49823104Seric (void) strcpy(hostbuf, hp->h_name); 49911147Seric return (hp->h_aliases); 50016877Seric } 50110758Seric else 50210758Seric return (NULL); 50310758Seric } 50410758Seric 50533932Sbostic /* 50633932Sbostic * MAPHOSTNAME -- turn a hostname into canonical form 50733932Sbostic * 50833932Sbostic * Parameters: 50933932Sbostic * hbuf -- a buffer containing a hostname. 51033932Sbostic * hbsize -- the size of hbuf. 51133932Sbostic * 51233932Sbostic * Returns: 51333932Sbostic * none. 51433932Sbostic * 51533932Sbostic * Side Effects: 51633932Sbostic * Looks up the host specified in hbuf. If it is not 51733932Sbostic * the canonical name for that host, replace it with 51833932Sbostic * the canonical name. If the name is unknown, or it 51933932Sbostic * is already the canonical name, leave it unchanged. 52033932Sbostic */ 52116911Seric maphostname(hbuf, hbsize) 52216911Seric char *hbuf; 52316911Seric int hbsize; 52416911Seric { 52516911Seric register struct hostent *hp; 52633932Sbostic u_long in_addr; 52733932Sbostic char ptr[256]; 52833932Sbostic struct hostent *gethostbyaddr(); 52916911Seric 53025574Smiriam /* 53133932Sbostic * If first character is a bracket, then it is an address 53233932Sbostic * lookup. Address is copied into a temporary buffer to 53333932Sbostic * strip the brackets and to preserve hbuf if address is 53433932Sbostic * unknown. 53533932Sbostic */ 53633932Sbostic if (*hbuf != '[') { 53729654Sbloom getcanonname(hbuf, hbsize); 53829654Sbloom return; 53925574Smiriam } 54033932Sbostic *index(strcpy(ptr, hbuf), ']') = '\0'; 54133932Sbostic in_addr = inet_addr(&ptr[1]); 54233932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 54333932Sbostic if (hp == NULL) 54433932Sbostic return; 54533932Sbostic if (strlen(hp->h_name) >= hbsize) 54633932Sbostic hp->h_name[hbsize - 1] = '\0'; 54733932Sbostic (void)strcpy(hbuf, hp->h_name); 54833932Sbostic } 54916911Seric 55010758Seric # else DAEMON 55116911Seric /* code for systems without sophisticated networking */ 55210758Seric 55310758Seric /* 55410758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 55511297Seric ** 55611297Seric ** Can't convert to upper case here because might be a UUCP name. 55712313Seric ** 55812313Seric ** Mark, you can change this to be anything you want...... 55910758Seric */ 56010758Seric 56110758Seric char ** 56212313Seric myhostname(hostbuf, size) 56310758Seric char hostbuf[]; 56412313Seric int size; 56510758Seric { 56610758Seric register FILE *f; 56710758Seric 56810758Seric hostbuf[0] = '\0'; 56910758Seric f = fopen("/usr/include/whoami", "r"); 57010758Seric if (f != NULL) 57110758Seric { 57212313Seric (void) fgets(hostbuf, size, f); 57310758Seric fixcrlf(hostbuf, TRUE); 57410758Seric (void) fclose(f); 57510758Seric } 57610758Seric return (NULL); 57710758Seric } 57816911Seric /* 57916911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 58016911Seric ** 58116911Seric ** Parameters: 58216911Seric ** hbuf -- a buffer containing a hostname. 58316911Seric ** hbsize -- the size of hbuf. 58416911Seric ** 58516911Seric ** Returns: 58616911Seric ** none. 58716911Seric ** 58816911Seric ** Side Effects: 58916911Seric ** Looks up the host specified in hbuf. If it is not 59016911Seric ** the canonical name for that host, replace it with 59116911Seric ** the canonical name. If the name is unknown, or it 59216911Seric ** is already the canonical name, leave it unchanged. 59316911Seric */ 59410758Seric 59516911Seric /*ARGSUSED*/ 59616911Seric maphostname(hbuf, hbsize) 59716911Seric char *hbuf; 59816911Seric int hbsize; 59916911Seric { 60016911Seric return; 60116911Seric } 60216911Seric 6035978Seric #endif DAEMON 604