17aa493c5Seric # include <errno.h> 27fa39d90Seric # include "sendmail.h" 37fa39d90Seric 4d0a9e852Seric #ifndef DAEMON 5*29dcf4baSeric SCCSID(@(#)daemon.c 4.8 08/11/84 (w/o daemon mode)); 6d0a9e852Seric #else 7d0a9e852Seric 8d0a9e852Seric #include <sys/socket.h> 91c71e510Seric #include <netinet/in.h> 101c71e510Seric #include <netdb.h> 111e1663f7Swnj #include <sys/wait.h> 12d0a9e852Seric 13*29dcf4baSeric SCCSID(@(#)daemon.c 4.8 08/11/84 (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 */ 639478786eSeric 64b7d7afcbSeric int DaemonSocket = -1; /* fd describing socket */ 659478786eSeric char *NetName = "ARPA"; /* name of home (local?) network */ 661c71e510Seric 677fa39d90Seric getrequests() 687fa39d90Seric { 691c71e510Seric int t; 702554d49fSeric union wait status; 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 { 1313a099713Seric register int pid; 132a44d5a5eSeric auto int lotherend; 133a44d5a5eSeric struct sockaddr_in otherend; 1343a099713Seric extern int RefuseLA; 1353a099713Seric 1363a099713Seric /* see if we are rejecting connections */ 1373a099713Seric while (getla() > RefuseLA) 1383a099713Seric sleep(5); 139a44d5a5eSeric 1401c71e510Seric /* wait for a connection */ 1411c71e510Seric do 1421c71e510Seric { 1431c71e510Seric errno = 0; 1441c71e510Seric lotherend = sizeof otherend; 145b7d7afcbSeric t = accept(DaemonSocket, &otherend, &lotherend, 0); 1461c71e510Seric } while (t < 0 && errno == EINTR); 1471c71e510Seric if (t < 0) 1481c71e510Seric { 1491c71e510Seric syserr("getrequests: accept"); 1501c71e510Seric sleep(5); 1511c71e510Seric continue; 1521c71e510Seric } 153d0a9e852Seric 154d0a9e852Seric /* 155d0a9e852Seric ** Create a subprocess to process the mail. 156d0a9e852Seric */ 157d0a9e852Seric 158d0a9e852Seric # ifdef DEBUG 15961e4310fSeric if (tTd(15, 2)) 1601c71e510Seric printf("getrequests: forking (fd = %d)\n", t); 161d0a9e852Seric # endif DEBUG 162eb889047Seric 163a8268164Seric pid = fork(); 164a8268164Seric if (pid < 0) 165a8268164Seric { 166a8268164Seric syserr("daemon: cannot fork"); 167a8268164Seric sleep(10); 1681c71e510Seric (void) close(t); 169a8268164Seric continue; 170a8268164Seric } 171a8268164Seric 172a8268164Seric if (pid == 0) 173a8268164Seric { 174a44d5a5eSeric extern struct hostent *gethostbyaddr(); 175a44d5a5eSeric register struct hostent *hp; 176a44d5a5eSeric extern char *RealHostName; /* srvrsmtp.c */ 177a44d5a5eSeric char buf[MAXNAME]; 178a44d5a5eSeric 179a8268164Seric /* 180a8268164Seric ** CHILD -- return to caller. 181a44d5a5eSeric ** Collect verified idea of sending host. 182a8268164Seric ** Verify calling user id if possible here. 183a8268164Seric */ 184a8268164Seric 185a44d5a5eSeric /* determine host name */ 186a44d5a5eSeric hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET); 187a44d5a5eSeric if (hp != NULL) 1889478786eSeric (void) sprintf(buf, "%s.%s", hp->h_name, NetName); 189a44d5a5eSeric else 190*29dcf4baSeric { 191*29dcf4baSeric extern char *inet_ntoa(); 192*29dcf4baSeric 193*29dcf4baSeric /* produce a dotted quad */ 194*29dcf4baSeric (void) sprintf(buf, "[%s]", 195*29dcf4baSeric inet_ntoa(otherend.sin_addr)); 196*29dcf4baSeric } 197*29dcf4baSeric 198*29dcf4baSeric /* should we check for illegal connection here? XXX */ 199*29dcf4baSeric 200a44d5a5eSeric RealHostName = newstr(buf); 201a44d5a5eSeric 202b7d7afcbSeric (void) close(DaemonSocket); 2031c71e510Seric InChannel = fdopen(t, "r"); 2041c71e510Seric OutChannel = fdopen(t, "w"); 205d0a9e852Seric # ifdef DEBUG 20661e4310fSeric if (tTd(15, 2)) 207d0a9e852Seric printf("getreq: returning\n"); 208d0a9e852Seric # endif DEBUG 209252c8a22Seric # ifdef LOG 210252c8a22Seric if (LogLevel > 11) 211252c8a22Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 212252c8a22Seric # endif LOG 213a8268164Seric return; 214a8268164Seric } 215a8268164Seric 216a8268164Seric /* 217a8268164Seric ** PARENT -- wait for child to terminate. 218a8268164Seric ** Perhaps we should allow concurrent processing? 219a8268164Seric */ 220a8268164Seric 221d0a9e852Seric # ifdef DEBUG 22261e4310fSeric if (tTd(15, 2)) 223d0a9e852Seric { 224d0a9e852Seric sleep(2); 225d0a9e852Seric printf("getreq: parent waiting\n"); 226d0a9e852Seric } 227d0a9e852Seric # endif DEBUG 228d0a9e852Seric 2292554d49fSeric /* close the port so that others will hang (for a while) */ 2301c71e510Seric (void) close(t); 2312554d49fSeric 232105e901cSeric /* pick up old zombies */ 233105e901cSeric while (wait3(&status, WNOHANG, 0) > 0) 234105e901cSeric continue; 235a8268164Seric } 236147303b1Seric /*NOTREACHED*/ 2377fa39d90Seric } 238d0a9e852Seric /* 239b7d7afcbSeric ** CLRDAEMON -- reset the daemon connection 240b7d7afcbSeric ** 241b7d7afcbSeric ** Parameters: 242b7d7afcbSeric ** none. 243b7d7afcbSeric ** 244b7d7afcbSeric ** Returns: 245b7d7afcbSeric ** none. 246b7d7afcbSeric ** 247b7d7afcbSeric ** Side Effects: 248b7d7afcbSeric ** releases any resources used by the passive daemon. 249b7d7afcbSeric */ 250b7d7afcbSeric 251b7d7afcbSeric clrdaemon() 252b7d7afcbSeric { 253b7d7afcbSeric if (DaemonSocket >= 0) 254b7d7afcbSeric (void) close(DaemonSocket); 255b7d7afcbSeric DaemonSocket = -1; 256b7d7afcbSeric } 257b7d7afcbSeric /* 2587aa493c5Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2597aa493c5Seric ** 2607aa493c5Seric ** Parameters: 2617aa493c5Seric ** host -- the name of the host. 26248ff0a9dSeric ** port -- the port number to connect to. 2637aa493c5Seric ** outfile -- a pointer to a place to put the outfile 2647aa493c5Seric ** descriptor. 2657aa493c5Seric ** infile -- ditto for infile. 2667aa493c5Seric ** 2677aa493c5Seric ** Returns: 2687aa493c5Seric ** An exit code telling whether the connection could be 2697aa493c5Seric ** made and if not why not. 2707aa493c5Seric ** 2717aa493c5Seric ** Side Effects: 2727aa493c5Seric ** none. 2737aa493c5Seric */ 2747aa493c5Seric 27548ff0a9dSeric makeconnection(host, port, outfile, infile) 2767aa493c5Seric char *host; 277210215eaSeric u_short port; 2787aa493c5Seric FILE **outfile; 2797aa493c5Seric FILE **infile; 2807aa493c5Seric { 2817aa493c5Seric register int s; 2827aa493c5Seric 2837aa493c5Seric /* 2847aa493c5Seric ** Set up the address for the mailer. 28571096d12Seric ** Accept "[a.b.c.d]" syntax for host name. 2867aa493c5Seric */ 2877aa493c5Seric 28871096d12Seric if (host[0] == '[') 28971096d12Seric { 290a44d5a5eSeric long hid; 291a44d5a5eSeric register char *p = index(host, ']'); 29271096d12Seric 293a44d5a5eSeric if (p != NULL) 29471096d12Seric { 295a44d5a5eSeric *p = '\0'; 296a44d5a5eSeric hid = inet_addr(&host[1]); 297a44d5a5eSeric *p = ']'; 29871096d12Seric } 299a44d5a5eSeric if (p == NULL || hid == -1) 30071096d12Seric { 30171096d12Seric usrerr("Invalid numeric domain spec \"%s\"", host); 30271096d12Seric return (EX_NOHOST); 30371096d12Seric } 30471096d12Seric SendmailAddress.sin_addr.s_addr = hid; 30571096d12Seric } 3061c71e510Seric else 3071c71e510Seric { 3081c71e510Seric register struct hostent *hp = gethostbyname(host); 3091c71e510Seric 310a44d5a5eSeric if (hp == NULL) 3117aa493c5Seric return (EX_NOHOST); 312*29dcf4baSeric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 3131c71e510Seric } 3141c71e510Seric 3151c71e510Seric /* 3161c71e510Seric ** Determine the port number. 3171c71e510Seric */ 3181c71e510Seric 319fd7c0790Seric if (port != 0) 320fd7c0790Seric SendmailAddress.sin_port = htons(port); 321fd7c0790Seric else 3221c71e510Seric { 3231c71e510Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3241c71e510Seric 3251c71e510Seric if (sp == NULL) 3261c71e510Seric { 3271c71e510Seric syserr("makeconnection: server \"smtp\" unknown"); 3281c71e510Seric return (EX_OSFILE); 3291c71e510Seric } 330fd7c0790Seric SendmailAddress.sin_port = sp->s_port; 3311c71e510Seric } 3327aa493c5Seric 3337aa493c5Seric /* 3347aa493c5Seric ** Try to actually open the connection. 3357aa493c5Seric */ 3367aa493c5Seric 3377aa493c5Seric # ifdef DEBUG 33861e4310fSeric if (tTd(16, 1)) 3397aa493c5Seric printf("makeconnection (%s)\n", host); 3407aa493c5Seric # endif DEBUG 3417aa493c5Seric 34210d42158Seric s = socket(AF_INET, SOCK_STREAM, 0, 0); 3437aa493c5Seric if (s < 0) 3447aa493c5Seric { 3457aa493c5Seric syserr("makeconnection: no socket"); 3467aa493c5Seric goto failure; 3477aa493c5Seric } 3487aa493c5Seric 3497aa493c5Seric # ifdef DEBUG 35061e4310fSeric if (tTd(16, 1)) 3517aa493c5Seric printf("makeconnection: %d\n", s); 3521b6e4a15Seric 3531b6e4a15Seric /* turn on network debugging? */ 3541b6e4a15Seric if (tTd(16, 14)) 3551b6e4a15Seric (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0); 3567aa493c5Seric # endif DEBUG 357877a6142Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 3584bd6a662Seric errno = 0; /* for debugging */ 3591c71e510Seric SendmailAddress.sin_family = AF_INET; 36010d42158Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0) 3617aa493c5Seric { 3627aa493c5Seric /* failure, decide if temporary or not */ 3637aa493c5Seric failure: 3647aa493c5Seric switch (errno) 3657aa493c5Seric { 3667aa493c5Seric case EISCONN: 3677aa493c5Seric case ETIMEDOUT: 368292668b9Seric case EINPROGRESS: 369292668b9Seric case EALREADY: 370292668b9Seric case EADDRINUSE: 37136bd23a8Seric case EHOSTDOWN: 372292668b9Seric case ENETDOWN: 373292668b9Seric case ENETRESET: 374292668b9Seric case ENOBUFS: 375a1645df8Seric case ECONNREFUSED: 376efe49624Seric case ECONNRESET: 377cae8261aSeric case EHOSTUNREACH: 378dca8e1f7Seric case ENETUNREACH: 3797aa493c5Seric /* there are others, I'm sure..... */ 380*29dcf4baSeric CurEnv->e_flags &= ~EF_FATALERRS; 3817aa493c5Seric return (EX_TEMPFAIL); 3827aa493c5Seric 383a44d5a5eSeric case EPERM: 384a44d5a5eSeric /* why is this happening? */ 385a44d5a5eSeric syserr("makeconnection: funny failure, addr=%lx, port=%x", 386a44d5a5eSeric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 3874bd6a662Seric return (EX_TEMPFAIL); 388a44d5a5eSeric 3897aa493c5Seric default: 3907aa493c5Seric return (EX_UNAVAILABLE); 3917aa493c5Seric } 3927aa493c5Seric } 3937aa493c5Seric 3947aa493c5Seric /* connection ok, put it into canonical form */ 3957aa493c5Seric *outfile = fdopen(s, "w"); 3967aa493c5Seric *infile = fdopen(s, "r"); 3977aa493c5Seric 398dca8e1f7Seric return (EX_OK); 3997aa493c5Seric } 400444eaf03Seric /* 401444eaf03Seric ** MYHOSTNAME -- return the name of this host. 402444eaf03Seric ** 403444eaf03Seric ** Parameters: 404444eaf03Seric ** hostbuf -- a place to return the name of this host. 405897f1869Seric ** size -- the size of hostbuf. 406444eaf03Seric ** 407444eaf03Seric ** Returns: 408444eaf03Seric ** A list of aliases for this host. 409444eaf03Seric ** 410444eaf03Seric ** Side Effects: 411444eaf03Seric ** none. 412444eaf03Seric */ 413444eaf03Seric 414444eaf03Seric char ** 415897f1869Seric myhostname(hostbuf, size) 416444eaf03Seric char hostbuf[]; 417897f1869Seric int size; 418444eaf03Seric { 419444eaf03Seric extern struct hostent *gethostbyname(); 420a44d5a5eSeric struct hostent *hp; 421444eaf03Seric 4229bc0e008Seric gethostname(hostbuf, size); 423a44d5a5eSeric hp = gethostbyname(hostbuf); 424a44d5a5eSeric if (hp != NULL) 4257364df9fSeric { 4267364df9fSeric strcpy(hostbuf, hp->h_name); 427a44d5a5eSeric return (hp->h_aliases); 4287364df9fSeric } 429444eaf03Seric else 430444eaf03Seric return (NULL); 431444eaf03Seric } 432444eaf03Seric 433444eaf03Seric # else DAEMON 434444eaf03Seric 435444eaf03Seric /* 436444eaf03Seric ** MYHOSTNAME -- stub version for case of no daemon code. 43721e9914dSeric ** 43821e9914dSeric ** Can't convert to upper case here because might be a UUCP name. 439897f1869Seric ** 440897f1869Seric ** Mark, you can change this to be anything you want...... 441444eaf03Seric */ 442444eaf03Seric 443444eaf03Seric char ** 444897f1869Seric myhostname(hostbuf, size) 445444eaf03Seric char hostbuf[]; 446897f1869Seric int size; 447444eaf03Seric { 448444eaf03Seric register FILE *f; 449444eaf03Seric 450444eaf03Seric hostbuf[0] = '\0'; 451444eaf03Seric f = fopen("/usr/include/whoami", "r"); 452444eaf03Seric if (f != NULL) 453444eaf03Seric { 454897f1869Seric (void) fgets(hostbuf, size, f); 455444eaf03Seric fixcrlf(hostbuf, TRUE); 456444eaf03Seric (void) fclose(f); 457444eaf03Seric } 458444eaf03Seric return (NULL); 459444eaf03Seric } 460d0a9e852Seric 461d0a9e852Seric #endif DAEMON 462