Skip to contentSkip to conversation

Draft: sshkey module to grab disk keys from ssh server

PATCH In review [email protected] 2 participants 2 messages Sep 10, 2010 – Oct 3, 2010 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Draft: sshkey module to grab disk keys from ssh server

From: Matt Smith <[email protected]>

See the README for information about it, and how to use it.

This module is not completed, but I decide to post it for feedback and any help. The code and setup works alright, but the networking has me stumpted. It just needs to come up for long enough to get the keys, and then go back down.

This may be simpler than I think, as I am not very familar with dracut's networking.

Oh, and when I say draft, I mean it still has my notes in it. But I did keep it clean and readable. And I tried to use [] in the readme whenever I had a concern or question about something.

---
 modules.d/90crypt/cryptroot-ask.sh |   83 +++++++++++++++++
 modules.d/95sshkey/README          |  174 ++++++++++++++++++++++++++++++++++++
 modules.d/95sshkey/check           |   23 +++++
 modules.d/95sshkey/install         |   14 +++
 modules.d/95sshkey/ssh_config      |   30 ++++++
 5 files changed, 324 insertions(+), 0 deletions(-)
 create mode 100644 modules.d/95sshkey/README
 create mode 100755 modules.d/95sshkey/check
 create mode 100755 modules.d/95sshkey/install
 create mode 100644 modules.d/95sshkey/ssh_config
modules.d/90crypt/cryptroot-ask.sh +83 -0
index efec56c..7341edd 100755
@@ -105,6 +105,72 @@ fi
 unset keypaths
 
 #
+# Get key by way of ssh
+#
+# We will use sftp to get keys, if requested and module is installed.
+
+# This may be better in its own script which is run earlier, and that keeps
+# the keys. That would save on connections and maybe start the network at a better time?
+# Currently there is no network setup.
+
+# Server user and host; user@server
+sshkey_connect="$(getargs rd_LUKS_SSHKEY_CONNECT)"
+# /path/to/key
+sshkey_path="$(getargs rd_LUKS_SSHKEY_PATH)"
+
+# checks
+if [ -n "$sshkey_connect" ] &&[ -z "$sshkey_path" ]; then
+    warn "ssh key file path is empty. Please set rd_LUKS_SSHKEY_PATH."
+
+elif [ -n "$sshkey_connect" ] && [ -n "$sshkey_path" ]; then
+    # save keys here
+    sshkey_save="/mnt/sshkey"
+    # get other args:  -o ciphers=blah -o port=2222 -o user=youruser
+    sshkey_args="$(getargs rd_LUKS_SSHKEY_ARGS)"
+    
+    # process args (old idea that I left in case people like it better)
+    # it left out the '-o', like args='port=222 user=username"
+    # I believe it will have a problem with spaces in the args
+#    $sshkey_args=""
+#    if [ "x$sshkey_args_line" != "x" ]; then
+#        $sshkey_args="$(echo $sshkey_args_line | sed 's/ / -o /g')"
+#    fi
+    info "Obtaining key/s from keyserver"
+    mkdir -p -m 700 "$sshkey_save"
+    sshkey_got=255
+    # loop to allow for failed connects and server problems, avoiding need to reboot
+    # this can be useful with network outages and higher security of keys
+    # except if specified no to do so (see last line)
+    while [ $sshkey_got -ne 0 ]; do
+        # dont quote sshkey_args, it contains multiple args
+	info "sftp -F /root/.ssh/config $sshkey_args $sshkey_connect:$sshkey_path/* $sshkey_save"
+	# info "$(ip addr)"
+	# get keys. Dont use recursive (-r) until more users have it. It was
+	# introduced in openssh 5.4, could cause problems, and this works fine.
+	sftp -F "/root/.ssh/config" $sshkey_args "$sshkey_connect:$sshkey_path/*" "$sshkey_save/"
+	sshkey_got=$?
+	[ $sshkey_got -eq 0 ] || sleep 10
+	info "$(ls $sshkey_save)"
+	[ getargs rd_LUKS_SSHKEY_NOLOOP ] && sshkey_got=0
+    done
+    
+    # make sure we got something
+    if [ -n "$(ls $sshkey_save)" ]; then
+        info "Got keys"
+    else
+	info "Did not get any keys. Reverting to password method"
+	rmdir "$sshkey_save"
+	sshkey_save=""
+    fi
+    
+    unset sshkey_connect sshkey_path sshkey_args
+    unset sshkey_got
+fi
+
+# [ -f /file ] && . /file
+
+
+#
 # Open LUKS device
 #
 
@@ -118,6 +184,23 @@ if [ -n "$keydev_uuid" ]; then
     umount "$mntp"
     rmdir -p "$mntp" 2>/dev/null
     unset mntp keypath keydev_uuid
