This file is indexed.

/usr/sbin/greenbone-nvt-sync is in openvas-scanner 5.1.1-3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#!/bin/sh
#
# greenbone-nvt-sync
# This script updates the local OpenVAS NVTs from the Greenbone Security Feed
# or the Community Feed. 
#
# Authors:
# Lukas Grunwald <lukas.grunwald@greenbone.net>
# Jan-Oliver Wagner <jan-oliver.wagner@greenbone.net>
# Michael Wiegand <michael.wiegand@greenbone.net>
#
# Copyright (C) 2009-2016 Greenbone Networks GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

VERSION=5.1.1

# SETTINGS
# ========

# PRIVATE_SUBDIR defines a subdirectory of the NVT directory that is excluded
# from the feed sync. This is where to place your own NVTs.
if [ -z "$PRIVATE_SUBDIR" ]
then
  PRIVATE_SUBDIR="private"
fi

# RSYNC_DELETE controls whether files which are not part of the repository will
# be removed from the local directory after synchronization. The default value
# for this setting is
# "--delete --exclude \"$PRIVATE_SUBDIR/\"",
# which means that files which are not part of the feed or private directory
# will be deleted.
RSYNC_DELETE="--delete --exclude $PRIVATE_SUBDIR/"

# RSYNC_SSH_OPTS contains options which should be passed to ssh for the rsync
# connection to the repository.
RSYNC_SSH_OPTS="-o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\""

# RSYNC_COMPRESS specifies the compression level to use for the rsync connection.
RSYNC_COMPRESS="--compress-level=9"

# RSYNC_CHMOD specifies the permissions to chmod the files to.
RSYNC_CHMOD="--perms --chmod=Fugo+r,Fug+w,Dugo-s,Dugo+rx,Dug+w"

# Verbosity flag for rsync. "-q" means a quiet rsync, "-v" a verbose rsync.
RSYNC_VERBOSE="-q"

# RSYNC_OPTIONS controls the general parameters for the rsync connection.
RSYNC_OPTIONS="--links --times --omit-dir-times $RSYNC_VERBOSE --recursive --partial --progress"

# Script and feed information which will be made available to user through
# command line options and automated tools.
SCRIPT_NAME="greenbone-nvt-sync"

# Result of selftest () is stored here. If it is not 0, the selftest has failed
# and the sync script is unlikely to work.
SELFTEST_FAIL=0

# Port to use for synchronization. Default value is 24.
PORT=24

# Directory where pidfiles are located
PIDFILEDIR="/var/run"

# Directory where the OpenVAS configuration is located
OPENVAS_SYSCONF_DIR="/etc/openvas"

# Location of the GSF Access Key
ACCESS_KEY="$OPENVAS_SYSCONF_DIR/gsf-access-key"

# If ENABLED is set to 0, the sync script will not perform a synchronization.
ENABLED=1

# If REFRESH_ONLY is set to 1, the sync script will only update OpenVAS
# Scanner and OpenVAS Manager. This can be controlled via
# the --refresh parameter.
REFRESH_ONLY=0

# If SYNC_ONLY is set to 1, the sync script will only perform the
# synchronization but will not update OpenVAS Scanner nor OpenVAS Manager.
# This can be controlled via the --sync-only parameter.
SYNC_ONLY=0

# LOG_CMD defines the command to use for logging. To have logger log to stderr
# as well as syslog, add "-s" here.
LOG_CMD="logger -t $SCRIPT_NAME"

# Source configuration file if it is readable
[ -r $OPENVAS_SYSCONF_DIR/greenbone-nvt-sync.conf ] && . $OPENVAS_SYSCONF_DIR/greenbone-nvt-sync.conf

log_write () {
  $LOG_CMD -p daemon.notice $1
}

log_debug () {
  $LOG_CMD -p daemon.debug "$1"
}

log_info () {
  $LOG_CMD -p daemon.info "$1"
}

log_notice () {
  $LOG_CMD -p daemon.notice "$1"
}

