* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)daemon.c 8.7 (Berkeley) %G% (with daemon mode)";
static char sccsid
[] = "@(#)daemon.c 8.7 (Berkeley) %G% (without daemon mode)";
# include <arpa/nameser.h>
** 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:
** Opens a port and initiates a connection.
** Returns in a child. Must set InChannel and
** OutChannel appropriately.
** 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
** makeconnection(host, port, outfile, infile, usesecureport)
** 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
** host_map_lookup(map, hbuf, avp, pstat)
** Convert the entry in hbuf into a canonical form.
static FILE *MailPort
; /* port that mail comes in on */
** GETREQUESTS -- open mail IPC port and get requests.
** 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.
int DaemonSocket
= -1; /* fd describing socket */
SOCKADDR DaemonAddr
; /* socket for incoming */
int ListenQueueSize
= 10; /* size of listen queue */
register struct servent
*sp
;
bool refusingconnections
= TRUE
;
** Set up the address for the mailer.
if (DaemonAddr
.sin
.sin_family
== 0)
DaemonAddr
.sin
.sin_family
= AF_INET
;
if (DaemonAddr
.sin
.sin_addr
.s_addr
== 0)
DaemonAddr
.sin
.sin_addr
.s_addr
= INADDR_ANY
;
if (DaemonAddr
.sin
.sin_port
== 0)
sp
= getservbyname("smtp", "tcp");
syserr("554 service \"smtp\" unknown");
DaemonAddr
.sin
.sin_port
= sp
->s_port
;
** Try to actually open the connection.
printf("getrequests: port 0x%x\n", DaemonAddr
.sin
.sin_port
);
/* get a socket for the SMTP connection */
DaemonSocket
= socket(DaemonAddr
.sa
.sa_family
, SOCK_STREAM
, 0);
/* probably another daemon already */
syserr("getrequests: can't create socket");
/* turn on network debugging? */
(void) setsockopt(DaemonSocket
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
, sizeof on
);
(void) setsockopt(DaemonSocket
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof on
);
(void) setsockopt(DaemonSocket
, SOL_SOCKET
, SO_KEEPALIVE
, (char *)&on
, sizeof on
);
switch (DaemonAddr
.sa
.sa_family
)
t
= sizeof DaemonAddr
.sin
;
t
= sizeof DaemonAddr
.siso
;
if (bind(DaemonSocket
, &DaemonAddr
.sa
, t
) < 0)
syserr("getrequests: cannot bind");
(void) close(DaemonSocket
);
(void) setsignal(SIGCHLD
, reapchild
);
/* write the pid to the log file for posterity */
pidf
= fopen(PidFile
, "w");
extern char *CommandLineArgs
;
/* write the process id on line 1 */
fprintf(pidf
, "%d\n", getpid());
/* line 2 contains all command line flags */
fprintf(pidf
, "%s\n", CommandLineArgs
);
printf("getrequests: %d\n", DaemonSocket
);
write(MailPort
, &wbuf
, sizeof wbuf
);
** MAKECONNECTION -- make a connection to an SMTP socket on another machine.
** host -- the name of the host.
** port -- the port number to connect to.
** mci -- a pointer to the mail connection information
** structure to be filled in.
** usesecureport -- if set, use a low numbered (reserved)
** port to provide some rudimentary authentication.
** An exit code telling whether the connection could be
** made and if not why not.
SOCKADDR CurHostAddr
; /* address of current host */
makeconnection(host
, port
, mci
, usesecureport
)
register struct hostent
*hp
= (struct hostent
*)NULL
;
** Set up the address for the mailer.
** Accept "[a.b.c.d]" syntax for host name.
bzero(&CurHostAddr
, sizeof CurHostAddr
);
register char *p
= strchr(host
, ']');
hid
= inet_addr(&host
[1]);
/* try it as a host name (avoid MX lookup) */
hp
= gethostbyname(&host
[1]);
usrerr("553 Invalid numeric domain spec \"%s\"", host
);
addr
.sin
.sin_family
= AF_INET
; /*XXX*/
addr
.sin
.sin_addr
.s_addr
= hid
;
hp
= gethostbyname(host
);
if (errno
== ETIMEDOUT
|| h_errno
== TRY_AGAIN
)
/* if name server is specified, assume temp fail */
if (errno
== ECONNREFUSED
&& UseNameServer
)
addr
.sa
.sa_family
= hp
->h_addrtype
;
** Determine the port number.
register struct servent
*sp
= getservbyname("smtp", "tcp");
syserr("554 makeconnection: service \"smtp\" unknown");
switch (addr
.sa
.sa_family
)
addr
.sin
.sin_port
= port
;
addrlen
= sizeof (struct sockaddr_in
);
/* assume two byte transport selector */
bcopy((char *) &port
, TSEL((struct sockaddr_iso
*) &addr
), 2);
addrlen
= sizeof (struct sockaddr_iso
);
syserr("Can't connect to address family %d", addr
.sa
.sa_family
);
** Try to actually open the connection.
/* if too many connections, don't bother trying */
if (!xla_noqueue_ok(host
))
printf("makeconnection (%s [%s])\n",
host
, anynet_ntoa(&addr
));
int rport
= IPPORT_RESERVED
- 1;
s
= socket(AF_INET
, SOCK_STREAM
, 0);
syserr("makeconnection: no socket");
printf("makeconnection: fd=%d\n", s
);
/* turn on network debugging? */
(void) setsockopt(DaemonSocket
, SOL_SOCKET
, SO_DEBUG
,
if (CurEnv
->e_xfp
!= NULL
)
(void) fflush(CurEnv
->e_xfp
); /* for debugging */
errno
= 0; /* for debugging */
if (connect(s
, (struct sockaddr
*) &addr
, addrlen
) >= 0)
/* couldn't connect.... figure out why */
if (hp
&& hp
->h_addr_list
[i
])
printf("Connect failed (%s); trying new address....\n",
switch (addr
.sa
.sa_family
)
bcopy(hp
->h_addr_list
[i
++],
bcopy(hp
->h_addr_list
[i
++],
/* failure, decide if temporary or not */
if (transienterror(sav_errno
))
message("%s", errstring(sav_errno
));
/* connection ok, put it into canonical form */
mci
->mci_out
= fdopen(s
, "w");
mci
->mci_in
= fdopen(dup(s
), "r");
** MYHOSTNAME -- return the name of this host.
** hostbuf -- a place to return the name of this host.
** size -- the size of hostbuf.
** A list of aliases for this host.
** Sets the MyIpAddrs buffer to a list of my IP addresses.
struct in_addr MyIpAddrs
[MAXIPADDR
+ 1];
myhostname(hostbuf
, size
)
register struct hostent
*hp
;
extern struct hostent
*gethostbyname();
if (gethostname(hostbuf
, size
) < 0)
(void) strcpy(hostbuf
, "localhost");
hp
= gethostbyname(hostbuf
);
(void) strncpy(hostbuf
, hp
->h_name
, size
- 1);
hostbuf
[size
- 1] = '\0';
if (hp
->h_addrtype
== AF_INET
&& hp
->h_length
== 4)
for (i
= 0; i
< MAXIPADDR
; i
++)
if (hp
->h_addr_list
[i
] == NULL
)
MyIpAddrs
[i
].s_addr
= *(u_long
*) hp
->h_addr_list
[i
];
** GETAUTHINFO -- get the real host name asociated with a file descriptor
** Uses RFC1413 protocol to try to get info from the other end.
** The user@host information associated with this descriptor.
** Sets RealHostName to the name of the host at the other end.
static jmp_buf CtxAuthTimeout
;
longjmp(CtxAuthTimeout
, 1);
register struct servent
*sp
;
static char hbuf
[MAXNAME
* 2 + 2];
extern char *hostnamebyanyaddr();
extern char RealUserName
[]; /* main.c */
if (getpeername(fd
, &fa
.sa
, &falen
) < 0 || falen
<= 0)
RealHostName
= "localhost";
(void) sprintf(hbuf
, "%s@localhost", RealUserName
);
printf("getauthinfo: %s\n", hbuf
);
p
= hostnamebyanyaddr(&fa
);
RealHostName
= newstr(p
);
if (fa
.sa
.sa_family
!= AF_INET
||
getsockname(fd
, &la
.sa
, &lalen
) < 0 || lalen
<= 0 ||
la
.sa
.sa_family
!= AF_INET
)
(void) sprintf(hbuf
, "%d,%d\r\n",
ntohs(fa
.sin
.sin_port
), ntohs(la
.sin
.sin_port
));
/* create local address */
/* create foreign address */
sp
= getservbyname("auth", "tcp");
fa
.sin
.sin_port
= sp
->s_port
;
fa
.sin
.sin_port
= htons(113);
if (setjmp(CtxAuthTimeout
) != 0)
/* put a timeout around the whole thing */
ev
= setevent(TimeOuts
.to_ident
, authtimeout
, 0);
/* connect to foreign IDENT server */
s
= socket(AF_INET
, SOCK_STREAM
, 0);
if (connect(s
, &fa
.sa
, sizeof fa
.sin
) < 0)
printf("getauthinfo: sent %s", hbuf
);
if (write(s
, hbuf
, strlen(hbuf
)) < 0)
i
= read(s
, hbuf
, sizeof hbuf
);
if (hbuf
[--i
] == '\n' && hbuf
[--i
] == '\r')
printf("getauthinfo: got %s\n", hbuf
);
while (isascii(*++p
) && isspace(*p
))
if (strncasecmp(p
, "userid", 6) != 0)
/* presumably an error string */
while (isascii(*p
) && isspace(*p
))
/* either useridxx or malformed response */
/* p now points to the OSTYPE field */
/* 1413 says don't do this -- but it's broken otherwise */
while (isascii(*++p
) && isspace(*p
))
/* p now points to the authenticated name */
(void) sprintf(hbuf
, "%s@%s", p
, RealHostName
);
(void) strcpy(hbuf
, RealHostName
);
if (RealHostName
[0] != '[')
(void) sprintf(p
, " [%s]", anynet_ntoa(&RealHostAddr
));
printf("getauthinfo: %s\n", hbuf
);
** HOST_MAP_LOOKUP -- turn a hostname into canonical form
** map -- a pointer to this map (unused).
** name -- the (presumably unqualified) hostname.
** av -- unused -- for compatibility with other mapping
** statp -- an exit status (out parameter) -- set to
** EX_TEMPFAIL if the name server is unavailable.
** The mapping, if found.
** NULL if no mapping found.
** Looks up the host specified in hbuf. If it is not
** the canonical name for that host, return the canonical
host_map_lookup(map
, name
, av
, statp
)
register struct hostent
*hp
;
extern struct hostent
*gethostbyaddr();
** See if we have already looked up this name. If so, just
s
= stab(name
, ST_NAMECANON
, ST_ENTER
);
if (bitset(NCF_VALID
, s
->s_namecanon
.nc_flags
))
printf("host_map_lookup(%s) => CACHE %s\n",
name
, s
->s_namecanon
.nc_cname
);
errno
= s
->s_namecanon
.nc_errno
;
h_errno
= s
->s_namecanon
.nc_herrno
;
*statp
= s
->s_namecanon
.nc_stat
;
return s
->s_namecanon
.nc_cname
;
** If first character is a bracket, then it is an address
** lookup. Address is copied into a temporary buffer to
** strip the brackets and to preserve name if address is
extern bool getcanonname();
printf("host_map_lookup(%s) => ", name
);
s
->s_namecanon
.nc_flags
|= NCF_VALID
; /* will be soon */
(void) strcpy(hbuf
, name
);
if (getcanonname(hbuf
, sizeof hbuf
- 1, TRUE
))
cp
= map_rewrite(map
, hbuf
, strlen(hbuf
), av
);
s
->s_namecanon
.nc_cname
= newstr(cp
);
register struct hostent
*hp
;
printf("FAIL (%d)\n", h_errno
);
s
->s_namecanon
.nc_errno
= errno
;
s
->s_namecanon
.nc_herrno
= h_errno
;
char *msg
= "Recipient domain nameserver timed out";
if (CurEnv
->e_message
== NULL
)
CurEnv
->e_message
= newstr(msg
);
s
->s_namecanon
.nc_stat
= *statp
;
if (*statp
!= EX_TEMPFAIL
|| UseNameServer
)
** Try to look it up in /etc/hosts
hp
= gethostbyname(name
);
/* no dice there either */
s
->s_namecanon
.nc_stat
= *statp
= EX_NOHOST
;
s
->s_namecanon
.nc_stat
= *statp
= EX_OK
;
cp
= map_rewrite(map
, hp
->h_name
, strlen(hp
->h_name
), av
);
s
->s_namecanon
.nc_cname
= newstr(cp
);
if ((cp
= strchr(name
, ']')) == NULL
)
in_addr
= inet_addr(&name
[1]);
/* check to see if this is one of our addresses */
for (i
= 0; MyIpAddrs
[i
].s_addr
!= 0; i
++)
if (MyIpAddrs
[i
].s_addr
== in_addr
)
return map_rewrite(map
, MyHostName
, strlen(MyHostName
), av
);
/* nope -- ask the name server */
hp
= gethostbyaddr((char *)&in_addr
, sizeof(struct in_addr
), AF_INET
);
s
->s_namecanon
.nc_errno
= errno
;
s
->s_namecanon
.nc_herrno
= h_errno
;
s
->s_namecanon
.nc_flags
|= NCF_VALID
; /* will be soon */
s
->s_namecanon
.nc_stat
= *statp
= EX_NOHOST
;
/* found a match -- copy out */
cp
= map_rewrite(map
, hp
->h_name
, strlen(hp
->h_name
), av
);
s
->s_namecanon
.nc_stat
= *statp
= EX_OK
;
s
->s_namecanon
.nc_cname
= newstr(cp
);
** ANYNET_NTOA -- convert a network address to printable form.
** sap -- a pointer to a sockaddr structure.
** A printable version of that sockaddr.
/* check for null/zero family */
if (sap
->sa
.sa_family
== 0)
if (sap
->sa
.sa_family
== AF_INET
)
extern char *inet_ntoa();
return inet_ntoa(((struct sockaddr_in
*) sap
)->sin_addr
);
/* unknown family -- just dump bytes */
(void) sprintf(buf
, "Family %d: ", sap
->sa
.sa_family
);
for (l
= sizeof sap
->sa
.sa_data
; --l
>= 0; )
(void) sprintf(bp
, "%02x:", *ap
++ & 0377);
** HOSTNAMEBYANYADDR -- return name of host based on address
** sap -- SOCKADDR pointer
** text representation of host name.
register struct hostent
*hp
;
/* shorten name server timeout to avoid higher level timeouts */
switch (sap
->sa
.sa_family
)
hp
= gethostbyaddr((char *) &sap
->sin
.sin_addr
,
sizeof sap
->sin
.sin_addr
,
hp
= gethostbyaddr((char *) &sap
->siso
.siso_addr
,
sizeof sap
->siso
.siso_addr
,
hp
= gethostbyaddr(sap
->sa
.sa_data
,
/* produce a dotted quad */
(void) sprintf(buf
, "[%s]", anynet_ntoa(sap
));
/* code for systems without sophisticated networking */
** MYHOSTNAME -- stub version for case of no daemon code.
** Can't convert to upper case here because might be a UUCP name.
** Mark, you can change this to be anything you want......
myhostname(hostbuf
, size
)
f
= fopen("/usr/include/whoami", "r");
(void) fgets(hostbuf
, size
, f
);
** GETAUTHINFO -- get the real host name asociated with a file descriptor
** The host name associated with this descriptor, if it can
** MAPHOSTNAME -- turn a hostname into canonical form
** map -- a pointer to the database map.
** name -- a buffer containing a hostname.
** avp -- a pointer to a (cf file defined) argument vector.
** statp -- an exit status (out parameter).
** Looks up the host specified in name. If it is not
** the canonical name for that host, replace it with
** the canonical name. If the name is unknown, or it
** is already the canonical name, leave it unchanged.
host_map_lookup(map
, name
, avp
, statp
)
register struct hostent
*hp
;
hp
= gethostbyname(name
);