summaryrefslogtreecommitdiff
path: root/scripts/genpair
blob: 1c0fa4c93bf8503065d6ef09823ffb48588d45ea (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
#
# Generate keypairs.
#
# This script is just a wrapper to easily generate keys for
# automated systems.
# 

# Generate a keypair, ssh version
function keygen_ssh {
  echo "Make sure that $homedir is atop of an encrypted volume."
  read -p "Hit ENTER to continue." prompt

  # TODO: programatically enter blank passphrase twice
  ssh-keygen -t dsa -f $homedir/id_dsa -C "root@$hostname"

  echo "Now make sure to save this key in a safe location."
  echo "You can export it by securely copying $contents to $hostname."
}

# Generate a keypair, gpg version
function keygen_gpg {
  echo "Make sure that $homedir is atop of an encrypted volume."
  read -p "Enter password for the private key: " passphrase
  
  # TODO: insert 279 random bytes
  gpg --homedir $homedir --gen-key <<EOF
    Key-Type: DSA
    Key-Length: 1024
    Subkey-Type: ELG-E
    Subkey-Length: 4096
    Name-Real: $hostname
    Name-Comment: backupninja   
    Name-Email: root@$hostname
    Expire-Date: 0
    Passphrase: $passphrase
    %commit
EOF                  
  
  echo "Now make sure to save this key in a safe location."
  echo "You can export it using 'gpg --homedir $homedir --armor --export-secret-keys'."
  echo "Then securely copy it to $hostname."
}

# Setup
keytype="$1"
homedir="$2"
hostname="$3"

# Verify
if [ -z "$hostname" ]; then
  echo "Usage: `basename $0` <gpg|ssh> <homedir> <hostname>"
  exit 1
elif [ -e "$homedir" ]; then
  echo "Folder $homedir already exists, leaving"
  exit 1
fi

# Prepare
mkdir -p $homedir && chmod 700 $homedir
if [ "$?" != "0" ]; then
  echo "Error setting up $homedir"
  exit 1
fi

# Dispatch
keygen_$keytype