* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
static char sccsid
[] = "@(#)daemon.c 5.28 (Berkeley) %G% (with daemon mode)";
static char sccsid
[] = "@(#)daemon.c 5.28 (Berkeley) %G% (without daemon mode)";
# 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.
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.
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");
/* 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
);
if (bind(DaemonSocket
, &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
);
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.
** 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, 0);
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
);
(void) fflush(CurEnv
->e_xfp
); /* for debugging */
errno
= 0; /* for debugging */
bind(s
, &SendmailAddress
, sizeof SendmailAddress
, 0);
if (connect(s
, &SendmailAddress
, sizeof SendmailAddress
, 0) < 0)
SendmailAddress
.sin_family
= AF_INET
;
if (connect(s
, &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
);
message(Arpa_Info
, "%s", errstring(sav_errno
));
/* connection ok, put it into canonical form */
*outfile
= fdopen(s
, "w");
*infile
= fdopen(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
);
*index(strcpy(ptr
, hbuf
), ']') = '\0';
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
)