16039Seric # include <errno.h> 24535Seric # include "sendmail.h" 34535Seric 45978Seric #ifndef DAEMON 5*8352Seric SCCSID(@(#)daemon.c 3.30 10/07/82 (w/o daemon mode)); 65978Seric #else 74535Seric 85978Seric # include <sys/socket.h> 95978Seric # include <net/in.h> 107117Seric # include <wait.h> 115978Seric 12*8352Seric SCCSID(@(#)daemon.c 3.30 10/07/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 807677Seric 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 1037677Seric if (tTd(15, 2)) 1045978Seric printf("getreq: returning\n"); 1055978Seric # endif DEBUG 1067876Seric # ifdef LOG 1077876Seric if (LogLevel > 11) 1087876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 1097876Seric # endif LOG 1104636Seric return; 1114631Seric } 1124631Seric 1134636Seric /* 1144636Seric ** PARENT -- wait for child to terminate. 1154636Seric ** Perhaps we should allow concurrent processing? 1164636Seric */ 1174631Seric 1185978Seric # ifdef DEBUG 1197677Seric if (tTd(15, 2)) 1205978Seric { 1215978Seric sleep(2); 1225978Seric printf("getreq: parent waiting\n"); 1235978Seric } 1245978Seric # endif DEBUG 1255978Seric 1267117Seric /* close the port so that others will hang (for a while) */ 1277009Seric (void) close(port); 1287117Seric 1297117Seric /* pick up old zombies; implement load limiting */ 1307117Seric numconnections++; 1317117Seric while (wait3(&status, numconnections < MAXCONNS ? WNOHANG : 0, 0) > 0) 1327117Seric numconnections--; 1334631Seric } 1344631Seric } 1355978Seric /* 1365978Seric ** GETCONNECTION -- make a connection with the outside world 1375978Seric ** 1385978Seric ** Parameters: 1395978Seric ** none. 1405978Seric ** 1415978Seric ** Returns: 1425978Seric ** The port for mail traffic. 1435978Seric ** 1445978Seric ** Side Effects: 1455978Seric ** Waits for a connection. 1465978Seric */ 1475978Seric 1487117Seric #define IPPORT_PLAYPORT 3055 /* random number */ 1497117Seric 1505979Seric struct sockaddr_in SendmailAddress = { AF_INET, IPPORT_SMTP }; 1515978Seric 1525978Seric getconnection() 1535978Seric { 1545978Seric register int s; 1555978Seric struct sockaddr otherend; 1565978Seric 1575978Seric /* 1585978Seric ** Set up the address for the mailer. 1595978Seric */ 1605978Seric 1616058Seric SendmailAddress.sin_addr.s_addr = 0; 1626633Seric SendmailAddress.sin_port = IPPORT_SMTP; 1637117Seric # ifdef DEBUG 1647677Seric if (tTd(15, 15)) 1657117Seric SendmailAddress.sin_port = IPPORT_PLAYPORT; 1667117Seric # endif DEBUG 1677117Seric SendmailAddress.sin_port = htons(SendmailAddress.sin_port); 1685978Seric 1695978Seric /* 1705978Seric ** Try to actually open the connection. 1715978Seric */ 1725978Seric 1735978Seric # ifdef DEBUG 1747677Seric if (tTd(15, 1)) 1756058Seric printf("getconnection\n"); 1765978Seric # endif DEBUG 1775978Seric 1788276Seric for (;;) 1797117Seric { 180*8352Seric int i; 181*8352Seric 1827556Seric /* get a socket for the SMTP connection */ 183*8352Seric /* do loop is to avoid 4.1b kernel bug (?) */ 184*8352Seric i = 50; 185*8352Seric do 186*8352Seric { 187*8352Seric s = socket(SOCK_STREAM, 0, &SendmailAddress, SO_ACCEPTCONN); 188*8352Seric if (s < 0) 189*8352Seric sleep(5); 190*8352Seric } while (--i > 0 && s < 0); 1917556Seric if (s < 0) 1927556Seric { 1937556Seric /* probably another daemon already */ 1947556Seric syserr("getconnection: can't create socket"); 1957892Seric return (-1); 1967556Seric } 1975978Seric 1985978Seric # ifdef DEBUG 1997677Seric if (tTd(15, 1)) 2007556Seric printf("getconnection: %d\n", s); 2015978Seric # endif DEBUG 2027556Seric 2037556Seric /* wait for a connection */ 2048276Seric /* contorted code is due to a 4.1a kernel bug */ 2058276Seric errno = 0; 2068276Seric if (accept(s, &otherend) >= 0) 2078276Seric return (s); 2088276Seric (void) close(s); 2098276Seric if (errno != EINTR) 2108276Seric syserr("getconnection: accept"); 2118351Seric sleep(5); 2127117Seric } 2135978Seric } 2146039Seric /* 2156039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2166039Seric ** 2176039Seric ** Parameters: 2186039Seric ** host -- the name of the host. 2196633Seric ** port -- the port number to connect to. 2206039Seric ** outfile -- a pointer to a place to put the outfile 2216039Seric ** descriptor. 2226039Seric ** infile -- ditto for infile. 2236039Seric ** 2246039Seric ** Returns: 2256039Seric ** An exit code telling whether the connection could be 2266039Seric ** made and if not why not. 2276039Seric ** 2286039Seric ** Side Effects: 2296039Seric ** none. 2306039Seric */ 2315978Seric 2326633Seric makeconnection(host, port, outfile, infile) 2336039Seric char *host; 2347286Seric u_short port; 2356039Seric FILE **outfile; 2366039Seric FILE **infile; 2376039Seric { 2386039Seric register int s; 2396039Seric 2406039Seric /* 2416039Seric ** Set up the address for the mailer. 2426039Seric */ 2436039Seric 2446039Seric if ((SendmailAddress.sin_addr.s_addr = rhost(&host)) == -1) 2456039Seric return (EX_NOHOST); 2466633Seric if (port == 0) 2476633Seric port = IPPORT_SMTP; 2487117Seric SendmailAddress.sin_port = htons(port); 2496039Seric 2506039Seric /* 2516039Seric ** Try to actually open the connection. 2526039Seric */ 2536039Seric 2546039Seric # ifdef DEBUG 2557677Seric if (tTd(16, 1)) 2566039Seric printf("makeconnection (%s)\n", host); 2576039Seric # endif DEBUG 2586039Seric 2597009Seric s = socket(SOCK_STREAM, 0, (struct sockaddr_in *) 0, 0); 2606039Seric if (s < 0) 2616039Seric { 2626039Seric syserr("makeconnection: no socket"); 2636039Seric goto failure; 2646039Seric } 2656039Seric 2666039Seric # ifdef DEBUG 2677677Seric if (tTd(16, 1)) 2686039Seric printf("makeconnection: %d\n", s); 2696039Seric # endif DEBUG 2707677Seric (void) fflush(Xscript); /* for debugging */ 2716039Seric if (connect(s, &SendmailAddress) < 0) 2726039Seric { 2736039Seric /* failure, decide if temporary or not */ 2746039Seric failure: 2756039Seric switch (errno) 2766039Seric { 2776039Seric case EISCONN: 2786039Seric case ETIMEDOUT: 2796897Seric case EINPROGRESS: 2806897Seric case EALREADY: 2816897Seric case EADDRINUSE: 2826897Seric case ENETDOWN: 2836897Seric case ENETRESET: 2846897Seric case ENOBUFS: 2857204Seric case ECONNREFUSED: 2866039Seric /* there are others, I'm sure..... */ 2876039Seric return (EX_TEMPFAIL); 2886039Seric 2896039Seric default: 2906039Seric return (EX_UNAVAILABLE); 2916039Seric } 2926039Seric } 2936039Seric 2946039Seric /* connection ok, put it into canonical form */ 2956039Seric *outfile = fdopen(s, "w"); 2966039Seric *infile = fdopen(s, "r"); 2976039Seric 2986039Seric return (0); 2996039Seric } 3006039Seric 3015978Seric #endif DAEMON 302