* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988 Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)daemon.c 5.37 (Berkeley) 3/2/91 (with daemon mode)";
static char sccsid
[] = "@(#)daemon.c 5.37 (Berkeley) 3/2/91 (without daemon mode)";
int la
; /* load average */
# include <sys/resource.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)
** 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
** maphostname(hbuf, hbufsize)
** Convert the entry in hbuf into a canonical form. It
** may not be larger than hbufsize.
** 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.
struct sockaddr_in SendmailAddress
;/* internet address of sendmail */
int DaemonSocket
= -1; /* fd describing socket */
char *NetName
; /* name of home (local?) network */
register struct servent
*sp
;
** Set up the address for the mailer.
sp
= getservbyname("smtp", "tcp");
syserr("server \"smtp\" unknown");
SendmailAddress
.sin_family
= AF_INET
;
SendmailAddress
.sin_addr
.s_addr
= INADDR_ANY
;
SendmailAddress
.sin_port
= sp
->s_port
;
** Try to actually open the connection.
printf("getrequests: port 0x%x\n", SendmailAddress
.sin_port
);
/* get a socket for the SMTP connection */
DaemonSocket
= socket(AF_INET
, SOCK_STREAM
, 0);
/* probably another daemon already */
syserr("getrequests: can't create socket");
syslog(LOG_ALERT
, "cannot get connection");
/* 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
);
(struct sockaddr
*)&SendmailAddress
, sizeof SendmailAddress
) < 0)
syserr("getrequests: cannot bind");
(void) close(DaemonSocket
);
if (listen(DaemonSocket
, 10) < 0)
syserr("getrequests: cannot listen");
(void) close(DaemonSocket
);
(void) signal(SIGCHLD
, reapchild
);
printf("getrequests: %d\n", DaemonSocket
);
/* see if we are rejecting connections */
while ((la
= getla()) > RefuseLA
)
setproctitle("rejecting connections: load average: %.2f", (double)la
);
/* wait for a connection */
setproctitle("accepting connections");
lotherend
= sizeof RealHostAddr
;
(struct sockaddr
*)&RealHostAddr
, &lotherend
);
} while (t
< 0 && errno
== EINTR
);
syserr("getrequests: accept");
** Create a subprocess to process the mail.
printf("getrequests: forking (fd = %d)\n", t
);
syserr("daemon: cannot fork");
extern struct hostent
*gethostbyaddr();
register struct hostent
*hp
;
** CHILD -- return to caller.
** Collect verified idea of sending host.
** Verify calling user id if possible here.
(void) signal(SIGCHLD
, SIG_DFL
);
/* determine host name */
hp
= gethostbyaddr((char *) &RealHostAddr
.sin_addr
, sizeof RealHostAddr
.sin_addr
, AF_INET
);
(void) strcpy(buf
, hp
->h_name
);
extern char *inet_ntoa();
/* produce a dotted quad */
(void) sprintf(buf
, "[%s]",
inet_ntoa(RealHostAddr
.sin_addr
));
/* should we check for illegal connection here? XXX */
RealHostName
= newstr(buf
);
(void) close(DaemonSocket
);
InChannel
= fdopen(t
, "r");
OutChannel
= fdopen(dup(t
), "w");
printf("getreq: returning\n");
syslog(LOG_DEBUG
, "connected, pid=%d", getpid());
/* close the port so that others will hang (for a while) */
** CLRDAEMON -- reset the daemon connection
** releases any resources used by the passive daemon.
(void) close(DaemonSocket
);
** MAKECONNECTION -- make a connection to an SMTP socket on another machine.
** host -- the name of the host.
** port -- the port number to connect to.
** outfile -- a pointer to a place to put the outfile
** infile -- ditto for infile.
** An exit code telling whether the connection could be
** made and if not why not.
makeconnection(host
, port
, outfile
, infile
)
register struct hostent
*hp
= (struct hostent
*)NULL
;
extern char *inet_ntoa();
** Set up the address for the mailer.
** Accept "[a.b.c.d]" syntax for host name.
register char *p
= index(host
, ']');
hid
= inet_addr(&host
[1]);
if (p
== NULL
|| hid
== -1)
usrerr("Invalid numeric domain spec \"%s\"", host
);
SendmailAddress
.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
)
** XXX Should look for mail forwarder record here
** XXX if (h_errno == NO_ADDRESS).
bcopy(hp
->h_addr
, (char *) &SendmailAddress
.sin_addr
, hp
->h_length
);
** Determine the port number.
SendmailAddress
.sin_port
= htons(port
);
register struct servent
*sp
= getservbyname("smtp", "tcp");
syserr("makeconnection: server \"smtp\" unknown");
SendmailAddress
.sin_port
= sp
->s_port
;
** Try to actually open the connection.
printf("makeconnection (%s [%s])\n", host
,
inet_ntoa(SendmailAddress
.sin_addr
.s_addr
));
s
= socket(AF_INET
, SOCK_STREAM
, 0);
syserr("makeconnection: no socket");
printf("makeconnection: %d\n", s
);
/* turn on network debugging? */
(void) setsockopt(DaemonSocket
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
, sizeof on
);
if (CurEnv
->e_xfp
!= NULL
)
(void) fflush(CurEnv
->e_xfp
); /* for debugging */
errno
= 0; /* for debugging */
SendmailAddress
.sin_family
= AF_INET
;
(struct sockaddr
*)&SendmailAddress
, sizeof SendmailAddress
) < 0)
if (hp
&& hp
->h_addr_list
[i
])
bcopy(hp
->h_addr_list
[i
++],
(char *)&SendmailAddress
.sin_addr
, hp
->h_length
);
/* failure, decide if temporary or not */
/* there are others, I'm sure..... */
/* why is this happening? */
syserr("makeconnection: funny failure, addr=%lx, port=%x",
SendmailAddress
.sin_addr
.s_addr
, SendmailAddress
.sin_port
);
extern char *errstring();
message(Arpa_Info
, "%s", errstring(sav_errno
));
/* connection ok, put it into canonical form */
*outfile
= fdopen(s
, "w");
*infile
= 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.
myhostname(hostbuf
, size
)
extern struct hostent
*gethostbyname();
if (gethostname(hostbuf
, size
) < 0)
(void) strcpy(hostbuf
, "localhost");
hp
= gethostbyname(hostbuf
);
(void) strcpy(hostbuf
, hp
->h_name
);
* MAPHOSTNAME -- turn a hostname into canonical form
* hbuf -- a buffer containing a hostname.
* hbsize -- the size of hbuf.
* Looks up the host specified in hbuf. 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.
maphostname(hbuf
, hbsize
)
register struct hostent
*hp
;
struct hostent
*gethostbyaddr();
* 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 hbuf if address is
getcanonname(hbuf
, hbsize
);
if ((cp
= index(strcpy(ptr
, hbuf
), ']')) == NULL
)
in_addr
= inet_addr(&ptr
[1]);
hp
= gethostbyaddr((char *)&in_addr
, sizeof(struct in_addr
), AF_INET
);
if (strlen(hp
->h_name
) >= hbsize
)
hp
->h_name
[hbsize
- 1] = '\0';
(void)strcpy(hbuf
, hp
->h_name
);
/* 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
);
** MAPHOSTNAME -- turn a hostname into canonical form
** hbuf -- a buffer containing a hostname.
** hbsize -- the size of hbuf.
** Looks up the host specified in hbuf. 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.
maphostname(hbuf
, hbsize
)