16039Seric # include <errno.h> 24535Seric # include "sendmail.h" 34535Seric 45978Seric #ifndef DAEMON 5*9308Seric SCCSID(@(#)daemon.c 3.33 11/20/82 (w/o daemon mode)); 65978Seric #else 74535Seric 85978Seric # include <sys/socket.h> 95978Seric # include <net/in.h> 107117Seric # include <wait.h> 115978Seric 12*9308Seric SCCSID(@(#)daemon.c 3.33 11/20/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 ** 1418822Seric ** This routine is horribly contorted to try to get around a bunch 1428822Seric ** of 4.1a IPC bugs. There appears to be nothing we can do to make 1438822Seric ** it "right" -- the code to interrupt accepts just doesn't work 1448822Seric ** right. However, this is an attempt to minimize the probablity 1458822Seric ** of problems. 1468822Seric ** 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 1878822Seric 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 (?) */ 1938822Seric i = 60; 1948352Seric do 1958352Seric { 1968352Seric s = socket(SOCK_STREAM, 0, &SendmailAddress, SO_ACCEPTCONN); 1978352Seric if (s < 0) 1988822Seric 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 */ 2138822Seric do 2148822Seric { 2158822Seric errno = 0; 2168822Seric if (accept(s, &otherend) >= 0) 2178822Seric return (s); 2188822Seric } while (errno == EINTR); 2198822Seric syserr("getconnection: accept"); 2208822Seric 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. 252*9308Seric ** Accept "[a.b.c.d]" syntax for host name. 2536039Seric */ 2546039Seric 255*9308Seric if (host[0] == '[') 256*9308Seric { 257*9308Seric long hid = 0; 258*9308Seric int i, j; 259*9308Seric register char *p = host; 260*9308Seric 261*9308Seric for (i = 3; i >= 0 && *p != ']' && *p != '\0'; i--) 262*9308Seric { 263*9308Seric j = 0; 264*9308Seric while (isdigit(*++p)) 265*9308Seric j = j * 10 + (*p - '0'); 266*9308Seric if (*p != (i == 0 ? ']' : '.') || j > 255 || j < 0) 267*9308Seric break; 268*9308Seric hid |= j << ((3 - i) * 8); 269*9308Seric } 270*9308Seric if (i >= 0 || *p != ']' || *++p != '\0') 271*9308Seric { 272*9308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 273*9308Seric return (EX_NOHOST); 274*9308Seric } 275*9308Seric SendmailAddress.sin_addr.s_addr = hid; 276*9308Seric } 277*9308Seric else if ((SendmailAddress.sin_addr.s_addr = rhost(&host)) == -1) 2786039Seric return (EX_NOHOST); 2796633Seric if (port == 0) 2806633Seric port = IPPORT_SMTP; 2817117Seric SendmailAddress.sin_port = htons(port); 2826039Seric 2836039Seric /* 2846039Seric ** Try to actually open the connection. 2856039Seric */ 2866039Seric 2876039Seric # ifdef DEBUG 2887677Seric if (tTd(16, 1)) 2896039Seric printf("makeconnection (%s)\n", host); 2906039Seric # endif DEBUG 2916039Seric 2927009Seric s = socket(SOCK_STREAM, 0, (struct sockaddr_in *) 0, 0); 2936039Seric if (s < 0) 2946039Seric { 2956039Seric syserr("makeconnection: no socket"); 2966039Seric goto failure; 2976039Seric } 2986039Seric 2996039Seric # ifdef DEBUG 3007677Seric if (tTd(16, 1)) 3016039Seric printf("makeconnection: %d\n", s); 3026039Seric # endif DEBUG 3037677Seric (void) fflush(Xscript); /* for debugging */ 3046039Seric if (connect(s, &SendmailAddress) < 0) 3056039Seric { 3066039Seric /* failure, decide if temporary or not */ 3076039Seric failure: 3086039Seric switch (errno) 3096039Seric { 3106039Seric case EISCONN: 3116039Seric case ETIMEDOUT: 3126897Seric case EINPROGRESS: 3136897Seric case EALREADY: 3146897Seric case EADDRINUSE: 3156897Seric case ENETDOWN: 3166897Seric case ENETRESET: 3176897Seric case ENOBUFS: 3187204Seric case ECONNREFUSED: 3196039Seric /* there are others, I'm sure..... */ 3206039Seric return (EX_TEMPFAIL); 3216039Seric 3226039Seric default: 3236039Seric return (EX_UNAVAILABLE); 3246039Seric } 3256039Seric } 3266039Seric 3276039Seric /* connection ok, put it into canonical form */ 3286039Seric *outfile = fdopen(s, "w"); 3296039Seric *infile = fdopen(s, "r"); 3306039Seric 3316039Seric return (0); 3326039Seric } 3336039Seric 3345978Seric #endif DAEMON 335