log_warning () {
  $LOG_CMD -p daemon.warning "$1"
}

log_err () {
  $LOG_CMD -p daemon.err "$1"
}

stderr_write ()
{
  echo "$1" > /dev/stderr
}

# Determine whether a GSF access key is present. If yes,
# then use the Greenbone Security Feed. Else use the
# Greenbone Community Feed.
if [ -e $ACCESS_KEY ]
then
  RESTRICTED=1

  if [ -z "$FEED_NAME" ] ; then
    FEED_NAME="Greenbone Security Feed"
  fi

  if [ -z "$FEED_VENDOR" ] ; then
    FEED_VENDOR="Greenbone Networks GmbH"
  fi

  if [ -z "$FEED_HOME" ] ; then
    FEED_HOME="http://www.greenbone.net/security-feed/"
  fi
else
  RESTRICTED=0

  if [ -z "$FEED_NAME" ] ; then
    FEED_NAME="OpenVAS NVT Feed"
  fi

  if [ -z "$FEED_VENDOR" ] ; then
    FEED_VENDOR="The OpenVAS Project"
  fi

  if [ -z "$FEED_HOME" ] ; then
    FEED_HOME="http://www.openvas.org/openvas-nvt-feed.html"
  fi

  if [ -z "$COMMUNITY_NVT_RSYNC_FEED" ]; then
    COMMUNITY_NVT_RSYNC_FEED=rsync://feed.openvas.org:/nvt-feed
    # An alternative syntax which might work if the above doesn't:
    # COMMUNITY_SCAP_RSYNC_FEED=rsync@feed.openvas.org::/nvt-feed
  fi

  if [ -z "$COMMUNITY_NVT_HTTP_FEED" ]; then
    COMMUNITY_NVT_HTTP_FEED=http://dl.greenbone.net/community-nvt-feed-current.tar.bz2
  fi
fi

RSYNC=`command -v rsync`
WGET=`command -v wget`
CURL=`command -v curl`

if [ -z "$TMPDIR" ]; then
  SYNC_TMP_DIR=/tmp
  # If we have mktemp, create a temporary dir (safer)
  if [ -n "`which mktemp`" ]; then
    SYNC_TMP_DIR=`mktemp -t -d greenbone-nvt-sync.XXXXXXXXXX` || { echo "ERROR: Cannot create temporary directory for file download" >&2; exit 1 ; }
    trap "rm -rf $SYNC_TMP_DIR" EXIT HUP INT TRAP TERM
  fi
else
  SYNC_TMP_DIR="$TMPDIR"
fi

init_sync ()
{
  do_self_test
  if [ $SELFTEST_FAIL -ne 0 ] ; then
    exit $SELFTEST_FAIL
  fi

  if [ -z "$NVT_DIR" ]
  then
    NVT_DIR="/var/lib/openvas/plugins"
  fi

  INFOFILE="$NVT_DIR/plugin_feed_info.inc"
  if [ -r $INFOFILE ] ; then
    FEED_VERSION=`grep PLUGIN_SET $INFOFILE | sed -e 's/[^0-9]//g'`
    FEED_NAME=`awk -F\" '/PLUGIN_FEED/ { print $2 }' $INFOFILE`
    FEED_VENDOR=`awk -F\" '/FEED_VENDOR/ { print $2 }' $INFOFILE`
    FEED_HOME=`awk -F\" '/FEED_HOME/ { print $2 }' $INFOFILE`
    FEED_PRESENT=1
  else
    FEED_PRESENT=0
  fi

  if [ -z "$FEED_NAME" ] ; then
    FEED_NAME="Greenbone Security Feed"
  fi

  if [ -z "$FEED_VENDOR" ] ; then
    FEED_VENDOR="Greenbone Networks GmbH"
  fi

  if [ -z "$FEED_HOME" ] ; then
    FEED_HOME="http://www.greenbone.net/security-feed/"
  fi

  FEED_CURRENT=0
}

