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*25574Smiriam static char SccsId[] = "@(#)daemon.c 5.14 (Berkeley) 12/09/85 (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*25574Smiriam static char SccsId[] = "@(#)daemon.c 5.14 (Berkeley) 12/09/85 (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. 577556Seric ** 587556Seric ** The semantics of both of these should be clean. 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 # ifdef DEBUG 1099610Seric if (tTd(15, 1)) 1109610Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 1119610Seric # endif DEBUG 1129610Seric 1139610Seric /* get a socket for the SMTP connection */ 11423120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 11510206Seric if (DaemonSocket < 0) 1169610Seric { 1179610Seric /* probably another daemon already */ 1189610Seric syserr("getrequests: can't create socket"); 1199610Seric severe: 1209610Seric # ifdef LOG 1219610Seric if (LogLevel > 0) 12224858Seric syslog(LOG_ALERT, "cannot get connection"); 1239610Seric # endif LOG 1249610Seric finis(); 1259610Seric } 12610347Seric 12710347Seric #ifdef DEBUG 12810347Seric /* turn on network debugging? */ 12910347Seric if (tTd(15, 15)) 13024945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 13110347Seric #endif DEBUG 13210347Seric 13325027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 13425027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 13525027Seric 13623120Seric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0) 1379610Seric { 1389610Seric syserr("getrequests: cannot bind"); 13910206Seric (void) close(DaemonSocket); 1409610Seric goto severe; 1419610Seric } 14223120Seric if (listen(DaemonSocket, 10) < 0) 14323120Seric { 14423120Seric syserr("getrequests: cannot listen"); 14523120Seric (void) close(DaemonSocket); 14623120Seric goto severe; 14723120Seric } 1489610Seric 14924955Seric (void) signal(SIGCHLD, reapchild); 15024945Seric 1519610Seric # ifdef DEBUG 1529610Seric if (tTd(15, 1)) 15310206Seric printf("getrequests: %d\n", DaemonSocket); 1549610Seric # endif DEBUG 1559610Seric 1564631Seric for (;;) 1574631Seric { 15814875Seric register int pid; 15911147Seric auto int lotherend; 16011147Seric struct sockaddr_in otherend; 16114875Seric extern int RefuseLA; 16211147Seric 16314875Seric /* see if we are rejecting connections */ 16414875Seric while (getla() > RefuseLA) 16514875Seric sleep(5); 16614875Seric 1679610Seric /* wait for a connection */ 1689610Seric do 1699610Seric { 1709610Seric errno = 0; 1719610Seric lotherend = sizeof otherend; 17223120Seric t = accept(DaemonSocket, &otherend, &lotherend); 1739610Seric } while (t < 0 && errno == EINTR); 1749610Seric if (t < 0) 1755978Seric { 1769610Seric syserr("getrequests: accept"); 1779610Seric sleep(5); 1789610Seric continue; 1795978Seric } 1804631Seric 1815978Seric /* 1825978Seric ** Create a subprocess to process the mail. 1835978Seric */ 1845978Seric 1855978Seric # ifdef DEBUG 1867677Seric if (tTd(15, 2)) 1879610Seric printf("getrequests: forking (fd = %d)\n", t); 1885978Seric # endif DEBUG 1895978Seric 1904636Seric pid = fork(); 1914636Seric if (pid < 0) 1924631Seric { 1934636Seric syserr("daemon: cannot fork"); 1944636Seric sleep(10); 1959610Seric (void) close(t); 1964636Seric continue; 1974631Seric } 1984631Seric 1994636Seric if (pid == 0) 2004631Seric { 20111147Seric extern struct hostent *gethostbyaddr(); 20211147Seric register struct hostent *hp; 20311147Seric char buf[MAXNAME]; 20411147Seric 2054636Seric /* 2064636Seric ** CHILD -- return to caller. 20711147Seric ** Collect verified idea of sending host. 2084636Seric ** Verify calling user id if possible here. 2094636Seric */ 2104631Seric 21124955Seric (void) signal(SIGCHLD, SIG_DFL); 21224950Seric 21311147Seric /* determine host name */ 21411147Seric hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET); 21511147Seric if (hp != NULL) 21616890Seric { 21723104Seric (void) strcpy(buf, hp->h_name); 21816890Seric if (NetName != NULL && NetName[0] != '\0' && 21916894Seric index(hp->h_name, '.') == NULL) 22016890Seric { 22123104Seric (void) strcat(buf, "."); 22223104Seric (void) strcat(buf, NetName); 22316890Seric } 22416890Seric } 22511147Seric else 22616884Seric { 22716884Seric extern char *inet_ntoa(); 22816884Seric 22916884Seric /* produce a dotted quad */ 23016884Seric (void) sprintf(buf, "[%s]", 23116884Seric inet_ntoa(otherend.sin_addr)); 23216884Seric } 23316884Seric 23416884Seric /* should we check for illegal connection here? XXX */ 23516884Seric 23611147Seric RealHostName = newstr(buf); 23711147Seric 23810206Seric (void) close(DaemonSocket); 2399610Seric InChannel = fdopen(t, "r"); 24021062Seric OutChannel = fdopen(dup(t), "w"); 2415978Seric # ifdef DEBUG 2427677Seric if (tTd(15, 2)) 2435978Seric printf("getreq: returning\n"); 2445978Seric # endif DEBUG 2457876Seric # ifdef LOG 2467876Seric if (LogLevel > 11) 2477876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2487876Seric # endif LOG 2494636Seric return; 2504631Seric } 2514631Seric 2527117Seric /* close the port so that others will hang (for a while) */ 2539610Seric (void) close(t); 2544631Seric } 2559886Seric /*NOTREACHED*/ 2564631Seric } 2575978Seric /* 25824945Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 25924945Seric ** 26024945Seric ** Parameters: 26124945Seric ** none. 26224945Seric ** 26324945Seric ** Returns: 26424945Seric ** none. 26524945Seric ** 26624945Seric ** Side Effects: 26724945Seric ** Picks up zombies. 26824945Seric */ 26924945Seric 27024945Seric reapchild() 27124945Seric { 27224945Seric union wait status; 27324945Seric 27424945Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 27524945Seric continue; 27624945Seric } 27724945Seric /* 27810206Seric ** CLRDAEMON -- reset the daemon connection 27910206Seric ** 28010206Seric ** Parameters: 28110206Seric ** none. 28210206Seric ** 28310206Seric ** Returns: 28410206Seric ** none. 28510206Seric ** 28610206Seric ** Side Effects: 28710206Seric ** releases any resources used by the passive daemon. 28810206Seric */ 28910206Seric 29010206Seric clrdaemon() 29110206Seric { 29210206Seric if (DaemonSocket >= 0) 29310206Seric (void) close(DaemonSocket); 29410206Seric DaemonSocket = -1; 29510206Seric } 29610206Seric /* 2976039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2986039Seric ** 2996039Seric ** Parameters: 3006039Seric ** host -- the name of the host. 3016633Seric ** port -- the port number to connect to. 3026039Seric ** outfile -- a pointer to a place to put the outfile 3036039Seric ** descriptor. 3046039Seric ** infile -- ditto for infile. 3056039Seric ** 3066039Seric ** Returns: 3076039Seric ** An exit code telling whether the connection could be 3086039Seric ** made and if not why not. 3096039Seric ** 3106039Seric ** Side Effects: 3116039Seric ** none. 3126039Seric */ 3135978Seric 31425475Smiriam int h_errno; /*this will go away when code implemented*/ 31525475Smiriam 3166633Seric makeconnection(host, port, outfile, infile) 3176039Seric char *host; 3187286Seric u_short port; 3196039Seric FILE **outfile; 3206039Seric FILE **infile; 3216039Seric { 3226039Seric register int s; 3236039Seric 3246039Seric /* 3256039Seric ** Set up the address for the mailer. 3269308Seric ** Accept "[a.b.c.d]" syntax for host name. 3276039Seric */ 3286039Seric 32925475Smiriam h_errno = 0; 33025475Smiriam errno = 0; 33125475Smiriam 3329308Seric if (host[0] == '[') 3339308Seric { 33411147Seric long hid; 33511147Seric register char *p = index(host, ']'); 3369308Seric 33711147Seric if (p != NULL) 3389308Seric { 33911147Seric *p = '\0'; 34011147Seric hid = inet_addr(&host[1]); 34111147Seric *p = ']'; 3429308Seric } 34311147Seric if (p == NULL || hid == -1) 3449308Seric { 3459308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3469308Seric return (EX_NOHOST); 3479308Seric } 3489308Seric SendmailAddress.sin_addr.s_addr = hid; 3499308Seric } 3509610Seric else 3519610Seric { 3529610Seric register struct hostent *hp = gethostbyname(host); 3539610Seric 35425475Smiriam if (hp == NULL) 35524945Seric { 35625475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 35725475Smiriam { 35825475Smiriam CurEnv->e_flags &= ~EF_FATALERRS; 35925475Smiriam return (EX_TEMPFAIL); 36025475Smiriam } 36125475Smiriam #ifdef notdef 36225475Smiriam if (h_errno == NO_ADDRESS) 36325475Smiriam ; /*look for mail forwarder records*/ 36425475Smiriam #endif notdef 36525475Smiriam return (EX_NOHOST); 36624945Seric } 36716884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 3689610Seric } 3699610Seric 3709610Seric /* 3719610Seric ** Determine the port number. 3729610Seric */ 3739610Seric 37410011Seric if (port != 0) 37510011Seric SendmailAddress.sin_port = htons(port); 37610011Seric else 3779610Seric { 3789610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3799610Seric 3809610Seric if (sp == NULL) 3819610Seric { 3829610Seric syserr("makeconnection: server \"smtp\" unknown"); 3839610Seric return (EX_OSFILE); 3849610Seric } 38510011Seric SendmailAddress.sin_port = sp->s_port; 3869610Seric } 3876039Seric 3886039Seric /* 3896039Seric ** Try to actually open the connection. 3906039Seric */ 3916039Seric 3926039Seric # ifdef DEBUG 3937677Seric if (tTd(16, 1)) 3946039Seric printf("makeconnection (%s)\n", host); 3956039Seric # endif DEBUG 3966039Seric 39723120Seric s = socket(AF_INET, SOCK_STREAM, 0); 3986039Seric if (s < 0) 3996039Seric { 4006039Seric syserr("makeconnection: no socket"); 4016039Seric goto failure; 4026039Seric } 4036039Seric 4046039Seric # ifdef DEBUG 4057677Seric if (tTd(16, 1)) 4066039Seric printf("makeconnection: %d\n", s); 40710347Seric 40810347Seric /* turn on network debugging? */ 40910347Seric if (tTd(16, 14)) 41024945Seric { 41124945Seric int on = 1; 41224945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 41324945Seric } 4146039Seric # endif DEBUG 4159546Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 41614383Seric errno = 0; /* for debugging */ 4179610Seric SendmailAddress.sin_family = AF_INET; 41823120Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0) 4196039Seric { 4206039Seric /* failure, decide if temporary or not */ 4216039Seric failure: 4226039Seric switch (errno) 4236039Seric { 4246039Seric case EISCONN: 4256039Seric case ETIMEDOUT: 4266897Seric case EINPROGRESS: 4276897Seric case EALREADY: 4286897Seric case EADDRINUSE: 42910123Seric case EHOSTDOWN: 4306897Seric case ENETDOWN: 4316897Seric case ENETRESET: 4326897Seric case ENOBUFS: 4337204Seric case ECONNREFUSED: 43411546Seric case ECONNRESET: 43510081Seric case EHOSTUNREACH: 43610098Seric case ENETUNREACH: 4376039Seric /* there are others, I'm sure..... */ 43816884Seric CurEnv->e_flags &= ~EF_FATALERRS; 4396039Seric return (EX_TEMPFAIL); 4406039Seric 44111147Seric case EPERM: 44211147Seric /* why is this happening? */ 44311147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 44411147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 44514383Seric return (EX_TEMPFAIL); 44611147Seric 4476039Seric default: 4486039Seric return (EX_UNAVAILABLE); 4496039Seric } 4506039Seric } 4516039Seric 4526039Seric /* connection ok, put it into canonical form */ 4536039Seric *outfile = fdopen(s, "w"); 4546039Seric *infile = fdopen(s, "r"); 4556039Seric 45610098Seric return (EX_OK); 4576039Seric } 45810758Seric /* 45910758Seric ** MYHOSTNAME -- return the name of this host. 46010758Seric ** 46110758Seric ** Parameters: 46210758Seric ** hostbuf -- a place to return the name of this host. 46312313Seric ** size -- the size of hostbuf. 46410758Seric ** 46510758Seric ** Returns: 46610758Seric ** A list of aliases for this host. 46710758Seric ** 46810758Seric ** Side Effects: 46910758Seric ** none. 47010758Seric */ 4716039Seric 47210758Seric char ** 47312313Seric myhostname(hostbuf, size) 47410758Seric char hostbuf[]; 47512313Seric int size; 47610758Seric { 47710758Seric extern struct hostent *gethostbyname(); 47811147Seric struct hostent *hp; 47910758Seric 48023120Seric if (gethostname(hostbuf, size) < 0) 48123120Seric { 48223120Seric (void) strcpy(hostbuf, "localhost"); 48323120Seric } 48411147Seric hp = gethostbyname(hostbuf); 48511147Seric if (hp != NULL) 48616877Seric { 48723104Seric (void) strcpy(hostbuf, hp->h_name); 48811147Seric return (hp->h_aliases); 48916877Seric } 49010758Seric else 49110758Seric return (NULL); 49210758Seric } 49316911Seric /* 49416911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 49516911Seric ** 49616911Seric ** Parameters: 49716911Seric ** hbuf -- a buffer containing a hostname. 49816911Seric ** hbsize -- the size of hbuf. 49916911Seric ** 50016911Seric ** Returns: 50116911Seric ** none. 50216911Seric ** 50316911Seric ** Side Effects: 50416911Seric ** Looks up the host specified in hbuf. If it is not 50516911Seric ** the canonical name for that host, replace it with 50616911Seric ** the canonical name. If the name is unknown, or it 50716911Seric ** is already the canonical name, leave it unchanged. 50816911Seric */ 50910758Seric 51016911Seric maphostname(hbuf, hbsize) 51116911Seric char *hbuf; 51216911Seric int hbsize; 51316911Seric { 51416911Seric register struct hostent *hp; 51516911Seric extern struct hostent *gethostbyname(); 51616911Seric 517*25574Smiriam /* 518*25574Smiriam * If first character is a bracket, then it is an address 519*25574Smiriam * lookup. Address is copied into a temporary buffer to 520*25574Smiriam * strip the brackets and to preserve hbuf if address is 521*25574Smiriam * unknown. 522*25574Smiriam */ 523*25574Smiriam 524*25574Smiriam if (*hbuf == '[') 525*25574Smiriam { 526*25574Smiriam extern struct hostent *gethostbyaddr(); 527*25574Smiriam u_long in_addr; 528*25574Smiriam char ptr[256]; 529*25574Smiriam char *bptr; 530*25574Smiriam 531*25574Smiriam strcpy(ptr,hbuf); 532*25574Smiriam bptr = index(ptr,']'); 533*25574Smiriam *bptr = '\0'; 534*25574Smiriam in_addr = inet_addr(&ptr[1]); 535*25574Smiriam hp = gethostbyaddr(&in_addr, sizeof(struct in_addr), AF_INET); 536*25574Smiriam if(hp == NULL) 537*25574Smiriam return(NULL); 538*25574Smiriam } 539*25574Smiriam else 540*25574Smiriam { 541*25574Smiriam makelower(hbuf); 542*25574Smiriam hp = gethostbyname(hbuf); 543*25574Smiriam } 54416911Seric if (hp != NULL) 54516911Seric { 54616911Seric int i = strlen(hp->h_name); 54716911Seric 54816911Seric if (i >= hbsize) 54916911Seric hp->h_name[--i] = '\0'; 55023104Seric (void) strcpy(hbuf, hp->h_name); 55116911Seric } 55216911Seric } 55316911Seric 55410758Seric # else DAEMON 55516911Seric /* code for systems without sophisticated networking */ 55610758Seric 55710758Seric /* 55810758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 55911297Seric ** 56011297Seric ** Can't convert to upper case here because might be a UUCP name. 56112313Seric ** 56212313Seric ** Mark, you can change this to be anything you want...... 56310758Seric */ 56410758Seric 56510758Seric char ** 56612313Seric myhostname(hostbuf, size) 56710758Seric char hostbuf[]; 56812313Seric int size; 56910758Seric { 57010758Seric register FILE *f; 57110758Seric 57210758Seric hostbuf[0] = '\0'; 57310758Seric f = fopen("/usr/include/whoami", "r"); 57410758Seric if (f != NULL) 57510758Seric { 57612313Seric (void) fgets(hostbuf, size, f); 57710758Seric fixcrlf(hostbuf, TRUE); 57810758Seric (void) fclose(f); 57910758Seric } 58010758Seric return (NULL); 58110758Seric } 58216911Seric /* 58316911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 58416911Seric ** 58516911Seric ** Parameters: 58616911Seric ** hbuf -- a buffer containing a hostname. 58716911Seric ** hbsize -- the size of hbuf. 58816911Seric ** 58916911Seric ** Returns: 59016911Seric ** none. 59116911Seric ** 59216911Seric ** Side Effects: 59316911Seric ** Looks up the host specified in hbuf. If it is not 59416911Seric ** the canonical name for that host, replace it with 59516911Seric ** the canonical name. If the name is unknown, or it 59616911Seric ** is already the canonical name, leave it unchanged. 59716911Seric */ 59810758Seric 59916911Seric /*ARGSUSED*/ 60016911Seric maphostname(hbuf, hbsize) 60116911Seric char *hbuf; 60216911Seric int hbsize; 60316911Seric { 60416911Seric return; 60516911Seric } 60616911Seric 60716911Seric 6085978Seric #endif DAEMON 609