16039Seric # include <errno.h> 24535Seric # include "sendmail.h" 34535Seric 45978Seric #ifndef DAEMON 5*7677Seric SCCSID(@(#)daemon.c 3.22 08/08/82 (w/o daemon mode)); 65978Seric #else 74535Seric 85978Seric # include <sys/socket.h> 95978Seric # include <net/in.h> 107117Seric # include <wait.h> 115978Seric 12*7677Seric SCCSID(@(#)daemon.c 3.22 08/08/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 { 715978Seric syserr("getrequests: getconnection failed"); 727556Seric finis(); 735978Seric } 744631Seric 755978Seric /* 765978Seric ** Create a subprocess to process the mail. 775978Seric */ 785978Seric 795978Seric # ifdef DEBUG 80*7677Seric if (tTd(15, 2)) 815978Seric printf("getrequests: forking (port = %d)\n", port); 825978Seric # endif DEBUG 835978Seric 844636Seric pid = fork(); 854636Seric if (pid < 0) 864631Seric { 874636Seric syserr("daemon: cannot fork"); 884636Seric sleep(10); 897009Seric (void) close(port); 904636Seric continue; 914631Seric } 924631Seric 934636Seric if (pid == 0) 944631Seric { 954636Seric /* 964636Seric ** CHILD -- return to caller. 974636Seric ** Verify calling user id if possible here. 984636Seric */ 994631Seric 1005978Seric InChannel = fdopen(port, "r"); 1015978Seric OutChannel = fdopen(port, "w"); 1025978Seric # ifdef DEBUG 103*7677Seric if (tTd(15, 2)) 1045978Seric printf("getreq: returning\n"); 1055978Seric # endif DEBUG 1064636Seric return; 1074631Seric } 1084631Seric 1094636Seric /* 1104636Seric ** PARENT -- wait for child to terminate. 1114636Seric ** Perhaps we should allow concurrent processing? 1124636Seric */ 1134631Seric 1145978Seric # ifdef DEBUG 115*7677Seric if (tTd(15, 2)) 1165978Seric { 1175978Seric sleep(2); 1185978Seric printf("getreq: parent waiting\n"); 1195978Seric } 1205978Seric # endif DEBUG 1215978Seric 1227117Seric /* close the port so that others will hang (for a while) */ 1237009Seric (void) close(port); 1247117Seric 1257117Seric /* pick up old zombies; implement load limiting */ 1267117Seric numconnections++; 1277117Seric while (wait3(&status, numconnections < MAXCONNS ? WNOHANG : 0, 0) > 0) 1287117Seric numconnections--; 1294631Seric } 1304631Seric } 1315978Seric /* 1325978Seric ** GETCONNECTION -- make a connection with the outside world 1335978Seric ** 1345978Seric ** Parameters: 1355978Seric ** none. 1365978Seric ** 1375978Seric ** Returns: 1385978Seric ** The port for mail traffic. 1395978Seric ** 1405978Seric ** Side Effects: 1415978Seric ** Waits for a connection. 1425978Seric */ 1435978Seric 1447117Seric #define IPPORT_PLAYPORT 3055 /* random number */ 1457117Seric 1465979Seric struct sockaddr_in SendmailAddress = { AF_INET, IPPORT_SMTP }; 1475978Seric 1485978Seric getconnection() 1495978Seric { 1505978Seric register int s; 1515978Seric struct sockaddr otherend; 1525978Seric 1535978Seric /* 1545978Seric ** Set up the address for the mailer. 1555978Seric */ 1565978Seric 1576058Seric SendmailAddress.sin_addr.s_addr = 0; 1586633Seric SendmailAddress.sin_port = IPPORT_SMTP; 1597117Seric # ifdef DEBUG 160*7677Seric if (tTd(15, 15)) 1617117Seric SendmailAddress.sin_port = IPPORT_PLAYPORT; 1627117Seric # endif DEBUG 1637117Seric SendmailAddress.sin_port = htons(SendmailAddress.sin_port); 1645978Seric 1655978Seric /* 1665978Seric ** Try to actually open the connection. 1675978Seric */ 1685978Seric 1695978Seric # ifdef DEBUG 170*7677Seric if (tTd(15, 1)) 1716058Seric printf("getconnection\n"); 1725978Seric # endif DEBUG 1735978Seric 1747556Seric for (;;) 1757117Seric { 1767556Seric /* get a socket for the SMTP connection */ 1777556Seric s = socket(SOCK_STREAM, 0, &SendmailAddress, SO_ACCEPTCONN); 1787556Seric if (s < 0) 1797556Seric { 1807556Seric /* probably another daemon already */ 1817556Seric syserr("getconnection: can't create socket"); 1827556Seric break; 1837556Seric } 1845978Seric 1855978Seric # ifdef DEBUG 186*7677Seric if (tTd(15, 1)) 1877556Seric printf("getconnection: %d\n", s); 1885978Seric # endif DEBUG 1897556Seric 1907556Seric /* wait for a connection */ 1917556Seric if (accept(s, &otherend) >= 0) 1927556Seric break; 1937556Seric 1947556Seric /* probably innocuous -- retry */ 1957614Seric if (errno == ETIMEDOUT) 1967614Seric continue; 1977556Seric syserr("getconnection: accept"); 1987286Seric (void) close(s); 1997556Seric sleep(20); 2007117Seric } 2015978Seric 2025978Seric return (s); 2035978Seric } 2046039Seric /* 2056039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2066039Seric ** 2076039Seric ** Parameters: 2086039Seric ** host -- the name of the host. 2096633Seric ** port -- the port number to connect to. 2106039Seric ** outfile -- a pointer to a place to put the outfile 2116039Seric ** descriptor. 2126039Seric ** infile -- ditto for infile. 2136039Seric ** 2146039Seric ** Returns: 2156039Seric ** An exit code telling whether the connection could be 2166039Seric ** made and if not why not. 2176039Seric ** 2186039Seric ** Side Effects: 2196039Seric ** none. 2206039Seric */ 2215978Seric 2226633Seric makeconnection(host, port, outfile, infile) 2236039Seric char *host; 2247286Seric u_short port; 2256039Seric FILE **outfile; 2266039Seric FILE **infile; 2276039Seric { 2286039Seric register int s; 2296039Seric 2306039Seric /* 2316039Seric ** Set up the address for the mailer. 2326039Seric */ 2336039Seric 2346039Seric if ((SendmailAddress.sin_addr.s_addr = rhost(&host)) == -1) 2356039Seric return (EX_NOHOST); 2366633Seric if (port == 0) 2376633Seric port = IPPORT_SMTP; 2387117Seric SendmailAddress.sin_port = htons(port); 2396039Seric 2406039Seric /* 2416039Seric ** Try to actually open the connection. 2426039Seric */ 2436039Seric 2446039Seric # ifdef DEBUG 245*7677Seric if (tTd(16, 1)) 2466039Seric printf("makeconnection (%s)\n", host); 2476039Seric # endif DEBUG 2486039Seric 2497009Seric s = socket(SOCK_STREAM, 0, (struct sockaddr_in *) 0, 0); 2506039Seric if (s < 0) 2516039Seric { 2526039Seric syserr("makeconnection: no socket"); 2536039Seric goto failure; 2546039Seric } 2556039Seric 2566039Seric # ifdef DEBUG 257*7677Seric if (tTd(16, 1)) 2586039Seric printf("makeconnection: %d\n", s); 2596039Seric # endif DEBUG 260*7677Seric (void) fflush(Xscript); /* for debugging */ 2616039Seric if (connect(s, &SendmailAddress) < 0) 2626039Seric { 2636039Seric /* failure, decide if temporary or not */ 2646039Seric failure: 2656039Seric switch (errno) 2666039Seric { 2676039Seric case EISCONN: 2686039Seric case ETIMEDOUT: 2696897Seric case EINPROGRESS: 2706897Seric case EALREADY: 2716897Seric case EADDRINUSE: 2726897Seric case ENETDOWN: 2736897Seric case ENETRESET: 2746897Seric case ENOBUFS: 2757204Seric case ECONNREFUSED: 2766039Seric /* there are others, I'm sure..... */ 2776039Seric return (EX_TEMPFAIL); 2786039Seric 2796039Seric default: 2806039Seric return (EX_UNAVAILABLE); 2816039Seric } 2826039Seric } 2836039Seric 2846039Seric /* connection ok, put it into canonical form */ 2856039Seric *outfile = fdopen(s, "w"); 2866039Seric *infile = fdopen(s, "r"); 2876039Seric 2886039Seric return (0); 2896039Seric } 2906039Seric 2915978Seric #endif DAEMON 292