Simon Josefsson: Unattended SSH with Smartcard
I have several backup servers that run the excellent rsnapshot software, which uses Secure Shell (SSH) for remote access. The SSH private key of the backup server can be a weak link in the overall security. To see how it can be a problem, consider if someone breaks into your backup server and manages to copy your SSH private key, they will now have the ability to login to all machines that you take backups off (and that should be all of your machines, right?).
The traditional way to mitigate SSH private key theft is by password protecting the private key. This works poorly in an unattended server environment because either the decryption password needs to be stored in disk (where the attacker can read it) or the decrypted private key has to be available in decrypted form in memory (where attacker can read it).
A better way to deal with the problem is to move the SSH private key to a smartcard. The idea is that the private key cannot be copied by an attacker who roots your backup server. (Careful readers may have spotted a flaw here, and I need to explain one weakness with my solution: an attacker will still be able to login to all your systems by going through your backup server, however it will require an open inbound network connection to your backup server and the attacker will never know what your private key is. What this does is to allow you to more easily do damage control by removing the smartcard from the backup server.)
In this writeup, I ll explain how to accomplish all this on a Debian/Ubuntu-system using a OpenPGP smartcard, a Gemalto USB Shell Token v2 with gpg-agent/scdaemon from GnuPG together with OpenSSH.

First we need to install some packages. The goal is to configure OpenSSH to talk to the gpg-agent which will start and talk to scdaemon which in turn talks to pcscd which talks to the smart card reader. For some strange reason, the scdaemon binary is shipped with GnuPG s S/MIME interface in the gpgsm package.

