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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     1
#!/usr/bin/python
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     2
3
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
     3
# WARNING
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
     4
# This script completely destroys your .ssh/authorized_keys
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
     5
# file every time it is run
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
     6
# WARNING
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
     7
0
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     8
import os
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     9
import os.path
3
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
    10
import re
0
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    11
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    12
akeyfile = os.path.expanduser("~/.ssh/authorized_keys")
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    13
3
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
    14
allowedchars = "A-Za-z0-9_.-"
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
    15
goodpathre = re.compile("([%s]+/)*[%s]+$" % (allowedchars, allowedchars))
0
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    16
akeys = open(akeyfile + "_new", "w")
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    17
for root, dirs, files in os.walk("keys"):
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    18
    for fn in files:
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    19
        ffn = os.path.join(root, fn)
3
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
    20
        if goodpathre.match(ffn) is None:
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
    21
            # ignore any path that contains dodgy characters
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
    22
            continue
0
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    23
        keyname = ffn[5:]
3
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
    24
        prefix=('command="%s",no-pty,no-port-forwarding,no-X11-forwarding,no-agent-forwarding'
7e659a6870de make more robus and less crufty
Paul Crowley <paul@lshift.net>
parents: 1
diff changeset
    25
            % ('./.hg-ssh-wrapper %s' % keyname))
0
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    26
        kf = open(ffn)
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    27
        try:
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    28
            for l in kf:
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    29
                #prefix='no-port-forwarding,no-X11-forwarding,no-agent-forwarding'
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    30
                akeys.write("%s %s\n" % (prefix, l.strip()))
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    31
        finally:
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    32
            kf.close()
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    33
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    34
os.rename(akeyfile + "_new", akeyfile)
41ecb5a3172c separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    35