~bennabiy/+junk/python-xlib

« back to all changes in this revision

Viewing changes to .pc/fix-ssh-tunnel-auth.patch/Xlib/support/unix_connect.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.support.unix_connect -- Unix-type display connection functions
 
2
#
 
3
#    Copyright (C) 2000,2002 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 re
 
20
import string
 
21
import os
 
22
import platform
 
23
import socket
 
24
 
 
25
# FCNTL is deprecated from Python 2.2, so only import it if we doesn't
 
26
# get the names we need.  Furthermore, FD_CLOEXEC seems to be missing
 
27
# in Python 2.2.
 
28
 
 
29
import fcntl
 
30
 
 
31
if hasattr(fcntl, 'F_SETFD'):
 
32
    F_SETFD = fcntl.F_SETFD
 
33
    if hasattr(fcntl, 'FD_CLOEXEC'):
 
34
        FD_CLOEXEC = fcntl.FD_CLOEXEC
 
35
    else:
 
36
        FD_CLOEXEC = 1
 
37
else:
 
38
    from FCNTL import F_SETFD, FD_CLOEXEC
 
39
 
 
40
 
 
41
from Xlib import error, xauth
 
42
 
 
43
uname = platform.uname()
 
44
if (uname[0] == 'Darwin') and ([int(x) for x in uname[2].split('.')] >= [9, 0]):
 
45
 
 
46
    display_re = re.compile(r'^([-a-zA-Z0-9._/]*):([0-9]+)(\.([0-9]+))?$')
 
47
 
 
48
else:
 
49
 
 
50
    display_re = re.compile(r'^([-a-zA-Z0-9._]*):([0-9]+)(\.([0-9]+))?$')
 
51
 
 
52
def get_display(display):
 
53
    # Use $DISPLAY if display isn't provided
 
54
    if display is None:
 
55
        display = os.environ.get('DISPLAY', '')
 
56
 
 
57
    m = display_re.match(display)
 
58
    if not m:
 
59
        raise error.DisplayNameError(display)
 
60
 
 
61
    name = display
 
62
    host = m.group(1)
 
63
    if host == 'unix':
 
64
        host = ''
 
65
    dno = int(m.group(2))
 
66
    screen = m.group(4)
 
67
    if screen:
 
68
        screen = int(screen)
 
69
    else:
 
70
        screen = 0
 
71
 
 
72
    return name, host, dno, screen
 
73
 
 
74
 
 
75
def get_socket(dname, host, dno):
 
76
    try:
 
77
        # Darwin funky socket
 
78
        if (uname[0] == 'Darwin') and host and host.startswith('/tmp/'):
 
79
            s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
 
80
            s.connect(dname)
 
81
 
 
82
        # If hostname (or IP) is provided, use TCP socket
 
83
        elif host:
 
84
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
85
            s.connect((host, 6000 + dno))
 
86
 
 
87
        # Else use Unix socket
 
88
        else:
 
89
            s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
 
90
            s.connect('/tmp/.X11-unix/X%d' % dno)
 
91
    except socket.error, val:
 
92
        raise error.DisplayConnectionError(dname, str(val))
 
93
 
 
94
    # Make sure that the connection isn't inherited in child processes
 
95
    fcntl.fcntl(s.fileno(), F_SETFD, FD_CLOEXEC)
 
96
 
 
97
    return s
 
98
 
 
99
 
 
100
def new_get_auth(sock, dname, host, dno):
 
101
    # Translate socket address into the xauth domain
 
102
    if (uname[0] == 'Darwin') and host and host.startswith('/tmp/'):
 
103
        family = xauth.FamilyLocal
 
104
        addr = socket.gethostname()
 
105
 
 
106
    elif host:
 
107
        family = xauth.FamilyInternet
 
108
 
 
109
        # Convert the prettyprinted IP number into 4-octet string.
 
110
        # Sometimes these modules are too damn smart...
 
111
        octets = string.split(sock.getpeername()[0], '.')
 
112
        addr = string.join(map(lambda x: chr(int(x)), octets), '')
 
113
    else:
 
114
        family = xauth.FamilyLocal
 
115
        addr = socket.gethostname()
 
116
 
 
117
    au = xauth.Xauthority()
 
118
    while 1:
 
119
        try:
 
120
            return au.get_best_auth(family, addr, dno)
 
121
        except error.XNoAuthError:
 
122
            pass
 
123
 
 
124
        # We need to do this to handle ssh's X forwarding.  It sets
 
125
        # $DISPLAY to localhost:10, but stores the xauth cookie as if
 
126
        # DISPLAY was :10.  Hence, if localhost and not found, try
 
127
        # again as a Unix socket.
 
128
        if family == xauth.FamilyInternet and addr == '\x7f\x00\x00\x01':
 
129
            family = xauth.FamilyLocal
 
130
            addr = socket.gethostname()
 
131
        else:
 
132
            return '', ''
 
133
 
 
134
 
 
135
def old_get_auth(sock, dname, host, dno):
 
136
    # Find authorization cookie
 
137
    auth_name = auth_data = ''
 
138
 
 
139
    try:
 
140
        # We could parse .Xauthority, but xauth is simpler
 
141
        # although more inefficient
 
142
        data = os.popen('xauth list %s 2>/dev/null' % dname).read()
 
143
 
 
144
        # If there's a cookie, it is of the format
 
145
        #      DISPLAY SCHEME COOKIE
 
146
        # We're interested in the two last parts for the
 
147
        # connection establishment
 
148
        lines = string.split(data, '\n')
 
149
        if len(lines) >= 1:
 
150
            parts = string.split(lines[0], None, 2)
 
151
            if len(parts) == 3:
 
152
                auth_name = parts[1]
 
153
                hexauth = parts[2]
 
154
                auth = ''
 
155
 
 
156
                # Translate hexcode into binary
 
157
                for i in range(0, len(hexauth), 2):
 
158
                    auth = auth + chr(string.atoi(hexauth[i:i+2], 16))
 
159
 
 
160
                auth_data = auth
 
161
    except os.error:
 
162
        pass
 
163
 
 
164
    return auth_name, auth_data
 
165
 
 
166
get_auth = new_get_auth