First we need to install some packages. The goal is to configure OpenSSH to talk to the gpg-agent which will start and talk to scdaemon which in turn talks to pcscd which talks to the smart card reader. For some strange reason, the scdaemon binary is shipped with GnuPG s S/MIME interface in the gpgsm package.
# apt-get install pcscd gnupg-agent gpgsmThe above command should install and start
pcscd and if all works well, you should be able to check the status of the smartcard using GnuPG.
# gpg --card-statusYou need to initialize the smartcard and generate a private key on it, again using GnuPG. If you trust GnuPG more than the smartcard to generate a good private key, you may generate the private key using GnuPG and then move it onto the smartcard (hint: use the
keytocard command). Make sure you don t leave a copy of the private key on the same machine!
# gpg --card-edit
gpg: detected reader Gemalto GemPC Key 00 00'
...
gpg/card> admin
Admin commands are allowed
gpg/card> name
Cardholder's surname:
Cardholder's given name: host.example.org
gpg: 3 Admin PIN attempts remaining before card is permanently locked
Please enter the Admin PIN
gpg: gpg-agent is not available in this session
gpg/card> lang
Language preferences: en
gpg/card> generate
Make off-card backup of encryption key? (Y/n) n
Please enter the PIN
What keysize do you want for the Signature key? (2048)
What keysize do you want for the Encryption key? (2048)
What keysize do you want for the Authentication key? (2048)
Please specify how long the key should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N) y
You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
"Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"
Real name: host.example.org
Email address:
Comment:
You selected this USER-ID:
"host.example.org"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
gpg: existing key will be replaced
gpg: please wait while key is being generated ...
gpg: key generation completed (33 seconds)
gpg: signatures created so far: 0
gpg: existing key will be replaced
gpg: please wait while key is being generated ...
gpg: key generation completed (18 seconds)
gpg: signatures created so far: 1
gpg: signatures created so far: 2
gpg: existing key will be replaced
gpg: please wait while key is being generated ...
gpg: key generation completed (23 seconds)
gpg: signatures created so far: 3
gpg: signatures created so far: 4
gpg: key 12345678 marked as ultimately trusted
public and secret key created and signed.
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
pub 2048R/12345678 2011-09-19
Key fingerprint = 1234 5678 1234 5678 1234 5678 1234 5678 1234 5678
uid host.example.org
sub 2048R/23456789 2011-09-19
sub 2048R/34567890 2011-09-19
gpg/card> quit
#
Now for the interesting part. OpenSSH talks to an agent for private key handling, and GnuPG s gpg-agent supports this protocol when the --enable-ssh-support parameter is given. During startup, gpg-agent will print some environment variables that needs to be set when ssh is run. Normally gpg-agent is invoked by the Xsession.d login scripts, so that the environment variables are inherited by all your processes. However, for an unattended machine without any normal login process, we need to write a script to start gpg-agent. First do these manual steps, to confirm that everything works.
# gpg-agent --daemon --enable-ssh-support > /var/run/gpg-agent-info.env # . /var/run/gpg-agent-info.env # ssh-add -L ssh-rsa AAAAB3N... cardno:000500000BD8 #If the final step printed a SSH public id, the (sometimes) tricky part in getting the hardware to work is (hopefully) complete. What remains is to script things so that
gpg-agent is started on boot and to make sure that your backup scripts has the proper environment variables before launching whatever processes will launch ssh. Further, since we will be running unattended, we need a mechanism to unlock the smartcard using a PIN interactively once on each boot of the machine. I prefer manually entering the PIN on every boot over having the PIN stored in a file on the disk.
I will use the /etc/rc.local mechanism to start gpg-agent, like this:
# cat> /etc/rc.local
#!/bin/sh -e
exec gpg-agent --daemon --enable-ssh-support \
--pinentry-program /usr/local/sbin/pinentry-unattended \
--write-env-file /var/run/gpg-agent-info.env
^D
The astute reader will now ask what /usr/local/sbin/pinentry-unattended is and why it is needed. Now here is the situation. scdaemon will normally query the user for a PIN using a tool called pinentry which reads and write to the user s TTY directly. This won t work in unattended mode, so we want the scdaemon to signal failure here unless we are actually unlocking the smartcard manually. Here is the entire script:
#!/bin/sh
# /usr/local/sbin/pinentry-unattended -- by Simon Josefsson
if test x"$PINENTRY_USER_DATA" = xinteractive; then
exec pinentry "$@"
fi
exit 1
What remains is a script to unlock the smartcard by providing the PIN. This is typically invoked manually if the server has restarted for some reason. Don t worry, any ssh sessions invoked by cron until you have managed to unlock the smartcard will fail with an authentication error it won t hang waiting for a PIN to be entered.
#!/bin/sh
# /usr/local/sbin/unlock-smartcard -- by Simon Josefsson.
. /var/run/gpg-agent-info.env; export GPG_AGENT_INFO SSH_AUTH_SOCK SSH_AGENT_PID
gpg-connect-agent 'scd killscd' /bye > /dev/null
while ! gpg-connect-agent 'scd serialno' /bye grep -q SERIALNO; do
sleep 1
done
PINENTRY_USER_DATA=interactive
export PINENTRY_USER_DATA
checkpin
And the script checkpin is as follows:
#!/bin/sh # /usr/local/sbin/checkpin -- by Simon Josefsson. id= gpg-connect-agent 'scd serialno' /bye head -1 cut -d\ -f3 gpg-connect-agent "scd checkpin $id" /bye grep -q OKAt this point, you should have everything configured and installed. Don t forget to
chmod +x the scripts. The typical use-pattern is as follows. After the machine has been started, gpg-agent is running but the smartcard is not unlocked with the PIN. You need to manually login to the machine and run unlock-smartcard and enter the PIN. In the script that runs the backup jobs, invoked via cron, make sure that the first line of the scripts reads (assuming Bourne shell script syntax):
. /var/run/gpg-agent-info.env; export GPG_AGENT_INFO SSH_AUTH_SOCK SSH_AGENT_PIDTo avoid needlessly attempting ssh connections if the smartcard is not unlocked, your backup script can also call the
checkpin code and abort if it doesn t return true.
checkpin exit 1Some final words about debugging. A basic command to run to check that the GnuPG side is working is
gpg --card-status, it should print some information about the smartcard if successful. To check that the SSH agent part is working, use ssh-add -L. If you get error messages, try killing the scdaemon process by running killall -9 scdaemon and let gpg-agent respawn a new scdaemon process.
That s it! If you like my writeup, please flattr it. 
There has been a lot of
Our friend Susan was going to be in Chicago for a conference, and as we hadn't
seen Chicago yet, and it was on the list of cities we wanted to visit, we made
the semi-spur of the moment decision to have a three day weekend there last
weekend.
We got the Virgin America red-eye flight from SFO, which left on Friday at
around 5pm, and got into Chicago at around 11:30pm, local time. Zoe didn't do
as well as she has previously, and didn't sleep very well on the flight
over, despite us getting lucky and scoring an empty seat between us on an
otherwise fairly full flight. I think she's getting too big to comfortably
sleep in the Ergo baby carrier (or isn't keen on sleeping vertically any
more).
We stayed at the
While
The first day of the Desktop Summit was yesterday, Saturday, August 6. I loved it and gave a presentation. There are two very different stories I can tell on the topic of gender equality in
free software. I'll start with the bad.

Short version arrival was quite easy, the 






















