Mail-IspMailGate

 view release on metacpan or  search on metacpan

ispMailGateD.PL  view on Meta::CPAN

	Usage();
    }
    if (!$daemon  &&  !@recipients) {
	print("Missing recipients.\n");
	Usage();
    }

    if ($deliveryHost eq 'ispmailgate') {
	$deliveryHost = undef;
    }

    my $uid = $cfg->{'mail_user'};
    if ($uid !~ /^\-?\d+$/  &&
	!defined($uid = ((getpwnam($uid))[2]))) {
	ErrExit("Unknown user: $cfg->{'mail_user'}");
    }
    my $gid = $cfg->{'mail_group'};
    if ($gid !~ /^\-?\d+$/  &&
	!defined($gid = ((getgrnam($gid))[2]))) {
	ErrExit("Unknown group: $cfg->{'mail_group'}");
    }

    if (defined(&Sys::Syslog::setlogsock)) {
	# This fails on some Solaris systems ...
        eval { Sys::Syslog::setlogsock('unix'); };
    }
    &Sys::Syslog::openlog('ispMailGateD', 'pid', $facility);

    my($attr) =
        { 'debug' => $debugging,
          'noMails' => !$mails,
	  'tmpDir' => $tmpDir
	};

    if (!$daemon) {
	$) = $gid;
	$( = $gid;
	$> = $uid;
	$< = $uid;
	Sys::Syslog::syslog('debug',
                            "Command line arguments: " . join(" ", @args))
	    if ($debugging);
	Main(\*STDIN, $from, \@recipients, $attr, $deliveryHost);
	exit(0);
    }

    #
    #   This is the servers main part
    #
    Sys::Syslog::syslog('notice', "ispMailGateD starting");
    if (!socket(SERVER, &Socket::AF_UNIX(), &Socket::SOCK_STREAM(), 0)) {
	ErrExit("Cannot create socket: $!");
    }
    unlink $unixSock;
    if (!bind(SERVER, &Socket::sockaddr_un($unixSock))) {
	ErrExit("Cannot bind: $!");
    }
    if (!listen(SERVER, &Socket::SOMAXCONN)) {
	ErrExit("Cannot listen: $!");
    }
    if (!chown $uid, $gid, $unixSock) {
	ErrExit("Cannot change ownership of $unixSock: $!");
    }
    if (!chmod 0660, $unixSock) {
	ErrExit("Cannot change mode of $unixSock: $!");
    }

    if ($fork) {
	$SIG{CHLD} = \&catchChilds;

	# Detach from the shell, as good as possible
	my $pid;
	ErrExit("Cannot fork(): $!") unless defined($pid = fork());
	exit 0 if $pid;

	if (open(PID, ">$pidFile")) {
	    print PID "$$";
	    close(PID);
	}

	open(STDOUT, ">/dev/null");
	open(STDERR, ">&STDOUT");
	open(STDIN, "</dev/null");
    }

    $) = $gid;
    $( = $gid;
    $> = $uid;
    $< = $uid;

    &Sys::Syslog::syslog('debug',
			 'Waiting for connections at %s', $unixSock)
        if $debugging;

    while (accept(CLIENT, SERVER)) {
	&Sys::Syslog::syslog('debug', 'Server accepting child')
	    if $debugging;

	#
	#  Fork a child
	#
	if ($fork) {
	    my $pid;
	    if (!defined($pid = fork())) {
		Sys::Syslog::syslog('err', "Cannot fork: $!");
		print STDERR "Cannot fork: $!";
		next;
	    } elsif ($pid) {
		#
		#  This is the parent
		#
		close(CLIENT);
		next;
	    }
	}

	#
	#  This is the child, read the environment array
	#
	my($path) = $ENV{'PATH'};
	%ENV = ();

ispMailGateD.PL  view on Meta::CPAN


=back


=head1 INSTALLATION

=head2 Requirements

To start with the requirements: You need

1.) A running sendmail (recommended: 8.9.3 or later); if you don't have
sendmail or an older version, you find the current release at

    ftp://ftp.sendmail.org/pub/sendmail

2.) A late version of Perl (5.005 or later); if you don't
have Perl, get it from any CPAN mirror, for example

    ftp://ftp.funet.fi/pub/languages/perl/CPAN/src/5.0

3.) The MIME-tools module (version 4.116 or later), its prerequired
modules (MailTools, MIME-Base64 and IO-Stringy) and the IO::Tee
module (version 0.61 or later). All these modules are available from
any CPAN mirror, for example

    ftp://ftp.funet.fi/pub/languages/perl/CPAN/modules/Mail
    ftp://ftp.funet.fi/pub/languages/perl/CPAN/modules/MIME
    ftp://ftp.funet.fi/pub/languages/perl/CPAN/modules/IO

Installing a Perl module is quite easy, btw. Either you use the
automatic CPAN interface (requires an Internet connection or
something similar) by executing

    perl -MCPAN -e shell

or you fetch the modules with FTP, extract the tar.gz files, go into
the distribution directory (for example MIME-tools-4.116) and do a

    perl Makefile.PL
    make
    make test
    make install

You'll like it! :-)

=head2 System preparation

Although ispMailGate is usually started as root, because certain
initialization settings need root permissions, it must not continue
running as root. Instead it impersonates itself to the same UID and
GID that sendmail uses for delivering mails. In what follows, I
assume UID I<daemon> and GID <mail>, as used on a Red Hat Linux
box.

