Prva inačica za bullseye
[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/dh2048.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 = /run/bacula
66   Maximum Concurrent Jobs = 20
67   Plugin Directory = /usr/lib/bacula
68   FDAddress = $IP
69
70   # you need these TLS entries so the FD and SD can communicate
71   TLS Enable = yes
72   TLS Require = yes
73   TLS CA Certificate File = "/etc/bacula/sysbackup.pem"
74   TLS Certificate = "/etc/bacula/bacula-fd.pem"
75   TLS Key = "/etc/bacula/bacula-fd.pem"
76
77   # you need these PKI entries to encrypt data before sending it to backup
78   PKI Signatures = Yes            # Enable Data Signing
79   PKI Encryption = Yes            # Enable Data Encryption
80   PKI Keypair = "/etc/bacula/bacula-fd.pem"    # Public and Private Keys
81 }
82
83 # Send all messages except skipped files back to Director
84 Messages {
85   Name = Standard
86   director = sysbackup-dir = all, !skipped, !restored
87 }
88 EOF
89     fi
90 }
91
92 generate_bconsole_config() {
93     BCONSOLE_CONFIG=/etc/bacula/bconsole.conf
94
95     if [ -s $BCONSOLE_CONFIG ] && grep -q 'Console {' $BCONSOLE_CONFIG; then
96         echo $BCONSOLE_CONFIG already exists, skipping.
97     else
98         umask 077
99
100         if [ -e $BCONSOLE_CONFIG -a ! -e $BCONSOLE_CONFIG.bak ]; then
101             cp -av $BCONSOLE_CONFIG $BCONSOLE_CONFIG.bak
102         fi
103
104         echo Generating $BCONSOLE_CONFIG
105         rm -f $REQUEST_FILE
106         cat >$BCONSOLE_CONFIG <<EOF
107 #
108 # Bacula User Agent (or Console) Configuration File
109 #
110
111 Director {
112   Name = sysbackup-dir
113   DIRport = 9101
114   address = sysbackup.carnet.hr
115   Password = "__INVALID__"   # not used
116
117   # you need these TLS entries so the bconsole and Director can communicate
118   TLS Enable = yes
119   TLS Require = yes
120   TLS CA Certificate File = "/etc/bacula/sysbackup.pem"
121   TLS Certificate = "/etc/bacula/bacula-fd.pem"
122   TLS Key = "/etc/bacula/bacula-fd.pem"
123 }
124
125 Console {
126   Name = $HOST-acl
127   Password = "$PASS_BCONSOLE"
128 }
129 EOF
130     fi
131 }
132
133 generate_dh() {
134     DH_FILE=/etc/bacula/dh2048.pem
135
136     if [ -s $DH_FILE ]; then
137         echo $DH_FILE already exists, skipping.
138     else
139         umask 077
140
141         echo Generating $DH_FILE
142         ###openssl dhparam -out $DH_FILE -5 1024
143         openssl dhparam -out $DH_FILE -2 2048
144         
145     fi
146 }
147
148 generate_cert() {
149     CERT_FILE=/etc/bacula/bacula-fd.pem
150
151     if [ -s $CERT_FILE ]; then
152         echo $CERT_FILE already exists, skipping.
153     else
154         umask 077
155
156         echo Generating $CERT_FILE
157         rm -f $REQUEST_FILE
158         openssl req -new -newkey rsa:2048 -nodes -keyout $CERT_FILE \
159             -subj "/C=HR/ST=Croatia/O=CARNet/OU=sysbackup/CN=$IP" \
160             -x509 -extensions usr_cert -days $((365*10)) \
161             -out $CERT_FILE
162     fi
163 }
164
165 restart_bacula() {
166     if [ -x "/etc/init.d/bacula-fd" ]; then
167         if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
168             invoke-rc.d bacula-fd restart || exit $?
169         else
170             /etc/init.d/bacula-fd restart || exit $?
171         fi
172     fi
173 }
174
175 random_string() {
176     head -n 20 /dev/urandom | openssl dgst -sha1 | sed 's/^.* //'
177 }
178
179 send_request() {
180     # cleanup gpg files
181     rm -rf /var/lib/bacula-cn
182
183     if [ -e $REQUEST_FILE ]; then
184         echo Config has not changed, skipping request.
185         return
186     fi
187
188     # report disk sizes
189     DF_FILE=$(mktemp bacula-cn.XXXXXXXXXX)
190     df -h > $DF_FILE
191
192     # dodaj i /media i /mnt
193     test -d /media && du -hs /media || true >> $DF_FILE
194     test -d /mnt && du -hs /mnt || true >> $DF_FILE
195
196     # report database sizes
197     DB_FILE=$(mktemp bacula-cn.XXXXXXXXXX)
198     if [ -d /var/lib/mysql -o -d /var/lib/postgresql ]; then
199         du -sh /var/lib/mysql /var/lib/postgresql >$DB_FILE 2>/dev/null || true
200     fi
201
202     # generate client config
203     FD_FILE=$(mktemp bacula-cn.XXXXXXXXXX)
204     cat >> $FD_FILE <<EOF
205 # Requested by $CONTACT on $DATE
206 Client {
207   Name = $HOST-fd
208   @/etc/bacula/include/client-debian-default.conf
209   Address = $IP
210   Password = "$PASS_FD"         # password for bacula-fd(8)
211   TLS CA Certificate File = "/etc/bacula/clients.d/$HOST-fd.pem"
212 }
213
214 Console {
215   Name = $HOST-acl
216   @/etc/bacula/include/acl-default.conf
217   Password = "$PASS_BCONSOLE"         # password for bconsole(8)
218   JobACL = $HOST, RestoreFiles
219   ClientACL = $HOST-fd
220 }
221
222 Job {
223   Name = "$HOST"
224   Client = $HOST-fd
225   JobDefs = "Job_TapeBackup"
226   Messages = messages-$HOST
227 }
228
229 Messages {
230   Name = messages-$HOST
231   @/etc/bacula/include/messages-defaults.conf
232   mail = $EMAIL = all, !skipped
233 }
234 EOF
235
236     # client certificate
237     PEM_FILE=$(mktemp bacula-cn.XXXXXXXXXX)
238     sed -n '/BEGIN CERTIFICATE/,/END CERTIFICATE/p' \
239         /etc/bacula/bacula-fd.pem > $PEM_FILE
240
241     # send using web service
242     echo Sending sysbackup request.
243     [ -s $DB_FILE ] && DB_PARAM="-F db=@$DB_FILE;filename=db.txt"
244     if curl --fail --silent --show-error \
245          -F "contact=$CONTACT" \
246          -F "host=$HOST" \
247          -F "ip=$IP" \
248          -F "df=@$DF_FILE;filename=df.txt" \
249          $DB_PARAM \
250          -F "fd=@$FD_FILE;filename=$HOST-fd.conf" \
251          -F "pem=@$PEM_FILE;filename=$HOST-fd.pem" \
252          --cacert /etc/ssl/certs/ca-certificates.crt \
253          https://sysbackup.carnet.hr/bacapp.php >/dev/null
254     then
255         touch $REQUEST_FILE
256     fi
257
258     # cleanup
259     rm -f $DF_FILE $DB_FILE $FD_FILE $PEM_FILE
260
261    # report problems
262     if [ ! -e $REQUEST_FILE ]; then
263         db_input high bacula-cn/request-failed || true
264         db_go || true
265     fi
266 }
267    
268 fix_privileges() {
269    # fix privileges 
270    
271     chown root:bacula /etc/bacula/bacula-fd.pem /etc/bacula/bacula-fd.conf /etc/bacula/bconsole.conf
272     chmod 640 /etc/bacula/bacula-fd.pem /etc/bacula/bacula-fd.conf /etc/bacula/bconsole.conf  
273 }
274  
275 set_pinning() {
276    # set pin to version from stretch, because bacula 9.* isn't compatible
277
278    PINFILE=/etc/apt/preferences.d/99-carnet
279
280    test -f $PINFILE || touch $PINFILE
281
282    cp-update bacula-cn $PINFILE <<EOF
283
284 Package: bacula-fd
285 Pin: version 7.4.4*
286 Pin-Priority: 1001
287
288 Package: bacula-common
289 Pin: version 7.4.4*
290 Pin-Priority: 1001
291
292 Package: bacula-console
293 Pin: version 7.4.4*
294 Pin-Priority: 1001
295
296 EOF
297
298 }
299
300 load_config() {
301     db_get bacula-cn/hostname
302     HOST=$RET
303
304     db_get bacula-cn/ip
305     IP=$RET
306
307     db_get bacula-cn/contact
308     CONTACT=$RET
309     EMAIL=${CONTACT#*<}
310     EMAIL=${EMAIL%>*}
311
312     PASS_FD=$( random_string )
313     PASS_BCONSOLE=$( random_string )
314
315     DATE=$( date '+%Y-%m-%d' )
316
317     REQUEST_FILE=/etc/bacula/bacula-fd.txt
318 }
319
320 case "$1" in
321     configure)
322         load_config
323
324         generate_cert
325         generate_dh
326         generate_fd_config
327         generate_bconsole_config
328
329         fix_privileges
330         set_pinning
331
332         restart_bacula
333         send_request
334
335         db_stop
336     ;;
337
338     abort-upgrade|abort-remove|abort-deconfigure)
339     ;;
340
341     *)
342         echo "postinst called with unknown argument \`$1'" >&2
343         exit 1
344     ;;
345 esac
346
347 # dh_installdeb will replace this with shell code automatically
348 # generated by other debhelper scripts.
349
350 #DEBHELPER#
351
352 exit 0