So I have a postscript color printer. This is nice, because I can
just throw postscript at it, and it will provide me with a hardcopy of
whatever that postscript describes. Since postscript is what Unix-like
systems like most, that's great.
Today, I needed to print something. It had been a while, and
apparently some things in sid that are related to printing had changed,
because it did not work anymore. I don't print every day (I
absolutely don't print every day), so it had been, well, "a
fair while" since I last used it. Of course it didn't work.
Of course, it is not the first time this happens. Every so often, my
printer loses track with the cups installation on my server, and then I
have to prod it a bit usually by restarting the printer or the
CUPS server, or both and then it works again. The usual CUPS
failure mode is that the printer was not available when I tried to print
something, which causes CUPS to mark the printer as 'offline' or
something, resulting in it being 'disabled'. The 'cupsenable' command is
supposed to fix this, except that once in a blue moon it doesn't. I've
learned to live with it.
But today was different.
Rather than the printer being disabled, something in the printer
stack would just discard print jobs. That is, they would show up in the
cups queue for a moment, and then disappear. I wouldn't get a piece of
paper with something useful on it. I would just get nothing at
all. This featureful printer supports syslog, and so I told it to
send syslog messages to a useful system somewhere on the network. But
that didn't produce anything interesting. It would tell me when the
system had been powercycled, sure, but not why something would fail.
After trying many things for about an hour, sick of it all, I purged
CUPS off my system and reinstalled it, then had it search for a printer
again. It did find the printer through zeroconf messages, and I allowed
the autoconfiguration stuff do things as it saw fit. When I asked it to
print the exact same postscript file again, something came out of the
printer. Finally, I thought, "success"!
Except it wasn't. Instead, the printer just produced gibberish. It
turned out that cups had decided that my HP 2550n network-enabled
postscript laserprinter was instead a HP 9xx-something USB-only PCL
printer. Going back to the configuration interface and telling it to use
the 2550n ppd file instead got me back to square one: the print job
would appear in the print queue for a few seconds, then magically
disappear.
At this point I was using 'tail -F' to monitor any logfile that might
remotely be related. /var/log/cups/*, /var/log/syslog,
/var/log/daemon.log, you name it. But nothing gave me any remote clue
what was happening with the postscript that I'd been throwing at
cups.
So I used netcat. This printer supports LPR, but will also accept
postscript on port 9100 (I forgot what HP calls it), so a simple 'nc
printer 9100 < foo.ps' should in theory just produce a printed page.
And since someone told me that a "modern" (yeah, right) Linux printing
system uses pdf throughout rather than postscript (for what, I don't
know), I thought it might've been possible that something somewhere was
just mangling the postscript in a way that things would not work
anymore.
I was right, ut I never expected to e right in this
manner.
Did you think this would be listed in the cups logs somewhere? Of
course not! That would be too easy!
But then of course fixing this was fairly easy: just throw the
original file from which this postscript was generated through a
different postscript-generating program, and try again. Magic: suddenly
the printer prints what I want! And it only took me 90 minutes.
Sometimes I wonder what the "userfriendly" police is smoking. Oh
well.
I mentioned this briefly yesterday, but now I'll try to summarize the
story of a great surprise and a big moment for me.
I d like to extend a warm welcome to our new batch of students selected for the 2011 Debian Google Summer of Code!
They should soon be posting on Debian Planet and you re welcome to come talk to them on #debian-soc on irc.debian.org
Further details will be posted in the coming days to our wiki:
FOSDEM ended yesterday and here I am sitting at Charleroi Airport (also known as Brussels South , quite a misleading name given that it s 80 Km from Brussels).
I have already passed all controls, check-in and everything. While I wait for boarding, I am watching the shameful spectacle of airport personnel (let me reiterate that: airport personnel, not Ryanair s) enforcing RyanAir s 10 Kg cabin baggage limit. According to RyanAir, they want to minimize the weight the plane carries to use less fuel. So far, so good.
Here is what I have seen: people who do not carry any baggage (very few, they have probably checked it in because it exceeded size or weight), people who are below the 10 Kg limit and people who are way over it (and have been told to check luggage in). I am OK with those cases.
There is still a fourth case: people who are slightly over 10 Kg.
I ve seen a woman whose bag was 10.15 Kg to be told to pay 20 EUR to check her bag, or go back to the RyanAir desk to check-in the bag. She opened her bag, took a scarf, put it on and now the bag matched the weight limit.
Yes, RyanAir is charging 20 EUR/Kg for hand baggage from 10 Kg on. What a rip-off.
A couple of East-European girls were about 1 Kg in excess each. They put a couple of extra jumpers on and now baggage was under 10 Kg.
Many people were about 1 Kg in excess. When they were told their suitcase better got lighter or pay 20 to 40 EUR. Most of them just took something (camera, food, slippers, whatever) and put in the pockets of their coats. Fortunately, RyanAir is not charging for body and clothes weight (yet?).
In all those cases the plane will end up transporting the same weight and RyanAir won t get one more dime, so why RyanAir? Why are you such a shameful company? Why are you enforcing ludicrous and pointless policies? Don t you know after passing the control everybody just put everything back into the suitcase? Of course you do.
So after watching this ridiculous spectacle go on for a while, I had a devious idea: let s organize a fat people conference and fly them all over to and from using RyanAir. Further, all of them should carry exactly 10 Kg hand baggage.