17aa493c5Seric # include <errno.h> 27fa39d90Seric # include "sendmail.h" 37fa39d90Seric 4d0a9e852Seric #ifndef DAEMON 5*897f1869Seric SCCSID(@(#)daemon.c 3.53 05/07/83 (w/o daemon mode)); 6d0a9e852Seric #else 7d0a9e852Seric 8d0a9e852Seric #include <sys/socket.h> 91c71e510Seric #include <netinet/in.h> 101c71e510Seric #include <netdb.h> 112554d49fSeric #include <wait.h> 12d0a9e852Seric 13*897f1869Seric SCCSID(@(#)daemon.c 3.53 05/07/83 (with daemon mode)); 147fa39d90Seric 157fa39d90Seric /* 167fa39d90Seric ** DAEMON.C -- routines to use when running as a daemon. 1747b12ae1Seric ** 1847b12ae1Seric ** This entire file is highly dependent on the 4.2 BSD 1947b12ae1Seric ** interprocess communication primitives. No attempt has 2047b12ae1Seric ** been made to make this file portable to Version 7, 2147b12ae1Seric ** Version 6, MPX files, etc. If you should try such a 2247b12ae1Seric ** thing yourself, I recommend chucking the entire file 2347b12ae1Seric ** and starting from scratch. Basic semantics are: 2447b12ae1Seric ** 2547b12ae1Seric ** getrequests() 2647b12ae1Seric ** Opens a port and initiates a connection. 2747b12ae1Seric ** Returns in a child. Must set InChannel and 2847b12ae1Seric ** OutChannel appropriately. 29b7d7afcbSeric ** clrdaemon() 30b7d7afcbSeric ** Close any open files associated with getting 31b7d7afcbSeric ** the connection; this is used when running the queue, 32b7d7afcbSeric ** etc., to avoid having extra file descriptors during 33b7d7afcbSeric ** the queue run and to avoid confusing the network 34b7d7afcbSeric ** code (if it cares). 3547b12ae1Seric ** makeconnection(host, port, outfile, infile) 3647b12ae1Seric ** Make a connection to the named host on the given 3747b12ae1Seric ** port. Set *outfile and *infile to the files 3847b12ae1Seric ** appropriate for communication. Returns zero on 3947b12ae1Seric ** success, else an exit status describing the 4047b12ae1Seric ** error. 4147b12ae1Seric ** 4247b12ae1Seric ** The semantics of both of these should be clean. 437fa39d90Seric */ 447fa39d90Seric /* 457fa39d90Seric ** GETREQUESTS -- open mail IPC port and get requests. 467fa39d90Seric ** 477fa39d90Seric ** Parameters: 487fa39d90Seric ** none. 497fa39d90Seric ** 507fa39d90Seric ** Returns: 517fa39d90Seric ** none. 527fa39d90Seric ** 537fa39d90Seric ** Side Effects: 547fa39d90Seric ** Waits until some interesting activity occurs. When 557fa39d90Seric ** it does, a child is created to process it, and the 567fa39d90Seric ** parent waits for completion. Return from this 57147303b1Seric ** routine is always in the child. The file pointers 58147303b1Seric ** "InChannel" and "OutChannel" should be set to point 59147303b1Seric ** to the communication channel. 607fa39d90Seric */ 617fa39d90Seric 62b7d7afcbSeric struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 63b7d7afcbSeric int DaemonSocket = -1; /* fd describing socket */ 6495639a73Seric int MaxConnections = 4; /* maximum simultaneous sendmails */ 651c71e510Seric 667fa39d90Seric getrequests() 677fa39d90Seric { 681c71e510Seric int t; 692554d49fSeric union wait status; 702554d49fSeric int numconnections = 0; 711c71e510Seric register struct servent *sp; 72eb889047Seric 73a8268164Seric /* 741c71e510Seric ** Set up the address for the mailer. 75eb889047Seric */ 76eb889047Seric 771c71e510Seric sp = getservbyname("smtp", "tcp"); 781c71e510Seric if (sp == NULL) 79d0a9e852Seric { 801c71e510Seric syserr("server \"smtp\" unknown"); 81a1961f2aSeric goto severe; 821c71e510Seric } 831c71e510Seric SendmailAddress.sin_family = AF_INET; 841c71e510Seric SendmailAddress.sin_addr.s_addr = INADDR_ANY; 85f43fa3c2Ssam SendmailAddress.sin_port = sp->s_port; 861c71e510Seric 871c71e510Seric /* 881c71e510Seric ** Try to actually open the connection. 891c71e510Seric */ 901c71e510Seric 911c71e510Seric # ifdef DEBUG 921c71e510Seric if (tTd(15, 1)) 931c71e510Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 941c71e510Seric # endif DEBUG 951c71e510Seric 961c71e510Seric /* get a socket for the SMTP connection */ 97b7d7afcbSeric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0, 0); 98b7d7afcbSeric if (DaemonSocket < 0) 991c71e510Seric { 1001c71e510Seric /* probably another daemon already */ 1011c71e510Seric syserr("getrequests: can't create socket"); 1021c71e510Seric severe: 103b0ba8827Seric # ifdef LOG 104b0ba8827Seric if (LogLevel > 0) 105b0ba8827Seric syslog(LOG_SALERT, "cannot get connection"); 106b0ba8827Seric # endif LOG 10747b12ae1Seric finis(); 108d0a9e852Seric } 1091b6e4a15Seric 1101b6e4a15Seric #ifdef DEBUG 1111b6e4a15Seric /* turn on network debugging? */ 1121b6e4a15Seric if (tTd(15, 15)) 1131b6e4a15Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 0, 0); 1141b6e4a15Seric #endif DEBUG 1151b6e4a15Seric 116b7d7afcbSeric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress, 0) < 0) 1171c71e510Seric { 1181c71e510Seric syserr("getrequests: cannot bind"); 119b7d7afcbSeric (void) close(DaemonSocket); 1201c71e510Seric goto severe; 1211c71e510Seric } 122b7d7afcbSeric listen(DaemonSocket, 10); 1231c71e510Seric 1241c71e510Seric # ifdef DEBUG 1251c71e510Seric if (tTd(15, 1)) 126b7d7afcbSeric printf("getrequests: %d\n", DaemonSocket); 1271c71e510Seric # endif DEBUG 1281c71e510Seric 1291c71e510Seric for (;;) 1301c71e510Seric { 131a44d5a5eSeric auto int lotherend; 132a44d5a5eSeric struct sockaddr_in otherend; 133a44d5a5eSeric 1341c71e510Seric /* wait for a connection */ 1351c71e510Seric register int pid; 1361c71e510Seric 1371c71e510Seric do 1381c71e510Seric { 1391c71e510Seric errno = 0; 1401c71e510Seric lotherend = sizeof otherend; 141b7d7afcbSeric t = accept(DaemonSocket, &otherend, &lotherend, 0); 1421c71e510Seric } while (t < 0 && errno == EINTR); 1431c71e510Seric if (t < 0) 1441c71e510Seric { 1451c71e510Seric syserr("getrequests: accept"); 1461c71e510Seric sleep(5); 1471c71e510Seric continue; 1481c71e510Seric } 149d0a9e852Seric 150d0a9e852Seric /* 151d0a9e852Seric ** Create a subprocess to process the mail. 152d0a9e852Seric */ 153d0a9e852Seric 154d0a9e852Seric # ifdef DEBUG 15561e4310fSeric if (tTd(15, 2)) 1561c71e510Seric printf("getrequests: forking (fd = %d)\n", t); 157d0a9e852Seric # endif DEBUG 158eb889047Seric 159a8268164Seric pid = fork(); 160a8268164Seric if (pid < 0) 161a8268164Seric { 162a8268164Seric syserr("daemon: cannot fork"); 163a8268164Seric sleep(10); 1641c71e510Seric (void) close(t); 165a8268164Seric continue; 166a8268164Seric } 167a8268164Seric 168a8268164Seric if (pid == 0) 169a8268164Seric { 170a44d5a5eSeric extern struct hostent *gethostbyaddr(); 171a44d5a5eSeric register struct hostent *hp; 172a44d5a5eSeric extern char *RealHostName; /* srvrsmtp.c */ 173a44d5a5eSeric char buf[MAXNAME]; 174a44d5a5eSeric 175a8268164Seric /* 176a8268164Seric ** CHILD -- return to caller. 177a44d5a5eSeric ** Collect verified idea of sending host. 178a8268164Seric ** Verify calling user id if possible here. 179a8268164Seric */ 180a8268164Seric 181a44d5a5eSeric /* determine host name */ 182a44d5a5eSeric hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET); 183a44d5a5eSeric if (hp != NULL) 184a44d5a5eSeric (void) sprintf(buf, "%s.ARPA", hp->h_name); 185a44d5a5eSeric else 186a44d5a5eSeric /* this should produce a dotted quad */ 187a44d5a5eSeric (void) sprintf(buf, "%lx", otherend.sin_addr.s_addr); 188a44d5a5eSeric RealHostName = newstr(buf); 189a44d5a5eSeric 190b7d7afcbSeric (void) close(DaemonSocket); 1911c71e510Seric InChannel = fdopen(t, "r"); 1921c71e510Seric OutChannel = fdopen(t, "w"); 193d0a9e852Seric # ifdef DEBUG 19461e4310fSeric if (tTd(15, 2)) 195d0a9e852Seric printf("getreq: returning\n"); 196d0a9e852Seric # endif DEBUG 197252c8a22Seric # ifdef LOG 198252c8a22Seric if (LogLevel > 11) 199252c8a22Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 200252c8a22Seric # endif LOG 201a8268164Seric return; 202a8268164Seric } 203a8268164Seric 204a8268164Seric /* 205a8268164Seric ** PARENT -- wait for child to terminate. 206a8268164Seric ** Perhaps we should allow concurrent processing? 207a8268164Seric */ 208a8268164Seric 209d0a9e852Seric # ifdef DEBUG 21061e4310fSeric if (tTd(15, 2)) 211d0a9e852Seric { 212d0a9e852Seric sleep(2); 213d0a9e852Seric printf("getreq: parent waiting\n"); 214d0a9e852Seric } 215d0a9e852Seric # endif DEBUG 216d0a9e852Seric 2172554d49fSeric /* close the port so that others will hang (for a while) */ 2181c71e510Seric (void) close(t); 2192554d49fSeric 2202554d49fSeric /* pick up old zombies; implement load limiting */ 2212554d49fSeric numconnections++; 22295639a73Seric while (wait3(&status, numconnections < MaxConnections ? WNOHANG : 0, 0) > 0) 2232554d49fSeric numconnections--; 224a8268164Seric } 225147303b1Seric /*NOTREACHED*/ 2267fa39d90Seric } 227d0a9e852Seric /* 228b7d7afcbSeric ** CLRDAEMON -- reset the daemon connection 229b7d7afcbSeric ** 230b7d7afcbSeric ** Parameters: 231b7d7afcbSeric ** none. 232b7d7afcbSeric ** 233b7d7afcbSeric ** Returns: 234b7d7afcbSeric ** none. 235b7d7afcbSeric ** 236b7d7afcbSeric ** Side Effects: 237b7d7afcbSeric ** releases any resources used by the passive daemon. 238b7d7afcbSeric */ 239b7d7afcbSeric 240b7d7afcbSeric clrdaemon() 241b7d7afcbSeric { 242b7d7afcbSeric if (DaemonSocket >= 0) 243b7d7afcbSeric (void) close(DaemonSocket); 244b7d7afcbSeric DaemonSocket = -1; 245b7d7afcbSeric } 246b7d7afcbSeric /* 2477aa493c5Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2487aa493c5Seric ** 2497aa493c5Seric ** Parameters: 2507aa493c5Seric ** host -- the name of the host. 25148ff0a9dSeric ** port -- the port number to connect to. 2527aa493c5Seric ** outfile -- a pointer to a place to put the outfile 2537aa493c5Seric ** descriptor. 2547aa493c5Seric ** infile -- ditto for infile. 2557aa493c5Seric ** 2567aa493c5Seric ** Returns: 2577aa493c5Seric ** An exit code telling whether the connection could be 2587aa493c5Seric ** made and if not why not. 2597aa493c5Seric ** 2607aa493c5Seric ** Side Effects: 2617aa493c5Seric ** none. 2627aa493c5Seric */ 2637aa493c5Seric 26448ff0a9dSeric makeconnection(host, port, outfile, infile) 2657aa493c5Seric char *host; 266210215eaSeric u_short port; 2677aa493c5Seric FILE **outfile; 2687aa493c5Seric FILE **infile; 2697aa493c5Seric { 2707aa493c5Seric register int s; 2717aa493c5Seric 2727aa493c5Seric /* 2737aa493c5Seric ** Set up the address for the mailer. 27471096d12Seric ** Accept "[a.b.c.d]" syntax for host name. 2757aa493c5Seric */ 2767aa493c5Seric 27771096d12Seric if (host[0] == '[') 27871096d12Seric { 279a44d5a5eSeric long hid; 280a44d5a5eSeric register char *p = index(host, ']'); 28171096d12Seric 282a44d5a5eSeric if (p != NULL) 28371096d12Seric { 284a44d5a5eSeric *p = '\0'; 285a44d5a5eSeric hid = inet_addr(&host[1]); 286a44d5a5eSeric *p = ']'; 28771096d12Seric } 288a44d5a5eSeric if (p == NULL || hid == -1) 28971096d12Seric { 29071096d12Seric usrerr("Invalid numeric domain spec \"%s\"", host); 29171096d12Seric return (EX_NOHOST); 29271096d12Seric } 29371096d12Seric SendmailAddress.sin_addr.s_addr = hid; 29471096d12Seric } 2951c71e510Seric else 2961c71e510Seric { 2971c71e510Seric register struct hostent *hp = gethostbyname(host); 2981c71e510Seric 299a44d5a5eSeric if (hp == NULL) 3007aa493c5Seric return (EX_NOHOST); 3011c71e510Seric bmove(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 3021c71e510Seric } 3031c71e510Seric 3041c71e510Seric /* 3051c71e510Seric ** Determine the port number. 3061c71e510Seric */ 3071c71e510Seric 308fd7c0790Seric if (port != 0) 309fd7c0790Seric SendmailAddress.sin_port = htons(port); 310fd7c0790Seric else 3111c71e510Seric { 3121c71e510Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3131c71e510Seric 3141c71e510Seric if (sp == NULL) 3151c71e510Seric { 3161c71e510Seric syserr("makeconnection: server \"smtp\" unknown"); 3171c71e510Seric return (EX_OSFILE); 3181c71e510Seric } 319fd7c0790Seric SendmailAddress.sin_port = sp->s_port; 3201c71e510Seric } 3217aa493c5Seric 3227aa493c5Seric /* 3237aa493c5Seric ** Try to actually open the connection. 3247aa493c5Seric */ 3257aa493c5Seric 3267aa493c5Seric # ifdef DEBUG 32761e4310fSeric if (tTd(16, 1)) 3287aa493c5Seric printf("makeconnection (%s)\n", host); 3297aa493c5Seric # endif DEBUG 3307aa493c5Seric 33110d42158Seric s = socket(AF_INET, SOCK_STREAM, 0, 0); 3327aa493c5Seric if (s < 0) 3337aa493c5Seric { 3347aa493c5Seric syserr("makeconnection: no socket"); 3357aa493c5Seric goto failure; 3367aa493c5Seric } 3377aa493c5Seric 3387aa493c5Seric # ifdef DEBUG 33961e4310fSeric if (tTd(16, 1)) 3407aa493c5Seric printf("makeconnection: %d\n", s); 3411b6e4a15Seric 3421b6e4a15Seric /* turn on network debugging? */ 3431b6e4a15Seric if (tTd(16, 14)) 3441b6e4a15Seric (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0); 3457aa493c5Seric # endif DEBUG 346877a6142Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 3471c71e510Seric SendmailAddress.sin_family = AF_INET; 34810d42158Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0) 3497aa493c5Seric { 3507aa493c5Seric /* failure, decide if temporary or not */ 3517aa493c5Seric failure: 3527aa493c5Seric switch (errno) 3537aa493c5Seric { 3547aa493c5Seric case EISCONN: 3557aa493c5Seric case ETIMEDOUT: 356292668b9Seric case EINPROGRESS: 357292668b9Seric case EALREADY: 358292668b9Seric case EADDRINUSE: 35936bd23a8Seric case EHOSTDOWN: 360292668b9Seric case ENETDOWN: 361292668b9Seric case ENETRESET: 362292668b9Seric case ENOBUFS: 363a1645df8Seric case ECONNREFUSED: 364efe49624Seric case ECONNRESET: 365cae8261aSeric case EHOSTUNREACH: 366dca8e1f7Seric case ENETUNREACH: 3677aa493c5Seric /* there are others, I'm sure..... */ 3687aa493c5Seric return (EX_TEMPFAIL); 3697aa493c5Seric 370a44d5a5eSeric case EPERM: 371a44d5a5eSeric /* why is this happening? */ 372a44d5a5eSeric syserr("makeconnection: funny failure, addr=%lx, port=%x", 373a44d5a5eSeric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 374a44d5a5eSeric /* explicit fall-through */ 375a44d5a5eSeric 3767aa493c5Seric default: 3777aa493c5Seric return (EX_UNAVAILABLE); 3787aa493c5Seric } 3797aa493c5Seric } 3807aa493c5Seric 3817aa493c5Seric /* connection ok, put it into canonical form */ 3827aa493c5Seric *outfile = fdopen(s, "w"); 3837aa493c5Seric *infile = fdopen(s, "r"); 3847aa493c5Seric 385dca8e1f7Seric return (EX_OK); 3867aa493c5Seric } 387444eaf03Seric /* 388444eaf03Seric ** MYHOSTNAME -- return the name of this host. 389444eaf03Seric ** 390444eaf03Seric ** Parameters: 391444eaf03Seric ** hostbuf -- a place to return the name of this host. 392*897f1869Seric ** size -- the size of hostbuf. 393444eaf03Seric ** 394444eaf03Seric ** Returns: 395444eaf03Seric ** A list of aliases for this host. 396444eaf03Seric ** 397444eaf03Seric ** Side Effects: 398444eaf03Seric ** none. 399444eaf03Seric */ 400444eaf03Seric 401444eaf03Seric char ** 402*897f1869Seric myhostname(hostbuf, size) 403444eaf03Seric char hostbuf[]; 404*897f1869Seric int size; 405444eaf03Seric { 406444eaf03Seric extern struct hostent *gethostbyname(); 407a44d5a5eSeric struct hostent *hp; 408*897f1869Seric auto int i = size; 40921e9914dSeric register char *p; 410444eaf03Seric 411107faed7Seric gethostname(hostbuf, &i); 412a44d5a5eSeric hp = gethostbyname(hostbuf); 41321e9914dSeric for (p = hostbuf; *p != '\0'; p++) 41421e9914dSeric if (islower(*p)) 41521e9914dSeric *p -= 'a' - 'A'; 416a44d5a5eSeric if (hp != NULL) 417a44d5a5eSeric return (hp->h_aliases); 418444eaf03Seric else 419444eaf03Seric return (NULL); 420444eaf03Seric } 421444eaf03Seric 422444eaf03Seric # else DAEMON 423444eaf03Seric 424444eaf03Seric /* 425444eaf03Seric ** MYHOSTNAME -- stub version for case of no daemon code. 42621e9914dSeric ** 42721e9914dSeric ** Can't convert to upper case here because might be a UUCP name. 428*897f1869Seric ** 429*897f1869Seric ** Mark, you can change this to be anything you want...... 430444eaf03Seric */ 431444eaf03Seric 432444eaf03Seric char ** 433*897f1869Seric myhostname(hostbuf, size) 434444eaf03Seric char hostbuf[]; 435*897f1869Seric int size; 436444eaf03Seric { 437444eaf03Seric register FILE *f; 438444eaf03Seric 439444eaf03Seric hostbuf[0] = '\0'; 440444eaf03Seric f = fopen("/usr/include/whoami", "r"); 441444eaf03Seric if (f != NULL) 442444eaf03Seric { 443*897f1869Seric (void) fgets(hostbuf, size, f); 444444eaf03Seric fixcrlf(hostbuf, TRUE); 445444eaf03Seric (void) fclose(f); 446444eaf03Seric } 447444eaf03Seric return (NULL); 448444eaf03Seric } 449d0a9e852Seric 450d0a9e852Seric #endif DAEMON 451