~bennabiy/+junk/python-xlib

« back to all changes in this revision

Viewing changes to .pc/python3.patch/Xlib/xauth.py

  • Committer: Package Import Robot
  • Author(s): Andrew Shadura, Ramkumar Ramachandra, Andrew Shadura
  • Date: 2015-08-13 08:14:19 UTC
  • mfrom: (6.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20150813081419-hdefinnghp2iydkx
Tags: 0.14+20091101-3
[ Ramkumar Ramachandra ]
* Remove useless debugging output (Closes: #565996)

[ Andrew Shadura ]
* Switch to 3.0 (quilt) format.
* Rename patches.
* Use debhelper 9 in its short form.
* Use pybuild.
* Bump Standards-Version.
* Don't build or install PostScript documentation and info files.
* Use system-provided texi2html instead of a shipped version
  (Closes: #795057).
* Update debian/copyright (Closes: #795057).
* Don't install Makefile or texi2html with the documentation.
* Set executable bit for examples.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Xlib.xauth -- ~/.Xauthority access
 
2
#
 
3
#    Copyright (C) 2000 Peter Liljenberg <petli@ctrl-c.liu.se>
 
4
#
 
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.
 
9
#
 
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.
 
14
#
 
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
 
18
 
 
19
import os
 
20
import struct
 
21
 
 
22
from Xlib import X, error
 
23
 
 
24
FamilyInternet = X.FamilyInternet
 
25
FamilyDECnet = X.FamilyDECnet
 
26
FamilyChaos = X.FamilyChaos
 
27
FamilyLocal = 256
 
28
 
 
29
class Xauthority:
 
30
    def __init__(self, filename = None):
 
31
        if filename is None:
 
32
            filename = os.environ.get('XAUTHORITY')
 
33
 
 
34
        if filename is None:
 
35
            try:
 
36
                filename = os.path.join(os.environ['HOME'], '.Xauthority')
 
37
            except KeyError:
 
38
                raise error.XauthError(
 
39
                    '$HOME not set, cannot find ~/.Xauthority')
 
40
 
 
41
        try:
 
42
            raw = open(filename, 'rb').read()
 
43
        except IOError, err:
 
44
            raise error.XauthError('~/.Xauthority: %s' % err)
 
45
 
 
46
        self.entries = []
 
47
 
 
48
        # entry format (all shorts in big-endian)
 
49
        #   short family;
 
50
        #   short addrlen;
 
51
        #   char addr[addrlen];
 
52
        #   short numlen;
 
53
        #   char num[numlen];
 
54
        #   short namelen;
 
55
        #   char name[namelen];
 
56
        #   short datalen;
 
57
        #   char data[datalen];
 
58
 
 
59
        n = 0
 
60
        try:
 
61
            while n < len(raw):
 
62
                family, = struct.unpack('>H', raw[n:n+2])
 
63
                n = n + 2
 
64
 
 
65
                length, = struct.unpack('>H', raw[n:n+2])
 
66
                n = n + length + 2
 
67
                addr = raw[n - length : n]
 
68
 
 
69
                length, = struct.unpack('>H', raw[n:n+2])
 
70
                n = n + length + 2
 
71
                num = raw[n - length : n]
 
72
 
 
73
                length, = struct.unpack('>H', raw[n:n+2])
 
74
                n = n + length + 2
 
75
                name = raw[n - length : n]
 
76
 
 
77
                length, = struct.unpack('>H', raw[n:n+2])
 
78
                n = n + length + 2
 
79
                data = raw[n - length : n]
 
80
 
 
81
                if len(data) != length:
 
82
                    break
 
83
 
 
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
 
87
            #pass
 
88
 
 
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..
 
92
 
 
93
    def __len__(self):
 
94
        return len(self.entries)
 
95
 
 
96
    def __getitem__(self, i):
 
97
        return self.entries[i]
 
98
 
 
99
    def get_best_auth(self, family, address, dispno,
 
100
                      types = ( "MIT-MAGIC-COOKIE-1", )):
 
101
 
 
102
        """Find an authentication entry matching FAMILY, ADDRESS and
 
103
        DISPNO.
 
104
 
 
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
 
107
        will be choosen.
 
108
 
 
109
        If an entry is found, the tuple (name, data) is returned,
 
110
        otherwise XNoAuthError is raised.
 
111
        """
 
112
 
 
113
        num = str(dispno)
 
114
 
 
115
        matches = {}
 
116
 
 
117
        for efam, eaddr, enum, ename, edata in self.entries:
 
118
            if efam == family and eaddr == address and num == enum:
 
119
                matches[ename] = edata
 
120
 
 
121
        for t in types:
 
122
            try:
 
123
                return (t, matches[t])
 
124
            except KeyError:
 
125
                pass
 
126
 
 
127
        raise error.XNoAuthError((family, address, dispno))