1
# Xlib.support.unix_connect -- Unix-type display connection functions
3
# Copyright (C) 2000,2002 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
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
31
if hasattr(fcntl, 'F_SETFD'):
32
F_SETFD = fcntl.F_SETFD
33
if hasattr(fcntl, 'FD_CLOEXEC'):
34
FD_CLOEXEC = fcntl.FD_CLOEXEC
38
from FCNTL import F_SETFD, FD_CLOEXEC
41
from Xlib import error, xauth
43
uname = platform.uname()
44
if (uname[0] == 'Darwin') and ([int(x) for x in uname[2].split('.')] >= [9, 0]):
46
display_re = re.compile(r'^([-a-zA-Z0-9._/]*):([0-9]+)(\.([0-9]+))?$')
50
display_re = re.compile(r'^([-a-zA-Z0-9._]*):([0-9]+)(\.([0-9]+))?$')
52
def get_display(display):
53
# Use $DISPLAY if display isn't provided
55
display = os.environ.get('DISPLAY', '')
57
m = display_re.match(display)
59
raise error.DisplayNameError(display)
72
return name, host, dno, screen
75
def get_socket(dname, host, dno):
78
if (uname[0] == 'Darwin') and host and host.startswith('/tmp/'):
79
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
82
# If hostname (or IP) is provided, use TCP socket
84
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
85
s.connect((host, 6000 + dno))
87
# Else use Unix socket
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))
94
# Make sure that the connection isn't inherited in child processes
95
fcntl.fcntl(s.fileno(), F_SETFD, FD_CLOEXEC)
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()
107
family = xauth.FamilyInternet
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), '')
114
family = xauth.FamilyLocal
115
addr = socket.gethostname()
117
au = xauth.Xauthority()
120
return au.get_best_auth(family, addr, dno)
121
except error.XNoAuthError:
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()
135
def old_get_auth(sock, dname, host, dno):
136
# Find authorization cookie
137
auth_name = auth_data = ''
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()
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')
150
parts = string.split(lines[0], None, 2)
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))
164
return auth_name, auth_data
166
get_auth = new_get_auth