# This function uses gos-state-manager to get information about the settings.
# If not gos-state-manager is installed this values of the settings can not be
# retrieved.
#
# Input: option
# Output: value as string or empty String if gos-state-manager is not installed
#         or option not set
get_value ()
{
  value=""
  key=$1
  if which gos-state-manager 1>/dev/null 2>&1
  then
    if gos-state-manager get "$key.value" 1>/dev/null 2>&1
    then
      value="$(gos-state-manager get "$key.value")"
    fi
  fi
  echo "$value"
}

# Creates a restricted access copy of the access key if necessary.
setup_temp_access_key () {
  if [ -e "$ACCESS_KEY" ]
  then
    FILE_ACCESS=`stat -c%a "$ACCESS_KEY" | cut -c2-`
  fi
  if [ -n "$FILE_ACCESS" ] && [ "00" != "$FILE_ACCESS" ]
  then
    TEMP_ACCESS_KEY_DIR=`mktemp -d`
    TEMP_ACCESS_KEY="$TEMP_ACCESS_KEY_DIR/gsf-access-key"
    cp "$ACCESS_KEY" "$TEMP_ACCESS_KEY"
    chmod 400 "$TEMP_ACCESS_KEY"
  else
    TEMP_ACCESS_KEY_DIR=""
    TEMP_ACCESS_KEY="$ACCESS_KEY"
  fi
}

# Deletes the read-only copy of the access key.
cleanup_temp_access_key () {
  if [ -n "$TEMP_ACCESS_KEY_DIR" ]
  then
    rm -rf "$TEMP_ACCESS_KEY_DIR"
  fi
  TEMP_ACCESS_KEY_DIR=""
  TEMP_ACCESS_KEY=""
}

is_feed_current () {
  if [ $REFRESH_ONLY -eq 0 ]
  then
    if [ -z "$FEED_VERSION" ]
    then
      log_write "Could not determine feed version."
      FEED_CURRENT=0
      return $FEED_CURRENT
    fi

    FEED_INFO_TEMP_DIR=`mktemp -d`

    if [ -e $ACCESS_KEY ]
    then
      if [ -n "$FIXED_METHOD" ] && [ "rsync" != "$FIXED_METHOD" ]
      then
        log_err "Sync with access key must be run with rsync. Aborting synchronization."
        stderr_write "Sync with access key must be run with rsync."
        exit 1
      fi

      gsmproxy=$(get_value proxy_feed | sed -r -e 's/^.*\/\///' -e 's/:([0-9]+)$/ \1/')
      syncport=$(get_value syncport)
      if [ "$syncport" ]
      then
        PORT="$syncport"
      fi

      read feeduser < $ACCESS_KEY
      custid=`awk -F@ 'NR > 1 { exit }; { print $1 }' $ACCESS_KEY`
      if [ -z "$feeduser" ] || [ -z "$custid" ]
      then
        log_err "Could not determine credentials, aborting synchronization."
        exit 1
      fi

      setup_temp_access_key

      if [ "$gsmproxy" = "proxy_feed" ] || [ -z "$gsmproxy" ]
      then
        RSYNC_SSH_PROXY_CMD=""
      else
        if [ -e $OPENVAS_SYSCONF_DIR/proxyauth ] && [ -r $OPENVAS_SYSCONF_DIR/proxyauth ]
        then
          RSYNC_SSH_PROXY_CMD="-o \"ProxyCommand corkscrew $gsmproxy %h %p $OPENVAS_SYSCONF_DIR/proxyauth\""
        else
          RSYNC_SSH_PROXY_CMD="-o \"ProxyCommand corkscrew $gsmproxy %h %p\""
        fi
      fi

      rsync -e "ssh $RSYNC_SSH_OPTS $RSYNC_SSH_PROXY_CMD -p $PORT -i $TEMP_ACCESS_KEY" $RSYNC_OPTIONS $RSYNC_DELETE $RSYNC_COMPRESS $RSYNC_CHMOD "$feeduser"plugin_feed_info.inc $FEED_INFO_TEMP_DIR

      if [ $? -ne 0 ]
      then
        log_err "Error: rsync failed."
        rm -rf "$FEED_INFO_TEMP_DIR"
        exit 1
      fi
    elif [ -n "$RSYNC" ] && [ -z "$FIXED_METHOD" -o "$FIXED_METHOD" = "rsync" ]
    then
      log_notice "No Greenbone Security Feed access key found, falling back to Greenbone Community Feed"
      eval "$RSYNC -ltvrP \"$COMMUNITY_NVT_RSYNC_FEED/plugin_feed_info.inc\" \"$FEED_INFO_TEMP_DIR\""
      if [ $? -ne 0 ]
      then
        log_err "rsync failed, aborting synchronization."
        rm -rf "$FEED_INFO_TEMP_DIR"
        exit 1
      fi
    else
      if [ -n "$FIXED_METHOD" ]
      then
        log_notice "non-rsync method selected, skipping feed version test"
      else
        log_notice "rsync not available, skipping feed version test"
      fi
      FEED_CURRENT=0
      rm -rf $FEED_INFO_TEMP_DIR
      cleanup_temp_access_key
      return 0
    fi

    FEED_VERSION_SERVER=`grep PLUGIN_SET $FEED_INFO_TEMP_DIR/plugin_feed_info.inc | sed -e 's/[^0-9]//g'`

    if [ -z "$FEED_VERSION_SERVER" ]
    then
      echo "Could not determine server feed version."
      rm -rf $FEED_INFO_TEMP_DIR
      cleanup_temp_access_key
      return -1
    fi
    # Check against FEED_VERSION
    if [ $FEED_VERSION -lt $FEED_VERSION_SERVER ] ; then
      FEED_CURRENT=0
    else
      FEED_CURRENT=1
    fi
    # Cleanup
    rm -rf "$FEED_INFO_TEMP_DIR"
    cleanup_temp_access_key
  fi

  return $FEED_CURRENT
}

