refresh-auth
author Paul Crowley <paul@lshift.net>
Tue, 15 Apr 2008 18:30:43 +0100
changeset 3 7e659a6870de
parent 1 5bc7446cd2d1
child 4 dcd195f3e52c
permissions -rwxr-xr-x
make more robus and less crufty

#!/usr/bin/python

# WARNING
# This script completely destroys your .ssh/authorized_keys
# file every time it is run
# WARNING

import os
import os.path
import re

akeyfile = os.path.expanduser("~/.ssh/authorized_keys")

allowedchars = "A-Za-z0-9_.-"
goodpathre = re.compile("([%s]+/)*[%s]+$" % (allowedchars, allowedchars))
akeys = open(akeyfile + "_new", "w")
for root, dirs, files in os.walk("keys"):
    for fn in files:
        ffn = os.path.join(root, fn)
        if goodpathre.match(ffn) is None:
            # ignore any path that contains dodgy characters
            continue
        keyname = ffn[5:]
        prefix=('command="%s",no-pty,no-port-forwarding,no-X11-forwarding,no-agent-forwarding'
            % ('./.hg-ssh-wrapper %s' % keyname))
        kf = open(ffn)
        try:
            for l in kf:
                #prefix='no-port-forwarding,no-X11-forwarding,no-agent-forwarding'
                akeys.write("%s %s\n" % (prefix, l.strip()))
        finally:
            kf.close()

os.rename(akeyfile + "_new", akeyfile)