86fc728ca472602456eb3ca5c5f1767ea30ae760
[unix-history] / usr / src / usr.sbin / sendmail / src / daemon.c
# include <errno.h>
# include "sendmail.h"
# include <sys/mx.h>
#ifndef DAEMON
SCCSID(@(#)daemon.c 3.46 %G% (w/o daemon mode));
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <wait.h>
SCCSID(@(#)daemon.c 3.46 %G% (with daemon mode));
/*
** DAEMON.C -- routines to use when running as a daemon.
**
** This entire file is highly dependent on the 4.2 BSD
** interprocess communication primitives. No attempt has
** been made to make this file portable to Version 7,
** Version 6, MPX files, etc. If you should try such a
** thing yourself, I recommend chucking the entire file
** and starting from scratch. Basic semantics are:
**
** getrequests()
** Opens a port and initiates a connection.
** Returns in a child. Must set InChannel and
** OutChannel appropriately.
** clrdaemon()
** Close any open files associated with getting
** the connection; this is used when running the queue,
** etc., to avoid having extra file descriptors during
** the queue run and to avoid confusing the network
** code (if it cares).
** makeconnection(host, port, outfile, infile)
** Make a connection to the named host on the given
** port. Set *outfile and *infile to the files
** appropriate for communication. Returns zero on
** success, else an exit status describing the
** error.
**
** The semantics of both of these should be clean.
*/
static FILE *MailPort; /* port that mail comes in on */
\f/*
** GETREQUESTS -- open mail IPC port and get requests.
**
** Parameters:
** none.
**
** Returns:
** none.
**
** Side Effects:
** Waits until some interesting activity occurs. When
** it does, a child is created to process it, and the
** parent waits for completion. Return from this
** routine is always in the child. The file pointers
** "InChannel" and "OutChannel" should be set to point
** to the communication channel.
*/
struct sockaddr_in SendmailAddress;/* internet address of sendmail */
int DaemonSocket = -1; /* fd describing socket */
int MaxConnections = 4; /* maximum simultaneous sendmails */
getrequests()
{
int t;
union wait status;
int numconnections = 0;
register struct servent *sp;
/*
** Set up the address for the mailer.
*/
sp = getservbyname("smtp", "tcp");
if (sp == NULL)
{
syserr("server \"smtp\" unknown");
goto severe;
}
SendmailAddress.sin_family = AF_INET;
SendmailAddress.sin_addr.s_addr = INADDR_ANY;
SendmailAddress.sin_port = sp->s_port;
/*
** Try to actually open the connection.
*/
# ifdef DEBUG
if (tTd(15, 1))
printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
# endif DEBUG
/* get a socket for the SMTP connection */
DaemonSocket = socket(AF_INET, SOCK_STREAM, 0, 0);
if (DaemonSocket < 0)
{
/* probably another daemon already */
syserr("getrequests: can't create socket");
severe:
# ifdef LOG
if (LogLevel > 0)
syslog(LOG_SALERT, "cannot get connection");
# endif LOG
finis();
}
#ifdef DEBUG
/* turn on network debugging? */
if (tTd(15, 15))
(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 0, 0);
#endif DEBUG
if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
{
syserr("getrequests: cannot bind");
(void) close(DaemonSocket);
goto severe;
}
listen(DaemonSocket, 10);
# ifdef DEBUG
if (tTd(15, 1))
printf("getrequests: %d\n", DaemonSocket);
# endif DEBUG
struct wh wbuf;
wbuf.index = index;
wbuf.count = 0;
wbuf.ccount = cnt;
wbuf.data = buf;
write(MailPort, &wbuf, sizeof wbuf);
}
\f/*
** MAKECONNECTION -- make a connection to an SMTP socket on another machine.
**
** Parameters:
** host -- the name of the host.
** port -- the port number to connect to.
** outfile -- a pointer to a place to put the outfile
** descriptor.
** infile -- ditto for infile.
**
** Returns:
** An exit code telling whether the connection could be
** made and if not why not.
**
** Side Effects:
** none.
*/
makeconnection(host, port, outfile, infile)
char *host;
u_short port;
FILE **outfile;
FILE **infile;
{
register int s;
/*
** Set up the address for the mailer.
** Accept "[a.b.c.d]" syntax for host name.
*/
if (host[0] == '[')
{
long hid = 0;
int i, j;
register char *p = host;
for (i = 3; i >= 0 && *p != ']' && *p != '\0'; i--)
{
j = 0;
while (isdigit(*++p))
j = j * 10 + (*p - '0');
if (*p != (i == 0 ? ']' : '.') || j > 255 || j < 0)
break;
hid |= j << ((3 - i) * 8);
}
if (i >= 0 || *p != ']' || *++p != '\0')
{
usrerr("Invalid numeric domain spec \"%s\"", host);
return (EX_NOHOST);
}
SendmailAddress.sin_addr.s_addr = hid;
}
else
{
register struct hostent *hp = gethostbyname(host);
if (hp == 0)
return (EX_NOHOST);
bmove(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
}
/*
** Determine the port number.
*/
if (port != 0)
SendmailAddress.sin_port = htons(port);
else
{
register struct servent *sp = getservbyname("smtp", "tcp");
if (sp == NULL)
{
syserr("makeconnection: server \"smtp\" unknown");
return (EX_OSFILE);
}
SendmailAddress.sin_port = sp->s_port;
}
/*
** Try to actually open the connection.
*/
# ifdef DEBUG
if (tTd(16, 1))
printf("makeconnection (%s)\n", host);
# endif DEBUG
#ifdef NVMUNIX
s = socket(AF_INET, SOCK_STREAM, 0, 0);
#else NVMUNIX
s = socket(AF_INET, SOCK_STREAM, 0, 0);
#endif NVMUNIX
if (s < 0)
{
syserr("makeconnection: no socket");
goto failure;
}
# ifdef DEBUG
if (tTd(16, 1))
printf("makeconnection: %d\n", s);
/* turn on network debugging? */
if (tTd(16, 14))
(void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0);
# endif DEBUG
(void) fflush(CurEnv->e_xfp); /* for debugging */
#ifdef NVMUNIX
bind(s, &SendmailAddress, sizeof SendmailAddress, 0);
if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
#else NVMUNIX
SendmailAddress.sin_family = AF_INET;
bind(s, &SendmailAddress, sizeof SendmailAddress, 0);
if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
#endif NVMUNIX
{
/* failure, decide if temporary or not */
failure:
switch (errno)
{
case EISCONN:
case ETIMEDOUT:
case EINPROGRESS:
case EALREADY:
case EADDRINUSE:
case EHOSTDOWN:
case ENETDOWN:
case ENETRESET:
case ENOBUFS:
case ECONNREFUSED:
case EHOSTUNREACH:
case ENETUNREACH:
/* there are others, I'm sure..... */
return (EX_TEMPFAIL);
default:
return (EX_UNAVAILABLE);
}
}
/* connection ok, put it into canonical form */
*outfile = fdopen(s, "w");
*infile = fdopen(s, "r");
return (EX_OK);
}
#endif DAEMON