File: clamav-freshclam.postinst.in

package info (click to toggle)
clamav 0.101.4+dfsg-0+deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 29,564 kB
  • sloc: ansic: 158,978; sh: 8,178; cpp: 5,173; makefile: 2,088; yacc: 1,351; lex: 714; python: 120; perl: 17
file content (385 lines) | stat: -rw-r--r-- 13,195 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#! /bin/sh
# postinst script for #PACKAGE#
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

#loading debconf module
. /usr/share/debconf/confmodule

#COMMON-FUNCTIONS#

FRESHCLAMCONFFILE=/etc/clamav/freshclam.conf
FRESHCLAMLOGFILE=/var/log/clamav/freshclam.log
DEBCONFFILE=/var/lib/clamav/freshclam.conf
LOGROTFILE=/etc/logrotate.d/clamav-freshclam
DEBROTFILE=/var/lib/clamav/clamav-freshclam
DO_RUN_AS_EVALUATION=0

case "$1" in
  configure)

  # Configure the hardcoded stuff
  dbowner=clamav
  udlogfile="$FRESHCLAMLOGFILE"
  maxatt=5

  # Read the configuration file
  slurp_config "$FRESHCLAMCONFFILE"

  # Get the debconf answers
  db_get clamav-freshclam/local_mirror || true
  [ "$RET" = "" ] || rawmirrors="$RET"
  if echo "$rawmirrors" | egrep -q '(\(|\))'; then
    mirrors=`echo "$rawmirrors" | awk '{print $1}'`
  else
    mirrors="$rawmirrors"
  fi
  db_get clamav-freshclam/autoupdate_freshclam || true
  runas="$RET"
  if [ "$runas" = "ifup.d" ]; then
    db_get clamav-freshclam/internet_interface || true
    if [ "$RET" != "" ]; then
      iface="$RET"
    else
      # Like the template promised
      runas="daemon"
    fi
  fi
  if [ "$runas" = "ifup.d" ] || [ "$runas" = "daemon" ] || [ "$runas" = "cron" ]; then
    db_get clamav-freshclam/update_interval || true
    if [ "$RET" != "" ]; then
      if [ "$runas" != "cron" ]; then
        checks="$RET"
      else
        if [ "$RET" -ge 24 ]; then
          echo "To check for updates more often than hourly, please run freshclam as a daemon."
          cronhour=1
        else
          cronhour="`expr 24 / $RET`"
        fi
      fi
    fi
  fi
  db_get clamav-freshclam/http_proxy || true
  if [ "$RET" != "" ]; then
    url="`echo "$RET" | sed -e 's,^http://,,g' | sed -e 's,/$,,g'`"
    phost="`echo "$url" | cut -d':' -f 1`"
    pport="`echo "$url" | cut -d':' -f 2`"
    if [ "$pport" = "$phost" ]; then
        pport=""
    fi
    fullurl="$RET"
    db_metaget clamav-freshclam/proxy_user value || true
    if [ "$RET" != "" ]; then
      fulluser="$RET"
      puser="`echo "$RET" | cut -d':' -f 1`"
      ppass="`echo "$RET" | cut -d':' -f 2`"
    fi
  fi
  db_get clamav-freshclam/NotifyClamd || true
  [ "$RET" = "true" ] && notify="/etc/clamav/clamd.conf"
  db_get clamav-freshclam/SafeBrowsing || true
  SafeBrowsing="$RET"
  db_get clamav-freshclam/Bytecode || true
  Bytecode="$RET"
  db_get clamav-freshclam/LogRotate || true
  LogRotate="$RET"
  db_get clamav-freshclam/PrivateMirror || true
  PrivateMirror="$RET"

  # Test for the broken versions, see #741675.
  if [ "$2" = "0.98.1+dfsg-3" ] || [ "$2" = "0.98.1+dfsg-1+deb7u2" ] || [ "$2" = "0.98.1+dfsg-1+deb6u2" ]; then
    # Use the defaults instead of the bogus values created by that versions.
    db_metaget clamav-freshclam/LogRotate default || true
    LogRotate="$RET"
    db_metaget clamav-freshclam/PrivateMirror default || true
    PrivateMirror="$RET"
  fi

  # Make sure user changes to unasked questions remain intact
  [ -n "$DatabaseOwner" ] && [ "$DatabaseOwner" != "$dbowner" ] && dbowner="$DatabaseOwner"
  [ -n "$UpdateLogFile" ] && [ "$UpdateLogFile" != "$udlogfile" ] && udlogfile="$UpdateLogFile"
  [ -n "$MaxAttempts" ] && [ "$MaxAttempts" != "$maxatt" ] && maxatt="$MaxAttempts"

  if [ -n "$udlogfile" ]; then
    if echo "$udlogfile" | grep -q '^/dev/'; then
      make_logrotate=false
    else
      if [ "$LogRotate" = "true" ]; then
        make_logrotate=true
      else
        make_logrotate=false
      fi
    fi
    [ -z "$User" ] && User=clamav
    if [ "$make_logrotate" = 'true' ]; then
      # update the logrotate file
      echo "$udlogfile {" > $DEBROTFILE
      echo "     rotate 12" >> $DEBROTFILE
      echo "     weekly" >> $DEBROTFILE
      echo "     compress" >> $DEBROTFILE
      echo "     delaycompress" >> $DEBROTFILE
      echo "     missingok" >> $DEBROTFILE
      echo "     create 640  $User adm" >> $DEBROTFILE
      echo "     postrotate" >> $DEBROTFILE
      echo "     if [ -d /run/systemd/system ]; then" >> $DEBROTFILE
      echo "         systemctl -q is-active clamav-freshclam && systemctl kill --signal=SIGHUP clamav-freshclam || true" >> $DEBROTFILE
      echo "     else" >> $DEBROTFILE
      echo "         /etc/init.d/clamav-freshclam reload-log > /dev/null || true" >> $DEBROTFILE
      echo "     fi" >> $DEBROTFILE
      echo "     endscript" >> $DEBROTFILE
      echo "     }" >> $DEBROTFILE
      touch "$udlogfile"
      if [ -f "$udlogfile" ] && [ ! -L "$udlogfile" ]; then
        chown "$User":adm "$udlogfile"
        chmod 0640 "$udlogfile"
      fi
      ucf_cleanup "$LOGROTFILE"
      ucf_upgrade_check "$LOGROTFILE" "$DEBROTFILE" /var/lib/ucf/cache/:etc:logrotate.d:clamav-freshclam
      rm -f $DEBROTFILE
    else
      if [ -e "$LOGROTFILE" ]; then
        echo "Disabling old logrotate script for clamav-freshclam"
        mv "$LOGROTFILE" "$CLAMAVROTATEFILE".dpkg-old
        ucf -p "$LOGROTFILE"
      fi
    fi
  else
    if [ -e "$LOGROTFILE" ]; then
      echo "Disabling old logrotate script for clamav-freshclam"
      mv "$LOGROTFILE" "$CLAMAVROTATEFILE".dpkg-old
      ucf -p "$LOGROTFILE"
    fi
  fi

  # Set up cron method
  if [ "$runas" = cron ]; then
    min=$(( `od -A n -N 2 -l  < /dev/urandom`  %  3600 / 60 ))
    # min=`perl -e 'print int(rand(60))'`
    FRESHCLAMCRON=/etc/cron.d/clamav-freshclam
    FRESHCLAMTEMP=/var/lib/clamav/freshclam.cron
    echo "$min */$cronhour * * *    $dbowner [ -x /usr/bin/freshclam ] && /usr/bin/freshclam --quiet >/dev/null" > "$FRESHCLAMTEMP"
    ucf_cleanup "$FRESHCLAMCRON"
    ucf_upgrade_check "$FRESHCLAMCRON" "$FRESHCLAMTEMP" /var/lib/ucf/cache/:etc:cron.d:clamav-freshclam
    rm -f "${FRESHCLAMCRON}.ucf-dist"
    rm -f "$FRESHCLAMTEMP"
  else
    if [ -e /etc/cron.d/clamav-freshclam ]; then
      echo -n "Disabling old cron script . . . "
      mv /etc/cron.d/clamav-freshclam /etc/cron.d/clamav-freshclam.dpkg-old
      ucf -p /etc/cron.d/clamav-freshclam > /dev/null 2>&1 || true
      echo "done"
    fi
  fi
  
  # Set up ifup.d method
  if [ "$runas" = 'ifup.d' ]; then
    [ -n "$iface" ] && echo "$iface" > /var/lib/clamav/interface
  else
    [ -f /var/lib/clamav/interface ] && rm -f /var/lib/clamav/interface

  fi
  # Set default values for options not configured via debconf
  [ -z "$DatabaseDirectory" ] && DatabaseDirectory='/var/lib/clamav'
  [ -z "$LogFileMaxSize" ] && LogFileMaxSize=0
  [ -z "$LogTime" ] && LogTime=true
  [ -z "$LogVerbose" ] && LogVerbose=false
  [ -z "$LogSyslog" ] && LogSyslog=false
  [ -z "$LogFacility" ] && LogFacility=LOG_LOCAL6
  [ -z "$DNSDatabaseInfo" ] && DNSDatabaseInfo='current.cvd.clamav.net'
  [ -z "$ScriptedUpdates" ] && ScriptedUpdates=yes
  [ -z "$CompressLocalDatabase" ] && CompressLocalDatabase=no
  [ -z "$Foreground" ] && Foreground=false
  [ -z "$Debug" ] && Debug=false
  [ -z "$ConnectTimeout" ] && ConnectTimeout=30
  [ -z "$ReceiveTimeout" ] && ReceiveTimeout=30
  [ -z "$TestDatabases" ] && TestDatabases=yes

  # Generate config file
  cat >> $DEBCONFFILE << EOF
