Use webservice instead of mail.
[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         rm -f $REQUEST_FILE
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         rm -f $REQUEST_FILE
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         rm -f $REQUEST_FILE
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_request() {
177     # cleanup gpg files
178     rm -rf /var/lib/bacula-cn
179
180     if [ -e $REQUEST_FILE ]; then
181         echo Config has not changed, skipping request.
182         return
183     fi
184
185     # report disk sizes
186     DF_FILE=$(mktemp bacula-cn.XXXXXXXXXX)
187     df -h > $DF_FILE
188
189     # report database sizes
190     DB_FILE=$(mktemp bacula-cn.XXXXXXXXXX)
191     if [ -d /var/lib/mysql -o -d /var/lib/postgresql ]; then
192         du -sh /var/lib/mysql /var/lib/postgresql >$DB_FILE 2>/dev/null || true
193     fi
194
195     # generate client config
196     FD_FILE=$(mktemp bacula-cn.XXXXXXXXXX)
197     cat >> $FD_FILE <<EOF
198 # Requested by $CONTACT on $DATE
199 Client {
200   Name = $HOST-fd
201   @/etc/bacula/include/client-debian-default.conf
202   Address = $IP
203   Password = "$PASS_FD"         # password for bacula-fd(8)
204   TLS CA Certificate File = "/etc/bacula/clients.d/$HOST-fd.pem"
205 }
206
207 Console {
208   Name = $HOST-acl
209   @/etc/bacula/include/acl-default.conf
210   Password = "$PASS_BCONSOLE"         # password for bconsole(8)
211   JobACL = $HOST, RestoreFiles
212   ClientACL = $HOST-fd
213 }
214
215 Job {
216   Name = "$HOST"
217   Client = $HOST-fd
218   JobDefs = "Job_TapeBackup"
219   Messages = messages-$HOST
220 }
221
222 Messages {
223   Name = messages-$HOST
224   @/etc/bacula/include/messages-defaults.conf
225   mail = $EMAIL = all, !skipped
226 }
227 EOF
228
229     # client certificate
230     PEM_FILE=$(mktemp bacula-cn.XXXXXXXXXX)
231     sed -n '/BEGIN CERTIFICATE/,/END CERTIFICATE/p' \
232         /etc/bacula/bacula-fd.pem > $PEM_FILE
233
234     # send using web service
235     echo Sending sysbackup request.
236     [ -s $DB_FILE ] && DB_PARAM="-F db=@$DB_FILE;filename=db.txt"
237     if curl --fail --silent --show-error \
238          -F "contact=$CONTACT" \
239          -F "host=$HOST" \
240          -F "ip=$IP" \
241          -F "df=@$DF_FILE;filename=df.txt" \
242          $DB_PARAM \
243          -F "fd=@$FD_FILE;filename=$HOST-fd.conf" \
244          -F "pem=@$PEM_FILE;filename=$HOST-fd.pem" \
245          --cacert /etc/ssl/certs/ca-certificates.crt \
246          https://sysbackup.carnet.hr/bacapp.php >/dev/null
247     then
248         touch $REQUEST_FILE
249     fi
250
251     # cleanup
252     rm -f $DF_FILE $DB_FILE $FD_FILE $PEM_FILE
253
254     # report problems
255     if [ ! -e $REQUEST_FILE ]; then
256         db_input high bacula-cn/request-failed || true
257         db_go || true
258     fi
259 }
260
261 load_config() {
262     db_get bacula-cn/hostname
263     HOST=$RET
264
265     db_get bacula-cn/ip
266     IP=$RET
267
268     db_get bacula-cn/contact
269     CONTACT=$RET
270     EMAIL=${CONTACT#*<}
271     EMAIL=${EMAIL%>*}
272
273     PASS_FD=$( random_string )
274     PASS_BCONSOLE=$( random_string )
275
276     DATE=$( date '+%Y-%m-%d' )
277
278     REQUEST_FILE=/etc/bacula/bacula-fd.txt
279 }
280
281 case "$1" in
282     configure)
283         load_config
284
285         generate_cert
286         generate_dh
287         generate_fd_config
288         generate_bconsole_config
289
290         restart_bacula
291         send_request
292
293         db_stop
294     ;;
295
296     abort-upgrade|abort-remove|abort-deconfigure)
297     ;;
298
299     *)
300         echo "postinst called with unknown argument \`$1'" >&2
301         exit 1
302     ;;
303 esac
304
305 # dh_installdeb will replace this with shell code automatically
306 # generated by other debhelper scripts.
307
308 #DEBHELPER#
309
310 exit 0