summaryrefslogtreecommitdiff
path: root/share/keyringer/recrypt
blob: 9153456963d2a4a40c877efaa54ffadaffd654bf (plain)
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
#!/bin/bash
#
# Re-encrypt files to multiple recipients.
#

# Load functions
LIB="`dirname $0`/../../lib/keyringer/functions"
source "$LIB" || exit 1

function keyringer_recrypt {
  # Get file
  keyringer_get_file "$1"

  # Set recipients file
  keyringer_set_recipients "$FILE"

  # Decrypt
  decrypted="$($GPG --use-agent -d "$KEYDIR/$FILE" 2> /dev/null)"

  if [ "$?" != "0" ]; then
    echo "Decryption error."
    exit 1
  fi

  # Recrypt
  echo "$decrypted" | $GPG --use-agent --armor -e -s $(keyringer_recipients "$RECIPIENTS_FILE") > "$KEYDIR/$FILE"

  if [ "$?" != "0" ]; then
    echo "Recryption error."
    exit 1
  fi

  unset decrypted
}

if [ ! -z "$2" ]; then
  keyringer_recrypt $2
else
  cd $KEYDIR && find | while read file; do
    if [ ! -d "$KEYDIR/$file" ]; then
      keyringer_recrypt "$file"
    fi
  done
fi