Shared-Mailbox Richard Kay Anti-Spammed Address: rich At copsewood Dot net APPLICATION NOTES This program is intended for use where a number of local and external mail destinations defined in local user and alias files (e.g. /etc/passwd and /etc/aliases) need to share a single POP3 or IMAP type remote mailbox which collects either all mail for a domain however it is prefixed, or all mail prefixes at a domain for which other mailboxes don't exist. By domain I mean here the part of a mail address after the @ symbol and by prefix I mean the part of the address before the @ symbol. The growth of the Net is restricting the use of 32 bit IP addresses, because the number of these is limited and finite, but a growing number of people, organisations and businesses have unique domains. As restrictions on the allocation of fixed IP addresses grow, more people are likely to need to set up mail facilities for multiple local domain users with various mail aliases or mailing list owner, subscribe, approval or unsubscribe addresses etc. through the use of catch-all (e.g. anything-you-want@your.own.site ). remote mailboxes. Any number of such mail address prefixes might be caught in the same remote mailbox and periodically downloaded using fetchmail or similar using POP3 or IMAP protocols. Traditionally when there were enough IP addresses to go round and few domains, giving a mail domain it's own fixed IP address wasn't a problem, and with a fixed IP used only for this domain, mail was routed to the computer processing mail for this domain using SMTP directly. If you have a fixed IP address then you can save yourself much bother by running a SMTP MTA program such as exim, postfix or sendmail and having your ISP setup a DNS MX record pointing towards your fixed IP address, generally having themselves as a relay for when your machine is offline or down. If you havn't got the luxury of a fixed IP address and an ISP willing to send you all mail for your domain using SMTP and need to receive mail for more addresses than you can afford seperate POP mailboxes for then shared-mailbox might be for you. A typical application might be a community drop in centre or school with one or more computers and many email users and locally hosted mailing lists where all of the mail to the local network is fed through a single mailbox. Perhaps there is only a dialup line, which can be scheduled to dialup to exchange electronic mail a few times a day. You could use this setup in poor countries or emergency relief situations giving you the cheapest possible access to email communication facilities to very many users imaginable. The locally defined aliases to which shared-mailbox will forward mail can include aliases for external addresses. Fetchmail has some of the facilities of shared-mailbox but the design notes of fetchmail states that it will not redirect to external addresses, to reduce the risk of mail loops. However, external redirection might be needed for sites using Majordomo where too many local and remote aliases are needed, to make use of seperate remotely fetched POP3/IMAP mailboxes cost effective for each address handled. However, if you don't need this facility then fetchmail might be able to do the local distribution job better for you. Or not. I suspect that rather than bloating fetchmail by getting it to do things it wasn't really intended for, having a seperate smaller program to redirect shared-mailbox mail to its intended local and/or remote recipients might be more generally useful. shared-mailbox has 2 features to detect and prevent mail loops: a. use of a header unique to the shared-mailbox installation which will be detected if a message loops around again, and b. a count of the Received headers and a cutout if this number exceeds a preset value, default 40. Some excellent mail and web hosting companies (see **PLUG below) provide facilities where mail sent to anything@a.domain.of.your.own is caught in a catch-all or shared mailbox. **PLUG MODE ON e.g. check out http://www.your-site.com/ who do catch-all mail as part of their currently $60/year web/cgi/mail hosting package. Please quote me (Richard Kay, A/C copsewood.net ) as the person who referred them to you to help support my free-software efforts if you decide to use their very high quality and cost-effective service. ). **PLUG MODE OFF WARNING The fetchmail manpage states that trying to do the kind of thing this program attempts is likely to bite. I have written it with a degree of caution based on a few years of experience with elecronic mail list and hub administration. I don't imagine that I have succeeded in handling all of the many possible problems yet. If you use it you may well get bitten. If you can figure out why it went wrong, help me do this, or even better how to fix it, then please get in touch so that I can maintain and improve this program. I am not offering any support though, so you may or may not get an answer to any enquiry. MAIL NETWORK DIAGRAM SHOWING SHARED MAILBOX AND INCOMING MAIL HANDLERS ---------- : ----------- ------------ --------- -addr1->| | : | | | | | |->addr1 | Catchall | : | Fetchmail | | SMTP mail | | Shared- | -addr2->| |->-| or other |->| Daemon e.g.|->| |->addr2 | Mailbox | : | POP3/IMAP | | Sendmail, | | Mailbox | -addr3->| | : | mail | | Postfix or | | |->addr3 | at ISP | : | fetcher | | Qmail | | | ---------- : ----------- ------------ --------- ISP Remote Network : Local computer or network SYSTEM REQUIREMENTS Before using this program you will need to get mail running on your Unix/Linux type system and know how to add local users. You will need access to the root account and know what shell commands are and how to edit text files. You will need an arrangement with an ISP to use a POP3 type mailbox which accepts mail addressed externally to the users whose mail shared-mailbox will distribute. You will need to install fetchmail or similar to get mail from this external mailbox, and be using a SMTP daemon program such as sendmail, postfix, qmail or exim etc. to: a. send outgoing mail, b. distribute incoming mail and c. distribute internal mail. Your system will need to have the perl interpreter installed. My version is 5.6.0, but the program will probably run on earlier versions of Perl. INSTALLATION 1. Create a local user login account just to run this program and configure fetchmail or whatever other MTA you use (postfix, qmail, exim etc) so that mail to the shared mailbox is all sent to this local user. 2. Program shared-mailbox.pl reads a mail message on stdin and forwards it based on the actual addressee before it arrived at a catchall address mailbox. This program is typically run from a .forward file (sendmail) or .qmail ( if you are using Qmail) which must be created in the shared mailbox user home directory. The .forward file might contain the following single line : | /home/catchall/shared-mailbox.pl where /home/catchall/shared-mailbox.pl is the path at which this program is installed, and catchall is the local user to which mail to the catch-all address is sent. 3. You will need to make the program executable: chmod +x /home/catchall/shared-mailbox.pl 4. If you are using sendmail you might also need to add a link to /home/catchall/shared-mailbox.pl in the /etc/smrsh directory, e.g. using the following 2 shell commands as user root : cd /etc/smrsh ln -s /home/catchall/shared-mailbox.pl shared-mailbox.pl and either restart sendmail or send its process ID a kill -1 signal 5. create or edit the file in the shared-mailbox user directory called other_mailboxes . If the shared mailbox receives all mail for your domain you will need to add just the name of this mailbox prefix (as it will appear in the mail headers ) into this file. If there are any other mailboxes for your domain you will need to edit these names into this file one mailbox name per line, so shared-mailbox knows that when mail it receives is also copied to these addresses it doesn't need to do anything with it. 6. Configure installation specific variables by editing the top part of the source code file shared-mailbox.pl as described below. PROGRAM CONFIGURATION Before you test the program installation you will need to change a few variables towards the top of the source code, to suit the location of Perl on your system and your email addresses and domain name (the bit after the @ in your email address). The start of the program source which will read something like: #!/usr/bin/perl # may have to be changed to where your perl program lives. You can # find this location using the # which perl # shell command. # # Setup variables you will need to change # 1. The shared mailbox address to which mail being processed was sent. E.G if # messages were sent to some-address@some-company.co.uk you would put # $shared_address='some-address@some-company.co.uk'; $shared_address='everyone@a-domain-of-your-own.com'; KNOWN BUGS 1. To get behaviour more similar to sendmail the $last_resort handling should probably be configurably able to bounce mail to (Return-Path: From: Reply-to: addresses in that order ?? ). 2. Doesn't do full RFC compliant mail header parsing e.g. where mail address prefixes contain embedded prefixes (does anyone actually use these ?). BUG FIXES 0.1.1 Initialisation of $matched_this_prefix = 0; was in the wrong place, and was incorrectly re-occuring. This bugfix was submitted by Ruediger Ludwig. 0.1.2 Loop detection using the special header was broken during development prior to initial release version 0.1, by adding X- style to the header. This has been fixed (I hope) more permanently by matching on the scalar variable: $ldk_header, rather than its (incorrect) previous value.