Skip to content

Instantly share code, notes, and snippets.

@danrue
Created October 16, 2012 15:40
Show Gist options
  • Save danrue/3900042 to your computer and use it in GitHub Desktop.
Save danrue/3900042 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Script to generate a gitolite style key directory from existing
# authorized_keys files. Supports multiple key files.
#
# IN: /home/*/.ssh/authorized_keys files
# OUT:
# keydir/
# keydir/user1
# keydir/user1/1/user.pub
# keydir/user1/2/user.pub
# keydir/user1/3/user.pub
# keydir/user1/4/user.pub
# keydir/user2
# keydir/user2/1/user.pub
# keydir/user3
# keydir/user3/1/user.pub
# keydir/user3/2/user.pub
#
# TODO:
# - If a .ssh directory is not readable, it will be silently ignored.
#
# Permissions can be fixed on /home with:
# # chmod a+rX /home/* && chmod a+rX /home/*/.ssh && chmod a+r /home/*/.ssh/authorized_keys
git pull
keydir=keydir
rm -rf ${keydir}
for file in `ls /home/*/.ssh/authorized_keys`; do
count=1
user=`echo $file | cut -d / -f 3`
cat ${file} | sort | uniq | while read line; do
# Skip lines that do not begin with "ssh-"
if [ ! $(echo ${line} | cut -c 1-4) = "ssh-" ]; then
continue
fi
# Add the keys
mkdir -p ${keydir}/${user}/${count}
echo ${line} > ${keydir}/${user}/${count}/${user}.pub
count=`expr $count + 1`
done
done
git add keydir
git commit -a -m "Update keydir" && git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment