a192ddcdb52ce0a9dd57cea299f64e57839606bb
[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 send_mail() {
173     REQUEST_FILE=/etc/bacula/bacula-fd.txt
174     GPG_HOME=/var/lib/bacula-cn/gpg
175
176     BOUNDARY=$( head -20 /dev/urandom | openssl dgst -sha1 )
177     GPG="gpg --homedir $GPG_HOME --batch --encrypt --armour --recipient rt@tt.carnet.hr --always-trust"
178
179     if [ -z "$CONFIG_CHANGED" ]; then
180         echo Config has not changed, skipping request.
181         return
182     fi
183
184     umask 022
185     chmod 0700 $GPG_HOME
186     echo Generating request in $REQUEST_FILE
187     rm -f $REQUEST_FILE
188
189     # Header
190     cat > $REQUEST_FILE <<EOF
191 From: $CONTACT
192 To: sysbackup@carnet.hr
193 Subject: Backup za $HOST
194 MIME-Version: 1.0
195 Content-Type: multipart/mixed; boundary="$BOUNDARY"
196
197 This is a message with multiple parts in MIME format.
198 --$BOUNDARY
199 Content-Type: text/plain
200 Content-Transfer-Encoding: 7bit
201 Content-Disposition: inline
202
203 Ime posluzitelja: $HOST
204 IP adresa: $IP
205 Kontakt adresa: $CONTACT
206 EOF
207
208 # attachment: disk sizes
209 cat >> $REQUEST_FILE <<EOF
210 --$BOUNDARY
211 Content-Type: text/plain
212 Content-Transfer-Encoding: 7bit
213 Content-Disposition: inline; filename="df.txt.gpg"
214
215 EOF
216
217   df -h | $GPG >> $REQUEST_FILE
218
219 # attachment: client config
220 cat >> $REQUEST_FILE <<EOF
221 --$BOUNDARY
222 Content-Type: text/plain
223 Content-Transfer-Encoding: 7bit
224 Content-Disposition: inline; filename="$HOST-fd.conf.gpg"
225
226 EOF
227
228     cat <<EOF | $GPG >> $REQUEST_FILE
229 # Requested by $CONTACT on $DATE
230 Client {
231   Name = $HOST-fd
232   Address = $IP
233   Password = "$PASS_FD"         # password for bacula-fd(8)
234   @/etc/bacula/include/client-debian-default.conf
235
236   TLS Enable = yes
237   TLS Require = yes
238   TLS CA Certificate File = "/etc/bacula/clients.d/$HOST-fd.pem"
239   TLS Certificate = "/etc/bacula/bacula.pem"
240   TLS Key = "/etc/bacula/bacula.key"
241 }
242
243 Job {
244   Name = "$HOST"
245   Client = $HOST-fd
246   JobDefs = "Job_SysBackup"
247 }
248
249 Console {
250   Name = $HOST-acl
251   Password = "$PASS_BCONSOLE"         # password for bconsole(8)
252   JobACL = $HOST, RestoreFiles
253   ClientACL = $HOST-fd
254   @/etc/bacula/include/acl-default.conf
255 }
256 EOF
257
258 # attachment: client certificate
259 cat >> $REQUEST_FILE <<EOF
260 --$BOUNDARY
261 Content-Type: text/plain
262 Content-Transfer-Encoding: 7bit
263 Content-Disposition: inline; filename="$HOST-fd.pem.gpg"
264
265 EOF
266
267     sed -n '/BEGIN CERTIFICATE/,/END CERTIFICATE/p' /etc/bacula/bacula-fd.pem \
268     | $GPG >> $REQUEST_FILE
269
270 cat >> $REQUEST_FILE <<EOF
271 --$BOUNDARY--
272 EOF
273
274     # Try to send it
275     if [ -x "`which sendmail 2>/dev/null`" ]; then
276         echo Mailing request from $REQUEST_FILE
277         if sendmail -t -oi < $REQUEST_FILE; then
278             requestsent=1
279         fi
280     fi
281
282     if [ -z "$requestsent" ]; then
283         db_input high bacula-cn/mail-failed || true
284         db_go || true
285     fi
286 }
287
288 load_config() {
289     db_get bacula-cn/hostname
290     HOST=$RET
291
292     db_get bacula-cn/ip
293     IP=$RET
294
295     db_get bacula-cn/contact
296     CONTACT=$RET
297
298     PASS_FD=$( head -20 /dev/urandom | openssl dgst -sha1 )
299     PASS_BCONSOLE=$( head -20 /dev/urandom | openssl dgst -sha1 )
300
301     DATE=$( date '+%Y-%m-%d' )
302
303     CONFIG_CHANGED=
304 }
305
306 case "$1" in
307     configure)
308         load_config
309
310         generate_cert
311         generate_dh
312         generate_fd_config
313         generate_bconsole_config
314
315         restart_bacula
316         send_mail
317
318         db_stop
319     ;;
320
321     abort-upgrade|abort-remove|abort-deconfigure)
322     ;;
323
324     *)
325         echo "postinst called with unknown argument \`$1'" >&2
326         exit 1
327     ;;
328 esac
329
330 # dh_installdeb will replace this with shell code automatically
331 # generated by other debhelper scripts.
332
333 #DEBHELPER#
334
335 exit 0