# Automatically created by the clamav-freshclam postinst
# Comments will get lost when you reconfigure the clamav-freshclam package

DatabaseOwner $dbowner
UpdateLogFile $udlogfile
LogVerbose $LogVerbose
LogSyslog $LogSyslog
LogFacility $LogFacility
LogFileMaxSize $LogFileMaxSize
LogRotate $LogRotate
LogTime $LogTime
Foreground $Foreground
Debug $Debug
MaxAttempts $maxatt
DatabaseDirectory $DatabaseDirectory
DNSDatabaseInfo $DNSDatabaseInfo
ConnectTimeout $ConnectTimeout
ReceiveTimeout $ReceiveTimeout
TestDatabases $TestDatabases
ScriptedUpdates $ScriptedUpdates
CompressLocalDatabase $CompressLocalDatabase
SafeBrowsing $SafeBrowsing
Bytecode $Bytecode
EOF

  if [ -n "$notify" ] ;then
    echo "NotifyClamd $notify" >> $DEBCONFFILE
  fi

  if [ "$runas" != "cron" ] || [ "$runas" != "manual" ]; then
    if [ -n "$checks" ] && [ "$checks" != "true" ]; then
      echo "# Check for new database $checks times a day" >> $DEBCONFFILE
      echo "Checks $checks" >> $DEBCONFFILE
    fi
  fi
  if [ -n "$mirrors" ]; then
    for i in $mirrors; do
      echo "DatabaseMirror $i" >> $DEBCONFFILE
    done
  fi
  # the first mirror is managed via debconf (and might have been changed)
  DatabaseMirror="`echo $DatabaseMirror | sed 's/^[[:space:]]*[^[:space:]]\+[[:space:]]\*//'`"
  DatabaseMirror="$DatabaseMirror database.clamav.net"
  for m in $DatabaseMirror; do
    grep -q "$m" "$DEBCONFFILE" || echo "DatabaseMirror $m" >> $DEBCONFFILE
  done

  if [ -n "$phost" ]; then
    echo "# Proxy: $fullurl" >> $DEBCONFFILE
    echo "HTTPProxyServer $phost" >> $DEBCONFFILE
    if [ -n "$pport" ]; then
      echo "HTTPProxyPort $pport" >> $DEBCONFFILE
    fi
  fi
  if [ -n "$puser" ] && [ -n "$ppass" ]; then
    echo "# Proxy authentication: $fulluser" >> $DEBCONFFILE
    echo "HTTPProxyUsername $puser" >> $DEBCONFFILE
    echo "HTTPProxyPassword $ppass" >> $DEBCONFFILE
  fi

  if `dpkg --compare-versions "$2" lt 0.99+dfsg~`; then
    # Remove the PidFile variable from the configuration, if it is the previous default value.
    if [ "$PidFile" = "/var/run/clamav/freshclam.pid" ]; then
      PidFile=''
    fi
  fi
  [ -n "$PidFile" ] && echo "PidFile $PidFile" >> $DEBCONFFILE
  [ -n "$HTTPUserAgent" ] && echo "HTTPUserAgent $HTTPUserAgent" >> $DEBCONFFILE
  [ -n "$OnOutdatedExecute" ] && echo "OnOutdatedExecute $OnOutdatedExecute" >> $DEBCONFFILE
  [ -n "$OnUpdateExecute" ] && echo "OnUpdateExecute $OnUpdateExecute" >> $DEBCONFFILE
  [ -n "$OnErrorExecute" ] && echo "OnErrorExecute $OnErrorExecute" >> $DEBCONFFILE
  [ -n "$LocalIPAddress" ] && echo "LocalIPAddress $LocalIPAddress" >> $DEBCONFFILE
  [ -n "$PrivateMirror" ] && echo "PrivateMirror $PrivateMirror" >> $DEBCONFFILE
  if [ -n "$DatabaseCustomURL" ]; then
    for i in $DatabaseCustomURL; do
      echo "DatabaseCustomURL $i" >> $DEBCONFFILE
    done
  fi
  if [ -n "$ExtraDatabase" ]; then
    for i in $ExtraDatabase; do
      echo "ExtraDatabase $i" >> $DEBCONFFILE
    done
  fi
  
  ucf_cleanup "$FRESHCLAMCONFFILE"
  ucf_upgrade_check "$FRESHCLAMCONFFILE" "$DEBCONFFILE" /var/lib/ucf/cache/:etc:clamav:freshclam.conf
  rm -f "$DEBCONFFILE"

  db_stop || true
  
  # Permissions are still fsck'd - repair manually
  for script in /etc/network/if-up.d/clamav-freshclam-ifupdown \
    /etc/network/if-down.d/clamav-freshclam-ifupdown \
    /etc/ppp/ip-down.d/clamav-freshclam-ifupdown \
    /etc/ppp/ip-up.d/clamav-freshclam-ifupdown; do
    if [ -e "$script" ]; then
      [ -x "$script" ] || chmod +x "$script"
    fi
  done
  touch $FRESHCLAMLOGFILE
  if [ -f "$FRESHCLAMLOGFILE" ] && [ ! -L "$FRESHCLAMLOGFILE" ]; then
    chmod 640 $FRESHCLAMLOGFILE
    chown "$dbowner":adm $FRESHCLAMLOGFILE
  fi
  
  if [ -f "$FRESHCLAMCONFFILE" ] && [ ! -L "$FRESHCLAMCONFFILE" ]; then
    # Tighten the permissions up if it contains a password
    if [ -n "$ppass" ]; then
      chmod 400 $FRESHCLAMCONFFILE
    else
      chmod 444 $FRESHCLAMCONFFILE
    fi

    chown "$dbowner":adm $FRESHCLAMCONFFILE
  fi
  
  DO_RUN_AS_EVALUATION=1
  ;;
  abort-upgrade|abort-remove|abort-deconfigure)
  ;;
  *)
  echo "postinst called with unknown argument \`$1'" >&2
  exit 1
  ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

if [ $DO_RUN_AS_EVALUATION -eq 1 ]
then
  if [ "$runas" = 'daemon' ]; then
    if [ -x "/etc/init.d/clamav-freshclam" ]; then
      update-rc.d clamav-freshclam defaults >/dev/null
    fi
    if pathfind invoke-rc.d; then
      invoke-rc.d clamav-freshclam start
    else
      /etc/init.d/clamav-freshclam start
    fi
  elif [ "$runas" = 'ifup.d' ]; then
    for intrface in $iface; do
      if route | grep -q "$intrface"; then
        if pathfind invoke-rc.d; then
          IFACE="$intrface" invoke-rc.d clamav-freshclam start || true
        else
          IFACE="$intrface" /etc/init.d/clamav-freshclam start || true
        fi
	break
      fi
    done
    update-rc.d -f clamav-freshclam disable > /dev/null 2>&1 || true
  else
    echo "Starting database update: "
    if pathfind invoke-rc.d; then
      invoke-rc.d clamav-freshclam no-daemon || true
    else
      /etc/init.d/clamav-freshclam no-daemon || true
    fi
    update-rc.d -f clamav-freshclam disable > /dev/null 2>&1 || true
  fi
fi

exit 0