16039Seric # include <errno.h> 24535Seric # include "sendmail.h" 34535Seric 45978Seric #ifndef DAEMON 5*11546Seric SCCSID(@(#)daemon.c 3.52 03/12/83 (w/o daemon mode)); 65978Seric #else 74535Seric 89610Seric #include <sys/socket.h> 99610Seric #include <netinet/in.h> 109610Seric #include <netdb.h> 119610Seric #include <wait.h> 125978Seric 13*11546Seric SCCSID(@(#)daemon.c 3.52 03/12/83 (with daemon mode)); 145978Seric 154535Seric /* 164535Seric ** DAEMON.C -- routines to use when running as a daemon. 177556Seric ** 187556Seric ** This entire file is highly dependent on the 4.2 BSD 197556Seric ** interprocess communication primitives. No attempt has 207556Seric ** been made to make this file portable to Version 7, 217556Seric ** Version 6, MPX files, etc. If you should try such a 227556Seric ** thing yourself, I recommend chucking the entire file 237556Seric ** and starting from scratch. Basic semantics are: 247556Seric ** 257556Seric ** getrequests() 267556Seric ** Opens a port and initiates a connection. 277556Seric ** Returns in a child. Must set InChannel and 287556Seric ** OutChannel appropriately. 2910206Seric ** clrdaemon() 3010206Seric ** Close any open files associated with getting 3110206Seric ** the connection; this is used when running the queue, 3210206Seric ** etc., to avoid having extra file descriptors during 3310206Seric ** the queue run and to avoid confusing the network 3410206Seric ** code (if it cares). 357556Seric ** makeconnection(host, port, outfile, infile) 367556Seric ** Make a connection to the named host on the given 377556Seric ** port. Set *outfile and *infile to the files 387556Seric ** appropriate for communication. Returns zero on 397556Seric ** success, else an exit status describing the 407556Seric ** error. 417556Seric ** 427556Seric ** The semantics of both of these should be clean. 434535Seric */ 444535Seric /* 454535Seric ** GETREQUESTS -- open mail IPC port and get requests. 464535Seric ** 474535Seric ** Parameters: 484535Seric ** none. 494535Seric ** 504535Seric ** Returns: 514535Seric ** none. 524535Seric ** 534535Seric ** Side Effects: 544535Seric ** Waits until some interesting activity occurs. When 554535Seric ** it does, a child is created to process it, and the 564535Seric ** parent waits for completion. Return from this 579886Seric ** routine is always in the child. The file pointers 589886Seric ** "InChannel" and "OutChannel" should be set to point 599886Seric ** to the communication channel. 604535Seric */ 614535Seric 6210206Seric struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 6310206Seric int DaemonSocket = -1; /* fd describing socket */ 6410342Seric int MaxConnections = 4; /* maximum simultaneous sendmails */ 659610Seric 664535Seric getrequests() 674535Seric { 689610Seric int t; 697117Seric union wait status; 707117Seric int numconnections = 0; 719610Seric register struct servent *sp; 727117Seric 739610Seric /* 749610Seric ** Set up the address for the mailer. 759610Seric */ 769610Seric 779610Seric sp = getservbyname("smtp", "tcp"); 789610Seric if (sp == NULL) 799610Seric { 809610Seric syserr("server \"smtp\" unknown"); 8110167Seric goto severe; 829610Seric } 839610Seric SendmailAddress.sin_family = AF_INET; 849610Seric SendmailAddress.sin_addr.s_addr = INADDR_ANY; 859740Ssam SendmailAddress.sin_port = sp->s_port; 869610Seric 879610Seric /* 889610Seric ** Try to actually open the connection. 899610Seric */ 909610Seric 919610Seric # ifdef DEBUG 929610Seric if (tTd(15, 1)) 939610Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 949610Seric # endif DEBUG 959610Seric 969610Seric /* get a socket for the SMTP connection */ 9710206Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0, 0); 9810206Seric if (DaemonSocket < 0) 999610Seric { 1009610Seric /* probably another daemon already */ 1019610Seric syserr("getrequests: can't create socket"); 1029610Seric severe: 1039610Seric # ifdef LOG 1049610Seric if (LogLevel > 0) 1059610Seric syslog(LOG_SALERT, "cannot get connection"); 1069610Seric # endif LOG 1079610Seric finis(); 1089610Seric } 10910347Seric 11010347Seric #ifdef DEBUG 11110347Seric /* turn on network debugging? */ 11210347Seric if (tTd(15, 15)) 11310347Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 0, 0); 11410347Seric #endif DEBUG 11510347Seric 11610206Seric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress, 0) < 0) 1179610Seric { 1189610Seric syserr("getrequests: cannot bind"); 11910206Seric (void) close(DaemonSocket); 1209610Seric goto severe; 1219610Seric } 12210206Seric listen(DaemonSocket, 10); 1239610Seric 1249610Seric # ifdef DEBUG 1259610Seric if (tTd(15, 1)) 12610206Seric printf("getrequests: %d\n", DaemonSocket); 1279610Seric # endif DEBUG 1289610Seric 1294631Seric for (;;) 1304631Seric { 13111147Seric auto int lotherend; 13211147Seric struct sockaddr_in otherend; 13311147Seric 1349610Seric /* wait for a connection */ 1354636Seric register int pid; 1364631Seric 1379610Seric do 1389610Seric { 1399610Seric errno = 0; 1409610Seric lotherend = sizeof otherend; 14110206Seric t = accept(DaemonSocket, &otherend, &lotherend, 0); 1429610Seric } while (t < 0 && errno == EINTR); 1439610Seric if (t < 0) 1445978Seric { 1459610Seric syserr("getrequests: accept"); 1469610Seric sleep(5); 1479610Seric continue; 1485978Seric } 1494631Seric 1505978Seric /* 1515978Seric ** Create a subprocess to process the mail. 1525978Seric */ 1535978Seric 1545978Seric # ifdef DEBUG 1557677Seric if (tTd(15, 2)) 1569610Seric printf("getrequests: forking (fd = %d)\n", t); 1575978Seric # endif DEBUG 1585978Seric 1594636Seric pid = fork(); 1604636Seric if (pid < 0) 1614631Seric { 1624636Seric syserr("daemon: cannot fork"); 1634636Seric sleep(10); 1649610Seric (void) close(t); 1654636Seric continue; 1664631Seric } 1674631Seric 1684636Seric if (pid == 0) 1694631Seric { 17011147Seric extern struct hostent *gethostbyaddr(); 17111147Seric register struct hostent *hp; 17211147Seric extern char *RealHostName; /* srvrsmtp.c */ 17311147Seric char buf[MAXNAME]; 17411147Seric 1754636Seric /* 1764636Seric ** CHILD -- return to caller. 17711147Seric ** Collect verified idea of sending host. 1784636Seric ** Verify calling user id if possible here. 1794636Seric */ 1804631Seric 18111147Seric /* determine host name */ 18211147Seric hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET); 18311147Seric if (hp != NULL) 18411147Seric (void) sprintf(buf, "%s.ARPA", hp->h_name); 18511147Seric else 18611147Seric /* this should produce a dotted quad */ 18711147Seric (void) sprintf(buf, "%lx", otherend.sin_addr.s_addr); 18811147Seric RealHostName = newstr(buf); 18911147Seric 19010206Seric (void) close(DaemonSocket); 1919610Seric InChannel = fdopen(t, "r"); 1929610Seric OutChannel = fdopen(t, "w"); 1935978Seric # ifdef DEBUG 1947677Seric if (tTd(15, 2)) 1955978Seric printf("getreq: returning\n"); 1965978Seric # endif DEBUG 1977876Seric # ifdef LOG 1987876Seric if (LogLevel > 11) 1997876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2007876Seric # endif LOG 2014636Seric return; 2024631Seric } 2034631Seric 2044636Seric /* 2054636Seric ** PARENT -- wait for child to terminate. 2064636Seric ** Perhaps we should allow concurrent processing? 2074636Seric */ 2084631Seric 2095978Seric # ifdef DEBUG 2107677Seric if (tTd(15, 2)) 2115978Seric { 2125978Seric sleep(2); 2135978Seric printf("getreq: parent waiting\n"); 2145978Seric } 2155978Seric # endif DEBUG 2165978Seric 2177117Seric /* close the port so that others will hang (for a while) */ 2189610Seric (void) close(t); 2197117Seric 2207117Seric /* pick up old zombies; implement load limiting */ 2217117Seric numconnections++; 22210342Seric while (wait3(&status, numconnections < MaxConnections ? WNOHANG : 0, 0) > 0) 2237117Seric numconnections--; 2244631Seric } 2259886Seric /*NOTREACHED*/ 2264631Seric } 2275978Seric /* 22810206Seric ** CLRDAEMON -- reset the daemon connection 22910206Seric ** 23010206Seric ** Parameters: 23110206Seric ** none. 23210206Seric ** 23310206Seric ** Returns: 23410206Seric ** none. 23510206Seric ** 23610206Seric ** Side Effects: 23710206Seric ** releases any resources used by the passive daemon. 23810206Seric */ 23910206Seric 24010206Seric clrdaemon() 24110206Seric { 24210206Seric if (DaemonSocket >= 0) 24310206Seric (void) close(DaemonSocket); 24410206Seric DaemonSocket = -1; 24510206Seric } 24610206Seric /* 2476039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2486039Seric ** 2496039Seric ** Parameters: 2506039Seric ** host -- the name of the host. 2516633Seric ** port -- the port number to connect to. 2526039Seric ** outfile -- a pointer to a place to put the outfile 2536039Seric ** descriptor. 2546039Seric ** infile -- ditto for infile. 2556039Seric ** 2566039Seric ** Returns: 2576039Seric ** An exit code telling whether the connection could be 2586039Seric ** made and if not why not. 2596039Seric ** 2606039Seric ** Side Effects: 2616039Seric ** none. 2626039Seric */ 2635978Seric 2646633Seric makeconnection(host, port, outfile, infile) 2656039Seric char *host; 2667286Seric u_short port; 2676039Seric FILE **outfile; 2686039Seric FILE **infile; 2696039Seric { 2706039Seric register int s; 2716039Seric 2726039Seric /* 2736039Seric ** Set up the address for the mailer. 2749308Seric ** Accept "[a.b.c.d]" syntax for host name. 2756039Seric */ 2766039Seric 2779308Seric if (host[0] == '[') 2789308Seric { 27911147Seric long hid; 28011147Seric register char *p = index(host, ']'); 2819308Seric 28211147Seric if (p != NULL) 2839308Seric { 28411147Seric *p = '\0'; 28511147Seric hid = inet_addr(&host[1]); 28611147Seric *p = ']'; 2879308Seric } 28811147Seric if (p == NULL || hid == -1) 2899308Seric { 2909308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 2919308Seric return (EX_NOHOST); 2929308Seric } 2939308Seric SendmailAddress.sin_addr.s_addr = hid; 2949308Seric } 2959610Seric else 2969610Seric { 2979610Seric register struct hostent *hp = gethostbyname(host); 2989610Seric 29911147Seric if (hp == NULL) 3009610Seric return (EX_NOHOST); 3019610Seric bmove(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 3029610Seric } 3039610Seric 3049610Seric /* 3059610Seric ** Determine the port number. 3069610Seric */ 3079610Seric 30810011Seric if (port != 0) 30910011Seric SendmailAddress.sin_port = htons(port); 31010011Seric else 3119610Seric { 3129610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3139610Seric 3149610Seric if (sp == NULL) 3159610Seric { 3169610Seric syserr("makeconnection: server \"smtp\" unknown"); 3179610Seric return (EX_OSFILE); 3189610Seric } 31910011Seric SendmailAddress.sin_port = sp->s_port; 3209610Seric } 3216039Seric 3226039Seric /* 3236039Seric ** Try to actually open the connection. 3246039Seric */ 3256039Seric 3266039Seric # ifdef DEBUG 3277677Seric if (tTd(16, 1)) 3286039Seric printf("makeconnection (%s)\n", host); 3296039Seric # endif DEBUG 3306039Seric 3319310Seric s = socket(AF_INET, SOCK_STREAM, 0, 0); 3326039Seric if (s < 0) 3336039Seric { 3346039Seric syserr("makeconnection: no socket"); 3356039Seric goto failure; 3366039Seric } 3376039Seric 3386039Seric # ifdef DEBUG 3397677Seric if (tTd(16, 1)) 3406039Seric printf("makeconnection: %d\n", s); 34110347Seric 34210347Seric /* turn on network debugging? */ 34310347Seric if (tTd(16, 14)) 34410347Seric (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0); 3456039Seric # endif DEBUG 3469546Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 3479610Seric SendmailAddress.sin_family = AF_INET; 3489310Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0) 3496039Seric { 3506039Seric /* failure, decide if temporary or not */ 3516039Seric failure: 3526039Seric switch (errno) 3536039Seric { 3546039Seric case EISCONN: 3556039Seric case ETIMEDOUT: 3566897Seric case EINPROGRESS: 3576897Seric case EALREADY: 3586897Seric case EADDRINUSE: 35910123Seric case EHOSTDOWN: 3606897Seric case ENETDOWN: 3616897Seric case ENETRESET: 3626897Seric case ENOBUFS: 3637204Seric case ECONNREFUSED: 364*11546Seric case ECONNRESET: 36510081Seric case EHOSTUNREACH: 36610098Seric case ENETUNREACH: 3676039Seric /* there are others, I'm sure..... */ 3686039Seric return (EX_TEMPFAIL); 3696039Seric 37011147Seric case EPERM: 37111147Seric /* why is this happening? */ 37211147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 37311147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 37411147Seric /* explicit fall-through */ 37511147Seric 3766039Seric default: 3776039Seric return (EX_UNAVAILABLE); 3786039Seric } 3796039Seric } 3806039Seric 3816039Seric /* connection ok, put it into canonical form */ 3826039Seric *outfile = fdopen(s, "w"); 3836039Seric *infile = fdopen(s, "r"); 3846039Seric 38510098Seric return (EX_OK); 3866039Seric } 38710758Seric /* 38810758Seric ** MYHOSTNAME -- return the name of this host. 38910758Seric ** 39010758Seric ** Parameters: 39110758Seric ** hostbuf -- a place to return the name of this host. 39210758Seric ** 39310758Seric ** Returns: 39410758Seric ** A list of aliases for this host. 39510758Seric ** 39610758Seric ** Side Effects: 39710758Seric ** none. 39810758Seric */ 3996039Seric 40010758Seric char ** 40110758Seric myhostname(hostbuf) 40210758Seric char hostbuf[]; 40310758Seric { 40410758Seric extern struct hostent *gethostbyname(); 40511147Seric struct hostent *hp; 40611276Seric auto int i = 30; 40711297Seric register char *p; 40810758Seric 40911276Seric gethostname(hostbuf, &i); 41011147Seric hp = gethostbyname(hostbuf); 41111297Seric for (p = hostbuf; *p != '\0'; p++) 41211297Seric if (islower(*p)) 41311297Seric *p -= 'a' - 'A'; 41411147Seric if (hp != NULL) 41511147Seric return (hp->h_aliases); 41610758Seric else 41710758Seric return (NULL); 41810758Seric } 41910758Seric 42010758Seric # else DAEMON 42110758Seric 42210758Seric /* 42310758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 42411297Seric ** 42511297Seric ** Can't convert to upper case here because might be a UUCP name. 42610758Seric */ 42710758Seric 42810758Seric char ** 42910758Seric myhostname(hostbuf) 43010758Seric char hostbuf[]; 43110758Seric { 43210758Seric register FILE *f; 43310758Seric 43410758Seric hostbuf[0] = '\0'; 43510758Seric f = fopen("/usr/include/whoami", "r"); 43610758Seric if (f != NULL) 43710758Seric { 43810758Seric (void) fgets(hostbuf, sizeof hostbuf, f); 43910758Seric fixcrlf(hostbuf, TRUE); 44010758Seric (void) fclose(f); 44110758Seric } 44210758Seric return (NULL); 44310758Seric } 44410758Seric 4455978Seric #endif DAEMON 446