Fix key file name.
[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 'sysbackup-dir' $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 = "$PASS_BCONSOLE"
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 EOF
124     fi
125 }
126
127 generate_dh() {
128     DH_FILE=/etc/bacula/dh1024.pem
129
130     if [ -s $DH_FILE ]; then
131         echo $DH_FILE already exists, skipping.
132     else
133         umask 077
134
135         echo Generating $DH_FILE
136         openssl dhparam -out $DH_FILE -5 1024
137     fi
138 }
139
140 generate_cert() {
141     CERT_FILE=/etc/bacula/bacula-fd.pem
142
143     if [ -s $CERT_FILE ]; then
144         echo $CERT_FILE already exists, skipping.
145     else
146         umask 077
147
148         echo Generating $CERT_FILE
149         CONFIG_CHANGED=1
150         openssl req -new -newkey rsa:2048 -nodes -keyout $CERT_FILE \
151             -subj "/C=HR/ST=Croatia/O=CARNet/OU=sysbackup/CN=$IP" \
152             -x509 -extensions usr_cert -days $((365*5)) \
153             -out $CERT_FILE
154     fi
155 }
156
157 restart_bacula() {
158     if [ -x "/etc/init.d/bacula-fd" ]; then
159         if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
160             invoke-rc.d bacula-fd restart || exit $?
161         else
162             /etc/init.d/bacula-fd restart || exit $?
163         fi
164     fi
165 }
166
167 send_mail() {
168     REQUEST_FILE=/etc/bacula/bacula-fd.txt
169     GPG_HOME=/var/lib/bacula-cn/gpg
170
171     BOUNDARY=$( head -20 /dev/urandom | openssl dgst -sha1 )
172     GPG="gpg --homedir $GPG_HOME --batch --encrypt --armour --recipient rt@tt.carnet.hr --always-trust"
173
174     if [ -z "$CONFIG_CHANGED" ]; then
175         echo Config has not changed, skipping request.
176         return
177     fi
178
179     umask 077
180     chmod 0700 $GPG_HOME
181     echo Generating request in $REQUEST_FILE
182
183     # Header
184     cat > $REQUEST_FILE <<EOF
185 From: $CONTACT
186 To: sysbackup@carnet.hr
187 Subject: Backup za $HOST
188 MIME-Version: 1.0
189 Content-Type: multipart/mixed; boundary="$BOUNDARY"
190
191 This is a message with multiple parts in MIME format.
192 --$BOUNDARY
193 Content-Type: text/plain
194 Content-Transfer-Encoding: 7bit
195 Content-Disposition: inline
196
197 Ime posluzitelja: $HOST
198 IP adresa: $IP
199 Kontakt adresa: $CONTACT
200 EOF
201
202 # attachment: disk sizes
203 cat >> $REQUEST_FILE <<EOF
204 --$BOUNDARY
205 Content-Type: text/plain
206 Content-Transfer-Encoding: 7bit
207 Content-Disposition: inline; filename="df.txt.gpg"
208
209 EOF
210
211   df -h | $GPG >> $REQUEST_FILE
212
213 # attachment: client config
214 cat >> $REQUEST_FILE <<EOF
215 --$BOUNDARY
216 Content-Type: text/plain
217 Content-Transfer-Encoding: 7bit
218 Content-Disposition: inline; filename="$HOST-fd.conf.gpg"
219
220 EOF
221
222     cat <<EOF | $GPG >> $REQUEST_FILE
223 # Requested by $CONTACT on $DATE
224 Client {
225   Name = $HOST-fd
226   Address = $IP
227   Password = "$PASS_FD"         # password for bacula-fd(8)
228   @/etc/bacula/include/client-debian-default.conf
229
230   TLS Enable = yes
231   TLS Require = yes
232   TLS CA Certificate File = "/etc/bacula/clients.d/$HOST-fd.pem"
233   TLS Certificate = "/etc/bacula/bacula.pem"
234   TLS Key = "/etc/bacula/bacula.key"
235 }
236
237 Job {
238   Name = "$HOST"
239   Client = $HOST-fd
240   JobDefs = "Job_SysBackup"
241 }
242
243 Console {
244   Name = $HOST-acl
245   Password = "$PASS_BCONSOLE"         # password for bconsole(8)
246   JobACL = $HOST, RestoreFiles
247   ClientACL = $HOST-fd
248   @/etc/bacula/include/acl-default.conf
249 }
250 EOF
251
252 # attachment: client certificate
253 cat >> $REQUEST_FILE <<EOF
254 --$BOUNDARY
255 Content-Type: text/plain
256 Content-Transfer-Encoding: 7bit
257 Content-Disposition: inline; filename="$HOST-fd.pem.gpg"
258
259 EOF
260
261     sed -n '/BEGIN CERTIFICATE/,/END CERTIFICATE/p' /etc/bacula/bacula-fd.pem \
262     | $GPG >> $REQUEST_FILE
263
264 cat >> $REQUEST_FILE <<EOF
265 --$BOUNDARY--
266 EOF
267
268     # Send it
269     echo Mailing request from $REQUEST_FILE
270     sendmail -t -oi < $REQUEST_FILE
271 }
272
273 load_config() {
274     db_get bacula-cn/hostname
275     HOST=$RET
276
277     db_get bacula-cn/ip
278     IP=$RET
279
280     db_get bacula-cn/contact
281     CONTACT=$RET
282
283     db_stop
284
285     PASS_FD=$( head -20 /dev/urandom | openssl dgst -sha1 )
286     PASS_BCONSOLE=$( head -20 /dev/urandom | openssl dgst -sha1 )
287
288     DATE=$( date '+%Y-%m-%d' )
289
290     CONFIG_CHANGED=
291 }
292
293 case "$1" in
294     configure)
295         load_config
296
297         generate_cert
298         generate_dh
299         generate_fd_config
300         generate_bconsole_config
301
302         restart_bacula
303         send_mail
304     ;;
305
306     abort-upgrade|abort-remove|abort-deconfigure)
307     ;;
308
309     *)
310         echo "postinst called with unknown argument \`$1'" >&2
311         exit 1
312     ;;
313 esac
314
315 # dh_installdeb will replace this with shell code automatically
316 # generated by other debhelper scripts.
317
318 #DEBHELPER#
319
320 exit 0