do_wget_community_feed () {
  TMP_NVT="$SYNC_TMP_DIR/openvas-feed-`date +%F`-$$.tar.bz2"

  log_info "Configured NVT http feed: $COMMUNITY_NVT_HTTP_FEED"
  log_info "Downloading to: $TMP_NVT"
  mkdir -p "$NVT_DIR"

  wget "$COMMUNITY_NVT_HTTP_FEED" -O $TMP_NVT

  if [ $? -ne 0 ]
  then
    rm -f $TMP_NVT
    log_err "wget failed"
    exit 1
  else
    log_info "Download complete"
  fi

  cd "$NVT_DIR" \
    && tar xvjf $TMP_NVT

  if [ $? -ne 0 ]
  then
    rm -f $TMP_NVT
    log_err "tar extraction failed."
    exit 1
  else
    rm -f $TMP_NVT
    log_info "NVTs extracted successfully"
  fi
}

do_curl_community_feed () {
  TMP_NVT="$SYNC_TMP_DIR/openvas-feed-`date +%F`-$$.tar.bz2"

  log_info "Configured NVT http feed: $COMMUNITY_NVT_HTTP_FEED"
  log_info "Downloading to: $TMP_NVT"
  mkdir -p "$NVT_DIR"

  curl "$COMMUNITY_NVT_HTTP_FEED" -o $TMP_NVT

  if [ $? -ne 0 ] || [ ! -f $TMP_NVT ]
  then
    rm -f $TMP_NVT
    log_err "curl failed"
    exit 1
  else
    log_info "Download complete"
  fi

  cd "$NVT_DIR" \
    && tar xvjf $TMP_NVT

  if [ $? -ne 0 ]
  then
    rm -f $TMP_NVT
    log_err "tar extraction failed."
    exit 1
  else
    rm -f $TMP_NVT
    log_info "NVTs extracted successfully"
  fi
}

