68f0916d34ed38034af6de0aad4b1a81509943dc
[bacula-cn.git] / debian / postinst
1 #!/bin/sh
2 # postinst script for bacula-cn
3 #
4 # see: dh_installdeb(1)
5
6 set -e
7
8 # Source debconf library.
9 . /usr/share/debconf/confmodule
10
11 # summary of how this script can be called:
12 #        * <postinst> `configure' <most-recently-configured-version>
13 #        * <old-postinst> `abort-upgrade' <new version>
14 #        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
15 #          <new-version>
16 #        * <postinst> `abort-remove'
17 #        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
18 #          <failed-install-package> <version> `removing'
19 #          <conflicting-package> <version>
20 # for details, see http://www.debian.org/doc/debian-policy/ or
21 # the debian-policy package
22
23 generate_fd_config() {
24     FD_CONFIG=/etc/bacula/bacula-fd.conf
25
26     if [ -s $FD_CONFIG ] && grep -q 'PKI Keypair' $FD_CONFIG; then
27         echo $FD_CONFIG already exists, skipping.
28     else
29         umask 077
30       
31         if [ -e $FD_CONFIG -a ! -e $FD_CONFIG.bak ]; then
32             cp -av $FD_CONFIG $FD_CONFIG.bak
33         fi
34
35         echo Generating $FD_CONFIG
36         CONFIG_CHANGED=1
37         cat >$FD_CONFIG <<EOF
38 #
39 # List Directors who are permitted to contact this File daemon
40 #
41 Director {
42   Name = sysbackup-dir
43   Password = "$PASS_FD"
44
45   TLS Enable = yes
46   TLS Require = yes
47   TLS Verify Peer = yes
48   # Allow only the Director to connect
49   TLS Allowed CN = "sysbackup.carnet.hr"
50   TLS CA Certificate File = "/etc/bacula/sysbackup.pem"
51   # This is a server certificate. It is used by connecting
52   # directors to verify the authenticity of this file daemon
53   TLS Certificate = "/etc/bacula/bacula-fd.pem"
54   TLS Key = "/etc/bacula/bacula-fd.pem"
55   TLS DH File = "/etc/bacula/dh1024.pem"
56 }
57
58 #
59 # "Global" File daemon configuration specifications
60 #
61 FileDaemon {                          # this is me
62   Name = $HOST-fd
63   FDport = 9102                  # where we listen for the director
64   WorkingDirectory = /var/lib/bacula
65   Pid Directory = /var/run/bacula
66   Maximum Concurrent Jobs = 20
67   FDAddress = $IP
68
69   # you need these TLS entries so the FD and SD can communicate
70   TLS Enable = yes
71   TLS Require = yes
72   TLS CA Certificate File = "/etc/bacula/sysbackup.pem"
73   TLS Certificate = "/etc/bacula/bacula-fd.pem"
74   TLS Key = "/etc/bacula/bacula-fd.pem"
75
76   # you need these PKI entries to encrypt data before sending it to backup
77   PKI Signatures = Yes            # Enable Data Signing
78   PKI Encryption = Yes            # Enable Data Encryption
79   PKI Keypair = "/etc/bacula/bacula-fd.pem"    # Public and Private Keys
80 }
81
82 # Send all messages except skipped files back to Director
83 Messages {
84   Name = Standard
85   director = sysbackup-dir = all, !skipped, !restored
86 }
87 EOF
88     fi
89 }
90
91 generate_bconsole_config() {
92     BCONSOLE_CONFIG=/etc/bacula/bconsole.conf
93
94     if [ -s $BCONSOLE_CONFIG ] && grep -q 'Console {' $BCONSOLE_CONFIG; then
95         echo $BCONSOLE_CONFIG already exists, skipping.
96     else
97         umask 077
98
99         if [ -e $BCONSOLE_CONFIG -a ! -e $BCONSOLE_CONFIG.bak ]; then
100             cp -av $BCONSOLE_CONFIG $BCONSOLE_CONFIG.bak
101         fi
102
103         echo Generating $BCONSOLE_CONFIG
104         CONFIG_CHANGED=1
105         cat >$BCONSOLE_CONFIG <<EOF
106 #
107 # Bacula User Agent (or Console) Configuration File
108 #
109
110 Director {
111   Name = sysbackup-dir
112   DIRport = 9101
113   address = sysbackup.carnet.hr
114   Password = "__INVALID__"   # not used
115
116   # you need these TLS entries so the bconsole and Director can communicate
117   TLS Enable = yes
118   TLS Require = yes
119   TLS CA Certificate File = "/etc/bacula/sysbackup.pem"
120   TLS Certificate = "/etc/bacula/bacula-fd.pem"
121   TLS Key = "/etc/bacula/bacula-fd.pem"
122 }
123
124 Console {
125   Name = $HOST-acl
126   Password = "$PASS_BCONSOLE"
127 }
128 EOF
129     fi
130 }
131
132 generate_dh() {
133     DH_FILE=/etc/bacula/dh1024.pem
134
135     if [ -s $DH_FILE ]; then
136         echo $DH_FILE already exists, skipping.
137     else
138         umask 077
139
140         echo Generating $DH_FILE
141         openssl dhparam -out $DH_FILE -5 1024
142     fi
143 }
144
145 generate_cert() {
146     CERT_FILE=/etc/bacula/bacula-fd.pem
147
148     if [ -s $CERT_FILE ]; then
149         echo $CERT_FILE already exists, skipping.
150     else
151         umask 077
152
153         echo Generating $CERT_FILE
154         CONFIG_CHANGED=1
155         openssl req -new -newkey rsa:2048 -nodes -keyout $CERT_FILE \
156             -subj "/C=HR/ST=Croatia/O=CARNet/OU=sysbackup/CN=$IP" \
157             -x509 -extensions usr_cert -days $((365*5)) \
158             -out $CERT_FILE
159     fi
160 }
161
162 restart_bacula() {
163     if [ -x "/etc/init.d/bacula-fd" ]; then
164         if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
165             invoke-rc.d bacula-fd restart || exit $?
166         else
167             /etc/init.d/bacula-fd restart || exit $?
168         fi
169     fi
170 }
171
172 random_string() {
173     head -n 20 /dev/urandom | openssl dgst -sha1 | sed 's/^.* //'
174 }
175
176 send_mail() {
177     REQUEST_FILE=/etc/bacula/bacula-fd.txt
178     GPG_HOME=/var/lib/bacula-cn/gpg
179
180     BOUNDARY=$( random_string )
181     GPG="gpg --homedir $GPG_HOME --batch --encrypt --armour --recipient rt@tt.carnet.hr --always-trust"
182
183     if [ -z "$CONFIG_CHANGED" ]; then
184         echo Config has not changed, skipping request.
185         return
186     fi
187
188     umask 022
189     chmod 0700 $GPG_HOME
190     echo Generating request in $REQUEST_FILE
191     rm -f $REQUEST_FILE
192
193     # Header
194     cat > $REQUEST_FILE <<EOF
195 From: $CONTACT
196 To: sysbackup@carnet.hr
197 Subject: Backup za $HOST
198 MIME-Version: 1.0
199 Content-Type: multipart/mixed; boundary="$BOUNDARY"
200
201 This is a message with multiple parts in MIME format.
202 --$BOUNDARY
203 Content-Type: text/plain
204 Content-Transfer-Encoding: 7bit
205 Content-Disposition: inline
206
207 Ime posluzitelja: $HOST
208 IP adresa: $IP
209 Kontakt adresa: $CONTACT
210 EOF
211
212 # attachment: disk sizes
213 cat >> $REQUEST_FILE <<EOF
214 --$BOUNDARY
215 Content-Type: text/plain
216 Content-Transfer-Encoding: 7bit
217 Content-Disposition: inline; filename="df.txt.gpg"
218
219 EOF
220
221     df -h | $GPG >> $REQUEST_FILE
222
223 # attachment: database sizes
224 if [ -d /var/lib/mysql -o -d /var/lib/postgresql ]; then
225     cat >> $REQUEST_FILE <<EOF
226 --$BOUNDARY
227 Content-Type: text/plain
228 Content-Transfer-Encoding: 7bit
229 Content-Disposition: inline; filename="db.txt.gpg"
230
231 EOF
232
233     du -sh /var/lib/mysql /var/lib/postgresql 2>/dev/null \
234     | $GPG >> $REQUEST_FILE
235 fi
236
237 # attachment: client config
238 cat >> $REQUEST_FILE <<EOF
239 --$BOUNDARY
240 Content-Type: text/plain
241 Content-Transfer-Encoding: 7bit
242 Content-Disposition: inline; filename="$HOST-fd.conf.gpg"
243
244 EOF
245
246     cat <<EOF | $GPG >> $REQUEST_FILE
247 # Requested by $CONTACT on $DATE
248 Client {
249   Name = $HOST-fd
250   @/etc/bacula/include/client-debian-default.conf
251   Address = $IP
252   Password = "$PASS_FD"         # password for bacula-fd(8)
253   TLS CA Certificate File = "/etc/bacula/clients.d/$HOST-fd.pem"
254 }
255
256 Console {
257   Name = $HOST-acl
258   @/etc/bacula/include/acl-default.conf
259   Password = "$PASS_BCONSOLE"         # password for bconsole(8)
260   JobACL = $HOST, RestoreFiles
261   ClientACL = $HOST-fd
262 }
263
264 Job {
265   Name = "$HOST"
266   Client = $HOST-fd
267   JobDefs = "Job_TapeBackup"
268   Messages = messages-$HOST
269 }
270
271 Messages {
272   Name = messages-$HOST
273   @/etc/bacula/include/messages-defaults.conf
274   mail = $EMAIL = all, !skipped
275 }
276 EOF
277
278 # attachment: client certificate
279 cat >> $REQUEST_FILE <<EOF
280 --$BOUNDARY
281 Content-Type: text/plain
282 Content-Transfer-Encoding: 7bit
283 Content-Disposition: inline; filename="$HOST-fd.pem.gpg"
284
285 EOF
286
287     sed -n '/BEGIN CERTIFICATE/,/END CERTIFICATE/p' /etc/bacula/bacula-fd.pem \
288     | $GPG >> $REQUEST_FILE
289
290 cat >> $REQUEST_FILE <<EOF
291 --$BOUNDARY--
292 EOF
293
294     # Try to send it
295     if [ -x "`which sendmail 2>/dev/null`" ]; then
296         echo Mailing request from $REQUEST_FILE
297         if sendmail -t -oi -f "$EMAIL" < $REQUEST_FILE; then
298             requestsent=1
299         fi
300     fi
301
302     if [ -z "$requestsent" ]; then
303         db_input high bacula-cn/mail-failed || true
304         db_go || true
305     fi
306 }
307
308 load_config() {
309     db_get bacula-cn/hostname
310     HOST=$RET
311
312     db_get bacula-cn/ip
313     IP=$RET
314
315     db_get bacula-cn/contact
316     CONTACT=$RET
317     EMAIL=${CONTACT#*<}
318     EMAIL=${EMAIL%>*}
319
320     PASS_FD=$( random_string )
321     PASS_BCONSOLE=$( random_string )
322
323     DATE=$( date '+%Y-%m-%d' )
324
325     CONFIG_CHANGED=
326 }
327
328 case "$1" in
329     configure)
330         load_config
331
332         generate_cert
333         generate_dh
334         generate_fd_config
335         generate_bconsole_config
336
337         restart_bacula
338         send_mail
339
340         db_stop
341     ;;
342
343     abort-upgrade|abort-remove|abort-deconfigure)
344     ;;
345
346     *)
347         echo "postinst called with unknown argument \`$1'" >&2
348         exit 1
349     ;;
350 esac
351
352 # dh_installdeb will replace this with shell code automatically
353 # generated by other debhelper scripts.
354
355 #DEBHELPER#
356
357 exit 0