16039Seric # include <errno.h> 24535Seric # include "sendmail.h" 34535Seric 45978Seric #ifndef DAEMON 5*13933Seric SCCSID(@(#)daemon.c 3.55 07/13/83 (w/o daemon mode)); 65978Seric #else 74535Seric 89610Seric #include <sys/socket.h> 99610Seric #include <netinet/in.h> 109610Seric #include <netdb.h> 1113586Swnj #include <sys/wait.h> 125978Seric 13*13933Seric SCCSID(@(#)daemon.c 3.55 07/13/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 */ 649610Seric 654535Seric getrequests() 664535Seric { 679610Seric int t; 687117Seric union wait status; 699610Seric register struct servent *sp; 707117Seric 719610Seric /* 729610Seric ** Set up the address for the mailer. 739610Seric */ 749610Seric 759610Seric sp = getservbyname("smtp", "tcp"); 769610Seric if (sp == NULL) 779610Seric { 789610Seric syserr("server \"smtp\" unknown"); 7910167Seric goto severe; 809610Seric } 819610Seric SendmailAddress.sin_family = AF_INET; 829610Seric SendmailAddress.sin_addr.s_addr = INADDR_ANY; 839740Ssam SendmailAddress.sin_port = sp->s_port; 849610Seric 859610Seric /* 869610Seric ** Try to actually open the connection. 879610Seric */ 889610Seric 899610Seric # ifdef DEBUG 909610Seric if (tTd(15, 1)) 919610Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 929610Seric # endif DEBUG 939610Seric 949610Seric /* get a socket for the SMTP connection */ 9510206Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0, 0); 9610206Seric if (DaemonSocket < 0) 979610Seric { 989610Seric /* probably another daemon already */ 999610Seric syserr("getrequests: can't create socket"); 1009610Seric severe: 1019610Seric # ifdef LOG 1029610Seric if (LogLevel > 0) 1039610Seric syslog(LOG_SALERT, "cannot get connection"); 1049610Seric # endif LOG 1059610Seric finis(); 1069610Seric } 10710347Seric 10810347Seric #ifdef DEBUG 10910347Seric /* turn on network debugging? */ 11010347Seric if (tTd(15, 15)) 11110347Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 0, 0); 11210347Seric #endif DEBUG 11310347Seric 11410206Seric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress, 0) < 0) 1159610Seric { 1169610Seric syserr("getrequests: cannot bind"); 11710206Seric (void) close(DaemonSocket); 1189610Seric goto severe; 1199610Seric } 12010206Seric listen(DaemonSocket, 10); 1219610Seric 1229610Seric # ifdef DEBUG 1239610Seric if (tTd(15, 1)) 12410206Seric printf("getrequests: %d\n", DaemonSocket); 1259610Seric # endif DEBUG 1269610Seric 1274631Seric for (;;) 1284631Seric { 12911147Seric auto int lotherend; 13011147Seric struct sockaddr_in otherend; 13111147Seric 1329610Seric /* wait for a connection */ 1334636Seric register int pid; 1344631Seric 1359610Seric do 1369610Seric { 1379610Seric errno = 0; 1389610Seric lotherend = sizeof otherend; 13910206Seric t = accept(DaemonSocket, &otherend, &lotherend, 0); 1409610Seric } while (t < 0 && errno == EINTR); 1419610Seric if (t < 0) 1425978Seric { 1439610Seric syserr("getrequests: accept"); 1449610Seric sleep(5); 1459610Seric continue; 1465978Seric } 1474631Seric 1485978Seric /* 1495978Seric ** Create a subprocess to process the mail. 1505978Seric */ 1515978Seric 1525978Seric # ifdef DEBUG 1537677Seric if (tTd(15, 2)) 1549610Seric printf("getrequests: forking (fd = %d)\n", t); 1555978Seric # endif DEBUG 1565978Seric 1574636Seric pid = fork(); 1584636Seric if (pid < 0) 1594631Seric { 1604636Seric syserr("daemon: cannot fork"); 1614636Seric sleep(10); 1629610Seric (void) close(t); 1634636Seric continue; 1644631Seric } 1654631Seric 1664636Seric if (pid == 0) 1674631Seric { 16811147Seric extern struct hostent *gethostbyaddr(); 16911147Seric register struct hostent *hp; 17011147Seric extern char *RealHostName; /* srvrsmtp.c */ 17111147Seric char buf[MAXNAME]; 17211147Seric 1734636Seric /* 1744636Seric ** CHILD -- return to caller. 17511147Seric ** Collect verified idea of sending host. 1764636Seric ** Verify calling user id if possible here. 1774636Seric */ 1784631Seric 17911147Seric /* determine host name */ 18011147Seric hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET); 18111147Seric if (hp != NULL) 18211147Seric (void) sprintf(buf, "%s.ARPA", hp->h_name); 18311147Seric else 18411147Seric /* this should produce a dotted quad */ 18511147Seric (void) sprintf(buf, "%lx", otherend.sin_addr.s_addr); 18611147Seric RealHostName = newstr(buf); 18711147Seric 18810206Seric (void) close(DaemonSocket); 1899610Seric InChannel = fdopen(t, "r"); 1909610Seric OutChannel = fdopen(t, "w"); 1915978Seric # ifdef DEBUG 1927677Seric if (tTd(15, 2)) 1935978Seric printf("getreq: returning\n"); 1945978Seric # endif DEBUG 1957876Seric # ifdef LOG 1967876Seric if (LogLevel > 11) 1977876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 1987876Seric # endif LOG 1994636Seric return; 2004631Seric } 2014631Seric 2024636Seric /* 2034636Seric ** PARENT -- wait for child to terminate. 2044636Seric ** Perhaps we should allow concurrent processing? 2054636Seric */ 2064631Seric 2075978Seric # ifdef DEBUG 2087677Seric if (tTd(15, 2)) 2095978Seric { 2105978Seric sleep(2); 2115978Seric printf("getreq: parent waiting\n"); 2125978Seric } 2135978Seric # endif DEBUG 2145978Seric 2157117Seric /* close the port so that others will hang (for a while) */ 2169610Seric (void) close(t); 2177117Seric 218*13933Seric /* pick up old zombies */ 219*13933Seric while (wait3(&status, WNOHANG, 0) > 0) 220*13933Seric continue; 2214631Seric } 2229886Seric /*NOTREACHED*/ 2234631Seric } 2245978Seric /* 22510206Seric ** CLRDAEMON -- reset the daemon connection 22610206Seric ** 22710206Seric ** Parameters: 22810206Seric ** none. 22910206Seric ** 23010206Seric ** Returns: 23110206Seric ** none. 23210206Seric ** 23310206Seric ** Side Effects: 23410206Seric ** releases any resources used by the passive daemon. 23510206Seric */ 23610206Seric 23710206Seric clrdaemon() 23810206Seric { 23910206Seric if (DaemonSocket >= 0) 24010206Seric (void) close(DaemonSocket); 24110206Seric DaemonSocket = -1; 24210206Seric } 24310206Seric /* 2446039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2456039Seric ** 2466039Seric ** Parameters: 2476039Seric ** host -- the name of the host. 2486633Seric ** port -- the port number to connect to. 2496039Seric ** outfile -- a pointer to a place to put the outfile 2506039Seric ** descriptor. 2516039Seric ** infile -- ditto for infile. 2526039Seric ** 2536039Seric ** Returns: 2546039Seric ** An exit code telling whether the connection could be 2556039Seric ** made and if not why not. 2566039Seric ** 2576039Seric ** Side Effects: 2586039Seric ** none. 2596039Seric */ 2605978Seric 2616633Seric makeconnection(host, port, outfile, infile) 2626039Seric char *host; 2637286Seric u_short port; 2646039Seric FILE **outfile; 2656039Seric FILE **infile; 2666039Seric { 2676039Seric register int s; 2686039Seric 2696039Seric /* 2706039Seric ** Set up the address for the mailer. 2719308Seric ** Accept "[a.b.c.d]" syntax for host name. 2726039Seric */ 2736039Seric 2749308Seric if (host[0] == '[') 2759308Seric { 27611147Seric long hid; 27711147Seric register char *p = index(host, ']'); 2789308Seric 27911147Seric if (p != NULL) 2809308Seric { 28111147Seric *p = '\0'; 28211147Seric hid = inet_addr(&host[1]); 28311147Seric *p = ']'; 2849308Seric } 28511147Seric if (p == NULL || hid == -1) 2869308Seric { 2879308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 2889308Seric return (EX_NOHOST); 2899308Seric } 2909308Seric SendmailAddress.sin_addr.s_addr = hid; 2919308Seric } 2929610Seric else 2939610Seric { 2949610Seric register struct hostent *hp = gethostbyname(host); 2959610Seric 29611147Seric if (hp == NULL) 2979610Seric return (EX_NOHOST); 2989610Seric bmove(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 2999610Seric } 3009610Seric 3019610Seric /* 3029610Seric ** Determine the port number. 3039610Seric */ 3049610Seric 30510011Seric if (port != 0) 30610011Seric SendmailAddress.sin_port = htons(port); 30710011Seric else 3089610Seric { 3099610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3109610Seric 3119610Seric if (sp == NULL) 3129610Seric { 3139610Seric syserr("makeconnection: server \"smtp\" unknown"); 3149610Seric return (EX_OSFILE); 3159610Seric } 31610011Seric SendmailAddress.sin_port = sp->s_port; 3179610Seric } 3186039Seric 3196039Seric /* 3206039Seric ** Try to actually open the connection. 3216039Seric */ 3226039Seric 3236039Seric # ifdef DEBUG 3247677Seric if (tTd(16, 1)) 3256039Seric printf("makeconnection (%s)\n", host); 3266039Seric # endif DEBUG 3276039Seric 3289310Seric s = socket(AF_INET, SOCK_STREAM, 0, 0); 3296039Seric if (s < 0) 3306039Seric { 3316039Seric syserr("makeconnection: no socket"); 3326039Seric goto failure; 3336039Seric } 3346039Seric 3356039Seric # ifdef DEBUG 3367677Seric if (tTd(16, 1)) 3376039Seric printf("makeconnection: %d\n", s); 33810347Seric 33910347Seric /* turn on network debugging? */ 34010347Seric if (tTd(16, 14)) 34110347Seric (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0); 3426039Seric # endif DEBUG 3439546Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 3449610Seric SendmailAddress.sin_family = AF_INET; 3459310Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0) 3466039Seric { 3476039Seric /* failure, decide if temporary or not */ 3486039Seric failure: 3496039Seric switch (errno) 3506039Seric { 3516039Seric case EISCONN: 3526039Seric case ETIMEDOUT: 3536897Seric case EINPROGRESS: 3546897Seric case EALREADY: 3556897Seric case EADDRINUSE: 35610123Seric case EHOSTDOWN: 3576897Seric case ENETDOWN: 3586897Seric case ENETRESET: 3596897Seric case ENOBUFS: 3607204Seric case ECONNREFUSED: 36111546Seric case ECONNRESET: 36210081Seric case EHOSTUNREACH: 36310098Seric case ENETUNREACH: 3646039Seric /* there are others, I'm sure..... */ 3656039Seric return (EX_TEMPFAIL); 3666039Seric 36711147Seric case EPERM: 36811147Seric /* why is this happening? */ 36911147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 37011147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 37111147Seric /* explicit fall-through */ 37211147Seric 3736039Seric default: 3746039Seric return (EX_UNAVAILABLE); 3756039Seric } 3766039Seric } 3776039Seric 3786039Seric /* connection ok, put it into canonical form */ 3796039Seric *outfile = fdopen(s, "w"); 3806039Seric *infile = fdopen(s, "r"); 3816039Seric 38210098Seric return (EX_OK); 3836039Seric } 38410758Seric /* 38510758Seric ** MYHOSTNAME -- return the name of this host. 38610758Seric ** 38710758Seric ** Parameters: 38810758Seric ** hostbuf -- a place to return the name of this host. 38912313Seric ** size -- the size of hostbuf. 39010758Seric ** 39110758Seric ** Returns: 39210758Seric ** A list of aliases for this host. 39310758Seric ** 39410758Seric ** Side Effects: 39510758Seric ** none. 39610758Seric */ 3976039Seric 39810758Seric char ** 39912313Seric myhostname(hostbuf, size) 40010758Seric char hostbuf[]; 40112313Seric int size; 40210758Seric { 40310758Seric extern struct hostent *gethostbyname(); 40411147Seric struct hostent *hp; 40512313Seric auto int i = size; 40611297Seric register char *p; 40710758Seric 40811276Seric gethostname(hostbuf, &i); 40911147Seric hp = gethostbyname(hostbuf); 41011297Seric for (p = hostbuf; *p != '\0'; p++) 41111297Seric if (islower(*p)) 41211297Seric *p -= 'a' - 'A'; 41311147Seric if (hp != NULL) 41411147Seric return (hp->h_aliases); 41510758Seric else 41610758Seric return (NULL); 41710758Seric } 41810758Seric 41910758Seric # else DAEMON 42010758Seric 42110758Seric /* 42210758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 42311297Seric ** 42411297Seric ** Can't convert to upper case here because might be a UUCP name. 42512313Seric ** 42612313Seric ** Mark, you can change this to be anything you want...... 42710758Seric */ 42810758Seric 42910758Seric char ** 43012313Seric myhostname(hostbuf, size) 43110758Seric char hostbuf[]; 43212313Seric int size; 43310758Seric { 43410758Seric register FILE *f; 43510758Seric 43610758Seric hostbuf[0] = '\0'; 43710758Seric f = fopen("/usr/include/whoami", "r"); 43810758Seric if (f != NULL) 43910758Seric { 44012313Seric (void) fgets(hostbuf, size, f); 44110758Seric fixcrlf(hostbuf, TRUE); 44210758Seric (void) fclose(f); 44310758Seric } 44410758Seric return (NULL); 44510758Seric } 44610758Seric 4475978Seric #endif DAEMON 448