do_rsync_community_feed () {
  if [ -z "$RSYNC" ]; then
    log_err "rsync not found!"
  else
    log_notice "Using rsync: $RSYNC"
    log_notice "Configured NVT rsync feed: $COMMUNITY_NVT_RSYNC_FEED"
    mkdir -p "$NVT_DIR"
    eval "$RSYNC -ltvrP $RSYNC_DELETE \"$COMMUNITY_NVT_RSYNC_FEED\" \"$NVT_DIR\""
    if [ $? -ne 0 ] ; then
      log_err "rsync failed."
      exit 1
    fi
  fi
}

do_sync_community_feed () {
  if [ "rsync" = "$FIXED_METHOD" ] ; then
    if [ -z "$RSYNC" ] ; then
      log_error "rsync requested but not found! Aborting synchronization."
      exit 1
    else
      log_info "Will use rsync"
      do_rsync_community_feed
    fi
  elif [ "wget" = "$FIXED_METHOD" ] ; then
    if [ -z "$WGET" ] ; then
      log_error "wget requested but not found! Aborting synchronization."
      exit 1
    else
      log_info "Will use wget"
      do_wget_community_feed
    fi
  elif [ "curl" = "$FIXED_METHOD" ] ; then
    if [ -z "$CURL" ] ; then
      log_error "curl requested but not found! Aborting synchronization."
      exit 1
    else
      log_info "Will use curl"
      do_curl_community_feed
    fi
  elif [ -z "$RSYNC" ] || [ $FEED_PRESENT -eq 0 ] ; then
    if [ $FEED_PRESENT -eq 0 ] ; then
      log_notice "rsync is not recommended for the initial sync. Falling back on http."
    else
      log_warning "rsync not found!"
    fi
    if [ -z "$WGET" ]; then
      log_warning "GNU wget not found!"
      if [ -z "$CURL" ]; then
        log_warning "curl not found!"
        log_err "no utility available in PATH environment variable to download plugins"
        exit 1
      else
        log_info "Will use curl"
        do_curl_community_feed
      fi
    else
      log_info "Will use wget"
      do_wget_community_feed
    fi
  else
    log_info "Will use rsync"
    do_rsync_community_feed
  fi
}