IspMailGate needs its own directory for creating temporary files.
Usually this could be C</var/spool/ispmailgate> or something
similar. Make sure that the daemon user from above, (but noone else)
has access to this directory:

    mkdir /var/spool/ispmailgate
    chown daemon /var/spool/ispmailgate
    chgrp mail /var/spool/ispmailgate
    chmod 700 /var/spool/ispmailgate

=head2 Program installation

The program is installable like any other Perl module. Indeed, you
can even use the automatic CPAN installation. If you are not used
to CPAN installation or cannot use the CPAN shell, you need to
perform a manual installation: First, fetch the archive from any
CPAN mirror, for example

    ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/JWIED

and extract the archive with

    gzip -cd Mail-IspMailGate-<version>.tar.gz | tar xf -

After that, do a

    cd Mail-IspMailGate-<version>

and do a

    perl Makefile.PL
    make
    make test		# You need to be root here!
    make install	# You need to be root here!

While running "perl Makefile.PL", you'll be prompted a lot of questions.
In general you can answer them by simply hitting return, the defaults
should be fine. The questions are explained in the section CONFIGURATION
FILE below, because your answers are used for creating this config file.
L<CONFIGURATION FILE>.

If "make test" reports any errors, let me know.


=head1 SENDMAIL CONFIGURATION

To understand the required sendmail configuration, let's first take
a look at the following diagram:

	+----------------+
	| Incoming       |
	| mail for       |
	| joe@ispsoft.de |
	+----------------+

	    |
	    | Port 25
	    | (SMTP)
	    |

	+----------------------+        +---------------------+
	|                      |        |                     |
	|  Sendmail, with      |        |  IspMailGate        |
	|  IspMailGate support |  ----> |  receives and       |
	|  integrated          |        |  processes the mail |
	|                      |        |                     |
	+----------------------+        +---------------------+

ispMailGateD.PL  view on Meta::CPAN


  divert(-1)
  include(`/usr/lib/sendmail-cf/m4/cf.m4')
  define(`confCW_FILE', `/etc/mail/sendmail.cw')
  define(`ALIAS_FILE', `/etc/mail/aliases')
  define(`QUEUE_DIR', `/var/spool/mqueue2')
  define(`confDAEMON_OPTIONS', `port=26')
  OSTYPE(`linux')
  FEATURE(use_cw_file)
  FEATURE(mailertable, `hash -o /etc/mail2/mailertable')
  FEATURE(virtusertable, `hash -o /etc/mail/virtusertable')
  FEATURE(access_db)
  MAILER(local)
  MAILER(smtp)

You see, this is almost the same file, with three exceptions:

=over

=item 1.)

The first sendmail is running on the default port 25, because it has
no special port settings. The second sendmail is running on port 26.

=item 2.)

The first sendmail is using F</etc/mail/mailertable>, the second has
F</etc/mail2/mailertable>.

=item 3.)

The second sendmail doesn't have a mailer ispmailgate.

=item 4.)

The first sendmail is using another spool directory, F</var/spool/mqueue2>.
(I am not sure, whether this is required, however it doesn't harm, so let's
be on the safe side.)

=back

=item /etc/mail/mailertable

  ispsoft.de	ispmailgate:ispsoft.de

=item /etc/mail2/mailertable

  ispsoft.de	ispmailgate:mail.ispsoft.de

You see, the only difference between these two instances of mailertable
is that the domain I<ispsoft.de> is treated different.

=back

Finally we have to execute some commands to get this configuration
running:

	mkdir /var/spool/mqueue2
	# The following should match the settings of
	# /var/spool/mqueue exactly.
	chown root /var/spool/mqueue2
	chgrp root /var/spool/mqueue2
	chmod 755 /var/spool/mqueue

	cd /etc/mail
	m4 sendmail.mc >sendmail.cf
	makemap hash mailertable <mailertable
	cd /etc/mail2
	m4 sendmail.mc >sendmail.cf
	makemap hash mailertable <mailertable

The above is fine for Linux. On other operating systems you might
need to use GNU m4 rather than the builtin m4 and replace the map
type I<hash> with I<dbm>. In particular this applies to Solaris.

Finally, kill any running sendmail and start the new versions
with

	chmod 755 /etc/mail /etc/mail2
	chmod 644 /etc/mail/sendmail.cf /etc/mail2/sendmail.cf
	/usr/sbin/sendmail -C /etc/mail/sendmail.cf -bd -q1h
	/usr/sbin/sendmail -C /etc/mail2/sendmail.cf -bd -q1h

(The chmod commands because sendmail is I<really> picky about
group writable files and directories.)


=head1 CONFIGURATION FILE

The program depends on a local configuration file, read as the
Mail::IspMailGate::Config module. In other words, this configuration file
is pure Perl code defining certain variables under the name space
Mail::IspMailGate::Config. The module is read from the file
~installsitelib~/Mail/IspMailGate/Config.pm.

The following variables are meaningful to the program:

=over 4

=item VERSION

The programs version; do not modify without a good reason.

=item tmp_dir

Set's the default directory for creating temporary files, currently
~tmp_dir~. You can modify this with the C<--tmpdir> directive, see
above.

=item unix_sock

The unix socket that the client connects to, currently ~unix_sock~.
You can use the C<--unixsock> argument for overwriting the default.

=item pid_file

The PID file where a running server stores its PID, currently ~pid_file~.
You can use the C<--pidfile> argument for overwriting the default.

=item mail_user



( run in 0.631 second using v1.01-cache-2.11-cpan-71847e10f99 )