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 15*23120Seric # ifndef DAEMON 16*23120Seric # ifndef lint 17*23120Seric static char SccsId[] = "@(#)daemon.c 5.4 (Berkeley) 06/08/85 (w/o daemon mode)"; 18*23120Seric # endif not lint 19*23120Seric # else 204535Seric 21*23120Seric # include <sys/socket.h> 22*23120Seric # include <netinet/in.h> 23*23120Seric # include <netdb.h> 24*23120Seric # include <sys/wait.h> 25*23120Seric # include <sys/time.h> 26*23120Seric # include <sys/resource.h> 275978Seric 28*23120Seric # ifndef lint 29*23120Seric static char SccsId[] = "@(#)daemon.c 5.4 (Berkeley) 06/08/85 (with daemon mode)"; 30*23120Seric # endif not lint 315978Seric 324535Seric /* 334535Seric ** DAEMON.C -- routines to use when running as a daemon. 347556Seric ** 357556Seric ** This entire file is highly dependent on the 4.2 BSD 367556Seric ** interprocess communication primitives. No attempt has 377556Seric ** been made to make this file portable to Version 7, 387556Seric ** Version 6, MPX files, etc. If you should try such a 397556Seric ** thing yourself, I recommend chucking the entire file 407556Seric ** and starting from scratch. Basic semantics are: 417556Seric ** 427556Seric ** getrequests() 437556Seric ** Opens a port and initiates a connection. 447556Seric ** Returns in a child. Must set InChannel and 457556Seric ** OutChannel appropriately. 4610206Seric ** clrdaemon() 4710206Seric ** Close any open files associated with getting 4810206Seric ** the connection; this is used when running the queue, 4910206Seric ** etc., to avoid having extra file descriptors during 5010206Seric ** the queue run and to avoid confusing the network 5110206Seric ** code (if it cares). 527556Seric ** makeconnection(host, port, outfile, infile) 537556Seric ** Make a connection to the named host on the given 547556Seric ** port. Set *outfile and *infile to the files 557556Seric ** appropriate for communication. Returns zero on 567556Seric ** success, else an exit status describing the 577556Seric ** error. 587556Seric ** 597556Seric ** The semantics of both of these should be clean. 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; 877117Seric union wait status; 889610Seric register struct servent *sp; 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 */ 114*23120Seric 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) 1229610Seric syslog(LOG_SALERT, "cannot get connection"); 1239610Seric # endif LOG 1249610Seric finis(); 1259610Seric } 12610347Seric 12710347Seric #ifdef DEBUG 12810347Seric /* turn on network debugging? */ 12910347Seric if (tTd(15, 15)) 13010347Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 0, 0); 13110347Seric #endif DEBUG 13210347Seric 133*23120Seric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0) 1349610Seric { 1359610Seric syserr("getrequests: cannot bind"); 13610206Seric (void) close(DaemonSocket); 1379610Seric goto severe; 1389610Seric } 139*23120Seric if (listen(DaemonSocket, 10) < 0) 140*23120Seric { 141*23120Seric syserr("getrequests: cannot listen"); 142*23120Seric (void) close(DaemonSocket); 143*23120Seric goto severe; 144*23120Seric } 1459610Seric 1469610Seric # ifdef DEBUG 1479610Seric if (tTd(15, 1)) 14810206Seric printf("getrequests: %d\n", DaemonSocket); 1499610Seric # endif DEBUG 1509610Seric 1514631Seric for (;;) 1524631Seric { 15314875Seric register int pid; 15411147Seric auto int lotherend; 15511147Seric struct sockaddr_in otherend; 15614875Seric extern int RefuseLA; 15711147Seric 15814875Seric /* see if we are rejecting connections */ 15914875Seric while (getla() > RefuseLA) 16014875Seric sleep(5); 16114875Seric 1629610Seric /* wait for a connection */ 1639610Seric do 1649610Seric { 1659610Seric errno = 0; 1669610Seric lotherend = sizeof otherend; 167*23120Seric t = accept(DaemonSocket, &otherend, &lotherend); 1689610Seric } while (t < 0 && errno == EINTR); 1699610Seric if (t < 0) 1705978Seric { 1719610Seric syserr("getrequests: accept"); 1729610Seric sleep(5); 1739610Seric continue; 1745978Seric } 1754631Seric 1765978Seric /* 1775978Seric ** Create a subprocess to process the mail. 1785978Seric */ 1795978Seric 1805978Seric # ifdef DEBUG 1817677Seric if (tTd(15, 2)) 1829610Seric printf("getrequests: forking (fd = %d)\n", t); 1835978Seric # endif DEBUG 1845978Seric 1854636Seric pid = fork(); 1864636Seric if (pid < 0) 1874631Seric { 1884636Seric syserr("daemon: cannot fork"); 1894636Seric sleep(10); 1909610Seric (void) close(t); 1914636Seric continue; 1924631Seric } 1934631Seric 1944636Seric if (pid == 0) 1954631Seric { 19611147Seric extern struct hostent *gethostbyaddr(); 19711147Seric register struct hostent *hp; 19811147Seric extern char *RealHostName; /* srvrsmtp.c */ 19911147Seric char buf[MAXNAME]; 20011147Seric 2014636Seric /* 2024636Seric ** CHILD -- return to caller. 20311147Seric ** Collect verified idea of sending host. 2044636Seric ** Verify calling user id if possible here. 2054636Seric */ 2064631Seric 20711147Seric /* determine host name */ 20811147Seric hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET); 20911147Seric if (hp != NULL) 21016890Seric { 21123104Seric (void) strcpy(buf, hp->h_name); 21216890Seric if (NetName != NULL && NetName[0] != '\0' && 21316894Seric index(hp->h_name, '.') == NULL) 21416890Seric { 21523104Seric (void) strcat(buf, "."); 21623104Seric (void) strcat(buf, NetName); 21716890Seric } 21816890Seric } 21911147Seric else 22016884Seric { 22116884Seric extern char *inet_ntoa(); 22216884Seric 22316884Seric /* produce a dotted quad */ 22416884Seric (void) sprintf(buf, "[%s]", 22516884Seric inet_ntoa(otherend.sin_addr)); 22616884Seric } 22716884Seric 22816884Seric /* should we check for illegal connection here? XXX */ 22916884Seric 23011147Seric RealHostName = newstr(buf); 23111147Seric 23210206Seric (void) close(DaemonSocket); 2339610Seric InChannel = fdopen(t, "r"); 23421062Seric OutChannel = fdopen(dup(t), "w"); 2355978Seric # ifdef DEBUG 2367677Seric if (tTd(15, 2)) 2375978Seric printf("getreq: returning\n"); 2385978Seric # endif DEBUG 2397876Seric # ifdef LOG 2407876Seric if (LogLevel > 11) 2417876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2427876Seric # endif LOG 2434636Seric return; 2444631Seric } 2454631Seric 2464636Seric /* 2474636Seric ** PARENT -- wait for child to terminate. 2484636Seric ** Perhaps we should allow concurrent processing? 2494636Seric */ 2504631Seric 2515978Seric # ifdef DEBUG 2527677Seric if (tTd(15, 2)) 2535978Seric { 2545978Seric sleep(2); 2555978Seric printf("getreq: parent waiting\n"); 2565978Seric } 2575978Seric # endif DEBUG 2585978Seric 2597117Seric /* close the port so that others will hang (for a while) */ 2609610Seric (void) close(t); 2617117Seric 26213933Seric /* pick up old zombies */ 263*23120Seric while (wait3(&status, WNOHANG, (struct rusage *) 0) > 0) 26413933Seric continue; 2654631Seric } 2669886Seric /*NOTREACHED*/ 2674631Seric } 2685978Seric /* 26910206Seric ** CLRDAEMON -- reset the daemon connection 27010206Seric ** 27110206Seric ** Parameters: 27210206Seric ** none. 27310206Seric ** 27410206Seric ** Returns: 27510206Seric ** none. 27610206Seric ** 27710206Seric ** Side Effects: 27810206Seric ** releases any resources used by the passive daemon. 27910206Seric */ 28010206Seric 28110206Seric clrdaemon() 28210206Seric { 28310206Seric if (DaemonSocket >= 0) 28410206Seric (void) close(DaemonSocket); 28510206Seric DaemonSocket = -1; 28610206Seric } 28710206Seric /* 2886039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2896039Seric ** 2906039Seric ** Parameters: 2916039Seric ** host -- the name of the host. 2926633Seric ** port -- the port number to connect to. 2936039Seric ** outfile -- a pointer to a place to put the outfile 2946039Seric ** descriptor. 2956039Seric ** infile -- ditto for infile. 2966039Seric ** 2976039Seric ** Returns: 2986039Seric ** An exit code telling whether the connection could be 2996039Seric ** made and if not why not. 3006039Seric ** 3016039Seric ** Side Effects: 3026039Seric ** none. 3036039Seric */ 3045978Seric 3056633Seric makeconnection(host, port, outfile, infile) 3066039Seric char *host; 3077286Seric u_short port; 3086039Seric FILE **outfile; 3096039Seric FILE **infile; 3106039Seric { 3116039Seric register int s; 3126039Seric 3136039Seric /* 3146039Seric ** Set up the address for the mailer. 3159308Seric ** Accept "[a.b.c.d]" syntax for host name. 3166039Seric */ 3176039Seric 3189308Seric if (host[0] == '[') 3199308Seric { 32011147Seric long hid; 32111147Seric register char *p = index(host, ']'); 3229308Seric 32311147Seric if (p != NULL) 3249308Seric { 32511147Seric *p = '\0'; 32611147Seric hid = inet_addr(&host[1]); 32711147Seric *p = ']'; 3289308Seric } 32911147Seric if (p == NULL || hid == -1) 3309308Seric { 3319308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3329308Seric return (EX_NOHOST); 3339308Seric } 3349308Seric SendmailAddress.sin_addr.s_addr = hid; 3359308Seric } 3369610Seric else 3379610Seric { 3389610Seric register struct hostent *hp = gethostbyname(host); 3399610Seric 34011147Seric if (hp == NULL) 3419610Seric return (EX_NOHOST); 34216884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 3439610Seric } 3449610Seric 3459610Seric /* 3469610Seric ** Determine the port number. 3479610Seric */ 3489610Seric 34910011Seric if (port != 0) 35010011Seric SendmailAddress.sin_port = htons(port); 35110011Seric else 3529610Seric { 3539610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3549610Seric 3559610Seric if (sp == NULL) 3569610Seric { 3579610Seric syserr("makeconnection: server \"smtp\" unknown"); 3589610Seric return (EX_OSFILE); 3599610Seric } 36010011Seric SendmailAddress.sin_port = sp->s_port; 3619610Seric } 3626039Seric 3636039Seric /* 3646039Seric ** Try to actually open the connection. 3656039Seric */ 3666039Seric 3676039Seric # ifdef DEBUG 3687677Seric if (tTd(16, 1)) 3696039Seric printf("makeconnection (%s)\n", host); 3706039Seric # endif DEBUG 3716039Seric 372*23120Seric s = socket(AF_INET, SOCK_STREAM, 0); 3736039Seric if (s < 0) 3746039Seric { 3756039Seric syserr("makeconnection: no socket"); 3766039Seric goto failure; 3776039Seric } 3786039Seric 3796039Seric # ifdef DEBUG 3807677Seric if (tTd(16, 1)) 3816039Seric printf("makeconnection: %d\n", s); 38210347Seric 38310347Seric /* turn on network debugging? */ 38410347Seric if (tTd(16, 14)) 38510347Seric (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0); 3866039Seric # endif DEBUG 3879546Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 38814383Seric errno = 0; /* for debugging */ 3899610Seric SendmailAddress.sin_family = AF_INET; 390*23120Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0) 3916039Seric { 3926039Seric /* failure, decide if temporary or not */ 3936039Seric failure: 3946039Seric switch (errno) 3956039Seric { 3966039Seric case EISCONN: 3976039Seric case ETIMEDOUT: 3986897Seric case EINPROGRESS: 3996897Seric case EALREADY: 4006897Seric case EADDRINUSE: 40110123Seric case EHOSTDOWN: 4026897Seric case ENETDOWN: 4036897Seric case ENETRESET: 4046897Seric case ENOBUFS: 4057204Seric case ECONNREFUSED: 40611546Seric case ECONNRESET: 40710081Seric case EHOSTUNREACH: 40810098Seric case ENETUNREACH: 4096039Seric /* there are others, I'm sure..... */ 41016884Seric CurEnv->e_flags &= ~EF_FATALERRS; 4116039Seric return (EX_TEMPFAIL); 4126039Seric 41311147Seric case EPERM: 41411147Seric /* why is this happening? */ 41511147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 41611147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 41714383Seric return (EX_TEMPFAIL); 41811147Seric 4196039Seric default: 4206039Seric return (EX_UNAVAILABLE); 4216039Seric } 4226039Seric } 4236039Seric 4246039Seric /* connection ok, put it into canonical form */ 4256039Seric *outfile = fdopen(s, "w"); 4266039Seric *infile = fdopen(s, "r"); 4276039Seric 42810098Seric return (EX_OK); 4296039Seric } 43010758Seric /* 43110758Seric ** MYHOSTNAME -- return the name of this host. 43210758Seric ** 43310758Seric ** Parameters: 43410758Seric ** hostbuf -- a place to return the name of this host. 43512313Seric ** size -- the size of hostbuf. 43610758Seric ** 43710758Seric ** Returns: 43810758Seric ** A list of aliases for this host. 43910758Seric ** 44010758Seric ** Side Effects: 44110758Seric ** none. 44210758Seric */ 4436039Seric 44410758Seric char ** 44512313Seric myhostname(hostbuf, size) 44610758Seric char hostbuf[]; 44712313Seric int size; 44810758Seric { 44910758Seric extern struct hostent *gethostbyname(); 45011147Seric struct hostent *hp; 45110758Seric 452*23120Seric if (gethostname(hostbuf, size) < 0) 453*23120Seric { 454*23120Seric (void) strcpy(hostbuf, "localhost"); 455*23120Seric } 45611147Seric hp = gethostbyname(hostbuf); 45711147Seric if (hp != NULL) 45816877Seric { 45923104Seric (void) strcpy(hostbuf, hp->h_name); 46011147Seric return (hp->h_aliases); 46116877Seric } 46210758Seric else 46310758Seric return (NULL); 46410758Seric } 46516911Seric /* 46616911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 46716911Seric ** 46816911Seric ** Parameters: 46916911Seric ** hbuf -- a buffer containing a hostname. 47016911Seric ** hbsize -- the size of hbuf. 47116911Seric ** 47216911Seric ** Returns: 47316911Seric ** none. 47416911Seric ** 47516911Seric ** Side Effects: 47616911Seric ** Looks up the host specified in hbuf. If it is not 47716911Seric ** the canonical name for that host, replace it with 47816911Seric ** the canonical name. If the name is unknown, or it 47916911Seric ** is already the canonical name, leave it unchanged. 48016911Seric */ 48110758Seric 48216911Seric maphostname(hbuf, hbsize) 48316911Seric char *hbuf; 48416911Seric int hbsize; 48516911Seric { 48616911Seric register struct hostent *hp; 48716911Seric extern struct hostent *gethostbyname(); 48816911Seric 48916911Seric makelower(hbuf); 49016911Seric hp = gethostbyname(hbuf); 49116911Seric if (hp != NULL) 49216911Seric { 49316911Seric int i = strlen(hp->h_name); 49416911Seric 49516911Seric if (i >= hbsize) 49616911Seric hp->h_name[--i] = '\0'; 49723104Seric (void) strcpy(hbuf, hp->h_name); 49816911Seric } 49916911Seric } 50016911Seric 50110758Seric # else DAEMON 50216911Seric /* code for systems without sophisticated networking */ 50310758Seric 50410758Seric /* 50510758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 50611297Seric ** 50711297Seric ** Can't convert to upper case here because might be a UUCP name. 50812313Seric ** 50912313Seric ** Mark, you can change this to be anything you want...... 51010758Seric */ 51110758Seric 51210758Seric char ** 51312313Seric myhostname(hostbuf, size) 51410758Seric char hostbuf[]; 51512313Seric int size; 51610758Seric { 51710758Seric register FILE *f; 51810758Seric 51910758Seric hostbuf[0] = '\0'; 52010758Seric f = fopen("/usr/include/whoami", "r"); 52110758Seric if (f != NULL) 52210758Seric { 52312313Seric (void) fgets(hostbuf, size, f); 52410758Seric fixcrlf(hostbuf, TRUE); 52510758Seric (void) fclose(f); 52610758Seric } 52710758Seric return (NULL); 52810758Seric } 52916911Seric /* 53016911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 53116911Seric ** 53216911Seric ** Parameters: 53316911Seric ** hbuf -- a buffer containing a hostname. 53416911Seric ** hbsize -- the size of hbuf. 53516911Seric ** 53616911Seric ** Returns: 53716911Seric ** none. 53816911Seric ** 53916911Seric ** Side Effects: 54016911Seric ** Looks up the host specified in hbuf. If it is not 54116911Seric ** the canonical name for that host, replace it with 54216911Seric ** the canonical name. If the name is unknown, or it 54316911Seric ** is already the canonical name, leave it unchanged. 54416911Seric */ 54510758Seric 54616911Seric /*ARGSUSED*/ 54716911Seric maphostname(hbuf, hbsize) 54816911Seric char *hbuf; 54916911Seric int hbsize; 55016911Seric { 55116911Seric return; 55216911Seric } 55316911Seric 55416911Seric 5555978Seric #endif DAEMON 556