sync_nvts(){
  if [ $ENABLED -ne 1 ] && [ $REFRESH_ONLY -ne 1 ]
  then
    log_write "NVT synchronization is disabled, exiting."
    exit 0
  fi

  if [ $REFRESH_ONLY -eq 1 ]
  then
    return
  fi

  if [ -e $ACCESS_KEY ]
  then
    if [ -n "$FIXED_METHOD" ] && [ "rsync" != "$FIXED_METHOD" ]
    then
      log_err "Sync with access key must be run with rsync. Aborting synchronization."
      stderr_write "Sync with access key must be run with rsync."
      exit 1
    fi

    log_write "Synchronizing NVTs from the Greenbone Security Feed into $NVT_DIR..."
    if [ $FEED_PRESENT -eq 1 ] ; then
      FEEDCOUNT=`grep -E "nasl$|inc$" $NVT_DIR/md5sums | wc -l`
      log_write "Current status: Using $FEED_NAME at version $FEED_VERSION ($FEEDCOUNT NVTs)"
    else
      log_write "Current status: No feed installed."
    fi
    notsynced=1
    retried=0

    mkdir -p "$NVT_DIR"
    read feeduser < $ACCESS_KEY
    custid=`awk -F@ 'NR > 1 { exit }; { print $1 }' $ACCESS_KEY`
    if [ -z "$feeduser" ] || [ -z "$custid" ]
    then
      log_err "Could not determine credentials, aborting synchronization."
      exit 1
    fi

    setup_temp_access_key

    while [ $notsynced -eq 1 ]
    do

      gsmproxy=$(get_value proxy_feed | sed -r -e 's/^.*\/\///' -e 's/:([0-9]+)$/ \1/')
      syncport=$(get_value syncport)
      if [ "$syncport" ]
      then
        PORT="$syncport"
      fi

      if [ "$gsmproxy" = "proxy_feed" ] || [ -z "$gsmproxy" ]
      then
        RSYNC_SSH_PROXY_CMD=""
      else
        if [ -e $OPENVAS_SYSCONF_DIR/proxyauth ] && [ -r $OPENVAS_SYSCONF_DIR/proxyauth ]; then
          RSYNC_SSH_PROXY_CMD="-o \"ProxyCommand corkscrew $gsmproxy %h %p $OPENVAS_SYSCONF_DIR/proxyauth\""
        else
          RSYNC_SSH_PROXY_CMD="-o \"ProxyCommand corkscrew $gsmproxy %h %p\""
        fi
      fi
      rsync -e "ssh $RSYNC_SSH_OPTS $RSYNC_SSH_PROXY_CMD -p $PORT -i $TEMP_ACCESS_KEY" $RSYNC_OPTIONS $RSYNC_DELETE $RSYNC_COMPRESS $RSYNC_CHMOD $feeduser $NVT_DIR
      if [ $? -ne 0 ]  ; then
        log_err "rsync failed, aborting synchronization."
        exit 1
      fi
      eval "cd \"$NVT_DIR\" ; md5sum -c --status \"$NVT_DIR/md5sums\""
      if [ $? -ne 0 ]  ; then
        if [ -n "$retried" ]
        then
          log_err "Feed integrity check failed twice, aborting synchronization."
          cleanup_temp_access_key
          exit 1
        else
          log_write "The feed integrity check failed. This may be due to a concurrent feed update or other temporary issues."
          log_write "Sleeping 15 seconds before retrying ..."
          sleep 15
          retried=1
        fi
      else
        notsynced=0
      fi
    done
    cleanup_temp_access_key
    log_write "Synchronization with the Greenbone Security Feed successful."
    init_sync
    if [ $FEED_PRESENT -eq 1 ] ; then
      FEEDCOUNT=`grep -E "nasl$|inc$" $NVT_DIR/md5sums | wc -l`
      log_write "Current status: Using $FEED_NAME at version $FEED_VERSION ($FEEDCOUNT NVTs)"
    else
      log_write "Current status: No feed installed."
    fi
  else
    log_notice "No Greenbone Security Feed access key found, falling back to Greenbone Community Feed"
    do_sync_community_feed
  fi
}

update_openvassd (){
  if [ $SYNC_ONLY -eq 1 ]
  then
    return
  fi

  SCANNER_PIDFILE="$PIDFILEDIR/openvassd.pid"
  if [ -r "$SCANNER_PIDFILE" ]
  then
    SCANNER_PID=`cat $SCANNER_PIDFILE`
    kill -HUP $SCANNER_PID
    log_write "Sent signal to OpenVAS Scanner to update (PID $SCANNER_PID)."
  else
    log_err "$PIDFILEDIR/openvassd.pid not found, not updating OpenVAS Scanner."
    log_write "Please restart OpenVAS Scanner or send SIGHUP to it and let OpenVAS"
    log_write "Manager do a '--update' or a '--rebuild' to enable new NVTs for use."
    SYNC_ONLY=1
  fi
}

update_openvasmd (){
  if [ $SYNC_ONLY -eq 1 ]
  then
    return
  fi

  MANAGER_PIDFILE="$PIDFILEDIR/openvasmd.pid"
  if [ -r "$MANAGER_PIDFILE" ]
  then
    MANAGER_PID=`cat $MANAGER_PIDFILE`
    kill -HUP $MANAGER_PID
    log_write "Sent signal to OpenVAS Manager to update (PID $MANAGER_PID)."
  else
    log_err "$PIDFILEDIR/openvasmd.pid not found, not updating OpenVAS Manager."
    log_write "Please let OpenVAS Manager do a '--update' or a '--rebuild' to"
    log_write "enable new NVTs for use."
  fi
}

