-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprovision.sh
220 lines (191 loc) · 4.72 KB
/
provision.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
# email2hook provision script
# - installs postfix if necessary
# - sets up initial configuration
#
# NOTE:
# - cannot be run as root - must be run as your user but your user must have sudo privs
USERNAME=`whoami`
MYUID=`echo $UID`
# make sure we're not root
# ==============================
if [ $USERNAME = "root" ]; then
echo -e "ERROR: cannot be run as root. Run as user with sudo privs"
exit 1
fi
# ensure postfix
# ==============================
echo -e "checking postfix"
OK=0
if apt -qq list --installed postfix 2>/dev/null | grep -q postfix; then
OK=1
fi
if [ "$OK" = 0 ]; then
echo -e "installing postfix"
if ! eval "sudo apt-get install postfix -y"; then
echo -e "ERROR: installing postfix failed"
exit 1
fi
fi
# ensure postfix-pcre
# ==============================
echo -e "checking postfix-pcre"
OK=0
if apt -qq list --installed postfix-pcre 2>/dev/null | grep -q postfix-pcre; then
OK=1
fi
if [ "$OK" = 0 ]; then
echo -e "installing postfix-pcre"
if ! eval "sudo apt-get install postfix-pcre -y"; then
echo -e "ERROR: installing postfix-pcre failed"
exit 1
fi
fi
# ensure php-cli
# ==============================
echo -e "checking php-cli"
OK=0
if apt -qq list --installed php-cli 2>/dev/null | grep -q php-cli; then
OK=1
fi
if [ "$OK" = 0 ]; then
echo -e "installing php-cli"
if ! eval "sudo apt-get install php-cli -y"; then
echo -e "ERROR: installing php-cli failed"
exit 1
fi
fi
# ensure php-curl
# ==============================
echo -e "checking php-curl"
OK=0
if apt -qq list --installed php-curl 2>/dev/null | grep -q php-curl; then
OK=1
fi
if [ "$OK" = 0 ]; then
echo -e "installing php-curl"
if ! eval "sudo apt-get install php-curl -y"; then
echo -e "ERROR: installing php-curl failed"
exit 1
fi
fi
# postfix config main.cf
# ==============================
echo -e "checking postfix config"
DELIM="# email2hook - do not touch"
CURDIR=$(
cd $(dirname "$0")
pwd
)
CONFIG=$(cat <<__EOT
$DELIM
virtual_mailbox_domains = pcre:$CURDIR/config/vdomains
virtual_mailbox_base = /home/$USERNAME/mail
virtual_mailbox_maps = pcre:$CURDIR/config/vmailbox
virtual_minimum_uid = $MYUID
virtual_uid_maps = static:$MYUID
virtual_gid_maps = static:$MYUID
virtual_mailbox_limit = 0
__EOT
)
EXISTING=$(sudo cat /etc/postfix/main.cf)
PART1=""
i=0
while read -r LINE; do
if [ "$LINE" == "$DELIM" ]; then
break
fi
PART1="${PART1}${LINE}"$'\n'
done <<< "$EXISTING"
REPLACE="${PART1}${CONFIG}"
if [ "$REPLACE" != "$EXISTING" ]; then
echo -e "replacing config"
echo "$REPLACE" | sudo tee /etc/postfix/main.cf > /dev/null
fi
# ensure virtual files and dirs
# ==============================
echo -e "checking presence of config files and dirs"
if [ ! -f $CURDIR/config/vdomains ]; then
touch $CURDIR/config/vdomains
fi
if [ ! -f $CURDIR/config/vmailbox ]; then
touch $CURDIR/config/vmailbox
fi
if [ ! -d /home/$USERNAME/mail ]; then
mkdir /home/$USERNAME/mail
fi
if [ ! -f /var/log/email2hook.log ]; then
sudo touch /var/log/email2hook.log
sudo chown $USERNAME:$USERNAME /var/log/email2hook.log
sudo chmod 664 /var/log/email2hook.log
fi
CONFIG=$(cat <<__EOT
<?php
// config
// [{name:"name",domains:["domain.com"],url:"http://domain.com/hook",count:1},...]
// where
// - name = a generic service name
// - domains = an array of email catch domains (can include * wildcard)
// - url = the http end-point to post to
// - count = number of daemons to run
// see config.sample.php for examples
\$config = [
];
__EOT
)
if [ ! -f $CURDIR/config/config.php ]; then
echo "$CONFIG" > $CURDIR/config/config.php
fi
# allow firewall if needed
# ==============================
echo -e "checking UFW"
if which ufw > /dev/null; then
OK=0
if sudo ufw status | grep -q "^Postfix[ \/].*ALLOW"; then
OK=1
fi
if [ "$OK" = 0 ]; then
echo -e "allowing postfix in ufw"
sudo ufw allow Postfix
fi
else
echo -e "ufw not detected"
fi
# reload postfix
# ==============================
echo -e "reloading postfix"
sudo postfix reload
# ensure cron job
# ==============================
echo -e "ensuring cron job"
if ! crontab -l | grep -qF "daemon_manager"; then
echo -e "installing cron"
crontab -l > /tmp/cron
echo -e "* * * * * bash $CURDIR/app/daemon_manager.sh" >> /tmp/cron
if ! eval "crontab /tmp/cron"; then
echo -e "ERROR: installing cron failed"
exit 1
fi
service cron reload
fi
# add to logrotate
# ==============================
echo -e "adding logroate conf"
CONFIG=$(cat <<__EOT
/home/$USERNAME/email2hook.log {
su $USERNAME $USERNAME
daily
missingok
rotate 4
compress
delaycompress
notifempty
create 664 $USERNAME $USERNAME
}
__EOT
)
echo "$CONFIG" | sudo tee /etc/logrotate.d/email2hook > /dev/null
# done
# ==============================
echo -e "done"
exit 0