Email: tutorial.txt

File tutorial.txt, 5.1 KB (added by devdas, 9 years ago)

Script for the creation of virtual mailboxes with Postfix, Courier-IMAP and Postgresql

Line 
1#!/bin/sh
2
3# Database configuration
4echo 'postgresql_enable="YES"' >> /etc/rc.conf
5/usr/local/etc/rc.d/postgresql initdb
6/usr/local/etc/rc.d/postgresql start
7
8createdb -U pgsql mail
9createuser -U pgsql -l -R -S -D postfix
10createuser -U pgsql -l -R -S -D courier
11
12cat > /tmp/basic.sql <<EOF
13CREATE TABLE virtual_aliases (
14        source                  text            NOT NULL        PRIMARY KEY,
15        destination             text            NOT NULL
16);
17
18COMMENT ON TABLE virtual_aliases IS 'A table for allowing users to maintain their own forwarding addresses';
19CREATE 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
26COMMENT ON TABLE virtual_mailboxes IS 'Store account information here. See passwd(5). UID/GID and shell are hardcoded in the configuration files';
27
28CREATE TABLE virtual_domains (
29        domain_name             text            NOT NULL        PRIMARY KEY
30);
31
32COMMENT 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
34INSERT INTO virtual_domains (domain_name) VALUES ('example.com');
35INSERT INTO virtual_domains (domain_name) VALUES ('example.org');
36
37INSERT INTO virtual_aliases VALUES ('postmaster@example.org', 'admin@example.org');
38INSERT INTO virtual_aliases VALUES ('abuse@example.org', 'admin@example.org');
39
40INSERT INTO virtual_mailboxes (email, passwd, gecos, homedir) VALUES ('admin@example.org', 'password', 'The admin user', 'example.org/admin/');
41INSERT INTO virtual_mailboxes (email, passwd, gecos, homedir) VALUES ('user1@example.org', 'password', 'The normal user', 'example.org/user1/');
42
43GRANT SELECT ON virtual_mailboxes TO postfix;
44GRANT SELECT ON virtual_domains TO postfix;
45GRANT SELECT ON virtual_aliases TO postfix;
46
47GRANT SELECT ON virtual_mailboxes TO courier;
48EOF
49psql -U pgsql mail < /tmp/basic.sql
50
51# System configuration
52echo "Create a user named vmail. This script assumes the existence of the
53vmail username later"
54/usr/sbin/adduser
55
56uid=`id -u vmail`
57gid=`id -g vmail`
58
59# Postfix maps
60cat > /usr/local/etc/postfix/virtual_mailboxes.cf <<EOF
61user = postfix
62dbname = mail
63query = SELECT homedir||'/Maildir/' FROM virtual_mailboxes WHERE email = '%s'
64hosts = 127.0.0.1:5432
65EOF
66
67# Test your map
68postmap -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.
71postmap -q 'admin@example.com' pgsql:/usr/local/etc/postfix/virtual_mailboxes.cf
72# Should print nothing
73
74cat > /usr/local/etc/postfix/virtual_aliases.cf <<EOF
75user = postfix
76dbname = mail
77query = SELECT destination FROM virtual_aliases WHERE source = '%s'
78hosts = 127.0.0.1:5432
79EOF
80
81# Test your map
82postmap -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.
85postmap -q 'admin@example.com' pgsql:/usr/local/etc/postfix/virtual_aliases.cf
86# Should print nothing
87
88cat > /usr/local/etc/postfix/virtual_mailbox_domains.cf <<EOF
89user = postfix
90dbname =  mail
91query = SELECT domain_name FROM virtual_domains WHERE domain_name = '%s'
92hosts = 127.0.0.1:5432
93EOF
94
95# Test your map
96postmap -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.
99postmap -q 'example.net' pgsql:/usr/local/etc/postfix/virtual_mailbox_domains.cf
100# Should print nothing
101
102# Postfix configuration
103postconf -e 'virtual_mailbox_base = /home/vmail'
104postconf -e "virtual_uid_maps = static:$uid"
105postconf -e "virtual_gid_maps = static:$gid"
106postconf -e 'virtual_mailbox_domains = pgsql:/usr/local/etc/postfix/virtual_mailbox_domains.cf'
107postconf -e 'virtual_mailbox_maps = pgsql:/usr/local/etc/postfix/virtual_mailboxes.cf'
108postconf -e 'virtual_alias_maps = pgsql:/usr/local/etc/postfix/virtual_aliases.cf'
109
110# Courier authentication configuration
111cat > /usr/local/etc/authlib/authpgsqlrc <<EOF
112PGSQL_HOST                      localhost
113PGSQL_PORT                      5432
114PGSQL_USERNAME                  courier
115PGSQL_DATABASE                  mail
116PGSQL_USER_TABLE                virtual_mailboxes
117PGSQL_CLEAR_PWFIELD             passwd
118PGSQL_UID_FIELD                 $uid
119PGSQL_GID_FIELD                 $gid
120PGSQL_LOGIN_FIELD               email
121PGSQL_HOME_FIELD                '/home/vmail/' || homedir || '/'
122PGSQL_NAME_FIELD                gecos
123EOF
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.
133echo "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
140echo "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.