do_self_test ()
{
  MD5SUM_AVAIL=`command -v md5sum`
  if [ $? -ne 0 ] ; then
    SELFTEST_FAIL=1
    stderr_write "The md5sum binary could not be found."
  fi
  if [ -s $ACCESS_KEY ] ; then
    RSYNC_AVAIL=`command -v rsync`
    if [ $? -ne 0 ] ; then
      SELFTEST_FAIL=1
      stderr_write "The rsync binary could not be found."
    fi
  fi
}

do_describe ()
{
  if [ -z "$NVT_DIR" ] ; then
    init_sync
  fi
  echo "This script synchronizes an NVT collection with the '$FEED_NAME'."
  echo "The '$FEED_NAME' is provided by '$FEED_VENDOR'."
  echo "Online information about this feed: '$FEED_HOME'."
}

do_feedversion () {
  if [ -z "$NVT_DIR" ] ; then
    init_sync
  fi
  if [ $FEED_PRESENT -eq 1 ] ; then
    echo $FEED_VERSION
  else
    stderr_write "The file containing the feed version could not be found."
    exit 1
  fi
}

do_sync ()
{
  init_sync

  if [ $REFRESH_ONLY -ne 1 ]
  then
    is_feed_current
  fi

  if [ $FEED_CURRENT -eq 1 ] && [ $REFRESH_ONLY -ne 1 ]
  then
    log_write "Feed is already current, skipping synchronization."
  else
    sync_nvts
    update_openvassd
    sleep 1 # to ensure sd started working before md starts
    update_openvasmd
  fi
}

do_help () {
  echo "$0: Sync NVT data"
  echo " --curl         only use curl to download feed files"
  echo " --describe     display current feed info"
  echo " --feedcurrent  just check if feed is up-to-date"
  echo " --feedversion  display version of this feed"
  echo " --help         display this help"
  echo " --identify     display information"
  echo " --nvtdir dir   set dir as NVT directory"
  echo " --refresh      update database without downloading new state"
  echo " --rsync        only use rsync to download feed files"
  echo " --selftest     perform self-test"
  echo " --sync-only    download new state without updating database"
  echo " --verbose      makes the sync process print details"
  echo " --version      display version"
  echo " --wget         only use wget to download feed files"
  echo ""
  echo ""
  echo "Environment variables:"
  echo "NVT_DIR         where to extract plugins (absolute path)"
  echo "PRIVATE_SUBDIR  subdirectory of \$NVT_DIR to exclude from synchronization"
  echo "TMPDIR          temporary directory used to download the files"
  echo "Note that you can use standard ones as well (e.g. http_proxy) for wget/curl"
  echo ""
  exit 0
}

while test $# -gt 0; do
  case "$1" in
    --version)
      echo $VERSION
      exit 0
      ;;
    --rsync)
      FIXED_METHOD="rsync"
      ;;
    --wget)
      FIXED_METHOD="wget"
      ;;
    --curl)
      FIXED_METHOD="curl"
      ;;
    --identify)
      init_sync
      echo "NVTSYNC|$SCRIPT_NAME|$VERSION|$FEED_NAME|$RESTRICTED|NVTSYNC"
      exit 0
      ;;
    --selftest)
      do_self_test
      exit $SELFTEST_FAIL
      ;;
    --describe)
      do_describe
      exit 0
      ;;
    --feedversion)
      do_feedversion
      exit 0
      ;;
    --help)
      do_help
      exit 0
      ;;
    --sync-only)
      SYNC_ONLY=1
      ;;
    --refresh)
      REFRESH_ONLY=1
      ;;
    --nvt-dir)
      NVT_DIR="$2"
      shift
      ;;
    --feedcurrent)
      init_sync
      is_feed_current
      exit $?
      ;;
    --verbose)
      RSYNC_VERBOSE="-v"
      ;;
  esac
  shift
done

do_sync

exit 0