16039Seric # include <errno.h> 24535Seric # include "sendmail.h" 34535Seric 45978Seric #ifndef DAEMON 5*8822Seric SCCSID(@(#)daemon.c 3.32 10/22/82 (w/o daemon mode)); 65978Seric #else 74535Seric 85978Seric # include <sys/socket.h> 95978Seric # include <net/in.h> 107117Seric # include <wait.h> 115978Seric 12*8822Seric SCCSID(@(#)daemon.c 3.32 10/22/82 (with daemon mode)); 135978Seric 144535Seric /* 154535Seric ** DAEMON.C -- routines to use when running as a daemon. 167556Seric ** 177556Seric ** This entire file is highly dependent on the 4.2 BSD 187556Seric ** interprocess communication primitives. No attempt has 197556Seric ** been made to make this file portable to Version 7, 207556Seric ** Version 6, MPX files, etc. If you should try such a 217556Seric ** thing yourself, I recommend chucking the entire file 227556Seric ** and starting from scratch. Basic semantics are: 237556Seric ** 247556Seric ** getrequests() 257556Seric ** Opens a port and initiates a connection. 267556Seric ** Returns in a child. Must set InChannel and 277556Seric ** OutChannel appropriately. 287556Seric ** makeconnection(host, port, outfile, infile) 297556Seric ** Make a connection to the named host on the given 307556Seric ** port. Set *outfile and *infile to the files 317556Seric ** appropriate for communication. Returns zero on 327556Seric ** success, else an exit status describing the 337556Seric ** error. 347556Seric ** 357556Seric ** The semantics of both of these should be clean. 364535Seric */ 374535Seric /* 384535Seric ** GETREQUESTS -- open mail IPC port and get requests. 394535Seric ** 404535Seric ** Parameters: 414535Seric ** none. 424535Seric ** 434535Seric ** Returns: 444535Seric ** none. 454535Seric ** 464535Seric ** Side Effects: 474535Seric ** Waits until some interesting activity occurs. When 484535Seric ** it does, a child is created to process it, and the 494535Seric ** parent waits for completion. Return from this 504535Seric ** routine is always in the child. 514535Seric */ 524535Seric 537117Seric # define MAXCONNS 4 /* maximum simultaneous sendmails */ 547117Seric 554535Seric getrequests() 564535Seric { 577117Seric union wait status; 587117Seric int numconnections = 0; 597117Seric 604631Seric for (;;) 614631Seric { 624636Seric register int pid; 635978Seric register int port; 644631Seric 654636Seric /* 664636Seric ** Wait for a connection. 674636Seric */ 684631Seric 695978Seric while ((port = getconnection()) < 0) 705978Seric { 718518Seric # ifdef LOG 728518Seric if (LogLevel > 0) 738518Seric syslog(LOG_SALERT, "cannot get connection"); 748518Seric # endif LOG 757556Seric finis(); 765978Seric } 774631Seric 785978Seric /* 795978Seric ** Create a subprocess to process the mail. 805978Seric */ 815978Seric 825978Seric # ifdef DEBUG 837677Seric if (tTd(15, 2)) 845978Seric printf("getrequests: forking (port = %d)\n", port); 855978Seric # endif DEBUG 865978Seric 874636Seric pid = fork(); 884636Seric if (pid < 0) 894631Seric { 904636Seric syserr("daemon: cannot fork"); 914636Seric sleep(10); 927009Seric (void) close(port); 934636Seric continue; 944631Seric } 954631Seric 964636Seric if (pid == 0) 974631Seric { 984636Seric /* 994636Seric ** CHILD -- return to caller. 1004636Seric ** Verify calling user id if possible here. 1014636Seric */ 1024631Seric 1035978Seric InChannel = fdopen(port, "r"); 1045978Seric OutChannel = fdopen(port, "w"); 1055978Seric # ifdef DEBUG 1067677Seric if (tTd(15, 2)) 1075978Seric printf("getreq: returning\n"); 1085978Seric # endif DEBUG 1097876Seric # ifdef LOG 1107876Seric if (LogLevel > 11) 1117876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 1127876Seric # endif LOG 1134636Seric return; 1144631Seric } 1154631Seric 1164636Seric /* 1174636Seric ** PARENT -- wait for child to terminate. 1184636Seric ** Perhaps we should allow concurrent processing? 1194636Seric */ 1204631Seric 1215978Seric # ifdef DEBUG 1227677Seric if (tTd(15, 2)) 1235978Seric { 1245978Seric sleep(2); 1255978Seric printf("getreq: parent waiting\n"); 1265978Seric } 1275978Seric # endif DEBUG 1285978Seric 1297117Seric /* close the port so that others will hang (for a while) */ 1307009Seric (void) close(port); 1317117Seric 1327117Seric /* pick up old zombies; implement load limiting */ 1337117Seric numconnections++; 1347117Seric while (wait3(&status, numconnections < MAXCONNS ? WNOHANG : 0, 0) > 0) 1357117Seric numconnections--; 1364631Seric } 1374631Seric } 1385978Seric /* 1395978Seric ** GETCONNECTION -- make a connection with the outside world 1405978Seric ** 141*8822Seric ** This routine is horribly contorted to try to get around a bunch 142*8822Seric ** of 4.1a IPC bugs. There appears to be nothing we can do to make 143*8822Seric ** it "right" -- the code to interrupt accepts just doesn't work 144*8822Seric ** right. However, this is an attempt to minimize the probablity 145*8822Seric ** of problems. 146*8822Seric ** 1475978Seric ** Parameters: 1485978Seric ** none. 1495978Seric ** 1505978Seric ** Returns: 1515978Seric ** The port for mail traffic. 1525978Seric ** 1535978Seric ** Side Effects: 1545978Seric ** Waits for a connection. 1555978Seric */ 1565978Seric 1577117Seric #define IPPORT_PLAYPORT 3055 /* random number */ 1587117Seric 1595979Seric struct sockaddr_in SendmailAddress = { AF_INET, IPPORT_SMTP }; 1605978Seric 1615978Seric getconnection() 1625978Seric { 1635978Seric register int s; 1645978Seric struct sockaddr otherend; 1655978Seric 1665978Seric /* 1675978Seric ** Set up the address for the mailer. 1685978Seric */ 1695978Seric 1706058Seric SendmailAddress.sin_addr.s_addr = 0; 1716633Seric SendmailAddress.sin_port = IPPORT_SMTP; 1727117Seric # ifdef DEBUG 1737677Seric if (tTd(15, 15)) 1747117Seric SendmailAddress.sin_port = IPPORT_PLAYPORT; 1757117Seric # endif DEBUG 1767117Seric SendmailAddress.sin_port = htons(SendmailAddress.sin_port); 1775978Seric 1785978Seric /* 1795978Seric ** Try to actually open the connection. 1805978Seric */ 1815978Seric 1825978Seric # ifdef DEBUG 1837677Seric if (tTd(15, 1)) 1846058Seric printf("getconnection\n"); 1855978Seric # endif DEBUG 1865978Seric 187*8822Seric for (;; sleep(15)) 1887117Seric { 1898352Seric int i; 1908352Seric 1917556Seric /* get a socket for the SMTP connection */ 1928352Seric /* do loop is to avoid 4.1b kernel bug (?) */ 193*8822Seric i = 60; 1948352Seric do 1958352Seric { 1968352Seric s = socket(SOCK_STREAM, 0, &SendmailAddress, SO_ACCEPTCONN); 1978352Seric if (s < 0) 198*8822Seric sleep(10); 1998352Seric } while (--i > 0 && s < 0); 2007556Seric if (s < 0) 2017556Seric { 2027556Seric /* probably another daemon already */ 2037556Seric syserr("getconnection: can't create socket"); 2047892Seric return (-1); 2057556Seric } 2065978Seric 2075978Seric # ifdef DEBUG 2087677Seric if (tTd(15, 1)) 2097556Seric printf("getconnection: %d\n", s); 2105978Seric # endif DEBUG 2117556Seric 2127556Seric /* wait for a connection */ 213*8822Seric do 214*8822Seric { 215*8822Seric errno = 0; 216*8822Seric if (accept(s, &otherend) >= 0) 217*8822Seric return (s); 218*8822Seric } while (errno == EINTR); 219*8822Seric syserr("getconnection: accept"); 220*8822Seric sleep(5); 2218276Seric (void) close(s); 2227117Seric } 2235978Seric } 2246039Seric /* 2256039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2266039Seric ** 2276039Seric ** Parameters: 2286039Seric ** host -- the name of the host. 2296633Seric ** port -- the port number to connect to. 2306039Seric ** outfile -- a pointer to a place to put the outfile 2316039Seric ** descriptor. 2326039Seric ** infile -- ditto for infile. 2336039Seric ** 2346039Seric ** Returns: 2356039Seric ** An exit code telling whether the connection could be 2366039Seric ** made and if not why not. 2376039Seric ** 2386039Seric ** Side Effects: 2396039Seric ** none. 2406039Seric */ 2415978Seric 2426633Seric makeconnection(host, port, outfile, infile) 2436039Seric char *host; 2447286Seric u_short port; 2456039Seric FILE **outfile; 2466039Seric FILE **infile; 2476039Seric { 2486039Seric register int s; 2496039Seric 2506039Seric /* 2516039Seric ** Set up the address for the mailer. 2526039Seric */ 2536039Seric 2546039Seric if ((SendmailAddress.sin_addr.s_addr = rhost(&host)) == -1) 2556039Seric return (EX_NOHOST); 2566633Seric if (port == 0) 2576633Seric port = IPPORT_SMTP; 2587117Seric SendmailAddress.sin_port = htons(port); 2596039Seric 2606039Seric /* 2616039Seric ** Try to actually open the connection. 2626039Seric */ 2636039Seric 2646039Seric # ifdef DEBUG 2657677Seric if (tTd(16, 1)) 2666039Seric printf("makeconnection (%s)\n", host); 2676039Seric # endif DEBUG 2686039Seric 2697009Seric s = socket(SOCK_STREAM, 0, (struct sockaddr_in *) 0, 0); 2706039Seric if (s < 0) 2716039Seric { 2726039Seric syserr("makeconnection: no socket"); 2736039Seric goto failure; 2746039Seric } 2756039Seric 2766039Seric # ifdef DEBUG 2777677Seric if (tTd(16, 1)) 2786039Seric printf("makeconnection: %d\n", s); 2796039Seric # endif DEBUG 2807677Seric (void) fflush(Xscript); /* for debugging */ 2816039Seric if (connect(s, &SendmailAddress) < 0) 2826039Seric { 2836039Seric /* failure, decide if temporary or not */ 2846039Seric failure: 2856039Seric switch (errno) 2866039Seric { 2876039Seric case EISCONN: 2886039Seric case ETIMEDOUT: 2896897Seric case EINPROGRESS: 2906897Seric case EALREADY: 2916897Seric case EADDRINUSE: 2926897Seric case ENETDOWN: 2936897Seric case ENETRESET: 2946897Seric case ENOBUFS: 2957204Seric case ECONNREFUSED: 2966039Seric /* there are others, I'm sure..... */ 2976039Seric return (EX_TEMPFAIL); 2986039Seric 2996039Seric default: 3006039Seric return (EX_UNAVAILABLE); 3016039Seric } 3026039Seric } 3036039Seric 3046039Seric /* connection ok, put it into canonical form */ 3056039Seric *outfile = fdopen(s, "w"); 3066039Seric *infile = fdopen(s, "r"); 3076039Seric 3086039Seric return (0); 3096039Seric } 3106039Seric 3115978Seric #endif DAEMON 312