1#!/bin/sh
2#
3# Configure postfix
4#
5_mail_log="/var/log/mail.log"
6_main_cf="/etc/postfix/main.cf"
7_main_cf_default="/etc/postfix/main.cf.default"
8_master_cf="/etc/postfix/master.cf"
9_master_cf_tmp="/etc/postfix/master.cf.tmp"
10_master_cf_orig="/etc/postfix/master.cf.orig"
11_master_cf_default="/etc/postfix/master.cf.default"
12_postfix_data_dir="/var/run/postfix"
13_other_plist="/etc/MailServicesOther.plist"
14_other_plist_default="/etc/MailServicesOther.plist.default"
15_mail_user_settings="/var/db/.mailusersettings.plist"
16
17echo "Configuring Postfix..."
18
19# Create the mail.log if it doesn't exist
20if [ ! -e "$_mail_log" ] ; then
21  touch "$_mail_log"
22fi
23
24# Set mail.log ownership
25if [ -e "$_mail_log" ] ; then
26  chown root:admin "$_mail_log"
27  chmod 640 "$_mail_log"
28fi
29
30# Create the mail user settings file if it doesn't exist
31if [ ! -e "$_mail_user_settings" ] ; then
32  touch "$_mail_user_settings"
33fi
34chown _postfix:mail $_mail_user_settings
35chmod 660 $_mail_user_settings
36
37# Create main.cf file
38if [ ! -e "$_main_cf" ] ; then
39  if [ -e "$_main_cf_default" ] ; then
40    cp "$_main_cf_default" "$_main_cf"
41    chmod 644 "$_main_cf"
42  else
43    echo "$_main_cf_default does not exist:  Could not create $_main_cf"
44  fi
45fi
46
47# Create master.cf
48if [ ! -e "$_master_cf" ] ; then
49  if [ -e "$_master_cf_default" ] ; then
50    cp "$_master_cf_default" "$_master_cf"
51    chmod 644 "$_master_cf"
52  else
53    echo "$_master_cf_default does not exist:  Could not create $_master_cf"
54  fi
55fi
56
57# Setup mail other settins file with defaults
58if [ ! -e "$_other_plist" ] ; then
59  if [ -e "$_master_cf_default" ] ; then
60    cp "$_other_plist_default" "$_other_plist_default"
61    chmod 644 "$_other_plist"
62  else
63    echo "$_other_plist_default does not exist:  Could not create $_other_plist"
64  fi
65fi
66
67# Create new data directory
68#  - Required by 2.5.5
69if [ ! -d "$_postfix_data_dir" ] ; then
70  mkdir "$_postfix_data_dir"
71  chmod 755 "$_postfix_data_dir"
72  chown _postfix "$_postfix_data_dir"
73else
74  chmod 755 "$_postfix_data_dir"
75  chown _postfix "$_postfix_data_dir"
76fi
77
78# Set content_filter to nil in master.cf
79#    This will override global settings
80if [ -e "$_master_cf" ] ; then
81  # Add the override
82  sed '/^pickup/ a\
83  \ \ -o content_filter=
84  ' < $_master_cf > $_master_cf_tmp
85  # If it succeeded, move original master.cf to master.cf.orig
86  #    Then move new .tmp to master.cf
87  if [ -e "$_master_cf_tmp" ] ; then
88    mv $_master_cf $_master_cf_orig
89    mv $_master_cf_tmp $_master_cf
90  fi
91fi
92
93# Set postfix owner and group IDs
94/usr/sbin/postconf -e mail_owner=_postfix
95/usr/sbin/postconf -e setgid_group=_postdrop
96/usr/sbin/postconf -e mailbox_size_limit=0
97/usr/sbin/postconf -e mydestination='$myhostname, localhost.$mydomain, localhost, $mydomain'
98
99# Set credentials for using URLAUTH with IMAP servers.
100/usr/sbin/postconf -e imap_submit_cred_file=/private/etc/postfix/submit.cred
101# Create submit.cred with either the same password dovecot is
102# configured for, or an unguessable random password.
103if [ ! -e /private/etc/postfix/submit.cred ] ; then
104	hostname=`grep "^myhostname *=" /private/etc/postfix/main.cf | sed 's,.*= *,,'`
105	if [ ! "$hostname" ] ; then
106		hostname=`hostname`
107	fi
108	if [ -s /private/etc/dovecot/submit.passdb ] ; then
109		pw=`grep "^submit:" /private/etc/dovecot/submit.passdb | sed -e 's,.*},,' -e 's,:.*,,'`
110	fi
111	if [ ! "$pw" ] ; then
112		pw=`dd if=/dev/urandom bs=256 count=1 | env LANG=C tr -dc a-zA-Z0-9 | cut -b 1-22`
113	fi
114	if [ "$pw" -a "$hostname" ]; then
115		echo "submitcred version 1" > /private/etc/postfix/submit.cred
116		echo "$hostname|submit|$pw" >> /private/etc/postfix/submit.cred
117	fi
118	chmod 600 /private/etc/postfix/submit.cred
119fi
120
121# Set spool permissions
122/usr/sbin/postfix check > /dev/null 2>&1
123
124# If postfix is not running we want to give it a quick start/stop so it creates all necessary sockets & pipes
125POSTFIX_PID=`ps -cxU root | grep master | sed -n -e "s/[^0-9]*\([0-9]*\).*/\1/p"`
126if [ "$POSTFIX_PID" = "" ] ; then
127  /usr/sbin/postfix start > /dev/null 2>&1
128  /usr/sbin/postfix stop > /dev/null 2>&1
129fi
130
131# Set log level to critical
132sed 's/^mail\.\*/mail.warn/' < /etc/syslog.conf > /etc/syslog.conf.$$ && mv /etc/syslog.conf.$$ /etc/syslog.conf
133