+
+elif [ -d "$sshkey_save" ]; then
+    # First, try a master key, then the uuid, then try all 
+    # keys (just to be nice, but they cannot have spaces)
+    for KEY in master "$luksname" $(ls "$sshkey_save"); do
+        try_key="$sshkey_save/$KEY"
+	if [ -f "$try_key" ] ; then
+	    info "Trying key $KEY"
+	    cryptsetup -d "$try_key" luksOpen "$device" "$luksname"
+            [ $? -eq 0 ] && break
+	fi
+    done
+    rm -f $sshkey_save/*
+    rmdir "$sshkey_save"
+    # TODO??: leave key to be deleted later (we may have multiple partitions to unlock)
+    # what happens to ramdisk after boot? if it completely disappears, we could leave keys
+    
 else
     # Prompt for password with plymouth, if installed.
     # Should we check if plymouthd is running?
modules.d/95sshkey/README new file +174 -0

174 lines not on this page · raw message

modules.d/95sshkey/check new file +23 -0
new file mode 100755
index 0000000..32bb1e4
@@ -0,0 +1,23 @@
+#!/bin/bash
+# We depend on network modules being loaded
+[ "$1" = "-d" ] && echo network
+
+# did 90crypt get loaded (how can we do this?)
+
+
+# If hostonly was requested, fail the check if there is not match in grub
+# (probably a terrible way to do this - we are suppose to be independant)
+if [ "$1" = "-h" ]; then
+    [ "$(grep rd_LUKS_SSHKEY_CONNECT /boot/grub/menu.lst)" ] || exit 1
+fi
+
+# if cryptsetup is not installed, then we cannot support encrypted devices.
+# no need if we can detect if 90crypt was successful
+which cryptsetup >/dev/null 2>&1 || exit 1
+
+# If our prerequisites are not met, fail anyways.
+which sftp >/dev/null 2>&1 || exit 1
+[ -s /root/.ssh/known_hosts ] || exit 1
+[ -s /root/.ssh/dracut_sshkey ] || exit 1
+
+exit 0
modules.d/95sshkey/install new file +14 -0
new file mode 100755
index 0000000..e958a9a
@@ -0,0 +1,14 @@
+#!/bin/sh
+dracut_install sftp
+# libs? ldd `which sftp` <- I did not need this
+
+dracut_install /root/.ssh/known_hosts
+dracut_install /root/.ssh/dracut_sshkey
+inst "$moddir"/ssh_config /root/.ssh/config
+# dracut_install /root/.ssh/config
+# (not needed - usually just extra) dracut_install /etc/ssh/ssh_config
+
+dracut_install /etc/hosts
+
+# 95nfs has a lot of info
+# do we need passwd?
modules.d/95sshkey/ssh_config new file +30 -0
new file mode 100644
index 0000000..a07d720
@@ -0,0 +1,30 @@
+# Remenber, it's the first found option that is used:
+# 1. command-line options
+# 2. user’s configuration file (this file)
+# 3. system-wide configuration file (/etc/ssh/ssh_config)
+
+Host *
+BatchMode yes
+IdentityFile /root/.ssh/dracut_sshkey
+UserKnownHostsFile /root/.ssh/known_hosts
+StrictHostKeyChecking yes
+ForwardAgent no
+ForwardX11 no
+ForwardX11Trusted no
+
+
+# 'Thrown out' ideas
+
+# We should not specify ciphers in case they get changed/dropped in
+# the future, and the user can specify their own options.
+## Ciphers aes256-ctr,arcfour256,aes192-ctr,arcfour128,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc
+
+# This won't work if server is using a closed encrypted partition
+# to store keys. It will just fail, and we will have to manually
+# reboot machine after opening the partition. It's better to just
+# loop it.
+## This will keep on trying to connect to ssh server, even 
+## if it is not there or is refusing connections (like during
+## a reboot), for 1157 days. Nice for tmp outages.
+## ConnectionAttempts 99999999
+
Signature · 1 line
1.7.2.2

A message that is not in the archive (sent off-list or not yet received): 1284120363-4011-1-git-send-email-shadowfax-KK0ffGbhmjU@public.gmane.org

#2 lore raw

Hello,

First of all, sorry for that late response. As I'm messing up most with crypt module I think I'm supposed to answer you. :-)

The idea and patch seems cool, but I've got a little question:

Excerpts from shadowfax's message of Fri Sep 10 14:06:03 +0200 2010:

modules.d/95sshkey/README line 105

Quoted 6 lines

+1. Physical Security.

modules.d/95sshkey/README new file +5 -0
+
+You must physically secure your machines. The private key that connects
+to the key server will be located on the /boot partition in plain text.
+Anyone with access to the machine could get that key and then get the
+encryption key from the key server.

Why just don't have key inside /boot? It's almost the same security level or even higher, 'cause it doesn't grant access to the keys-server.

Storing keys remotely this way doesn't increase security, but complicates maintenance. If you're assuming that intruder cannot have physical access to the machine, there's no need to encrypt the data. The cryptLUKS protects against physical access.

Cheers,

Signature · 3 lines
Amadeusz Żołnowski

PGP key: 1024D/C284750D