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*24858Seric static char SccsId[] = "@(#)daemon.c 5.7 (Berkeley) 09/17/85 (w/o daemon mode)"; 1823120Seric # endif not lint 1923120Seric # else 204535Seric 2123120Seric # include <sys/socket.h> 2223120Seric # include <netinet/in.h> 2323120Seric # include <netdb.h> 2423120Seric # include <sys/wait.h> 2523120Seric # include <sys/time.h> 2623120Seric # include <sys/resource.h> 275978Seric 2823120Seric # ifndef lint 29*24858Seric static char SccsId[] = "@(#)daemon.c 5.7 (Berkeley) 09/17/85 (with daemon mode)"; 3023120Seric # 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 */ 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) 122*24858Seric 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)) 13024201Smiriam { 13124201Smiriam int on = 1; 13224201Smiriam (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 13324201Smiriam } 13410347Seric #endif DEBUG 13510347Seric 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 1499610Seric # ifdef DEBUG 1509610Seric if (tTd(15, 1)) 15110206Seric printf("getrequests: %d\n", DaemonSocket); 1529610Seric # endif DEBUG 1539610Seric 1544631Seric for (;;) 1554631Seric { 15614875Seric register int pid; 15711147Seric auto int lotherend; 15811147Seric struct sockaddr_in otherend; 15914875Seric extern int RefuseLA; 16011147Seric 16114875Seric /* see if we are rejecting connections */ 16214875Seric while (getla() > RefuseLA) 16314875Seric sleep(5); 16414875Seric 1659610Seric /* wait for a connection */ 1669610Seric do 1679610Seric { 1689610Seric errno = 0; 1699610Seric lotherend = sizeof otherend; 17023120Seric t = accept(DaemonSocket, &otherend, &lotherend); 1719610Seric } while (t < 0 && errno == EINTR); 1729610Seric if (t < 0) 1735978Seric { 1749610Seric syserr("getrequests: accept"); 1759610Seric sleep(5); 1769610Seric continue; 1775978Seric } 1784631Seric 1795978Seric /* 1805978Seric ** Create a subprocess to process the mail. 1815978Seric */ 1825978Seric 1835978Seric # ifdef DEBUG 1847677Seric if (tTd(15, 2)) 1859610Seric printf("getrequests: forking (fd = %d)\n", t); 1865978Seric # endif DEBUG 1875978Seric 1884636Seric pid = fork(); 1894636Seric if (pid < 0) 1904631Seric { 1914636Seric syserr("daemon: cannot fork"); 1924636Seric sleep(10); 1939610Seric (void) close(t); 1944636Seric continue; 1954631Seric } 1964631Seric 1974636Seric if (pid == 0) 1984631Seric { 19911147Seric extern struct hostent *gethostbyaddr(); 20011147Seric register struct hostent *hp; 20111147Seric extern char *RealHostName; /* srvrsmtp.c */ 20211147Seric char buf[MAXNAME]; 20311147Seric 2044636Seric /* 2054636Seric ** CHILD -- return to caller. 20611147Seric ** Collect verified idea of sending host. 2074636Seric ** Verify calling user id if possible here. 2084636Seric */ 2094631Seric 21011147Seric /* determine host name */ 21111147Seric hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET); 21211147Seric if (hp != NULL) 21316890Seric { 21423104Seric (void) strcpy(buf, hp->h_name); 21516890Seric if (NetName != NULL && NetName[0] != '\0' && 21616894Seric index(hp->h_name, '.') == NULL) 21716890Seric { 21823104Seric (void) strcat(buf, "."); 21923104Seric (void) strcat(buf, NetName); 22016890Seric } 22116890Seric } 22211147Seric else 22316884Seric { 22416884Seric extern char *inet_ntoa(); 22516884Seric 22616884Seric /* produce a dotted quad */ 22716884Seric (void) sprintf(buf, "[%s]", 22816884Seric inet_ntoa(otherend.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"); 2385978Seric # ifdef DEBUG 2397677Seric if (tTd(15, 2)) 2405978Seric printf("getreq: returning\n"); 2415978Seric # endif DEBUG 2427876Seric # ifdef LOG 2437876Seric if (LogLevel > 11) 2447876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2457876Seric # endif LOG 2464636Seric return; 2474631Seric } 2484631Seric 2494636Seric /* 2504636Seric ** PARENT -- wait for child to terminate. 2514636Seric ** Perhaps we should allow concurrent processing? 2524636Seric */ 2534631Seric 2545978Seric # ifdef DEBUG 2557677Seric if (tTd(15, 2)) 2565978Seric { 2575978Seric sleep(2); 2585978Seric printf("getreq: parent waiting\n"); 2595978Seric } 2605978Seric # endif DEBUG 2615978Seric 2627117Seric /* close the port so that others will hang (for a while) */ 2639610Seric (void) close(t); 2647117Seric 26513933Seric /* pick up old zombies */ 26623120Seric while (wait3(&status, WNOHANG, (struct rusage *) 0) > 0) 26713933Seric continue; 2684631Seric } 2699886Seric /*NOTREACHED*/ 2704631Seric } 2715978Seric /* 27210206Seric ** CLRDAEMON -- reset the daemon connection 27310206Seric ** 27410206Seric ** Parameters: 27510206Seric ** none. 27610206Seric ** 27710206Seric ** Returns: 27810206Seric ** none. 27910206Seric ** 28010206Seric ** Side Effects: 28110206Seric ** releases any resources used by the passive daemon. 28210206Seric */ 28310206Seric 28410206Seric clrdaemon() 28510206Seric { 28610206Seric if (DaemonSocket >= 0) 28710206Seric (void) close(DaemonSocket); 28810206Seric DaemonSocket = -1; 28910206Seric } 29010206Seric /* 2916039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2926039Seric ** 2936039Seric ** Parameters: 2946039Seric ** host -- the name of the host. 2956633Seric ** port -- the port number to connect to. 2966039Seric ** outfile -- a pointer to a place to put the outfile 2976039Seric ** descriptor. 2986039Seric ** infile -- ditto for infile. 2996039Seric ** 3006039Seric ** Returns: 3016039Seric ** An exit code telling whether the connection could be 3026039Seric ** made and if not why not. 3036039Seric ** 3046039Seric ** Side Effects: 3056039Seric ** none. 3066039Seric */ 3075978Seric 3086633Seric makeconnection(host, port, outfile, infile) 3096039Seric char *host; 3107286Seric u_short port; 3116039Seric FILE **outfile; 3126039Seric FILE **infile; 3136039Seric { 3146039Seric register int s; 3156039Seric 3166039Seric /* 3176039Seric ** Set up the address for the mailer. 3189308Seric ** Accept "[a.b.c.d]" syntax for host name. 3196039Seric */ 3206039Seric 3219308Seric if (host[0] == '[') 3229308Seric { 32311147Seric long hid; 32411147Seric register char *p = index(host, ']'); 3259308Seric 32611147Seric if (p != NULL) 3279308Seric { 32811147Seric *p = '\0'; 32911147Seric hid = inet_addr(&host[1]); 33011147Seric *p = ']'; 3319308Seric } 33211147Seric if (p == NULL || hid == -1) 3339308Seric { 3349308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3359308Seric return (EX_NOHOST); 3369308Seric } 3379308Seric SendmailAddress.sin_addr.s_addr = hid; 3389308Seric } 3399610Seric else 3409610Seric { 3419610Seric register struct hostent *hp = gethostbyname(host); 3429610Seric 34324041Smiriam if (errno == ETIMEDOUT) 34424041Smiriam { 34524041Smiriam CurEnv->e_flags &= ~EF_FATALERRS; 34624041Smiriam return (EX_TEMPFAIL); 34724041Smiriam } 34811147Seric if (hp == NULL) 3499610Seric return (EX_NOHOST); 35016884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 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 3756039Seric # ifdef DEBUG 3767677Seric if (tTd(16, 1)) 3776039Seric printf("makeconnection (%s)\n", host); 3786039Seric # endif DEBUG 3796039Seric 38023120Seric s = socket(AF_INET, SOCK_STREAM, 0); 3816039Seric if (s < 0) 3826039Seric { 3836039Seric syserr("makeconnection: no socket"); 3846039Seric goto failure; 3856039Seric } 3866039Seric 3876039Seric # ifdef DEBUG 3887677Seric if (tTd(16, 1)) 3896039Seric printf("makeconnection: %d\n", s); 39010347Seric 39110347Seric /* turn on network debugging? */ 39210347Seric if (tTd(16, 14)) 39324201Smiriam { 39424201Smiriam int on = 1; 39524201Smiriam (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 39624201Smiriam } 3976039Seric # endif DEBUG 3989546Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 39914383Seric errno = 0; /* for debugging */ 4009610Seric SendmailAddress.sin_family = AF_INET; 40123120Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0) 4026039Seric { 4036039Seric /* failure, decide if temporary or not */ 4046039Seric failure: 4056039Seric switch (errno) 4066039Seric { 4076039Seric case EISCONN: 4086039Seric case ETIMEDOUT: 4096897Seric case EINPROGRESS: 4106897Seric case EALREADY: 4116897Seric case EADDRINUSE: 41210123Seric case EHOSTDOWN: 4136897Seric case ENETDOWN: 4146897Seric case ENETRESET: 4156897Seric case ENOBUFS: 4167204Seric case ECONNREFUSED: 41711546Seric case ECONNRESET: 41810081Seric case EHOSTUNREACH: 41910098Seric case ENETUNREACH: 4206039Seric /* there are others, I'm sure..... */ 42116884Seric CurEnv->e_flags &= ~EF_FATALERRS; 4226039Seric return (EX_TEMPFAIL); 4236039Seric 42411147Seric case EPERM: 42511147Seric /* why is this happening? */ 42611147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 42711147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 42814383Seric return (EX_TEMPFAIL); 42911147Seric 4306039Seric default: 4316039Seric return (EX_UNAVAILABLE); 4326039Seric } 4336039Seric } 4346039Seric 4356039Seric /* connection ok, put it into canonical form */ 4366039Seric *outfile = fdopen(s, "w"); 4376039Seric *infile = fdopen(s, "r"); 4386039Seric 43910098Seric return (EX_OK); 4406039Seric } 44110758Seric /* 44210758Seric ** MYHOSTNAME -- return the name of this host. 44310758Seric ** 44410758Seric ** Parameters: 44510758Seric ** hostbuf -- a place to return the name of this host. 44612313Seric ** size -- the size of hostbuf. 44710758Seric ** 44810758Seric ** Returns: 44910758Seric ** A list of aliases for this host. 45010758Seric ** 45110758Seric ** Side Effects: 45210758Seric ** none. 45310758Seric */ 4546039Seric 45510758Seric char ** 45612313Seric myhostname(hostbuf, size) 45710758Seric char hostbuf[]; 45812313Seric int size; 45910758Seric { 46010758Seric extern struct hostent *gethostbyname(); 46111147Seric struct hostent *hp; 46210758Seric 46323120Seric if (gethostname(hostbuf, size) < 0) 46423120Seric { 46523120Seric (void) strcpy(hostbuf, "localhost"); 46623120Seric } 46711147Seric hp = gethostbyname(hostbuf); 46811147Seric if (hp != NULL) 46916877Seric { 47023104Seric (void) strcpy(hostbuf, hp->h_name); 47111147Seric return (hp->h_aliases); 47216877Seric } 47310758Seric else 47410758Seric return (NULL); 47510758Seric } 47616911Seric /* 47716911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 47816911Seric ** 47916911Seric ** Parameters: 48016911Seric ** hbuf -- a buffer containing a hostname. 48116911Seric ** hbsize -- the size of hbuf. 48216911Seric ** 48316911Seric ** Returns: 48416911Seric ** none. 48516911Seric ** 48616911Seric ** Side Effects: 48716911Seric ** Looks up the host specified in hbuf. If it is not 48816911Seric ** the canonical name for that host, replace it with 48916911Seric ** the canonical name. If the name is unknown, or it 49016911Seric ** is already the canonical name, leave it unchanged. 49116911Seric */ 49210758Seric 49316911Seric maphostname(hbuf, hbsize) 49416911Seric char *hbuf; 49516911Seric int hbsize; 49616911Seric { 49716911Seric register struct hostent *hp; 49816911Seric extern struct hostent *gethostbyname(); 49916911Seric 50016911Seric makelower(hbuf); 50116911Seric hp = gethostbyname(hbuf); 50216911Seric if (hp != NULL) 50316911Seric { 50416911Seric int i = strlen(hp->h_name); 50516911Seric 50616911Seric if (i >= hbsize) 50716911Seric hp->h_name[--i] = '\0'; 50823104Seric (void) strcpy(hbuf, hp->h_name); 50916911Seric } 51016911Seric } 51116911Seric 51210758Seric # else DAEMON 51316911Seric /* code for systems without sophisticated networking */ 51410758Seric 51510758Seric /* 51610758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 51711297Seric ** 51811297Seric ** Can't convert to upper case here because might be a UUCP name. 51912313Seric ** 52012313Seric ** Mark, you can change this to be anything you want...... 52110758Seric */ 52210758Seric 52310758Seric char ** 52412313Seric myhostname(hostbuf, size) 52510758Seric char hostbuf[]; 52612313Seric int size; 52710758Seric { 52810758Seric register FILE *f; 52910758Seric 53010758Seric hostbuf[0] = '\0'; 53110758Seric f = fopen("/usr/include/whoami", "r"); 53210758Seric if (f != NULL) 53310758Seric { 53412313Seric (void) fgets(hostbuf, size, f); 53510758Seric fixcrlf(hostbuf, TRUE); 53610758Seric (void) fclose(f); 53710758Seric } 53810758Seric return (NULL); 53910758Seric } 54016911Seric /* 54116911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 54216911Seric ** 54316911Seric ** Parameters: 54416911Seric ** hbuf -- a buffer containing a hostname. 54516911Seric ** hbsize -- the size of hbuf. 54616911Seric ** 54716911Seric ** Returns: 54816911Seric ** none. 54916911Seric ** 55016911Seric ** Side Effects: 55116911Seric ** Looks up the host specified in hbuf. If it is not 55216911Seric ** the canonical name for that host, replace it with 55316911Seric ** the canonical name. If the name is unknown, or it 55416911Seric ** is already the canonical name, leave it unchanged. 55516911Seric */ 55610758Seric 55716911Seric /*ARGSUSED*/ 55816911Seric maphostname(hbuf, hbsize) 55916911Seric char *hbuf; 56016911Seric int hbsize; 56116911Seric { 56216911Seric return; 56316911Seric } 56416911Seric 56516911Seric 5665978Seric #endif DAEMON 567