Ticket #3984: 3984-sshpublickeydatabase-userdir-lookup-2.patch

File 3984-sshpublickeydatabase-userdir-lookup-2.patch, 7.1 KB (added by esteve, 9 years ago)
  • twisted/conch/checkers.py

    # Bazaar merge directive format 2 (Bazaar 0.90)
    # revision_id: esteve@fluidinfo.com-20090831175331-f1l7cossz2hd0zz0
    # target_branch: ../trunk/
    # testament_sha1: eb03d0a4355f7c35562be7a51ee6575b5458dbdb
    # timestamp: 2009-08-31 20:00:21 +0200
    # base_revision_id: svn-v4:bbbe8e31-12d6-0310-92fd-\
    #   ac37d47ddeeb:trunk:27242
    # 
    # Begin patch
    === modified file 'twisted/conch/checkers.py'
     
    132132        Retrieve the keys of the user specified by the credentials, and check
    133133        if one matches the blob in the credentials.
    134134        """
    135         sshDir = os.path.expanduser(
    136             os.path.join("~", credentials.username, ".ssh"))
     135        sshDir = os.path.join(
     136            os.path.expanduser("~" + credentials.username), ".ssh")
     137
    137138        if sshDir.startswith('~'): # didn't expand
    138139            return False
    139140        uid, gid = os.geteuid(), os.getegid()
  • twisted/conch/test/test_checkers.py

    === modified file 'twisted/conch/test/test_checkers.py'
     
    3737    Tests for L{SSHPublicKeyDatabase}.
    3838    """
    3939
     40    mockuser = "user"
     41    mockhomedir = "home"
     42
    4043    if pwd is None:
    4144        skip = "Cannot run without pwd module"
    4245    elif SSHPublicKeyDatabase is None:
     
    4447
    4548    def setUp(self):
    4649        self.checker = SSHPublicKeyDatabase()
    47         self.sshDir = FilePath(self.mktemp())
     50
     51        self.mockos = MockOS()
     52        self.mockuserhome = FilePath(self.mktemp()).child(
     53                    self.mockhomedir).child(self.mockuser)
     54        self.sshDir = self.mockuserhome.child(".ssh")
    4855        self.sshDir.makedirs()
    4956
    5057        self.key1 = base64.encodestring("foobar")
    5158        self.key2 = base64.encodestring("eggspam")
    5259        self.content = "t1 %s foo\nt2 %s egg\n" % (self.key1, self.key2)
    5360
    54         self.mockos = MockOS()
    55         self.mockos.path = self.sshDir.path
     61        self.mockos.path = self.sshDir.parent().parent().parent().path
     62        self.mockos.users[self.mockuser] = self.mockuserhome.path
     63
    5664        self.patch(os.path, "expanduser", self.mockos.expanduser)
    5765        self.patch(pwd, "getpwnam", self.mockos.getpwnam)
    5866        self.patch(os, "seteuid", self.mockos.seteuid)
     
    6169
    6270    def _testCheckKey(self, filename):
    6371        self.sshDir.child(filename).setContent(self.content)
    64         user = UsernamePassword("user", "password")
     72        user = UsernamePassword(self.mockuser, "password")
    6573        user.blob = "foobar"
    6674        self.assertTrue(self.checker.checkKey(user))
    6775        user.blob = "eggspam"
     
    106114            keyFile.chmod(0777)
    107115            return savedSeteuid(euid)
    108116        self.patch(os, "seteuid", seteuid)
    109         user = UsernamePassword("user", "password")
     117        user = UsernamePassword(self.mockuser, "password")
    110118        user.blob = "foobar"
    111119        self.assertTrue(self.checker.checkKey(user))
    112120        self.assertEquals(self.mockos.seteuidCalls, [0, 1, 0, os.getuid()])
  • twisted/test/test_process.py

    === modified file 'twisted/test/test_process.py'
     
    12211221    waitChild = None
    12221222    euid = 0
    12231223    egid = 0
     1224    uid = 1237
     1225    gid = 1235
    12241226    path = None
     1227    users = {}
    12251228
    12261229    def __init__(self):
    12271230        """
     
    13701373        """
    13711374        Override C{os.getgid}. Return a dumb number.
    13721375        """
    1373         return 1235
     1376        return self.gid
    13741377
    13751378
    13761379    def getuid(self):
    13771380        """
    13781381        Override C{os.getuid}. Return a dumb number.
    13791382        """
    1380         return 1237
     1383        return self.uid
    13811384
    13821385
    13831386    def setuid(self, val):
     
    14541457        """
    14551458        Mock C{os.path.expanduser}.
    14561459        """
    1457         return self.path
     1460        if not path.startswith('~'):
     1461            return path
     1462        i = path.find('/', 1)
     1463        if i < 0:
     1464            i = len(path)
     1465        if i == 1:
     1466            userdir = self.users.get(self.user)
     1467        else:
     1468            userdir = self.users.get(path[1:i])
     1469        if userdir is None:
     1470            return path
     1471        userhome = os.path.join(self.path, userdir)
     1472        userhome = userhome.rstrip('/')
     1473        if not os.path.exists(userhome):
     1474            return path
     1475        return userhome + path[i:]
    14581476
    14591477
    14601478    def getpwnam(self, user):