| 1 | #!/bin/sh |
|---|
| 2 | |
|---|
| 3 | # Database configuration |
|---|
| 4 | echo 'postgresql_enable="YES"' >> /etc/rc.conf |
|---|
| 5 | /usr/local/etc/rc.d/postgresql initdb |
|---|
| 6 | /usr/local/etc/rc.d/postgresql start |
|---|
| 7 | |
|---|
| 8 | createdb -U pgsql mail |
|---|
| 9 | createuser -U pgsql -l -R -S -D postfix |
|---|
| 10 | createuser -U pgsql -l -R -S -D courier |
|---|
| 11 | |
|---|
| 12 | cat > /tmp/basic.sql <<EOF |
|---|
| 13 | CREATE TABLE virtual_aliases ( |
|---|
| 14 | source text NOT NULL PRIMARY KEY, |
|---|
| 15 | destination text NOT NULL |
|---|
| 16 | ); |
|---|
| 17 | |
|---|
| 18 | COMMENT ON TABLE virtual_aliases IS 'A table for allowing users to maintain their own forwarding addresses'; |
|---|
| 19 | CREATE TABLE virtual_mailboxes ( |
|---|
| 20 | email text NOT NULL PRIMARY KEY, |
|---|
| 21 | passwd text NOT NULL, |
|---|
| 22 | gecos text, |
|---|
| 23 | homedir text NOT NULL |
|---|
| 24 | ); |
|---|
| 25 | |
|---|
| 26 | COMMENT ON TABLE virtual_mailboxes IS 'Store account information here. See passwd(5). UID/GID and shell are hardcoded in the configuration files'; |
|---|
| 27 | |
|---|
| 28 | CREATE TABLE virtual_domains ( |
|---|
| 29 | domain_name text NOT NULL PRIMARY KEY |
|---|
| 30 | ); |
|---|
| 31 | |
|---|
| 32 | COMMENT ON TABLE virtual_domains IS 'Store the list of hosted domains here. If a domain is not listed, we will not be able to accept and deliver mail for that domain on our systems'; |
|---|
| 33 | |
|---|
| 34 | INSERT INTO virtual_domains (domain_name) VALUES ('example.com'); |
|---|
| 35 | INSERT INTO virtual_domains (domain_name) VALUES ('example.org'); |
|---|
| 36 | |
|---|
| 37 | INSERT INTO virtual_aliases VALUES ('postmaster@example.org', 'admin@example.org'); |
|---|
| 38 | INSERT INTO virtual_aliases VALUES ('abuse@example.org', 'admin@example.org'); |
|---|
| 39 | |
|---|
| 40 | INSERT INTO virtual_mailboxes (email, passwd, gecos, homedir) VALUES ('admin@example.org', 'password', 'The admin user', 'example.org/admin/'); |
|---|
| 41 | INSERT INTO virtual_mailboxes (email, passwd, gecos, homedir) VALUES ('user1@example.org', 'password', 'The normal user', 'example.org/user1/'); |
|---|
| 42 | |
|---|
| 43 | GRANT SELECT ON virtual_mailboxes TO postfix; |
|---|
| 44 | GRANT SELECT ON virtual_domains TO postfix; |
|---|
| 45 | GRANT SELECT ON virtual_aliases TO postfix; |
|---|
| 46 | |
|---|
| 47 | GRANT SELECT ON virtual_mailboxes TO courier; |
|---|
| 48 | EOF |
|---|
| 49 | psql -U pgsql mail < /tmp/basic.sql |
|---|
| 50 | |
|---|
| 51 | # System configuration |
|---|
| 52 | echo "Create a user named vmail. This script assumes the existence of the |
|---|
| 53 | vmail username later" |
|---|
| 54 | /usr/sbin/adduser |
|---|
| 55 | |
|---|
| 56 | uid=`id -u vmail` |
|---|
| 57 | gid=`id -g vmail` |
|---|
| 58 | |
|---|
| 59 | # Postfix maps |
|---|
| 60 | cat > /usr/local/etc/postfix/virtual_mailboxes.cf <<EOF |
|---|
| 61 | user = postfix |
|---|
| 62 | dbname = mail |
|---|
| 63 | query = SELECT homedir||'/Maildir/' FROM virtual_mailboxes WHERE email = '%s' |
|---|
| 64 | hosts = 127.0.0.1:5432 |
|---|
| 65 | EOF |
|---|
| 66 | |
|---|
| 67 | # Test your map |
|---|
| 68 | postmap -q 'admin@example.org' pgsql:/usr/local/etc/postfix/virtual_mailboxes.cf |
|---|
| 69 | # If the above line doesn't print "example.org/admin" without the quotes |
|---|
| 70 | # stop to debug your configuration. |
|---|
| 71 | postmap -q 'admin@example.com' pgsql:/usr/local/etc/postfix/virtual_mailboxes.cf |
|---|
| 72 | # Should print nothing |
|---|
| 73 | |
|---|
| 74 | cat > /usr/local/etc/postfix/virtual_aliases.cf <<EOF |
|---|
| 75 | user = postfix |
|---|
| 76 | dbname = mail |
|---|
| 77 | query = SELECT destination FROM virtual_aliases WHERE source = '%s' |
|---|
| 78 | hosts = 127.0.0.1:5432 |
|---|
| 79 | EOF |
|---|
| 80 | |
|---|
| 81 | # Test your map |
|---|
| 82 | postmap -q 'postmaster@example.org' pgsql:/usr/local/etc/postfix/virtual_aliases.cf |
|---|
| 83 | # If the above line doesn't print "admin@example.org" without the quotes |
|---|
| 84 | # Stop to debug your configuration. |
|---|
| 85 | postmap -q 'admin@example.com' pgsql:/usr/local/etc/postfix/virtual_aliases.cf |
|---|
| 86 | # Should print nothing |
|---|
| 87 | |
|---|
| 88 | cat > /usr/local/etc/postfix/virtual_mailbox_domains.cf <<EOF |
|---|
| 89 | user = postfix |
|---|
| 90 | dbname = mail |
|---|
| 91 | query = SELECT domain_name FROM virtual_domains WHERE domain_name = '%s' |
|---|
| 92 | hosts = 127.0.0.1:5432 |
|---|
| 93 | EOF |
|---|
| 94 | |
|---|
| 95 | # Test your map |
|---|
| 96 | postmap -q 'example.org' pgsql:/usr/local/etc/postfix/virtual_mailbox_domains.cf |
|---|
| 97 | # If the above line doesn't print "admin@example.org" without the quotes |
|---|
| 98 | # Stop to debug your configuration. |
|---|
| 99 | postmap -q 'example.net' pgsql:/usr/local/etc/postfix/virtual_mailbox_domains.cf |
|---|
| 100 | # Should print nothing |
|---|
| 101 | |
|---|
| 102 | # Postfix configuration |
|---|
| 103 | postconf -e 'virtual_mailbox_base = /home/vmail' |
|---|
| 104 | postconf -e "virtual_uid_maps = static:$uid" |
|---|
| 105 | postconf -e "virtual_gid_maps = static:$gid" |
|---|
| 106 | postconf -e 'virtual_mailbox_domains = pgsql:/usr/local/etc/postfix/virtual_mailbox_domains.cf' |
|---|
| 107 | postconf -e 'virtual_mailbox_maps = pgsql:/usr/local/etc/postfix/virtual_mailboxes.cf' |
|---|
| 108 | postconf -e 'virtual_alias_maps = pgsql:/usr/local/etc/postfix/virtual_aliases.cf' |
|---|
| 109 | |
|---|
| 110 | # Courier authentication configuration |
|---|
| 111 | cat > /usr/local/etc/authlib/authpgsqlrc <<EOF |
|---|
| 112 | PGSQL_HOST localhost |
|---|
| 113 | PGSQL_PORT 5432 |
|---|
| 114 | PGSQL_USERNAME courier |
|---|
| 115 | PGSQL_DATABASE mail |
|---|
| 116 | PGSQL_USER_TABLE virtual_mailboxes |
|---|
| 117 | PGSQL_CLEAR_PWFIELD passwd |
|---|
| 118 | PGSQL_UID_FIELD $uid |
|---|
| 119 | PGSQL_GID_FIELD $gid |
|---|
| 120 | PGSQL_LOGIN_FIELD email |
|---|
| 121 | PGSQL_HOME_FIELD '/home/vmail/' || homedir || '/' |
|---|
| 122 | PGSQL_NAME_FIELD gecos |
|---|
| 123 | EOF |
|---|
| 124 | |
|---|
| 125 | # Let the services know something has changed |
|---|
| 126 | /usr/local/etc/rc.d/postfix restart |
|---|
| 127 | /usr/local/etc/rc.d/courier-authdaemond restart |
|---|
| 128 | |
|---|
| 129 | # Send a mail to admin@example.org |
|---|
| 130 | # This creates the maildir for the user, after which the user |
|---|
| 131 | # can login normally. |
|---|
| 132 | # If the Maildir is not created, then logins will fail. |
|---|
| 133 | echo "Welcome, humble user." | mail -s 'Welcome mail' admin@example.org |
|---|
| 134 | |
|---|
| 135 | # Test via a MUA |
|---|
| 136 | # Login: admin@example.org |
|---|
| 137 | # Password: password |
|---|
| 138 | |
|---|
| 139 | # Send a mail to an alias |
|---|
| 140 | echo "Welcome, all-powerful postmaster." | mail -s 'Welcome mail' postmaster@example.org |
|---|
| 141 | |
|---|
| 142 | # Test via a MUA |
|---|
| 143 | |
|---|
| 144 | # See your mail delivery logs for how aliases and account delivery shows up differently. |
|---|