1
# Xlib.xauth -- ~/.Xauthority access
3
# Copyright (C) 2000 Peter Liljenberg <petli@ctrl-c.liu.se>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
from Xlib import X, error
24
FamilyInternet = X.FamilyInternet
25
FamilyDECnet = X.FamilyDECnet
26
FamilyChaos = X.FamilyChaos
30
def __init__(self, filename = None):
32
filename = os.environ.get('XAUTHORITY')
36
filename = os.path.join(os.environ['HOME'], '.Xauthority')
38
raise error.XauthError(
39
'$HOME not set, cannot find ~/.Xauthority')
42
raw = open(filename, 'rb').read()
44
raise error.XauthError('~/.Xauthority: %s' % err)
48
# entry format (all shorts in big-endian)
62
family, = struct.unpack('>H', raw[n:n+2])
65
length, = struct.unpack('>H', raw[n:n+2])
67
addr = raw[n - length : n]
69
length, = struct.unpack('>H', raw[n:n+2])
71
num = raw[n - length : n]
73
length, = struct.unpack('>H', raw[n:n+2])
75
name = raw[n - length : n]
77
length, = struct.unpack('>H', raw[n:n+2])
79
data = raw[n - length : n]
81
if len(data) != length:
84
self.entries.append((family, addr, num, name, data))
85
except struct.error, e:
86
print "Xlib.xauth: warning, failed to parse part of xauthority file (%s), aborting all further parsing" % filename
89
if len(self.entries) == 0:
90
print "Xlib.xauth: warning, no xauthority details available"
91
# raise an error? this should get partially caught by the XNoAuthError in get_best_auth..
94
return len(self.entries)
96
def __getitem__(self, i):
97
return self.entries[i]
99
def get_best_auth(self, family, address, dispno,
100
types = ( "MIT-MAGIC-COOKIE-1", )):
102
"""Find an authentication entry matching FAMILY, ADDRESS and
105
The name of the auth scheme must match one of the names in
106
TYPES. If several entries match, the first scheme in TYPES
109
If an entry is found, the tuple (name, data) is returned,
110
otherwise XNoAuthError is raised.
117
for efam, eaddr, enum, ename, edata in self.entries:
118
if efam == family and eaddr == address and num == enum:
119
matches[ename] = edata
123
return (t, matches[t])
127
raise error.XNoAuthError((family, address, dispno))